================
@@ -7541,18 +7541,53 @@ Sema::BuildCompoundLiteralExpr(SourceLocation
LParenLoc, TypeSourceInfo *TInfo,
// "If the compound literal occurs outside the body of a function, the
// initializer list shall consist of constant expressions."
if (IsFileScope)
- if (auto ILE = dyn_cast<InitListExpr>(LiteralExpr))
+ if (auto ILE = dyn_cast<InitListExpr>(LiteralExpr)) {
+ // A default argument or default member initializer containing an
+ // immediate call or source_location is rebuilt at each use site.
+ bool InDefaultArgOrInit =
+ isCheckingDefaultArgumentOrInitializer() ||
+ InnermostDeclarationWithDelayedImmediateInvocations().has_value();
for (unsigned i = 0, j = ILE->getNumInits(); i != j; i++) {
Expr *Init = ILE->getInit(i);
- if (!Init->isTypeDependent() && !Init->isValueDependent() &&
- !Init->isConstantInitializer(Context)) {
+ // An immediate invocation is already a ConstantExpr and receives its
+ // value at the end of the full-expression.
+ if (isa<ConstantExpr>(Init))
+ continue;
+ if (Init->isTypeDependent() || Init->isValueDependent()) {
+ ILE->setInit(i, ConstantExpr::Create(Context, Init));
+ continue;
+ }
+ // A glvalue element binds a reference member; store its address.
+ bool IsRef = Init->isGLValue();
+ Expr::EvalResult Eval;
+ bool Evaluated =
+ IsRef ? Init->EvaluateAsLValue(Eval, Context,
+ /*InConstantContext=*/true)
+ : Init->EvaluateAsRValue(Eval, Context,
+ /*InConstantContext=*/true);
+ Evaluated = Evaluated && !Eval.HasSideEffects && Eval.Val.hasValue();
+ // Not every constant initializer evaluates to a value, e.g. a union
+ // that is non-trivial to destroy; fall back to the structural rules.
+ if (!Evaluated && !Init->isConstantInitializer(Context, IsRef)) {
----------------
akash-manna-sky wrote:
Yes. Under ARC:
```cpp
typedef union { id f0; __weak id f1; } U0;
typedef struct { U0 f0; id f1; } S0;
S0 *p = &(S0){ .f0 = {0}, .f1 = 0 };
```
The `U0` element is materialized as a temporary, and since the type needs
destruction the cleanup counts as a side effect, so no value comes out; the
structural rules recurse per field and never see that. Added this as `sg5` in
`SemaObjC/non-trivial-c-union.m`.
https://github.com/llvm/llvm-project/pull/221390
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits