Skip to content

Conversation

@Kirito-89
Copy link

Clang's constant evaluator would unconditionally reject the initializer of any [[gnu::weak]] variable. This was intended to prevent unsafe constant folding but also incorrectly blocked valid constant evaluation in C++14 (e.g., static constexpr initialization).

This patch refines the logic to only apply the check when the evaluation mode is not EvaluationMode::ConstantExpression. This fixes the bug by allowing required constexpr evaluation to proceed while still preventing unsafe folding in other contexts.

@github-actions
Copy link

github-actions bot commented Nov 6, 2025

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 Nov 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 6, 2025

@llvm/pr-subscribers-clang

Author: Adamya Verma (Kirito-89)

Changes

Clang's constant evaluator would unconditionally reject the initializer of any [[gnu::weak]] variable. This was intended to prevent unsafe constant folding but also incorrectly blocked valid constant evaluation in C++14 (e.g., static constexpr initialization).

This patch refines the logic to only apply the check when the evaluation mode is not EvaluationMode::ConstantExpression. This fixes the bug by allowing required constexpr evaluation to proceed while still preventing unsafe folding in other contexts.


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

1 Files Affected:

  • (modified) clang/lib/AST/ExprConstant.cpp (+1-1)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 97eeba8b9d6cc..cb3a359a6df95 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -3611,7 +3611,7 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
 
   // Never use the initializer of a weak variable, not even for constant
   // folding. We can't be sure that this is the definition that will be used.
-  if (VD->isWeak()) {
+  if (VD->isWeak()&& (Info.EvalMode != EvaluationMode::ConstantExpression)) {
     Info.FFDiag(E, diag::note_constexpr_var_init_weak) << VD;
     NoteLValueLocation(Info, Base);
     return false;

@hstk30-hw hstk30-hw changed the title #141797 issue fixed [[gnu::weak]] disables constant evaluation of constexpr variables in C++14 Nov 7, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants