[clang] [clang] Profile variable templates found through using-declarations by declaration (PR #249391)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Dec 6 02:06:48 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Avi Kivity (avikivity)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/249391.diff
2 Files Affected:
- (modified) clang/lib/AST/StmtProfile.cpp (+7-2)
- (added) clang/test/SemaTemplate/var-template-using-declaration-equivalence.cpp (+23)
``````````diff
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>({}); }
``````````
</details>
https://github.com/llvm/llvm-project/pull/249391
More information about the cfe-commits
mailing list