[clang] [Clang][Sema] Reject template arguments not equivalent to their copies (part of P2308R1) (PR #193754)

Yanzuo Liu via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 23 06:57:15 PDT 2026


https://github.com/zwuis created https://github.com/llvm/llvm-project/pull/193754

Implement not implemented part of [P2308R1 - Template parameter initialization](https://wg21.link/p2308r1) (#73103).

AI was used to review changes.

Assisted-by: GPT-5.4

>From a868262385c2d2594a44be3b9ccab1de64a820d0 Mon Sep 17 00:00:00 2001
From: Yanzuo Liu <zwuis at outlook.com>
Date: Thu, 23 Apr 2026 21:48:57 +0800
Subject: [PATCH] Reject template arguments not equivalent to their copies
 (part of P2308R1)

---
 clang/docs/ReleaseNotes.rst                   |  2 +
 .../clang/Basic/DiagnosticSemaKinds.td        |  5 ++
 clang/lib/AST/ASTContext.cpp                  |  2 +
 clang/lib/Sema/SemaTemplate.cpp               | 86 +++++++++++++++++--
 .../SemaTemplate/temp_arg_nontype_cxx20.cpp   | 57 ++++++++++--
 5 files changed, 134 insertions(+), 18 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 03362cf4e0f8a..957caabc89845 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -49,6 +49,8 @@ C++ Specific Potentially Breaking Changes
 - Clang now correctly rejects ``export`` declarations in module implementation
   partitions. (#GH107602)
 
+- Invalid template arguments which aren't equivalent to their copies are correctly rejected.
+
 ABI Changes in This Version
 ---------------------------
 
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index cc82bb6a51e52..fe12b2ed39591 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5749,8 +5749,13 @@ def err_template_arg_not_object_or_func : Error<
   "non-type template argument does not refer to an object or function">;
 def err_template_arg_not_pointer_to_member_form : Error<
   "non-type template argument is not a pointer to member constant">;
+def err_template_arg_not_equivalent_to_its_copy : Error<
+  "non-type template argument is not equivalent to its copy">;
+def note_template_arg_requires_copy : Note<
+  "non-type template argument is required to be copyable">;
 def err_template_arg_invalid : Error<
   "non-type template argument '%0' is invalid">;
+
 def err_pointer_to_member_type : Error<
   "invalid use of pointer to member type after %select{.*|->*}0">;
 def err_pointer_to_member_call_drops_quals : Error<
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index abf5f8a832043..e520ef7eec0ce 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -13793,6 +13793,8 @@ ASTContext::getUnnamedGlobalConstantDecl(QualType Ty,
 TemplateParamObjectDecl *
 ASTContext::getTemplateParamObjectDecl(QualType T, const APValue &V) const {
   assert(T->isRecordType() && "template param object of unexpected type");
+  assert(!T->isInstantiationDependentType() &&
+         "instantiation-dependent types are not supported");
 
   // C++ [temp.param]p8:
   //   [...] a static storage duration object of type 'const T' [...]
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 7b7d43ef3234c..f468967abc89a 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -7130,6 +7130,72 @@ static bool CheckTemplateArgumentPointerToMember(
   return true;
 }
 
+// P2308R1  C++26 [temp.arg.nontype]p4:
+//   ... If, for the initialization from any candidate initializer,
+//     - ...
+//     - the initialization would cause P to not be
+//       template-argument-equivalent ([temp.type]) to v,
+//   the program is ill-formed.
+// TODO: Cache accepted APValue for performance improvement.
+static bool CheckTemplateArgumentCopyEquivalence(Sema &S, NamedDecl *Param,
+                                                 QualType ParamType,
+                                                 const APValue &Value,
+                                                 SourceLocation ArgLoc) {
+  assert(ParamType->isRecordType() && "no need to check copy equivalence");
+
+  SourceLocation ParamLoc = Param->getLocation();
+
+  // Instead of creating a variable (C++26 [temp.arg.nontype]p3),
+  // create a template parameter object to represent the candidate initializer.
+  // They are equivalent during creating a copy.
+  auto *CandidateInitializer =
+      S.BuildDeclRefExpr(S.Context.getTemplateParamObjectDecl(ParamType, Value),
+                         ParamType.withConst(), VK_LValue, ArgLoc);
+  InitializationKind Kind = InitializationKind::CreateForInit(
+      ArgLoc, /*DirectInit=*/false, CandidateInitializer);
+  Expr *Inits[1] = {CandidateInitializer};
+  InitializedEntity Entity =
+      InitializedEntity::InitializeTemplateParameter(ParamType, Param);
+  InitializationSequence InitSeq(S, Entity, Kind, Inits);
+  ExprResult Result = InitSeq.Perform(S, Entity, Kind, Inits);
+  if (Result.isInvalid()) {
+    S.Diag(ParamLoc, diag::note_template_arg_requires_copy);
+    return true;
+  }
+
+  // Fast path. Trivial copy constructor performs per-element copy.
+  if (cast<CXXConstructExpr>(Result.get())->getConstructor()->isTrivial())
+    return false;
+
+  Result = S.ActOnConstantExpression(Result.get());
+  Result = S.ActOnFinishFullExpr(AssertSuccess(Result), ArgLoc,
+                                 /*DiscardedValue=*/false,
+                                 /*IsConstexpr=*/true,
+                                 /*IsTemplateArgument=*/true);
+
+  APValue ValueAfterCopy, PreNarrowingValue;
+  Result = S.EvaluateConvertedConstantExpression(
+      AssertSuccess(Result), ParamType, ValueAfterCopy, CCEKind::TemplateArg,
+      /*RequireInt=*/false, PreNarrowingValue);
+  if (Result.isInvalid()) {
+    S.Diag(ParamLoc, diag::note_template_arg_requires_copy);
+    return true;
+  }
+
+  llvm::FoldingSetNodeID VID, CID;
+  Value.Profile(VID);
+  ValueAfterCopy.Profile(CID);
+  if (VID == CID)
+    return false;
+
+  S.Diag(ArgLoc, diag::err_template_arg_not_equivalent_to_its_copy);
+  if (Param->getDeclName())
+    S.Diag(ParamLoc, diag::note_parameter_named_here) << Param->getDeclName();
+  else
+    S.Diag(ParamLoc, diag::note_parameter_here);
+  return true;
+}
+
 /// Check a template argument against its corresponding
 /// non-type template parameter.
 ///
@@ -7272,14 +7338,10 @@ ExprResult Sema::CheckTemplateArgument(NamedDecl *Param, QualType ParamType,
     return Arg;
   }
 
-  // Avoid making a copy when initializing a template parameter of class type
-  // from a template parameter object of the same type. This is going beyond
-  // the standard, but is required for soundness: in
-  //   template<A a> struct X { X *p; X<a> *q; };
-  // ... we need p and q to have the same type.
-  //
-  // Similarly, don't inject a call to a copy constructor when initializing
-  // from a template parameter of the same type.
+  // Fast path. A valid template parameter object is equivalent to its copy. No
+  // need to make a copy to check again when initializing a template parameter
+  // of class type from a template parameter object of the same type.
+  // TODO: Remove `ParamType->isRecordType()` in condition?
   Expr *InnerArg = DeductionArg->IgnoreParenImpCasts();
   if (ParamType->isRecordType() && isa<DeclRefExpr>(InnerArg) &&
       Context.hasSameUnqualifiedType(ParamType, InnerArg->getType())) {
@@ -7417,6 +7479,12 @@ ExprResult Sema::CheckTemplateArgument(NamedDecl *Param, QualType ParamType,
     if (Value.isAddrLabelDiff())
       return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff);
 
+    if (ParamType->isRecordType() &&
+        !ParamType->isInstantiationDependentType() &&
+        CheckTemplateArgumentCopyEquivalence(*this, Param, ParamType, Value,
+                                             StartLoc))
+      return ExprError();
+
     if (ArgPE) {
       SugaredConverted = TemplateArgument(Arg, /*IsCanonical=*/false);
       CanonicalConverted =
@@ -7430,7 +7498,7 @@ ExprResult Sema::CheckTemplateArgument(NamedDecl *Param, QualType ParamType,
   }
 
   // These should have all been handled above using the C++17 rules.
-  assert(!ArgPE && !StrictCheck);
+  assert(!ArgPE && !StrictCheck && !ParamType->isRecordType());
 
   // C++ [temp.arg.nontype]p5:
   //   The following conversions are performed on each expression used
diff --git a/clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp b/clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp
index 8450ff037e184..463eb714fa675 100644
--- a/clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp
+++ b/clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp
@@ -106,30 +106,69 @@ namespace ConvertedConstant {
 }
 
 namespace CopyCounting {
-  // Make sure we don't use the copy constructor when transferring the "same"
-  // template parameter object around.
   struct A { int n; constexpr A(int n = 0) : n(n) {} constexpr A(const A &a) : n(a.n+1) {} };
-  template<A a> struct X {};
+  template<A a> struct X {}; // expected-note {{passing argument to parameter 'a' here}}
   template<A a> constexpr int f(X<a> x) { return a.n; }
 
-  static_assert(f(X<A{}>()) == 0);
+  static_assert(f(X<A{}>()) == 0); // expected-error {{non-type template argument is not equivalent to its copy}}
 
-  template<A a> struct Y { void f(); };
+  template<A a> struct Y { void f(); }; // expected-note {{passing argument to parameter 'a' here}}
   template<A a> void g(Y<a> y) { y.Y<a>::f(); }
-  void h() { constexpr A a; g<a>(Y<a>{}); }
+  void h() { constexpr A a; g<a>(Y<a>{}); }  // expected-error {{non-type template argument is not equivalent to its copy}}
 
-  template<A a> struct Z {
+  template<A a> struct Z { // expected-note 2 {{passing argument to parameter 'a' here}}
     constexpr int f() {
       constexpr A v = a; // this is {a.n+1}
       return Z<v>().f() + 1; // this is Z<{a.n+2}>
     }
   };
-  template<> struct Z<A{20}> {
+  template<> struct Z<A{20}> {  // expected-error {{non-type template argument is not equivalent to its copy}}
     constexpr int f() {
       return 32;
     }
   };
-  static_assert(Z<A{}>().f() == 42);
+  static_assert(Z<A{}>().f() == 42); // expected-error {{non-type template argument is not equivalent to its copy}}
+}
+
+namespace ConditionallyModify {
+struct A {
+  int x;
+  constexpr A(int x) : x(x) {}
+  constexpr A(const A& a) : x(a.x + (a.x > 1)) {}
+};
+template <A a> struct X {}; // expected-note {{passing argument to parameter 'a' here}}
+template struct X<1>;
+template struct X<2>; // expected-error {{non-type template argument is not equivalent to its copy}}
+}
+
+namespace CannotCopy {
+template <typename T, template <T> typename TEMPLATE>
+concept C = (TEMPLATE<{}>{}, true);
+
+struct S1 {
+  constexpr S1() = default; // expected-note {{candidate constructor not viable: requires 0 arguments, but 1 was provided}}
+  constexpr S1(S1&) = default; // expected-note {{candidate constructor not viable: 1st argument ('const S1') would lose const qualifier}}
+};
+template <S1> struct T1 {}; // expected-note {{passing argument to parameter here}} expected-note {{non-type template argument is required to be copyable}}
+template struct T1<{}>; // expected-error {{no matching constructor for initialization of 'S1'}}
+static_assert(!C<S1, T1>);
+
+struct S2 {
+  constexpr S2() = default; // expected-note {{candidate constructor not viable: requires 0 arguments, but 1 was provided}}
+  explicit constexpr S2(const S2&) = default; // expected-note {{explicit constructor is not a candidate}}
+};
+template <S2> struct T2 {}; // expected-note {{passing argument to parameter here}} expected-note {{non-type template argument is required to be copyable}}
+template struct T2<{}>; // expected-error {{no matching constructor for initialization of 'S2'}}
+static_assert(!C<S2, T2>);
+
+struct S3 {
+  constexpr S3() = default;
+  S3(const S3&) {} // expected-note {{declared here}}
+};
+template <S3> struct T3 {}; // expected-note {{non-type template argument is required to be copyable}}
+template struct T3<{}>; // expected-error {{non-type template argument is not a constant expression}} \
+                           expected-note {{non-constexpr constructor 'S3' cannot be used in a constant expression}}
+static_assert(!C<S3, T3>);
 }
 
 namespace StableAddress {



More information about the cfe-commits mailing list