diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -4289,6 +4289,17 @@ } } + if (const VarDecl *VD = dyn_cast_or_null( + LVal.Base.dyn_cast())) { + if (const CompoundLiteralExpr *CLE = dyn_cast_or_null( + VD->getAnyInitializer()->IgnoreCasts())) { + QualType CLET = CLE->getType().getCanonicalType(); + if (!CLET.isConstant(Info.Ctx)) { + Info.FFDiag(Conv); + } + } + } + CompleteObject Obj = findCompleteObject(Info, Conv, AK, LVal, Type); return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal, AK); } diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp --- a/clang/test/SemaCXX/constant-expression-cxx11.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp @@ -1595,10 +1595,13 @@ namespace CompoundLiteral { // Matching GCC, file-scope array compound literals initialized by constants // are lifetime-extended. - constexpr int *p = (int*)(int[1]){3}; // expected-warning {{C99}} + constexpr int *p = (int*)(const int[1]){3}; // expected-warning {{C99}} static_assert(*p == 3, ""); static_assert((int[2]){1, 2}[1] == 2, ""); // expected-warning {{C99}} + constexpr int *q = (int *)(int[1]){3}; // expected-warning {{C99}} + static_assert(*q == 3, ""); // expected-error {{constant expression}} + // Other kinds are not. struct X { int a[2]; }; constexpr int *n = (X){1, 2}.a; // expected-warning {{C99}} expected-warning {{temporary}}