diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -437,7 +437,8 @@ ``__builtin_dynamic_object_size`` on structs containing flexible array members. (`#62789 `_). - +- Fix a crash when instantiating a non-type template argument in a dependent scope. + (`#62533 `_). Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -5490,10 +5490,10 @@ def err_template_kw_refers_to_dependent_non_template : Error< "%0%select{| following the 'template' keyword}1 " "cannot refer to a dependent template">; -def err_template_kw_refers_to_class_template : Error< - "'%0%1' instantiated to a class template, not a function template">; -def note_referenced_class_template : Note< - "class template declared here">; +def err_template_kw_refers_to_type_template : Error< + "'%0%1' is expected to be a non-type template, but instantiated to a %select{class|type alias}2 template">; +def note_referenced_type_template : Note< + "%select{class|type alias}0 template declared here">; def err_template_kw_missing : Error< "missing 'template' keyword prior to dependent template name '%0%1'">; def ext_template_outside_of_template : ExtWarn< diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -5001,13 +5001,20 @@ return ExprError(); } - if (ClassTemplateDecl *Temp = R.getAsSingle()) { - Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) - << SS.getScopeRep() - << NameInfo.getName().getAsString() << SS.getRange(); - Diag(Temp->getLocation(), diag::note_referenced_class_template); + auto DiagnoseTypeTemplateDecl = [&](TemplateDecl *Temp, + bool isTypeAliasTemplateDecl) { + Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_type_template) + << SS.getScopeRep() << NameInfo.getName().getAsString() << SS.getRange() + << isTypeAliasTemplateDecl; + Diag(Temp->getLocation(), diag::note_referenced_type_template) << 0; return ExprError(); - } + }; + + if (ClassTemplateDecl *Temp = R.getAsSingle()) + return DiagnoseTypeTemplateDecl(Temp, false); + + if (TypeAliasTemplateDecl *Temp = R.getAsSingle()) + return DiagnoseTypeTemplateDecl(Temp, true); return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); } diff --git a/clang/test/SemaCXX/PR62533.cpp b/clang/test/SemaCXX/PR62533.cpp new file mode 100644 --- /dev/null +++ b/clang/test/SemaCXX/PR62533.cpp @@ -0,0 +1,33 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +template +struct test { + template using fun_diff = char; // expected-note 2{{class template declared here}} +}; + +template +decltype(T::template fun_diff) foo1() {} +// expected-note@-1 {{candidate template ignored: substitution failure [with T = test, V = int]: 'test::fun_diff' is expected to be a non-type template, but instantiated to a type alias template}} + +template +void foo2() { + // expected-error@+1 {{test::fun_diff' is expected to be a non-type template, but instantiated to a type alias template}} + int a = test::template fun_diff; +} + +template +struct has_fun_diff { + using type = double; +}; + +template +struct has_fun_diff { + // expected-error@+1 {{'test::fun_diff' is expected to be a non-type template, but instantiated to a type alias template}} + using type = decltype(T::template fun_diff); +}; + +void bar() { + foo1, int>(); // expected-error {{no matching function for call to 'foo1'}} + foo2(); // expected-note {{in instantiation of function template specialization}} + has_fun_diff, int>::type a; // expected-note {{in instantiation of template class}} +} diff --git a/clang/test/SemaTemplate/ms-sizeof-missing-typename.cpp b/clang/test/SemaTemplate/ms-sizeof-missing-typename.cpp --- a/clang/test/SemaTemplate/ms-sizeof-missing-typename.cpp +++ b/clang/test/SemaTemplate/ms-sizeof-missing-typename.cpp @@ -50,7 +50,7 @@ } namespace ambiguous_missing_parens { -// expected-error@+1 {{'Q::U' instantiated to a class template, not a function template}} +// expected-error@+1 {{'Q::U' is expected to be a non-type template, but instantiated to a class template}} template void f() { int a = sizeof T::template U<0> + 4; } struct Q { // expected-note@+1 {{class template declared here}}