diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 73aaaad8b32e5..9b02a5b923386 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -495,6 +495,8 @@ Bug Fixes to C++ Support nontrivial member when another member has an initializer. (#GH81774) - Fixed a template depth issue when parsing lambdas inside a type constraint. (#GH162092) - Diagnose unresolved overload sets in non-dependent compound requirements. (#GH51246) (#GH97753) +- Fixed a crash that occurred when a static constexpr data member of a class + template was declared with an incomplete type. (#GH120371) Bug Fixes to AST Handling ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index fc3aabf5741ca..db9e4ac4b7897 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -14933,7 +14933,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}} +}