[clang] [Clang] Use canonical decl for variable template specialization matching (PR #245195)

Younan Zhang via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 03:22:37 PST 2026


https://github.com/zyn0217 created https://github.com/llvm/llvm-project/pull/245195

getPartialSpecializations returns the most recent declaration which could be the declaration hidden inside modules that we don't want it to participate in declaration comparison.

We always use the canonical decl for that purpose. This ensures that they all point to the same template entity.

Fixes https://github.com/llvm/llvm-project/issues/244974

>From 4cbe75a5afd1703af04b8cd812899e77dfd25d2f Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Mon, 23 Nov 2026 19:14:33 +0800
Subject: [PATCH] [Clang] Use canonical decl for variable template
 specialization matching

getPartialSpecializations returns the most recent declaration which could
be the declaration hidden inside modules that we don't want it to participate in
declaration comparison.

We always use the canonical decl for that purpose. This ensures that they
all point to the same template entity.
---
 clang/docs/ReleaseNotes.md                    |  3 ++
 clang/lib/Sema/SemaTemplate.cpp               |  3 +-
 .../constrained-variable-specialization.cpp   | 28 +++++++++++++++++++
 3 files changed, 33 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaCXX/constrained-variable-specialization.cpp

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 409d2459b0580b..d0587452e23819 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -938,6 +938,9 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
   misparsed as function declarations in direct-initialization contexts under
   `-fms-extensions` or in HLSL mode.
 
+- Fixed a crash on partial variable template specializations when part of which are
+  imported from modules. (#GH244974)
+
 - Fixed the destruction timing of temporaries created by default member
   initializers during aggregate initialization. Such an initializer is part of
   the full-expression containing the aggregate initialization. (#GH85601)
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 0caf7593ca9d5b..4c987135b59204 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -4762,7 +4762,8 @@ Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc,
       (void)Result;
     } else {
       Matched.push_back(PartialSpecMatchResult());
-      Matched.back().Partial = Partial;
+      Matched.back().Partial = cast<VarTemplatePartialSpecializationDecl>(
+          Partial->getCanonicalDecl());
       Matched.back().Args = Info.takeSugared();
     }
   }
diff --git a/clang/test/SemaCXX/constrained-variable-specialization.cpp b/clang/test/SemaCXX/constrained-variable-specialization.cpp
new file mode 100644
index 00000000000000..ce59e76c5f1bd1
--- /dev/null
+++ b/clang/test/SemaCXX/constrained-variable-specialization.cpp
@@ -0,0 +1,28 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+
+// RUN: %clang_cc1 -std=c++20 -I%t %t/a.cpp -emit-module-interface -o %t/a.pcm
+// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t -I%t %t/b.cpp -verify -fsyntax-only
+
+//--- header.hpp
+template<typename> inline constexpr bool x = true;
+template<class T> requires true inline constexpr bool x<T> = true;
+
+//--- a.cpp
+module;
+#include "header.hpp"
+export module a;
+static_assert(x<void>);
+
+//--- b.cpp
+// expected-no-diagnostics
+#include "header.hpp"
+import a;
+
+template<typename = void>
+struct s;
+
+template<typename T>
+constexpr bool x<s<T>> = true;
+
+static_assert(x<s<void>>);



More information about the cfe-commits mailing list