diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 90525f386468a..c901c447ad0d7 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -13775,7 +13775,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) { } // Perform the initialization. - ParenListExpr *CXXDirectInit = dyn_cast(Init); + bool InitializedFromParenListExpr = false; bool IsParenListInit = false; if (!VDecl->isInvalidDecl()) { InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); @@ -13783,9 +13783,14 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) { VDecl->getLocation(), DirectInit, Init); MultiExprArg Args = Init; - if (CXXDirectInit) - Args = MultiExprArg(CXXDirectInit->getExprs(), - CXXDirectInit->getNumExprs()); + if (auto *CXXDirectInit = dyn_cast(Init)) { + Args = + MultiExprArg(CXXDirectInit->getExprs(), CXXDirectInit->getNumExprs()); + InitializedFromParenListExpr = true; + } else if (auto *CXXDirectInit = dyn_cast(Init)) { + Args = CXXDirectInit->getInitExprs(); + InitializedFromParenListExpr = true; + } // Try to correct any TypoExprs in the initialization arguments. for (size_t Idx = 0; Idx < Args.size(); ++Idx) { @@ -14083,10 +14088,9 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) { // special case code. // C++ 8.5p11: - // The form of initialization (using parentheses or '=') is generally - // insignificant, but does matter when the entity being initialized has a - // class type. - if (CXXDirectInit) { + // The form of initialization (using parentheses or '=') matters + // when the entity being initialized has class type. + if (InitializedFromParenListExpr) { assert(DirectInit && "Call-style initializer must be direct init."); VDecl->setInitStyle(IsParenListInit ? VarDecl::ParenListInit : VarDecl::CallInit); diff --git a/clang/test/SemaCXX/paren-list-agg-init.cpp b/clang/test/SemaCXX/paren-list-agg-init.cpp index ba7dffdc1af9f..680fdcdbe7b1c 100644 --- a/clang/test/SemaCXX/paren-list-agg-init.cpp +++ b/clang/test/SemaCXX/paren-list-agg-init.cpp @@ -403,3 +403,25 @@ void test() { S{}.f(); // beforecxx20-note {{requested here}} } } + +namespace GH72880_regression { +struct E { + int i = 42; +}; +struct G { + E e; +}; +template +struct Test { + void f() { + constexpr E e; + //FIXME: We should only warn one + constexpr G g(e); // beforecxx20-warning 2{{C++20 extension}} + static_assert(g.e.i == 42); + } +}; +void test() { + Test{}.f(); // beforecxx20-note {{requested here}} +} + +}