From fa221f544012b7f6c0c79aba07fec1bbc3fdc10c Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 17:25:11 +0000 Subject: [PATCH 1/3] Translate if consteval --- cpp2rust/converter/converter.cpp | 8 ++- tests/std/stmt.stmt/stmt.select/stmt.if.cpp | 62 +++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 5ce8d433f4..8f4667318e 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1457,6 +1457,12 @@ void Converter::ConvertCondition(clang::Expr *cond) { } bool Converter::VisitIfStmt(clang::IfStmt *stmt) { + if (stmt->isConsteval()) { + ConvertBody(constant_evaluated_ != stmt->isNegatedConsteval() + ? stmt->getThen() + : stmt->getElse()); + return false; + } // The variables of the init statement and of the condition are only // visible in the if. auto *cond_var = stmt->getConditionVariableDeclStmt(); @@ -1468,7 +1474,7 @@ bool Converter::VisitIfStmt(clang::IfStmt *stmt) { Convert(cond_var); } if (bool taken = false; - !stmt->getConditionVariable() && !stmt->isConsteval() && + !stmt->getConditionVariable() && !stmt->getCond()->isValueDependent() && stmt->getCond()->EvaluateAsBooleanCondition(taken, ctx_)) { ConvertBody(taken ? stmt->getThen() : stmt->getElse()); diff --git a/tests/std/stmt.stmt/stmt.select/stmt.if.cpp b/tests/std/stmt.stmt/stmt.select/stmt.if.cpp index fec5762915..a4bccefe26 100644 --- a/tests/std/stmt.stmt/stmt.select/stmt.if.cpp +++ b/tests/std/stmt.stmt/stmt.select/stmt.if.cpp @@ -40,7 +40,69 @@ void test() { } } // namespace p3_init_statement_variable_lives_for_whole_statement +// N4950 [stmt.if]/5: If a consteval if statement is evaluated in a context +// that is manifestly constant-evaluated (7.7), the first substatement is +// executed. Otherwise, if the else part of the selection statement is present, +// then the second substatement is executed. +namespace p5_consteval_if_at_run_time_executes_else { +int pick() { + if consteval { + return 1; + } else { + return 2; + } +} + +int only_then() { + int r = 3; + if consteval { + r = 4; + } + return r; +} + +void test() { + assert(pick() == 2); + assert(only_then() == 3); +} +} // namespace p5_consteval_if_at_run_time_executes_else + +// N4950 [stmt.if]/6: An if statement of the form +// if ! consteval compound-statement +// is not itself a consteval if statement, but is equivalent to the consteval if +// statement +// if consteval { } else compound-statement +// An if statement of the form +// if ! consteval compound-statement1 else statement2 +// is not itself a consteval if statement, but is equivalent to the consteval if +// statement +// if consteval statement2 else compound-statement1 +namespace p6_negated_consteval_if_at_run_time_executes_first { +int pick() { + if !consteval { + return 2; + } else { + return 1; + } +} + +int only_then() { + int r = 3; + if !consteval { + r = 4; + } + return r; +} + +void test() { + assert(pick() == 2); + assert(only_then() == 4); +} +} // namespace p6_negated_consteval_if_at_run_time_executes_first + int main() { p3_init_statement_variable_lives_for_whole_statement::test(); + p5_consteval_if_at_run_time_executes_else::test(); + p6_negated_consteval_if_at_run_time_executes_first::test(); return 0; } From 85e28110c80902fd3f5f64277073737d47887c1b Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 17:48:29 +0000 Subject: [PATCH 2/3] Format --- cpp2rust/converter/converter.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 8f4667318e..8e329bf6af 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1474,8 +1474,7 @@ bool Converter::VisitIfStmt(clang::IfStmt *stmt) { Convert(cond_var); } if (bool taken = false; - !stmt->getConditionVariable() && - !stmt->getCond()->isValueDependent() && + !stmt->getConditionVariable() && !stmt->getCond()->isValueDependent() && stmt->getCond()->EvaluateAsBooleanCondition(taken, ctx_)) { ConvertBody(taken ? stmt->getThen() : stmt->getElse()); return false; From dfa197536c0a732b7c166bff1328ecf5624ea15f Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 17:48:29 +0000 Subject: [PATCH 3/3] Update tests --- .../stmt.select/out/refcount/stmt.if.rs | 34 +++++++++++++++++++ .../stmt.select/out/unsafe/stmt.if.rs | 34 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/tests/std/stmt.stmt/stmt.select/out/refcount/stmt.if.rs b/tests/std/stmt.stmt/stmt.select/out/refcount/stmt.if.rs index 75f94bcfa9..4f369f5ee3 100644 --- a/tests/std/stmt.stmt/stmt.select/out/refcount/stmt.if.rs +++ b/tests/std/stmt.stmt/stmt.select/out/refcount/stmt.if.rs @@ -112,12 +112,46 @@ pub fn test_6() { assert!((dtorCount_2.with(|rc| *rc.borrow()) == 2)); assert!((choice == 9)); } +pub fn pick_7() -> i32 { + { + return 2; + } + panic!("ub: non-void function does not return a value") +} +pub fn only_then_8() -> i32 { + let mut r: i32 = 3; + {} + return r; +} +pub fn test_9() { + assert!((({ pick_7() }) == 2)); + assert!((({ only_then_8() }) == 3)); +} +pub fn pick_10() -> i32 { + { + return 2; + } + panic!("ub: non-void function does not return a value") +} +pub fn only_then_11() -> i32 { + let mut r: i32 = 3; + { + r = 4; + } + return r; +} +pub fn test_12() { + assert!((({ pick_10() }) == 2)); + assert!((({ only_then_11() }) == 4)); +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); } fn main_0() -> i32 { ({ test_6() }); + ({ test_9() }); + ({ test_12() }); return 0; } pub trait LifetimeTrackedImpl { diff --git a/tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.if.rs b/tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.if.rs index 6aa41655c5..e6715dbdbd 100644 --- a/tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.if.rs +++ b/tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.if.rs @@ -103,6 +103,38 @@ pub unsafe fn test_6() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)) == (2))); assert!(((choice) == (9))); } +pub unsafe fn pick_7() -> i32 { + { + return 2; + } + panic!("ub: non-void function does not return a value") +} +pub unsafe fn only_then_8() -> i32 { + let mut r: i32 = 3; + {} + return r; +} +pub unsafe fn test_9() { + assert!(((unsafe { pick_7() }) == (2))); + assert!(((unsafe { only_then_8() }) == (3))); +} +pub unsafe fn pick_10() -> i32 { + { + return 2; + } + panic!("ub: non-void function does not return a value") +} +pub unsafe fn only_then_11() -> i32 { + let mut r: i32 = 3; + { + r = 4; + } + return r; +} +pub unsafe fn test_12() { + assert!(((unsafe { pick_10() }) == (2))); + assert!(((unsafe { only_then_11() }) == (4))); +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -111,6 +143,8 @@ pub fn main() { } unsafe fn main_0() -> i32 { (unsafe { test_6() }); + (unsafe { test_9() }); + (unsafe { test_12() }); return 0; } pub unsafe fn __cpp2rust_init_globals() {