diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 0069b08f1991a..95aa7fbc0a321 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -14919,7 +14919,7 @@ void Sema::CheckCompleteVariableDeclaration(VarDecl *var) { // Apply section attributes and pragmas to global variables. if (GlobalStorage && var->isThisDeclarationADefinition() && - !inTemplateInstantiation()) { + !var->getDeclContext()->isDependentContext()) { PragmaStack *Stack = nullptr; int SectionFlags = ASTContext::PSF_Read; bool MSVCEnv = diff --git a/clang/test/SemaCXX/attr-section.cpp b/clang/test/SemaCXX/attr-section.cpp index 1c07e3dd8bba2..6ed432f1cf7e0 100644 --- a/clang/test/SemaCXX/attr-section.cpp +++ b/clang/test/SemaCXX/attr-section.cpp @@ -65,7 +65,30 @@ struct t1 { constexpr t1(int) { } }; extern const t1 v1; -__attribute__((section("non_trivial_ctor"))) const t1 v1; // expected-note {{declared here}} +__attribute__((section("non_trivial_ctor"))) const t1 v1; // expected-note 2 {{declared here}} extern const t1 v2; __attribute__((section("non_trivial_ctor"))) const t1 v2{3}; // expected-error {{'v2' causes a section type conflict with 'v1'}} } // namespace non_trivial_ctor + +namespace dependent_context { +template +struct A { + struct B; + static constexpr B b{nullptr}; // This used to crash. +}; + +struct B1 { void *p; }; +template +struct A1 { + __attribute__((section("non_trivial_ctor"))) + static constexpr B1 b{nullptr}; // no diagnostic expected +}; + +template +struct C { + __attribute__((section("non_trivial_ctor"))) + static constexpr int m{123}; // expected-error {{'m' causes a section type conflict with 'v1'}} +}; + +auto *p = &C::m; // expected-note {{in instantiation of static data member 'dependent_context::C::m' requested here}} +}