https://github.com/Prajwal-kp-18 created 
https://github.com/llvm/llvm-project/pull/243403

A simple alias template, such as

  template <class... Ts> using A = C<Ts...>;

is now treated as equivalent to the class template it names, following the 
provisional resolution of CWG1286 and matching GCC and EDG. An alias template 
qualifies when its template parameters match the class template's, it passes 
each of them through unchanged and in order, it has no requires-clause and adds 
no cv-qualifiers, and neither template has default template arguments.

This is done in ASTContext::getCanonicalTemplateName, so template argument 
deduction, partial specialization matching, type identity and mangling all 
agree. A deduction guide must still name the class template itself, and 
template diffing still compares alias templates as written.

This changes the Itanium mangling of such template template arguments to match 
GCC, the old behavior is kept under -fclang-abi-compat=23. The change is not 
applied for the Microsoft ABI, where MSVC mangles alias templates distinctly.

Fixes #167320.

>From bf4ae82a2e2d3a3ef8225dfb03f5ce8884da5707 Mon Sep 17 00:00:00 2001
From: Prajwal <[email protected]>
Date: Tue, 17 Nov 2026 14:48:47 +0530
Subject: [PATCH] [clang] Implement CWG1286 for simple alias templates

A simple alias template, such as

  template <class... Ts> using A = C<Ts...>;

is now treated as equivalent to the class template it names, following the
provisional resolution of CWG1286 and matching GCC and EDG. An alias template
qualifies when its template parameters match the class template's, it passes
each of them through unchanged and in order, it has no requires-clause and
adds no cv-qualifiers, and neither template has default template arguments.

This is done in ASTContext::getCanonicalTemplateName, so template argument
deduction, partial specialization matching, type identity and mangling all
agree. A deduction guide must still name the class template itself, and
template diffing still compares alias templates as written.

This changes the Itanium mangling of such template template arguments to
match GCC; the old behavior is kept under -fclang-abi-compat=23. The change
is not applied for the Microsoft ABI, where MSVC mangles alias templates
distinctly.

Fixes #167320.
---
 clang/docs/ReleaseNotes.md                    |  9 ++
 clang/include/clang/Basic/ABIVersions.def     |  2 +
 clang/lib/AST/ASTContext.cpp                  | 69 ++++++++++++++
 clang/lib/AST/ASTDiagnostic.cpp               |  9 ++
 clang/lib/Sema/SemaDeclCXX.cpp                |  9 +-
 clang/test/CXX/temp/temp.type/p1-0x.cpp       |  9 +-
 .../test/CodeGenCXX/mangle-alias-template.cpp |  4 +-
 clang/test/CodeGenCXX/mangle-cwg1286.cpp      | 14 +++
 clang/test/SemaTemplate/cwg1286.cpp           | 93 +++++++++++++++++++
 9 files changed, 212 insertions(+), 6 deletions(-)
 create mode 100644 clang/test/CodeGenCXX/mangle-cwg1286.cpp
 create mode 100644 clang/test/SemaTemplate/cwg1286.cpp

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index c3088b7ef65a60..f38af506ab85e6 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -195,6 +195,10 @@ features cannot lower the translation-unit ABI level;
   to generate more efficient code, if compatibility with either the platform
   ABI or old Clang-built objects is not necessary. (#GH236450)
 
+- On Itanium ABI targets, a simple alias template used as a template template
+  argument is now mangled as the equivalent class template, matching GCC
+  (CWG1286).
+
 ### AST Dumping Potentially Breaking Changes
 
 - The AST printer (`-ast-print`) now prints a floating-point literal as the
@@ -316,6 +320,11 @@ documentation](CIR/index.md) for more information 
regarding ClangIR's status and
   when partial ordering constant template parameters declared
   with placeholder types.
 
+- Implemented the provisional resolution of
+  [CWG1286](https://wg21.link/cwg1286), making a simple alias template
+  equivalent to the class template it names, except on Microsoft ABI
+  targets. (#GH167320)
+
 ### C Language Changes
 
 #### C2y Feature Support
diff --git a/clang/include/clang/Basic/ABIVersions.def 
b/clang/include/clang/Basic/ABIVersions.def
index 399be760fc7a3a..544e387079626e 100644
--- a/clang/include/clang/Basic/ABIVersions.def
+++ b/clang/include/clang/Basic/ABIVersions.def
@@ -169,6 +169,8 @@ ABI_VER_MAJOR(22)
 ///     leaves part of a wider access unit unclassified.)
 ///   - On 32-bit x86, use the legacy `regparm` classification for 
floating-point
 ///     types other than `float` and `double`, complex types, and unions.
+///   - Treat a simple alias template as distinct from the class template it
+///     names (CWG1286).
 ABI_VER_MAJOR(23)
 
 /// Conform to the underlying platform's C and C++ ABIs as closely as we can.
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 2bf3e08502d0a6..9b4cb43095f0e3 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -7416,6 +7416,66 @@ ASTContext::getDefaultTemplateArgumentOrNone(const 
NamedDecl *P) const {
   }
 }
 
+static bool isArgForParam(TemplateArgument Arg, const NamedDecl *Param) {
+  if (Param->isTemplateParameterPack()) {
+    if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 ||
+        !Arg.pack_begin()->isPackExpansion())
+      return false;
+    Arg = Arg.pack_begin()->getPackExpansionPattern();
+  }
+  auto Same = [](const auto *X, const auto *Y) {
+    return X && X->getDepth() == Y->getDepth() &&
+           X->getIndex() == Y->getIndex();
+  };
+  if (const auto *P = dyn_cast<TemplateTypeParmDecl>(Param))
+    return Arg.getKind() == TemplateArgument::Type &&
+           Same(Arg.getAsType()->getAs<TemplateTypeParmType>(), P);
+  if (const auto *P = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
+    if (Arg.getKind() != TemplateArgument::Expression)
+      return false;
+    const auto *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()->IgnoreImpCasts());
+    return DRE && Same(dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()), P);
+  }
+  return Arg.getKind() == TemplateArgument::Template &&
+         Same(dyn_cast_or_null<TemplateTemplateParmDecl>(
+                  Arg.getAsTemplate().getAsTemplateDecl()),
+              cast<TemplateTemplateParmDecl>(Param));
+}
+
+/// If \p AT is equivalent to a class template under the proposed resolution of
+/// CWG1286, returns that class template; otherwise returns null.
+static TemplateDecl *
+getSimpleAliasUnderlyingTemplate(const ASTContext &Ctx,
+                                 const TypeAliasTemplateDecl *AT) {
+  const TypeAliasDecl *Pattern = AT->getTemplatedDecl();
+  if (!Pattern || Pattern->isInvalidDecl())
+    return nullptr;
+  QualType RHS = Pattern->getUnderlyingType().getCanonicalType();
+  const auto *TST = dyn_cast<TemplateSpecializationType>(RHS);
+  if (!TST || RHS.hasLocalQualifiers())
+    return nullptr;
+  auto *CTD = dyn_cast_or_null<ClassTemplateDecl>(
+      TST->getTemplateName().getAsTemplateDecl());
+  if (!CTD)
+    return nullptr;
+
+  const TemplateParameterList *AP = AT->getTemplateParameters();
+  const TemplateParameterList *CP =
+      CTD->getMostRecentDecl()->getTemplateParameters();
+  ArrayRef<TemplateArgument> Args = TST->template_arguments();
+  if (AP->getRequiresClause() || AP->size() != CP->size() ||
+      Args.size() != AP->size())
+    return nullptr;
+  for (unsigned I = 0, N = AP->size(); I != N; ++I) {
+    const NamedDecl *P = AP->getParam(I), *Q = CP->getParam(I);
+    if (Ctx.getDefaultTemplateArgumentOrNone(P) ||
+        Ctx.getDefaultTemplateArgumentOrNone(Q) ||
+        !Ctx.isSameTemplateParameter(P, Q) || !isArgForParam(Args[I], P))
+      return nullptr;
+  }
+  return CTD->getCanonicalDecl();
+}
+
 TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name,
                                                   bool IgnoreDeduced) const {
   while (std::optional<TemplateName> UnderlyingOrNone =
@@ -7427,6 +7487,15 @@ TemplateName 
ASTContext::getCanonicalTemplateName(TemplateName Name,
     TemplateDecl *Template = Name.getAsTemplateDecl();
     if (auto *TTP  = dyn_cast<TemplateTemplateParmDecl>(Template))
       Template = getCanonicalTemplateTemplateParmDecl(TTP);
+    else if (auto *AT = dyn_cast<TypeAliasTemplateDecl>(Template);
+             AT && !getTargetInfo().getCXXABI().isMicrosoft() &&
+             getLangOpts().getClangABICompat() > LangOptions::ClangABI::Ver23) 
{
+      // CWG1286: a simple alias template is equivalent to the class template
+      // it names. Not applied for the Microsoft ABI, for MSVC compatibility.
+      if (TemplateDecl *Underlying =
+              getSimpleAliasUnderlyingTemplate(*this, AT))
+        Template = Underlying;
+    }
 
     // The canonical template name is the canonical template declaration.
     return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
diff --git a/clang/lib/AST/ASTDiagnostic.cpp b/clang/lib/AST/ASTDiagnostic.cpp
index 0925fbdcbaeb7a..6ed558b5f6589c 100644
--- a/clang/lib/AST/ASTDiagnostic.cpp
+++ b/clang/lib/AST/ASTDiagnostic.cpp
@@ -1457,6 +1457,15 @@ class TemplateDiff {
   static bool hasSameBaseTemplate(ASTContext &Context,
                                   const TemplateSpecializationType *FromTST,
                                   const TemplateSpecializationType *ToTST) {
+    // hasSameTemplate walks alias templates explicitly, so compare them by
+    // declaration rather than by canonical template name.
+    TemplateDecl *FromTD =
+        FromTST->getTemplateName().getAsTemplateDecl(/*IgnoreDeduced=*/true);
+    TemplateDecl *ToTD =
+        ToTST->getTemplateName().getAsTemplateDecl(/*IgnoreDeduced=*/true);
+    if (isa_and_nonnull<TypeAliasTemplateDecl>(FromTD) ||
+        isa_and_nonnull<TypeAliasTemplateDecl>(ToTD))
+      return declaresSameEntity(FromTD, ToTD);
     return Context.getCanonicalTemplateName(FromTST->getTemplateName(),
                                             /*IgnoreDeduced=*/true) ==
            Context.getCanonicalTemplateName(ToTST->getTemplateName(),
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 62cbcc03ea42fe..5aabb7e5a6b54f 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -12427,8 +12427,13 @@ bool Sema::CheckDeductionGuideDeclarator(Declarator 
&D, QualType &R,
     if (auto RetTST =
             TSI->getTypeLoc().getAsAdjusted<TemplateSpecializationTypeLoc>()) {
       TemplateName SpecifiedName = RetTST.getTypePtr()->getTemplateName();
-      bool TemplateMatches = Context.hasSameTemplateName(
-          SpecifiedName, GuidedTemplate, /*IgnoreDeduced=*/true);
+      // [temp.deduct.guide]p3 requires the same template-name, so an alias
+      // template that is equivalent under CWG1286 does not match.
+      bool TemplateMatches =
+          !isa_and_nonnull<TypeAliasTemplateDecl>(
+              SpecifiedName.getAsTemplateDecl()) &&
+          Context.hasSameTemplateName(SpecifiedName, GuidedTemplate,
+                                      /*IgnoreDeduced=*/true);
 
       const QualifiedTemplateName *Qualifiers =
           SpecifiedName.getAsQualifiedTemplateName();
diff --git a/clang/test/CXX/temp/temp.type/p1-0x.cpp 
b/clang/test/CXX/temp/temp.type/p1-0x.cpp
index 35d00c2fab2041..68346d8602e93a 100644
--- a/clang/test/CXX/temp/temp.type/p1-0x.cpp
+++ b/clang/test/CXX/temp/temp.type/p1-0x.cpp
@@ -1,4 +1,7 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -triple x86_64-linux-gnu %s
+// RUN: %clang_cc1 -fsyntax-only -verify=old -std=c++11 -triple 
x86_64-linux-gnu -fclang-abi-compat=23 %s
+// RUN: %clang_cc1 -fsyntax-only -verify=old -std=c++11 -triple 
x86_64-windows-msvc %s
+// expected-no-diagnostics
 
 namespace Old {
   template<template<class> class TT> struct X { };
@@ -7,8 +10,8 @@ namespace Old {
   X<Y> y;
   X<Z> z;
 
-  using SameType = decltype(y); // expected-note {{here}}
-  using SameType = decltype(z); // expected-error {{different types}}
+  using SameType = decltype(y); // old-note {{here}}
+  using SameType = decltype(z); // old-error {{different types}}
 }
 
 namespace New {
diff --git a/clang/test/CodeGenCXX/mangle-alias-template.cpp 
b/clang/test/CodeGenCXX/mangle-alias-template.cpp
index 803672928d7b6c..090f190d4c2989 100644
--- a/clang/test/CodeGenCXX/mangle-alias-template.cpp
+++ b/clang/test/CodeGenCXX/mangle-alias-template.cpp
@@ -1,5 +1,6 @@
 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin10 -emit-llvm -o - %s 
| FileCheck %s
 // RUN: %if cir-enabled %{ %clang_cc1 -fclangir -std=c++11 -triple 
x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s %}
+// RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin10 -emit-llvm 
-fclang-abi-compat=23 -o - %s | FileCheck %s --check-prefix=CLANG23
 
 template<typename T> struct alloc {};
 template<typename T> using Alloc = alloc<T>;
@@ -30,7 +31,8 @@ void z() {
   // CHECK: call void @_Z1hI5allocEvT_IiE(
 
   h<Alloc>(AC);
-  // CHECK: call void @_Z1hI5AllocEvT_IiE(
+  // CHECK: call void @_Z1hI5allocEvT_IiE(
+  // CLANG23: call void @_Z1hI5AllocEvT_IiE(
 
   Vec<char> VC;
   g<Vec<char>>(VC);
diff --git a/clang/test/CodeGenCXX/mangle-cwg1286.cpp 
b/clang/test/CodeGenCXX/mangle-cwg1286.cpp
new file mode 100644
index 00000000000000..7e8963628363e8
--- /dev/null
+++ b/clang/test/CodeGenCXX/mangle-cwg1286.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-linux-gnu -emit-llvm -o - %s | 
FileCheck %s --check-prefixes=CHECK,NEW
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-linux-gnu -emit-llvm 
-fclang-abi-compat=23 -o - %s | FileCheck %s --check-prefixes=CHECK,OLD
+
+template <template <class> class> struct X {};
+template <class> struct Y {};
+template <class T> using Z = Y<T>;
+template <class T> using W = const Y<T>;
+
+// NEW: define {{.*}}void @_Z1f1XI1YE(
+// OLD: define {{.*}}void @_Z1f1XI1ZE(
+void f(X<Z>) {}
+
+// CHECK: define {{.*}}void @_Z1g1XI1WE(
+void g(X<W>) {}
diff --git a/clang/test/SemaTemplate/cwg1286.cpp 
b/clang/test/SemaTemplate/cwg1286.cpp
new file mode 100644
index 00000000000000..09d0f5f625300b
--- /dev/null
+++ b/clang/test/SemaTemplate/cwg1286.cpp
@@ -0,0 +1,93 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++20 -triple x86_64-linux-gnu -verify 
-DEQUIV=1 %s
+// RUN: %clang_cc1 -fsyntax-only -std=c++20 -triple x86_64-linux-gnu -verify 
-DEQUIV=0 -fclang-abi-compat=23 %s
+// RUN: %clang_cc1 -fsyntax-only -std=c++20 -triple x86_64-windows-msvc 
-verify -DEQUIV=0 %s
+
+namespace GH167320 {
+template <template <typename...> class, class> struct match {
+  static constexpr bool value = false;
+};
+template <template <typename...> class TT, typename... Ts>
+struct match<TT, TT<Ts...>> {
+  static constexpr bool value = true;
+};
+template <typename...> struct my_type {};
+template <typename... Ts> using my_type_alias = my_type<Ts...>;
+
+static_assert(match<my_type, my_type<int, int>>::value);
+static_assert(match<my_type_alias, my_type<int, int>>::value == EQUIV);
+} // namespace GH167320
+
+namespace GH65843 {
+template <template <class> class> struct X { static constexpr int value = 1; };
+template <class> struct Y {};
+template <class T> using Z = Y<T>;
+template <> struct X<Y> { static constexpr int value = 2; };
+
+static_assert(__is_same(X<Y>, X<Z>) == EQUIV);
+static_assert(X<Z>::value == (EQUIV ? 2 : 1));
+} // namespace GH65843
+
+namespace GH177530 {
+template <class...> struct tuple {};
+template <template <class...> class P> struct A {
+  template <class... Args> using packer_alias = P<Args...>;
+};
+template <class T, template <class...> class OP> struct B {
+  static constexpr bool value = false;
+};
+template <template <class...> class OP, class... Args>
+struct B<OP<Args...>, OP> {
+  static constexpr bool value = true;
+};
+
+static_assert(B<tuple<int, float>, tuple>::value);
+static_assert(B<tuple<int, float>, A<tuple>::packer_alias>::value == EQUIV);
+} // namespace GH177530
+
+namespace which_aliases {
+template <class T, class U> struct P {};
+template <class T, class U = int> struct PD {};
+template <template <class, class> class> struct X {};
+
+template <class V, class W> using B = P<V, W>;
+template <class V, class W> using G = B<V, W>;
+static_assert(__is_same(X<P>, X<B>) == EQUIV);
+static_assert(__is_same(X<P>, X<G>) == EQUIV);
+static_assert(__is_same(X<B>, X<G>) == EQUIV);
+
+template <class V, class W> using C = P<V, V>;
+template <class V, class W> using E = P<W, V>;
+template <class V, class W> using I = P<V, typename W::type>;
+template <class V, class W = int> using F = P<V, W>;
+template <class V, class W> using PDA = PD<V, W>;
+template <class V, class W> using K = const P<V, W>;
+static_assert(!__is_same(X<P>, X<C>));
+static_assert(!__is_same(X<P>, X<E>));
+static_assert(!__is_same(X<P>, X<I>));
+static_assert(!__is_same(X<P>, X<F>));
+static_assert(!__is_same(X<PD>, X<PDA>));
+static_assert(!__is_same(X<P>, X<K>));
+
+template <class> concept Any = true;
+template <template <class V, class W> requires Any<V> class> struct XC {};
+template <class V, class W> requires Any<V> using L = P<V, W>;
+static_assert(!__is_same(XC<P>, XC<L>));
+} // namespace which_aliases
+
+namespace parameter_kinds {
+template <int N> struct IntBox {};
+template <template <int> class> struct XN {};
+template <int M> using IntBoxAlias = IntBox<M>;
+static_assert(__is_same(XN<IntBox>, XN<IntBoxAlias>) == EQUIV);
+
+template <template <class> class> struct TTBox {};
+template <template <template <class> class> class> struct XT {};
+template <template <class> class TT> using TTBoxAlias = TTBox<TT>;
+static_assert(__is_same(XT<TTBox>, XT<TTBoxAlias>) == EQUIV);
+} // namespace parameter_kinds
+
+namespace deduction_guide {
+template <class T> struct DG { DG(T); };
+template <class T> using DGAlias = DG<T>;
+template <class T> DG(T *) -> DGAlias<T>; // expected-error {{is not written 
as a specialization of template 'DG'}}
+} // namespace deduction_guide

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to