Skip to content

Commit 9835ca9

Browse files
authored
Fix Default for floating types and floating to boolean casts (#505)
1 parent 2cd9e5d commit 9835ca9

5 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2604,7 +2604,7 @@ void Converter::ConvertIntegralToBooleanCast(clang::ImplicitCastExpr *expr) {
26042604
PushParen paren(*this);
26052605
Convert(sub_expr);
26062606
StrCat(token::kDiff);
2607-
StrCat(token::kZero);
2607+
StrCat(sub_expr->getType()->isFloatingType() ? "0.0" : "0");
26082608
computed_expr_type_ = ComputedExprType::FreshValue;
26092609
}
26102610

@@ -2710,6 +2710,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
27102710
Convert(sub_expr);
27112711
break;
27122712
case clang::CastKind::CK_IntegralToBoolean:
2713+
case clang::CastKind::CK_FloatingToBoolean:
27132714
ConvertIntegralToBooleanCast(expr);
27142715
break;
27152716
case clang::CastKind::CK_PointerToBoolean:

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,6 +2131,8 @@ clang::Expr *NormalizeToBool(clang::Expr *expr, clang::ASTContext &ctx) {
21312131
clang::CastKind cast_kind;
21322132
if (expr->getType()->isPointerType()) {
21332133
cast_kind = clang::CK_PointerToBoolean;
2134+
} else if (expr->getType()->isFloatingType()) {
2135+
cast_kind = clang::CK_FloatingToBoolean;
21342136
} else /* expr->getType()->isIntegerType() */ {
21352137
cast_kind = clang::CK_IntegralToBoolean;
21362138
}

‎tests/unit/expr_as_bool_cpp.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,25 @@ int main() {
5555
assert(both_null(nullptr, nullptr) == 1);
5656
assert(both_null(p1, nullptr) == 0);
5757

58+
double d = 0.5;
59+
int taken = 0;
60+
if (d) {
61+
taken = 1;
62+
}
63+
assert(taken == 1);
64+
while (d) {
65+
d = 0.0;
66+
}
67+
assert(d == 0.0);
68+
69+
float f = 2.0f;
70+
bool fb = f;
71+
bool nf = !f;
72+
assert(fb);
73+
assert(!nf);
74+
assert(!0.0);
75+
int fi = (bool)f;
76+
assert(fi == 1);
77+
5878
return 0;
5979
}

‎tests/unit/out/refcount/expr_as_bool_cpp.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ fn main_0() -> i32 {
7474
assert!((({ cmp_or_ptr_1(Ptr::<i8>::null(), Ptr::<i8>::null(),) }) == 0));
7575
assert!((({ both_null_2(Ptr::<i8>::null(), Ptr::<i8>::null(),) }) == 1));
7676
assert!((({ both_null_2((p1).clone(), Ptr::<i8>::null(),) }) == 0));
77+
let mut d: f64 = 0.5_f64;
78+
let mut taken: i32 = 0;
79+
if (d != 0.0) {
80+
taken = 1;
81+
}
82+
assert!((taken == 1));
83+
'loop_: while (d != 0.0) {
84+
d = 0_f64;
85+
}
86+
assert!((d == 0_f64));
87+
let mut f: f32 = 2_f32;
88+
let mut fb: bool = (f != 0.0);
89+
let mut nf: bool = !(f != 0.0);
90+
assert!(fb);
91+
assert!(!(nf));
92+
assert!(!(0_f64 != 0.0));
93+
let mut fi: i32 = ((f != 0.0) as i32);
94+
assert!((fi == 1));
7795
return 0;
7896
}
7997
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/expr_as_bool_cpp.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ unsafe fn main_0() -> i32 {
7676
assert!(((unsafe { cmp_or_ptr_1(std::ptr::null(), std::ptr::null(),) }) == (0)));
7777
assert!(((unsafe { both_null_2(std::ptr::null(), std::ptr::null(),) }) == (1)));
7878
assert!(((unsafe { both_null_2(p1, std::ptr::null(),) }) == (0)));
79+
let mut d: f64 = 0.5_f64;
80+
let mut taken: i32 = 0;
81+
if (d != 0.0) {
82+
taken = 1;
83+
}
84+
assert!(((taken) == (1)));
85+
'loop_: while (d != 0.0) {
86+
d = 0_f64;
87+
}
88+
assert!(((d) == (0_f64)));
89+
let mut f: f32 = 2_f32;
90+
let mut fb: bool = (f != 0.0);
91+
let mut nf: bool = !(f != 0.0);
92+
assert!(fb);
93+
assert!(!(nf));
94+
assert!(!(0_f64 != 0.0));
95+
let mut fi: i32 = ((f != 0.0) as i32);
96+
assert!(((fi) == (1)));
7997
return 0;
8098
}
8199
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)