Skip to content

Commit 5c063be

Browse files
cor3ntinshafik
andauthored
[Clang] Fix a regression introduced by #138518 (#141342)
We did not handle the case where a variable could be initialized by a CXXParenListInitExpr. --------- Co-authored-by: Shafik Yaghmour <[email protected]>
1 parent 11b5e29 commit 5c063be

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

clang/lib/Sema/SemaDecl.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13774,17 +13774,22 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
1377413774
}
1377513775

1377613776
// Perform the initialization.
13777-
ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init);
13777+
bool InitializedFromParenListExpr = false;
1377813778
bool IsParenListInit = false;
1377913779
if (!VDecl->isInvalidDecl()) {
1378013780
InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl);
1378113781
InitializationKind Kind = InitializationKind::CreateForInit(
1378213782
VDecl->getLocation(), DirectInit, Init);
1378313783

1378413784
MultiExprArg Args = Init;
13785-
if (CXXDirectInit)
13786-
Args = MultiExprArg(CXXDirectInit->getExprs(),
13787-
CXXDirectInit->getNumExprs());
13785+
if (auto *CXXDirectInit = dyn_cast<ParenListExpr>(Init)) {
13786+
Args =
13787+
MultiExprArg(CXXDirectInit->getExprs(), CXXDirectInit->getNumExprs());
13788+
InitializedFromParenListExpr = true;
13789+
} else if (auto *CXXDirectInit = dyn_cast<CXXParenListInitExpr>(Init)) {
13790+
Args = CXXDirectInit->getInitExprs();
13791+
InitializedFromParenListExpr = true;
13792+
}
1378813793

1378913794
// Try to correct any TypoExprs in the initialization arguments.
1379013795
for (size_t Idx = 0; Idx < Args.size(); ++Idx) {
@@ -14082,10 +14087,9 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
1408214087
// special case code.
1408314088

1408414089
// C++ 8.5p11:
14085-
// The form of initialization (using parentheses or '=') is generally
14086-
// insignificant, but does matter when the entity being initialized has a
14087-
// class type.
14088-
if (CXXDirectInit) {
14090+
// The form of initialization (using parentheses or '=') matters
14091+
// when the entity being initialized has class type.
14092+
if (InitializedFromParenListExpr) {
1408914093
assert(DirectInit && "Call-style initializer must be direct init.");
1409014094
VDecl->setInitStyle(IsParenListInit ? VarDecl::ParenListInit
1409114095
: VarDecl::CallInit);

clang/test/SemaCXX/paren-list-agg-init.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,25 @@ void test() {
403403
S<int>{}.f(); // beforecxx20-note {{requested here}}
404404
}
405405
}
406+
407+
namespace GH72880_regression {
408+
struct E {
409+
int i = 42;
410+
};
411+
struct G {
412+
E e;
413+
};
414+
template <typename>
415+
struct Test {
416+
void f() {
417+
constexpr E e;
418+
//FIXME: We should only warn one
419+
constexpr G g(e); // beforecxx20-warning 2{{C++20 extension}}
420+
static_assert(g.e.i == 42);
421+
}
422+
};
423+
void test() {
424+
Test<int>{}.f(); // beforecxx20-note {{requested here}}
425+
}
426+
427+
}

0 commit comments

Comments
 (0)