@@ -410,6 +410,35 @@ bool Sema::LookupTemplateName(LookupResult &Found, Scope *S, CXXScopeSpec &SS,
410410 LookupParsedName (Found, S, &SS, ObjectType,
411411 /* AllowBuiltinCreation=*/ false , EnteringContext);
412412
413+ bool ObjectTypeSearchedInScope = false ;
414+
415+ // C++ [basic.lookup.qual.general]p2:
416+ // A member-qualified name is the (unique) component name, if any, of
417+ // - an unqualified-id or
418+ // - a nested-name-specifier of the form type-name :: or namespace-name ::
419+ // in the id-expression of a class member access expression.
420+ //
421+ // C++ [basic.lookup.qual.general]p3:
422+ // [...] If nothing is found by qualified lookup for a member-qualified
423+ // name that is the terminal name of a nested-name-specifier and is not
424+ // dependent, it undergoes unqualified lookup.
425+ //
426+ // In 'x.A::B::y', 'A' will undergo unqualified lookup if qualified lookup
427+ // in the type of 'x' finds nothing. If the lookup context is dependent,
428+ // we perform the unqualified lookup in the template definition context
429+ // and store the results so we can replicate the lookup during instantiation.
430+ if (MayBeNNS && Found.empty () && !ObjectType.isNull () &&
431+ (!getLangOpts ().CPlusPlus23 ||
432+ !Found.wasNotFoundInCurrentInstantiation ())) {
433+ if (S) {
434+ LookupName (Found, S);
435+ } else if (!SS.getUnqualifiedLookups ().empty ()) {
436+ Found.addAllDecls (SS.getUnqualifiedLookups ());
437+ Found.resolveKind ();
438+ }
439+ ObjectTypeSearchedInScope = true ;
440+ }
441+
413442 // C++ [basic.lookup.qual.general]p3:
414443 // [...] Unless otherwise specified, a qualified name undergoes qualified
415444 // name lookup in its lookup context from the point where it appears unless
@@ -445,33 +474,6 @@ bool Sema::LookupTemplateName(LookupResult &Found, Scope *S, CXXScopeSpec &SS,
445474 if (Found.wasNotFoundInCurrentInstantiation ())
446475 return false ;
447476
448- bool ObjectTypeSearchedInScope = false ;
449-
450- // C++ [basic.lookup.qual.general]p2:
451- // A member-qualified name is the (unique) component name, if any, of
452- // - an unqualified-id or
453- // - a nested-name-specifier of the form type-name :: or namespace-name ::
454- // in the id-expression of a class member access expression.
455- //
456- // C++ [basic.lookup.qual.general]p3:
457- // [...] If nothing is found by qualified lookup for a member-qualified
458- // name that is the terminal name of a nested-name-specifier and is not
459- // dependent, it undergoes unqualified lookup.
460- //
461- // In 'x.A::B::y', 'A' will undergo unqualified lookup if qualified lookup
462- // in the type of 'x' finds nothing. If the lookup context is dependent,
463- // we perform the unqualified lookup in the template definition context
464- // and store the results so we can replicate the lookup during instantiation.
465- if (MayBeNNS && Found.empty () && !ObjectType.isNull ()) {
466- if (S) {
467- LookupName (Found, S);
468- } else if (!SS.getUnqualifiedLookups ().empty ()) {
469- Found.addAllDecls (SS.getUnqualifiedLookups ());
470- Found.resolveKind ();
471- }
472- ObjectTypeSearchedInScope = true ;
473- }
474-
475477 if (Found.isAmbiguous ())
476478 return false ;
477479
@@ -542,7 +544,10 @@ bool Sema::LookupTemplateName(LookupResult &Found, Scope *S, CXXScopeSpec &SS,
542544
543545 NamedDecl *ExampleLookupResult =
544546 Found.empty () ? nullptr : Found.getRepresentativeDecl ();
545- FilterAcceptableTemplateNames (Found);
547+ FilterAcceptableTemplateNames (
548+ Found,
549+ /* AllowFunctionTemplates=*/ getLangOpts ().CPlusPlus23 ||
550+ !ObjectTypeSearchedInScope);
546551 if (Found.empty ()) {
547552 // If a 'template' keyword was used, a lookup that finds only non-template
548553 // names is an error.
0 commit comments