https://github.com/zyn0217 created https://github.com/llvm/llvm-project/pull/240754
The RHS could be a non-dependent type alias, and thus they are not always TemplateSpecializationType. This addresses https://github.com/llvm/llvm-project/pull/147675#discussion_r4487870799 >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] [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}} + +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
