[clang] [clang] Profile variable templates found through using-declarations by declaration (PR #249391)

Avi Kivity via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 6 02:06:46 PST 2026


https://github.com/avikivity updated https://github.com/llvm/llvm-project/pull/249391

>From ba9d437ec37c6c2ba672b1641b4e0eebba2f4025 Mon Sep 17 00:00:00 2001
From: Avi Kivity <avi at scylladb.com>
Date: Sun, 6 Dec 2026 11:10:06 +0200
Subject: [PATCH] [clang] Profile variable templates found through
 using-declarations by declaration

StmtProfiler::VisitOverloadExpr profiles a dependent reference to a
single variable template by the template declaration, but only checked
for a VarTemplateDecl directly. When the template is found through a
using-declaration the decl is a UsingShadowDecl, so the profile fell back
to the qualifier and name, and m::v<T> and n::v<T> (with
'namespace m { using n::v; }') profiled differently. Equivalent
redeclarations of alias templates and function templates were then
rejected as redefinitions with different types or as ambiguous overloads.

Look through the using-declaration.

Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>

Fixes #249391
---
 clang/lib/AST/StmtProfile.cpp                 |  9 ++++++--
 ...template-using-declaration-equivalence.cpp | 23 +++++++++++++++++++
 2 files changed, 30 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/SemaTemplate/var-template-using-declaration-equivalence.cpp

diff --git a/clang/lib/AST/StmtProfile.cpp b/clang/lib/AST/StmtProfile.cpp
index 476610b9956c71..c23658b26d80c1 100644
--- a/clang/lib/AST/StmtProfile.cpp
+++ b/clang/lib/AST/StmtProfile.cpp
@@ -2340,10 +2340,15 @@ StmtProfiler::VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *S) {
 
 void StmtProfiler::VisitOverloadExpr(const OverloadExpr *S) {
   VisitExpr(S);
+  // Look through using-declarations: m::v<T> names the same variable
+  // template as n::v<T> after 'namespace m { using n::v; }'.
+  const NamedDecl *Single = S->getNumDecls() == 1
+                                ? (*S->decls_begin())->getUnderlyingDecl()
+                                : nullptr;
   bool DescribingDependentVarTemplate =
-      S->getNumDecls() == 1 && isa<VarTemplateDecl>(*S->decls_begin());
+      isa_and_nonnull<VarTemplateDecl>(Single);
   if (DescribingDependentVarTemplate) {
-    VisitDecl(*S->decls_begin());
+    VisitDecl(Single);
   } else {
     VisitNestedNameSpecifier(S->getQualifier());
     VisitName(S->getName(), /*TreatAsDecl*/ true);
diff --git a/clang/test/SemaTemplate/var-template-using-declaration-equivalence.cpp b/clang/test/SemaTemplate/var-template-using-declaration-equivalence.cpp
new file mode 100644
index 00000000000000..f3ad6fc8d9f75e
--- /dev/null
+++ b/clang/test/SemaTemplate/var-template-using-declaration-equivalence.cpp
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+// A dependent reference to a variable template names the same template
+// whether it's found directly or through a using-declaration, so these
+// declarations are equivalent ([temp.over.link]).
+
+namespace n {
+template <class T> constexpr bool v = true;
+template <bool> struct S {};
+} // namespace n
+
+namespace m {
+using n::v;
+} // namespace m
+
+template <class T> using A = n::S<m::v<T>>;
+template <class T> using A = n::S<n::v<T>>;
+
+template <class T> void f(n::S<m::v<T>>);
+template <class T> void f(n::S<n::v<T>>) {}
+
+void g() { f<int>({}); }



More information about the cfe-commits mailing list