https://github.com/keinflue created 
https://github.com/llvm/llvm-project/pull/152864

If a template argument in a partial specialization of a variable template 
directly refers to a NTTP of the specialization without implicit type 
conversion it was assumed that the NTTP is identical to that of the primary 
template.

This doesn't hold if the primary template's NTTP uses a deduced type, so 
instead compare the types explicitly as well.

The affected function is used only to provide an improved early error if the 
partial specialization has identical template arguments to the primary 
template. The actual check that the partial specialization is more specialized 
happens later.

Fixes #118190
Fixes #152750

>From f36c8dbedace87b378d2870402eb1119a2ed253f Mon Sep 17 00:00:00 2001
From: keinflue <keinf...@posteo.de>
Date: Sat, 9 Aug 2025 09:33:20 +0200
Subject: [PATCH] [clang] Distinguish NTTPs with deduced types in variable
 template partial specializations

If a template argument in a partial specialization of a variable template 
directly refers to a NTTP of the
specialization without implicit type conversion it was assumed that the NTTP is 
identical to that of the
primary template.

This doesn't hold if the primary template's NTTP uses a deduced type, so 
instead compare the types explicitly
as well.

Fixes #118190
Fixes #152750
---
 clang/lib/Sema/SemaTemplate.cpp                    | 6 +++++-
 clang/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp | 5 +++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 2d8fdb5b766fc..ae16323535bdc 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -4126,7 +4126,11 @@ static bool isTemplateArgumentTemplateParameter(const 
TemplateArgument &Arg,
       return false;
     const NonTypeTemplateParmDecl *NTTP =
         dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
-    return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index;
+    if (!NTTP || NTTP->getDepth() != Depth || NTTP->getIndex() != Index)
+      return false;
+    QualType ParamType = cast<NonTypeTemplateParmDecl>(Param)->getType();
+    QualType NTTPType = NTTP->getType();
+    return ParamType.getCanonicalType() == NTTPType.getCanonicalType();
   }
 
   case TemplateArgument::Template:
diff --git a/clang/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp 
b/clang/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp
index c35743b87adbc..9c25e26f43c36 100644
--- a/clang/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp
+++ b/clang/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp
@@ -621,3 +621,8 @@ namespace GH73460 {
   int j;
   template struct A<int&, j, j>;
 } // namespace GH73460
+
+namespace GH118190 {
+  template <auto> int x;
+  template <int i> int x<i>;
+}

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to