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
3 changes: 2 additions & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2604,7 +2604,7 @@ void Converter::ConvertIntegralToBooleanCast(clang::ImplicitCastExpr *expr) {
PushParen paren(*this);
Convert(sub_expr);
StrCat(token::kDiff);
StrCat(token::kZero);
StrCat(sub_expr->getType()->isFloatingType() ? "0.0" : "0");
computed_expr_type_ = ComputedExprType::FreshValue;
}

Expand Down Expand Up @@ -2710,6 +2710,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
Convert(sub_expr);
break;
case clang::CastKind::CK_IntegralToBoolean:
case clang::CastKind::CK_FloatingToBoolean:
ConvertIntegralToBooleanCast(expr);
break;
case clang::CastKind::CK_PointerToBoolean:
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2131,6 +2131,8 @@ clang::Expr *NormalizeToBool(clang::Expr *expr, clang::ASTContext &ctx) {
clang::CastKind cast_kind;
if (expr->getType()->isPointerType()) {
cast_kind = clang::CK_PointerToBoolean;
} else if (expr->getType()->isFloatingType()) {
cast_kind = clang::CK_FloatingToBoolean;
} else /* expr->getType()->isIntegerType() */ {
cast_kind = clang::CK_IntegralToBoolean;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/expr_as_bool_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,25 @@ int main() {
assert(both_null(nullptr, nullptr) == 1);
assert(both_null(p1, nullptr) == 0);

double d = 0.5;
int taken = 0;
if (d) {
taken = 1;
}
assert(taken == 1);
while (d) {
d = 0.0;
}
assert(d == 0.0);

float f = 2.0f;
bool fb = f;
bool nf = !f;
assert(fb);
assert(!nf);
assert(!0.0);
int fi = (bool)f;
assert(fi == 1);

return 0;
}
18 changes: 18 additions & 0 deletions tests/unit/out/refcount/expr_as_bool_cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ fn main_0() -> i32 {
assert!((({ cmp_or_ptr_1(Ptr::<i8>::null(), Ptr::<i8>::null(),) }) == 0));
assert!((({ both_null_2(Ptr::<i8>::null(), Ptr::<i8>::null(),) }) == 1));
assert!((({ both_null_2((p1).clone(), Ptr::<i8>::null(),) }) == 0));
let mut d: f64 = 0.5_f64;
let mut taken: i32 = 0;
if (d != 0.0) {
taken = 1;
}
assert!((taken == 1));
'loop_: while (d != 0.0) {
d = 0_f64;
}
assert!((d == 0_f64));
let mut f: f32 = 2_f32;
let mut fb: bool = (f != 0.0);
let mut nf: bool = !(f != 0.0);
assert!(fb);
assert!(!(nf));
assert!(!(0_f64 != 0.0));
let mut fi: i32 = ((f != 0.0) as i32);
assert!((fi == 1));
return 0;
}
pub fn __cpp2rust_init_globals() {}
18 changes: 18 additions & 0 deletions tests/unit/out/unsafe/expr_as_bool_cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ unsafe fn main_0() -> i32 {
assert!(((unsafe { cmp_or_ptr_1(std::ptr::null(), std::ptr::null(),) }) == (0)));
assert!(((unsafe { both_null_2(std::ptr::null(), std::ptr::null(),) }) == (1)));
assert!(((unsafe { both_null_2(p1, std::ptr::null(),) }) == (0)));
let mut d: f64 = 0.5_f64;
let mut taken: i32 = 0;
if (d != 0.0) {
taken = 1;
}
assert!(((taken) == (1)));
'loop_: while (d != 0.0) {
d = 0_f64;
}
assert!(((d) == (0_f64)));
let mut f: f32 = 2_f32;
let mut fb: bool = (f != 0.0);
let mut nf: bool = !(f != 0.0);
assert!(fb);
assert!(!(nf));
assert!(!(0_f64 != 0.0));
let mut fi: i32 = ((f != 0.0) as i32);
assert!(((fi) == (1)));
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
Loading