Skip to content

Conversation

@Mr-Anyone
Copy link
Contributor

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

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 11, 2025
@llvmbot
Copy link
Member

llvmbot commented May 11, 2025

@llvm/pr-subscribers-clang

Author: Vincent (Mr-Anyone)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/139479.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaExpr.cpp (+11)
  • (modified) clang/test/SemaCXX/cxx2a-consteval.cpp (+22)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index deb8d2edfc5c9..e06b6fc233fcc 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -7220,6 +7220,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())
+          if (!Init->isConstantInitializer(Context, false)) {
+            Diag(Init->getExprLoc(), diag::err_init_element_not_constant)
+                << Init->getSourceBitField();
+            return ExprError();
+          }
+
         ILE->setInit(i, ConstantExpr::Create(Context, Init));
       }
 
diff --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp
index d9d144cafdbcc..d9932e4dd8241 100644
--- a/clang/test/SemaCXX/cxx2a-consteval.cpp
+++ b/clang/test/SemaCXX/cxx2a-consteval.cpp
@@ -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}}
+};
+
+

Comment on lines 7226 to 7252
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
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

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

This change needs a release note.
Please add an entry to clang/docs/ReleaseNotes.rst in the section the most adapted to the change, and referencing any Github issue this change fixes. Thanks!

@cor3ntin cor3ntin requested a review from AaronBallman May 12, 2025 06:37
// initializer list shall consist of constant expressions."
if (!Init->isTypeDependent() && !Init->isValueDependent() &&
!Init->getType()->isDependentType())
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.

Comment on lines 7226 to 7252
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.

There is a if (IsFileScope) 4 lines above.

@Mr-Anyone Mr-Anyone requested review from Fznamznon and cor3ntin May 12, 2025 12:22
Mr-Anyone added 2 commits May 13, 2025 17:44
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 llvm#139160
@Mr-Anyone Mr-Anyone force-pushed the compound_literal_fix branch from 4e1d5b3 to b969ec1 Compare May 13, 2025 21:49
for (unsigned i = 0, j = ILE->getNumInits(); i != j; i++) {
Expr *Init = ILE->getInit(i);
if (!Init->isTypeDependent() && !Init->isValueDependent() &&
!Init->getType()->isDependentType())
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!

@Mr-Anyone Mr-Anyone requested a review from AaronBallman May 14, 2025 18:06
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

Just a minor nit from me, otherwise LG!

@AaronBallman AaronBallman merged commit d58d5d6 into llvm:main May 15, 2025
4 of 6 checks passed
@github-actions
Copy link

@Mr-Anyone Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Assertion `!isa<ConstantExpr>(E)' failed." with compound literal

5 participants