[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 05:44:19 PST 2026


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

>From 81963288b38b0c9166efa4908611ba316fca1fef 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 1/2] [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 #249390
---
 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>({}); }

>From 633a43d24e47f6efc258bb50a4019fb00fb96c70 Mon Sep 17 00:00:00 2001
From: Avi Kivity <avi at scylladb.com>
Date: Sun, 6 Dec 2026 15:20:30 +0200
Subject: [PATCH 2/2] Address review comments

- Add OverloadExpr::getAsSingleDecl() and use it in StmtProfile,
  SemaTemplateVariadic and SemaOverload.
- Move the test into temp.over.link/p4.cpp.
- Add a release note.

Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
 clang/docs/ReleaseNotes.md                    |  4 ++++
 clang/include/clang/AST/ExprCXX.h             |  6 +++++
 clang/lib/AST/StmtProfile.cpp                 | 12 ++++------
 clang/lib/Sema/SemaOverload.cpp               |  7 ++----
 clang/lib/Sema/SemaTemplateVariadic.cpp       | 10 ++++----
 .../temp.decls/temp.fct/temp.over.link/p4.cpp | 22 ++++++++++++++++++
 ...template-using-declaration-equivalence.cpp | 23 -------------------
 7 files changed, 43 insertions(+), 41 deletions(-)
 delete mode 100644 clang/test/SemaTemplate/var-template-using-declaration-equivalence.cpp

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index b4acef06540023..deab67bca90e6e 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1432,6 +1432,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 - Fixed a crash when template substitution produces an invalid controlling type
   for `_Generic`. (#GH242122)
 
+- Fixed equivalent redeclarations of templates being rejected as redefinitions
+  or ambiguous overloads when they refer to the same variable template, once
+  directly and once through a using-declaration. (#GH249390)
+
 #### Bug Fixes to AST Handling
 
 - Fixed a non-deterministic ordering of unused local typedefs that made
diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h
index e648bdaef5d98a..4d5194762aaa7a 100644
--- a/clang/include/clang/AST/ExprCXX.h
+++ b/clang/include/clang/AST/ExprCXX.h
@@ -3246,6 +3246,12 @@ class OverloadExpr : public Expr {
   /// Gets the number of declarations in the unresolved set.
   unsigned getNumDecls() const { return OverloadExprBits.NumResults; }
 
+  /// If the unresolved set contains exactly one declaration, returns it;
+  /// otherwise returns null.
+  NamedDecl *getAsSingleDecl() const {
+    return getNumDecls() == 1 ? *decls_begin() : nullptr;
+  }
+
   /// Gets the full name info.
   const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
 
diff --git a/clang/lib/AST/StmtProfile.cpp b/clang/lib/AST/StmtProfile.cpp
index c23658b26d80c1..2608132207dfe3 100644
--- a/clang/lib/AST/StmtProfile.cpp
+++ b/clang/lib/AST/StmtProfile.cpp
@@ -2342,13 +2342,11 @@ 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 =
-      isa_and_nonnull<VarTemplateDecl>(Single);
-  if (DescribingDependentVarTemplate) {
-    VisitDecl(Single);
+  const NamedDecl *Single = S->getAsSingleDecl();
+  const auto *VTD =
+      Single ? dyn_cast<VarTemplateDecl>(Single->getUnderlyingDecl()) : nullptr;
+  if (VTD) {
+    VisitDecl(VTD);
   } else {
     VisitNestedNameSpecifier(S->getQualifier());
     VisitName(S->getName(), /*TreatAsDecl*/ true);
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 4497c7f54e0adc..f5a3c03dce77de 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -14989,11 +14989,8 @@ bool Sema::buildOverloadedCallSet(Scope *S, Expr *Fn,
 
     // We don't perform ADL for implicit declarations of builtins.
     // Verify that this was correctly set up.
-    FunctionDecl *F;
-    if (ULE->decls_begin() != ULE->decls_end() &&
-        ULE->decls_begin() + 1 == ULE->decls_end() &&
-        (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) &&
-        F->getBuiltinID() && F->isImplicit())
+    if (auto *F = dyn_cast_if_present<FunctionDecl>(ULE->getAsSingleDecl());
+        F && F->getBuiltinID() && F->isImplicit())
       llvm_unreachable("performing ADL for builtin");
 
     // We don't perform ADL in C.
diff --git a/clang/lib/Sema/SemaTemplateVariadic.cpp b/clang/lib/Sema/SemaTemplateVariadic.cpp
index 2d6e75bd77b402..a533389bd15a33 100644
--- a/clang/lib/Sema/SemaTemplateVariadic.cpp
+++ b/clang/lib/Sema/SemaTemplateVariadic.cpp
@@ -375,12 +375,10 @@ class CollectUnexpandedParameterPacksVisitor
     }
 
     bool TraverseUnresolvedLookupExpr(UnresolvedLookupExpr *E) override {
-      if (E->getNumDecls() == 1) {
-        NamedDecl *ND = *E->decls_begin();
-        if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(ND);
-            TTP && TTP->isParameterPack())
-          addUnexpanded(ND, E->getBeginLoc());
-      }
+      if (auto *TTP = dyn_cast_if_present<TemplateTemplateParmDecl>(
+              E->getAsSingleDecl());
+          TTP && TTP->isParameterPack())
+        addUnexpanded(TTP, E->getBeginLoc());
       return DynamicRecursiveASTVisitor::TraverseUnresolvedLookupExpr(E);
     }
 
diff --git a/clang/test/CXX/temp/temp.decls/temp.fct/temp.over.link/p4.cpp b/clang/test/CXX/temp/temp.decls/temp.fct/temp.over.link/p4.cpp
index d24a3fb71d5df9..39c718cbbbf8f4 100644
--- a/clang/test/CXX/temp/temp.decls/temp.fct/temp.over.link/p4.cpp
+++ b/clang/test/CXX/temp/temp.decls/temp.fct/temp.over.link/p4.cpp
@@ -12,3 +12,25 @@ template<int I> void f0();
 template<typename T> void f0();
 
 
+
+// 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.
+namespace GH249390 {
+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>({}); }
+} // namespace GH249390
diff --git a/clang/test/SemaTemplate/var-template-using-declaration-equivalence.cpp b/clang/test/SemaTemplate/var-template-using-declaration-equivalence.cpp
deleted file mode 100644
index f3ad6fc8d9f75e..00000000000000
--- a/clang/test/SemaTemplate/var-template-using-declaration-equivalence.cpp
+++ /dev/null
@@ -1,23 +0,0 @@
-// 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