https://github.com/zyn0217 updated https://github.com/llvm/llvm-project/pull/240754
>From 7fb5e5e535bce3cc4ed5a1de38695d4c35efdb11 Mon Sep 17 00:00:00 2001 From: Younan Zhang <[email protected]> Date: Tue, 10 Nov 2026 16:12:37 +0800 Subject: [PATCH 1/2] [Clang] Fix nullptr dereference in getRHSTemplateDeclAndArgs The RHS could be a non-dependent type alias, and thus they are not always TemplateSpecializationType. --- clang/lib/Sema/SemaTemplateDeductionGuide.cpp | 20 +++++++++++++++---- clang/test/SemaCXX/cxx20-ctad-type-alias.cpp | 17 ++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp index fa0918ae30282b..ae69a7f8010c60 100644 --- a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp +++ b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp @@ -1196,10 +1196,22 @@ getRHSTemplateDeclAndArgs(Sema &SemaRef, TypeAliasTemplateDecl *AliasTemplate) { // Cases where the RHS of the alias is dependent. e.g. // template<typename T> // using AliasFoo1 = Foo<T>; // a class/type alias template specialization - Template = TST->getTemplateName().getAsTemplateDecl(); - AliasRhsTemplateArgs = - TST->getAsNonAliasTemplateSpecializationType()->template_arguments(); - } else if (const auto *RT = RhsType->getAs<RecordType>()) { + + // Look through RHS until we find a non-alias template. + // This is basically Type::getAsNonAliasTemplateSpecializationType, but we + // don't assume RHS is always TemplateSpecializationType. + while (TST && TST->isTypeAlias()) { + RhsType = TST->desugar(); + TST = RhsType->getAs<TemplateSpecializationType>(); + } + if (TST) { + Template = TST->getTemplateName().getAsTemplateDecl(); + AliasRhsTemplateArgs = TST->template_arguments(); + return {Template, AliasRhsTemplateArgs}; + } + // We continue here in case RHS is non-dependent now. + } + if (const auto *RT = RhsType->getAs<RecordType>()) { // Cases where template arguments in the RHS of the alias are not // dependent. e.g. // using AliasFoo = Foo<bool>; diff --git a/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp b/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp index 78911cbaed67be..fa4f74e1e8cdbb 100644 --- a/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp +++ b/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp @@ -629,3 +629,20 @@ template <typename T> using S3 = S2<T>; // expected-note {{candidate function no // expected-note {{cannot deduce template arguments for 'GH190517::S3' from 'GH190517::S1<char>'}} S3 foo(42); // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'S3'}} } + +namespace post_GH147675 { + +template<class T> struct Foo { using self = Foo; Foo(T); }; + +template<class> using Int = typename Foo<int>::self; + +template<class T> using A = Int<T>; // #post_GH147675_A + +A a = 1; + +A b = nullptr; +// expected-error@-1 {{no viable constructor or deduction guide}} +// expected-note@#post_GH147675_A 2{{candidate function}} +// expected-note@#post_GH147675_A 2{{implicit deduction guide}} + +} >From a5df633a89e975efadbc601be1157ddc463e30b6 Mon Sep 17 00:00:00 2001 From: Younan Zhang <[email protected]> Date: Tue, 10 Nov 2026 19:30:11 +0800 Subject: [PATCH 2/2] Fix tests --- clang/lib/Sema/SemaTemplateDeductionGuide.cpp | 7 ++----- clang/test/SemaCXX/cxx20-ctad-type-alias.cpp | 21 +++++++++++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp index ae69a7f8010c60..3e31d67d81c1cb 100644 --- a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp +++ b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp @@ -1196,7 +1196,7 @@ getRHSTemplateDeclAndArgs(Sema &SemaRef, TypeAliasTemplateDecl *AliasTemplate) { // Cases where the RHS of the alias is dependent. e.g. // template<typename T> // using AliasFoo1 = Foo<T>; // a class/type alias template specialization - + Template = TST->getTemplateName().getAsTemplateDecl(); // Look through RHS until we find a non-alias template. // This is basically Type::getAsNonAliasTemplateSpecializationType, but we // don't assume RHS is always TemplateSpecializationType. @@ -1205,13 +1205,10 @@ getRHSTemplateDeclAndArgs(Sema &SemaRef, TypeAliasTemplateDecl *AliasTemplate) { TST = RhsType->getAs<TemplateSpecializationType>(); } if (TST) { - Template = TST->getTemplateName().getAsTemplateDecl(); AliasRhsTemplateArgs = TST->template_arguments(); return {Template, AliasRhsTemplateArgs}; } - // We continue here in case RHS is non-dependent now. - } - if (const auto *RT = RhsType->getAs<RecordType>()) { + } else if (const auto *RT = RhsType->getAs<RecordType>()) { // Cases where template arguments in the RHS of the alias are not // dependent. e.g. // using AliasFoo = Foo<bool>; diff --git a/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp b/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp index fa4f74e1e8cdbb..db86872110d780 100644 --- a/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp +++ b/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp @@ -636,13 +636,30 @@ template<class T> struct Foo { using self = Foo; Foo(T); }; template<class> using Int = typename Foo<int>::self; +template<class T> using Int2 = Foo<T>; + +template<class T> using Int3 = Foo<int>; + template<class T> using A = Int<T>; // #post_GH147675_A -A a = 1; +template<class T> using B = Int2<T>; // #post_GH147675_B -A b = nullptr; +template<class T> using C = Int3<T>; // #post_GH147675_C + +A a = 1; // expected-error@-1 {{no viable constructor or deduction guide}} // expected-note@#post_GH147675_A 2{{candidate function}} +// expected-note@#post_GH147675_A 2{{cannot deduce template arguments}} // expected-note@#post_GH147675_A 2{{implicit deduction guide}} +B b = 1; +static_assert(__is_same(decltype(b), Foo<int>)); + +C c = 1; +// expected-error@-1 {{no viable constructor or deduction guide}} +// expected-note@#post_GH147675_C 2{{candidate function}} +// expected-note@#post_GH147675_C 2{{cannot deduce template arguments}} +// expected-note@#post_GH147675_C 2{{implicit deduction guide}} + + } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
