llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: None (Anupamvashistha2002) <details> <summary>Changes</summary> Fixes #<!-- -->106358 This PR ensures the `preferred_name` attribute is propagated to specializations that were instantiated before the attribute was seen on the primary template. The fix iterates through the redeclaration chain of matching specializations and applies a cloned attribute to each, ensuring consistent diagnostic output (the 'aka' message) regardless of which declaration node is accessed. Validated with new test: `clang/test/SemaCXX/gh106358.cpp` --- Full diff: https://github.com/llvm/llvm-project/pull/186741.diff 2 Files Affected: - (modified) clang/lib/Sema/SemaDeclAttr.cpp (+15-1) - (added) clang/test/SemaCXX/gh106358.cpp (+18) ``````````diff diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 3abc69d0e4b96..0f1c867acdcfe 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -1222,7 +1222,21 @@ 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; } } 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'}} +} `````````` </details> https://github.com/llvm/llvm-project/pull/186741 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
