Skip to content

release/20.x: [clang] Don't evaluate the initializer of constexpr-unknown parameters. (#142498) #142648

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

Conversation

efriedma-quic
Copy link
Collaborator

Backport 9788521

@efriedma-quic efriedma-quic requested a review from shafik June 3, 2025 17:49
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 3, 2025

@llvm/pr-subscribers-clang

Author: Eli Friedman (efriedma-quic)

Changes

Backport 9788521


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 --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);
+  }
+}

Copy link
Contributor

@rockwotj rockwotj left a comment

Choose a reason for hiding this comment

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

Thank you!

@tstellar tstellar 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
@tstellar tstellar moved this from Needs Triage to Needs Review in LLVM Release Status Jun 3, 2025
@rockwotj
Copy link
Contributor

@shafik @efriedma-quic any chance to get this merged?

@efriedma-quic
Copy link
Collaborator Author

Didn't realize this was still waiting for review... added a couple more reviewers.

@tstellar
Copy link
Collaborator

ping @cor3ntin @tbaederr

@github-project-automation github-project-automation bot moved this from Needs Review to Needs Merge in LLVM Release Status Jun 12, 2025
@tstellar tstellar force-pushed the constexpr-unknown-param-20.x branch from c40a997 to 6fa0cdf Compare June 12, 2025 18:41
@tstellar tstellar merged commit 6fa0cdf into llvm:release/20.x Jun 12, 2025
7 of 8 checks passed
@github-project-automation github-project-automation bot moved this from Needs Merge to Done in LLVM Release Status Jun 12, 2025
Copy link

@efriedma-quic (or anyone else). If you would like to add a note about this fix in the release notes (completely optional). Please reply to this comment with a one or two sentence description of the fix. When you are done, please add the release:note label to this PR.

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
Development

Successfully merging this pull request may close these issues.

5 participants