[clang] [Sema] Propagate preferred_name attribute to existing specializations (PR #186741)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 17 01:13:07 PDT 2026
https://github.com/Anupamvashistha2002 updated https://github.com/llvm/llvm-project/pull/186741
>From 7ad44dde5bd674ae5f433ce16278713ffeec1118 Mon Sep 17 00:00:00 2001
From: Anupam Vashistha <anupamvashistha44 at gmail.com>
Date: Mon, 16 Mar 2026 12:13:16 +0530
Subject: [PATCH] [Sema] Propagate preferred_name attribute to existing
specializations
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaDeclAttr.cpp | 16 +++++++++++++---
clang/test/SemaCXX/gh106358.cpp | 18 ++++++++++++++++++
3 files changed, 32 insertions(+), 3 deletions(-)
create mode 100644 clang/test/SemaCXX/gh106358.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index af124fce2bc64..a6661fac84987 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -299,6 +299,7 @@ Bug Fixes in This Version
- Fixed an assertion failure when evaluating ``_Countof`` on invalid ``void``-typed operands. (#GH180893)
- Fixed an assertion failure in the serialized diagnostic printer when it is destroyed without calling ``finish()``. (#GH140433)
- Fixed an assertion failure caused by error recovery while extending a nested name specifier with results from ordinary lookup. (#GH181470)
+- Fixed a bug where the ``preferred_name`` attribute was not propagated to existing specializations. (#GH106358)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 3abc69d0e4b96..519c0d59c3214 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -1209,7 +1209,6 @@ static void handlePreferredName(Sema &S, Decl *D, const ParsedAttr &AL) {
TSI = S.Context.getTrivialTypeSourceInfo(T, AL.getLoc());
if (!T.hasQualifiers() && T->isTypedefNameType()) {
- // Find the template name, if this type names a template specialization.
const TemplateDecl *Template = nullptr;
if (const auto *CTSD = dyn_cast_if_present<ClassTemplateSpecializationDecl>(
T->getAsCXXRecordDecl())) {
@@ -1222,7 +1221,19 @@ static void handlePreferredName(Sema &S, Decl *D, const ParsedAttr &AL) {
}
if (Template && declaresSameEntity(Template, CTD)) {
- D->addAttr(::new (S.Context) PreferredNameAttr(S.Context, AL, TSI));
+ auto *PNA = ::new (S.Context) PreferredNameAttr(S.Context, AL, TSI);
+ D->addAttr(PNA);
+
+ for (auto *Spec : CTD->specializations()) {
+ const TypeDecl *TD = static_cast<const TypeDecl *>(Spec);
+ if (S.Context.hasSameType(S.Context.getTypeDeclType(TD),
+ TSI->getType())) {
+ for (auto *R : Spec->redecls()) {
+ if (!R->hasAttr<PreferredNameAttr>())
+ R->addAttr(PNA->clone(S.Context));
+ }
+ }
+ }
return;
}
}
@@ -1233,7 +1244,6 @@ static void handlePreferredName(Sema &S, Decl *D, const ParsedAttr &AL) {
S.Diag(TT->getDecl()->getLocation(), diag::note_entity_declared_at)
<< TT->getDecl();
}
-
static void handleNoSpecializations(Sema &S, Decl *D, const ParsedAttr &AL) {
StringRef Message;
if (AL.getNumArgs() != 0)
diff --git a/clang/test/SemaCXX/gh106358.cpp b/clang/test/SemaCXX/gh106358.cpp
new file mode 100644
index 0000000000000..82d6378ad12eb
--- /dev/null
+++ b/clang/test/SemaCXX/gh106358.cpp
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// GH106358: Test that preferred_name propagates to specializations
+// instantiated before the attribute is seen.
+
+template<typename T>
+void foo(T arg) {} // expected-note {{candidate function template not viable: no known conversion from 'int' to 'my_string' (aka 'my_basic_string<char>') for 1st argument}}
+
+template<typename T> struct my_basic_string;
+using my_string = my_basic_string<char>;
+
+// This attribute application now correctly propagates to the existing specialization
+template<typename T>
+struct __attribute__((__preferred_name__(my_string))) my_basic_string {};
+
+int main() {
+ foo<my_string>(123); // expected-error {{no matching function for call to 'foo'}}
+}
More information about the cfe-commits
mailing list