Skip to content

Conversation

@efriedma-quic
Copy link
Collaborator

If we see a parameter of reference type that isn't part of the frame, don't try to evaluate its default argument. Just treat it as a constexpr-unknown value.

Fixes #141114. Fixes #141858.

If we see a parameter of reference type that isn't part of the frame,
don't try to evaluate its defualt argument. Just treat it as a
constexpr-unknown value.

Fixes llvm#141114.  Fixes llvm#141858.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 2, 2025

@llvm/pr-subscribers-clang

Author: Eli Friedman (efriedma-quic)

Changes

If we see a parameter of reference type that isn't part of the frame, don't try to evaluate its default argument. Just treat it as a constexpr-unknown value.

Fixes #141114. Fixes #141858.


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

2 Files Affected:

  • (modified) clang/lib/AST/ExprConstant.cpp (+6-1)
  • (modified) clang/test/SemaCXX/constant-expression-p2280r4.cpp (+12)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index b20e2690d0eee..ab964e592de80 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -3550,7 +3550,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 c14250a268f6c..50637917ba210 100644
--- a/clang/test/SemaCXX/constant-expression-p2280r4.cpp
+++ b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
@@ -250,3 +250,15 @@ namespace uninit_reference_used {
   // expected-note {{in call to 'g5()'}}
 
 }
+
+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);
+  }
+}

Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

I was literally just writing this PR and saw you just submitted this one, thank you!

LGTM

@efriedma-quic efriedma-quic merged commit 9788521 into llvm:main Jun 3, 2025
13 of 14 checks passed
@efriedma-quic efriedma-quic added this to the LLVM 20.X Release milestone Jun 3, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Jun 3, 2025
@efriedma-quic
Copy link
Collaborator Author

/cherry-pick 9788521

@llvmbot
Copy link
Member

llvmbot commented Jun 3, 2025

Failed to cherry-pick: 9788521

https://github.com/llvm/llvm-project/actions/runs/15423178980

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

@efriedma-quic
Copy link
Collaborator Author

Backported manually: #142648

@tstellar tstellar moved this from Needs Triage to Done in LLVM Release Status Jun 3, 2025
swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request Jun 12, 2025
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 release:cherry-pick-failed

Projects

Development

Successfully merging this pull request may close these issues.

bad compilation with default arguments in clang 20 Unknown references and pointers related bad codegen

3 participants