[clang] [Clang] Correctly handle allocations in the condition of a `if constexpr` (PR #146890)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 3 14:44:48 PDT 2025


================
@@ -1931,15 +1931,13 @@ Parser::ParseCXXCondition(StmtResult *InitStmt, SourceLocation Loc,
       return ParseCXXCondition(nullptr, Loc, CK, MissingOK);
     }
 
-    ExprResult Expr = [&] {
-      EnterExpressionEvaluationContext Eval(
-          Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated,
-          /*LambdaContextDecl=*/nullptr,
-          /*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_Other,
-          /*ShouldEnter=*/CK == Sema::ConditionKind::ConstexprIf);
-      // Parse the expression.
-      return ParseExpression(); // expression
-    }();
+    EnterExpressionEvaluationContext Eval(
+        Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated,
+        /*LambdaContextDecl=*/nullptr,
+        /*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_Other,
+        /*ShouldEnter=*/CK == Sema::ConditionKind::ConstexprIf);
----------------
efriedma-quic wrote:

Do you need to make an equivalent change to TreeTransform?

I came up with the following testcase:

```
struct S {
    char* c = new char;
    consteval S(){}
    constexpr ~S() {
        delete c;
    }
};
template<typename T> 
int f() {
    if constexpr((T{}, true)) {
        return 1;
    }
    return 0;
}
auto ff = f<S>;
```

https://github.com/llvm/llvm-project/pull/146890


More information about the cfe-commits mailing list