Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -1468,8 +1474,7 @@ bool Converter::VisitIfStmt(clang::IfStmt *stmt) {
Convert(cond_var);
}
if (bool taken = false;
!stmt->getConditionVariable() && !stmt->isConsteval() &&
!stmt->getCond()->isValueDependent() &&
!stmt->getConditionVariable() && !stmt->getCond()->isValueDependent() &&
stmt->getCond()->EvaluateAsBooleanCondition(taken, ctx_)) {
ConvertBody(taken ? stmt->getThen() : stmt->getElse());
return false;
Expand Down
34 changes: 34 additions & 0 deletions tests/std/stmt.stmt/stmt.select/out/refcount/stmt.if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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() {
Expand Down
62 changes: 62 additions & 0 deletions tests/std/stmt.stmt/stmt.select/stmt.if.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading