[clang] d58d5d6 - [clang] Compound Literal Statement Constant Expression Assertion Fix (#139479)
via cfe-commits
cfe-commits at lists.llvm.org
Thu May 15 09:23:48 PDT 2025
Author: Vincent
Date: 2025-05-15T12:23:45-04:00
New Revision: d58d5d6e9d6761f88344f439e7fdf63d41c8c384
URL: https://github.com/llvm/llvm-project/commit/d58d5d6e9d6761f88344f439e7fdf63d41c8c384
DIFF: https://github.com/llvm/llvm-project/commit/d58d5d6e9d6761f88344f439e7fdf63d41c8c384.diff
LOG: [clang] Compound Literal Statement Constant Expression Assertion Fix (#139479)
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
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaExpr.cpp
clang/test/SemaCXX/cxx2a-consteval.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f271a8303cbc9..f5ada716d40ec 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -601,6 +601,7 @@ Bug Fixes in This Version
- Fixed a crash with an invalid member function parameter list with a default
argument which contains a pragma. (#GH113722)
- Fixed assertion failures when generating name lookup table in modules. (#GH61065, #GH134739)
+- Fixed an assertion failure in constant compound literal statements. (#GH139160)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index ae0626ebadb18..91e63c7cb8677 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -7318,10 +7318,20 @@ Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo,
? VK_PRValue
: VK_LValue;
+ // 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 (IsFileScope)
if (auto ILE = dyn_cast<InitListExpr>(LiteralExpr))
for (unsigned i = 0, j = ILE->getNumInits(); i != j; i++) {
Expr *Init = ILE->getInit(i);
+ if (!Init->isTypeDependent() && !Init->isValueDependent() &&
+ !Init->isConstantInitializer(Context, /*IsForRef=*/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 at -1 {{non-constexpr function 'f' cannot be used in a constant expression}}
+ // expected-error at -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 at -1 {{call to consteval function 'GH139160::get_value' is not a constant expression}}
+ // expected-note at -2 {{non-constexpr function 'make_struct' cannot be used in a constant expression}}
+};
+
+
More information about the cfe-commits
mailing list