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

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 18:25:15 PST 2026


Author: Younan Zhang
Date: 2026-11-24T10:25:09+08:00
New Revision: c9c633c6e9bad64718575df90f4046c37e5190ec

URL: https://github.com/llvm/llvm-project/commit/c9c633c6e9bad64718575df90f4046c37e5190ec
DIFF: https://github.com/llvm/llvm-project/commit/c9c633c6e9bad64718575df90f4046c37e5190ec.diff

LOG: [Clang] Use canonical decl for variable template specialization matching (#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

Added: 
    clang/test/SemaCXX/constrained-variable-specialization.cpp

Modified: 
    clang/docs/ReleaseNotes.md
    clang/lib/Sema/SemaTemplate.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 3d4726f4c9cc4..2c472c64e0fa6 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1078,6 +1078,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 411ff948012a2..5e45176e432b2 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -4764,7 +4764,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 0000000000000..ce59e76c5f1bd
--- /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