[clang] [clang] Fix assertion crash in CTAD for alias templates with non-dependent type (PR #191885)
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 13 14:09:48 PDT 2026
https://github.com/hokein created https://github.com/llvm/llvm-project/pull/191885
When building deduction guides, clang assumes that the return type of the deduction guide would always be a dependent type (`TemplateSpecializationType`), but this is not true for invalid case, where the alias RHS is a non-dependent class template specialization, it is represented as a `RecordType` instead.
Fixes #190517.
>From edb85991093016ad7bcbfa6749f9a0fdc8ead622 Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Mon, 13 Apr 2026 23:03:10 +0200
Subject: [PATCH] [clang] Fix assertion crash in CTAD for alias templates with
non-dependent specializations
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaTemplateDeductionGuide.cpp | 15 +++++++++++++--
clang/test/SemaCXX/cxx20-ctad-type-alias.cpp | 10 ++++++++++
3 files changed, 24 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1a70fdea4bb04..638c9ea78ebe0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -469,6 +469,7 @@ Miscellaneous Clang Crashes Fixed
- Fixed a crash when explicitly casting a complex type to or from an atomic complex type. (#GH172208)
- Fixed a crash when explicitly casting a scalar to an atomic complex. (#GH114885)
- Fixed an assertion failure when parsing an invalid out-of-line enum definition with template parameters. (#GH187909)
+- Fixed an assertion failure when using CTAD for alias templates where the RHS resolves to a non-dependent class template specialization. (#GH190517)
OpenACC Specific Changes
------------------------
diff --git a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
index 8d55fb087193f..11a671cb9ff8d 100644
--- a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
+++ b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
@@ -1124,7 +1124,18 @@ BuildDeductionGuideForTypeAlias(Sema &SemaRef,
FReturnType = cast<TemplateSpecializationType>(
ICNT->getDecl()->getCanonicalTemplateSpecializationType(
SemaRef.Context));
- assert(FReturnType && "expected to see a return type");
+
+ ArrayRef<TemplateArgument> FReturnTemplateArgs;
+ if (FReturnType) {
+ FReturnTemplateArgs = FReturnType->template_arguments();
+ } else if (const auto *RT = RType->getAs<RecordType>()) {
+ // If the return type is a non-dependent class template specialization,
+ // it might be resolved to a RecordType.
+ if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()))
+ FReturnTemplateArgs = CTSD->getTemplateArgs().asArray();
+ }
+ assert(!FReturnTemplateArgs.empty() && "expected to see template arguments");
+
// Deduce template arguments of the deduction guide f from the RHS of
// the alias.
//
@@ -1156,7 +1167,7 @@ BuildDeductionGuideForTypeAlias(Sema &SemaRef,
// performing deduction for rest of arguments to align with the C++
// standard.
SemaRef.DeduceTemplateArguments(
- F->getTemplateParameters(), FReturnType->template_arguments(),
+ F->getTemplateParameters(), FReturnTemplateArgs,
AliasRhsTemplateArgs, TDeduceInfo, DeduceResults,
/*NumberOfArgumentsMustMatch=*/false);
diff --git a/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp b/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
index 404b928903c8e..0349428295f54 100644
--- a/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
+++ b/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
@@ -618,3 +618,13 @@ Alias b(42);
// expected-error at -1 {{alias template 'Alias' requires template arguments; argument deduction only allowed for class templates or alias template}}
// expected-note@#gh130604-alias {{template is declared here}}
}
+
+namespace GH190517 {
+template <typename T> struct S1 {};
+template <typename T> using S2 = S1<char>;
+template <typename T> using S3 = S2<T>; // expected-note {{candidate function template not viable}} \
+ // expected-note {{implicit deduction guide declared}} \
+ // expected-note {{candidate function template not viable}} \
+ // expected-note {{implicit deduction guide declared}}
+S3 foo(42); // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'S3'}}
+}
More information about the cfe-commits
mailing list