[llvm-branch-commits] [clang] release/20.x: [clang] Don't evaluate the initializer of constexpr-unknown parameters. (#142498) (PR #142648)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Jun 3 10:50:13 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Eli Friedman (efriedma-quic)
<details>
<summary>Changes</summary>
Backport 97885213bd4507b204b050c3cd570e365d21cc7d
---
Full diff: https://github.com/llvm/llvm-project/pull/142648.diff
2 Files Affected:
- (modified) clang/lib/AST/ExprConstant.cpp (+6-1)
- (modified) clang/test/SemaCXX/constant-expression-p2280r4.cpp (+12)
``````````diff
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e0746f4532245..209b269122a8e 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -3525,7 +3525,12 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
// should begin within the evaluation of E
// Used to be C++20 [expr.const]p5.12.2:
// ... its lifetime began within the evaluation of E;
- if (isa<ParmVarDecl>(VD) && !AllowConstexprUnknown) {
+ if (isa<ParmVarDecl>(VD)) {
+ if (AllowConstexprUnknown) {
+ Result = &Info.CurrentCall->createConstexprUnknownAPValues(VD, Base);
+ return true;
+ }
+
// Assume parameters of a potential constant expression are usable in
// constant expressions.
if (!Info.checkingPotentialConstantExpression() ||
diff --git a/clang/test/SemaCXX/constant-expression-p2280r4.cpp b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
index 87beeb4d3dc84..dbaebb81b93e8 100644
--- a/clang/test/SemaCXX/constant-expression-p2280r4.cpp
+++ b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
@@ -200,3 +200,15 @@ int f() {
return !get_value<Dummy>(); // contextually convert the function call result to bool
}
}
+
+namespace param_reference {
+ constexpr int arbitrary = -12345;
+ constexpr void f(const int &x = arbitrary) { // expected-note {{declared here}}
+ constexpr const int &v1 = x; // expected-error {{must be initialized by a constant expression}} \
+ // expected-note {{reference to 'x' is not a constant expression}}
+ constexpr const int &v2 = (x, arbitrary); // expected-warning {{left operand of comma operator has no effect}}
+ constexpr int v3 = x; // expected-error {{must be initialized by a constant expression}}
+ static_assert(x==arbitrary); // expected-error {{static assertion expression is not an integral constant expression}}
+ static_assert(&x - &x == 0);
+ }
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/142648
More information about the llvm-branch-commits
mailing list