Skip to content

[clang] Compound Literal Statement Constant Expression Assertion Fix #139479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[clang] Compound Literal Statement Constant Expression Assertion Fix
Compound literals initializer-list should be a constant expression if it is defined outside the body of a function.

Emit error instead of falling through tripping assertion error.

fixes #139160
  • Loading branch information
Mr-Anyone committed May 13, 2025
commit 86029ca424ddee771b95182b53d5bbdfd7fd049d
11 changes: 11 additions & 0 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7245,6 +7245,17 @@ Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo,
if (auto ILE = dyn_cast<InitListExpr>(LiteralExpr))
for (unsigned i = 0, j = ILE->getNumInits(); i != j; i++) {
Expr *Init = ILE->getInit(i);
// C99 6.5.2.5
// "If the compound literal occurs outside the body of a function, the
// initializer list shall consist of constant expressions."
if (!Init->isTypeDependent() && !Init->isValueDependent() &&
!Init->getType()->isDependentType())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not checking for "outside of the function".
Note that GCC errors on these cases too https://compiler-explorer.com/z/TaqzEdnbe
I think you need to update the comment to better describe the check

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a if (IsFileScope) 4 lines above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have moved the comment to the if (IsFileScope), is that alright?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need the check for !Init->getType()->isDependentType() because computeDependence() on the expression should already account for that, shouldn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need the check for !Init->getType()->isDependentType() because computeDependence() on the expression should already account for that, shouldn't it?

You're absolutely right—I just removed that check. Thanks!

if (!Init->isConstantInitializer(Context, false)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A code style nit, we try to add comments explaining the values with parameter name

Suggested change
if (!Init->isConstantInitializer(Context, false)) {
if (!Init->isConstantInitializer(Context, /*IsForRef=*/ false)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, thanks.

Diag(Init->getExprLoc(), diag::err_init_element_not_constant)
<< Init->getSourceBitField();
return ExprError();
}

ILE->setInit(i, ConstantExpr::Create(Context, Init));
}

Expand Down
22 changes: 22 additions & 0 deletions clang/test/SemaCXX/cxx2a-consteval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1300,3 +1300,25 @@ void foo() {
}

}

// https://github.com/llvm/llvm-project/issues/139160
namespace GH139160{
// original test case taken from Github
struct A {int x[1]; };
A f(); // expected-note {{declared here}}
typedef int *t[];
consteval int* f(int* x) { return x; }

int ** x = (t){f(f().x)}; // expected-error {{call to consteval function 'GH139160::f' is not a constant expression}}
// expected-note@-1 {{non-constexpr function 'f' cannot be used in a constant expression}}
// expected-error@-2 {{initializer element is not a compile-time constant}}

struct B {int value, value_two;};
B make_struct() {return {10, 20};} // expected-note {{declared here}}
consteval int get_value(B container) {return container.value;}
B result = (B){10, get_value(make_struct())}; // expected-error {{initializer element is not a compile-time constant}}
// expected-error@-1 {{call to consteval function 'GH139160::get_value' is not a constant expression}}
// expected-note@-2 {{non-constexpr function 'make_struct' cannot be used in a constant expression}}
};