Author: Krystian Stasiowski Date: 2024-03-05T08:52:20-05:00 New Revision: a642eb89bdaf10c6b4994fc1187de27b441236ed
URL: https://github.com/llvm/llvm-project/commit/a642eb89bdaf10c6b4994fc1187de27b441236ed DIFF: https://github.com/llvm/llvm-project/commit/a642eb89bdaf10c6b4994fc1187de27b441236ed.diff LOG: [Clang][Sema] Fix crash when using name of UnresolvedUsingValueDecl with template arguments (#83842) The following snippet causes a crash: ``` template<typename T> struct A : T { using T::f; void f(); void g() { f<int>(); // crash here } }; ``` This happens because we cast the result of `getAsTemplateNameDecl` as a `TemplateDecl` in `Sema::ClassifyName`, which we cannot do for an `UnresolvedUsingValueDecl`. This patch fixes the crash by considering a name to be that of a template if _any_ function declaration is found per [temp.names] p3.3. Added: clang/test/SemaTemplate/unqual-unresolved-using-value.cpp Modified: clang/docs/ReleaseNotes.rst clang/lib/Sema/SemaDecl.cpp clang/lib/Sema/SemaTemplate.cpp Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index c328ae18c024ce..de5f339cc7f0ba 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -305,6 +305,8 @@ Bug Fixes to C++ Support our attention by an attempt to fix in (#GH77703). Fixes (#GH83385). - Fix evaluation of some immediate calls in default arguments. Fixes (#GH80630) +- Fix a crash when an explicit template argument list is used with a name for which lookup + finds a non-template function and a dependent using declarator. Bug Fixes to AST Handling ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 3ae78748a4e499..8fcaf6ab312b04 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -1110,7 +1110,9 @@ Sema::NameClassification Sema::ClassifyName(Scope *S, CXXScopeSpec &SS, // unqualified-id followed by a < and name lookup finds either one // or more functions or finds nothing. if (!IsFilteredTemplateName) - FilterAcceptableTemplateNames(Result); + FilterAcceptableTemplateNames(Result, + /*AllowFunctionTemplates=*/true, + /*AllowDependent=*/true); bool IsFunctionTemplate; bool IsVarTemplate; @@ -1120,6 +1122,7 @@ Sema::NameClassification Sema::ClassifyName(Scope *S, CXXScopeSpec &SS, Template = Context.getOverloadedTemplateName(Result.begin(), Result.end()); } else if (!Result.empty()) { + assert(!Result.isUnresolvableResult()); auto *TD = cast<TemplateDecl>(getAsTemplateNameDecl( *Result.begin(), /*AllowFunctionTemplates=*/true, /*AllowDependent=*/false)); diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 873ea10ebe0660..df20f83ac8afe6 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -491,18 +491,20 @@ bool Sema::LookupTemplateName(LookupResult &Found, // To keep our behavior consistent, we apply the "finds nothing" part in // all language modes, and diagnose the empty lookup in ActOnCallExpr if we // successfully form a call to an undeclared template-id. - bool AllFunctions = - getLangOpts().CPlusPlus20 && llvm::all_of(Found, [](NamedDecl *ND) { + bool AnyFunctions = + getLangOpts().CPlusPlus20 && llvm::any_of(Found, [](NamedDecl *ND) { return isa<FunctionDecl>(ND->getUnderlyingDecl()); }); - if (AllFunctions || (Found.empty() && !IsDependent)) { + if (AnyFunctions || (Found.empty() && !IsDependent)) { // If lookup found any functions, or if this is a name that can only be // used for a function, then strongly assume this is a function // template-id. *ATK = (Found.empty() && Found.getLookupName().isIdentifier()) ? AssumedTemplateKind::FoundNothing : AssumedTemplateKind::FoundFunctions; - Found.clear(); + FilterAcceptableTemplateNames(Found, + /*AllowFunctionTemplates*/ true, + /*AllowDependent*/ true); return false; } } diff --git a/clang/test/SemaTemplate/unqual-unresolved-using-value.cpp b/clang/test/SemaTemplate/unqual-unresolved-using-value.cpp new file mode 100644 index 00000000000000..688e7a0a10b779 --- /dev/null +++ b/clang/test/SemaTemplate/unqual-unresolved-using-value.cpp @@ -0,0 +1,30 @@ +// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s + +template<typename T> +struct A : T { + using T::f; + using T::g; + using T::h; + + void f(); + void g(); + + void i() { + f<int>(); + g<int>(); // expected-error{{no member named 'g' in 'A<B>'}} + h<int>(); // expected-error{{expected '(' for function-style cast or type construction}} + // expected-error@-1{{expected expression}} + } +}; + +struct B { + template<typename T> + void f(); + + void g(); + + template<typename T> + void h(); +}; + +template struct A<B>; // expected-note{{in instantiation of member function 'A<B>::i' requested here}} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits