Skip to content

Commit 3fe7b38

Browse files
committed
fix: avoid panic when booleans and integers are compared.
Operators `==` and `!=` can be used for comparing two boolean values or an integer and a boolean. In some cases this type of expressions were causing a panic. For instance, in `pe.is_pe == pe.is_pe`.
1 parent 45bf49c commit 3fe7b38

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/src/compiler/emit.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ macro_rules! emit_operands {
6363
lhs_type = Type::Float;
6464
}
6565

66+
// If the left operand is bool, promote it from i32 to i64.
67+
if lhs_type == Type::Bool {
68+
$instr.unop(UnaryOp::I64ExtendUI32);
69+
}
70+
6671
emit_expr($ctx, $ir, rhs, $instr);
6772

6873
// If the right operand is integer, but the left one is float,
@@ -72,6 +77,11 @@ macro_rules! emit_operands {
7277
rhs_type = Type::Float;
7378
}
7479

80+
// If the right operand is bool, promote it from i32 to i64.
81+
if rhs_type == Type::Bool {
82+
$instr.unop(UnaryOp::I64ExtendUI32);
83+
}
84+
7585
(lhs_type, rhs_type)
7686
}};
7787
}
@@ -104,7 +114,10 @@ macro_rules! emit_arithmetic_op {
104114
macro_rules! emit_comparison_op {
105115
($ctx:ident, $ir:ident, $lhs:expr, $rhs:expr, $int_op:tt, $float_op:tt, $str_op:expr, $instr:ident) => {{
106116
match emit_operands!($ctx, $ir, $lhs, $rhs, $instr) {
107-
(Type::Integer, Type::Integer) => {
117+
(Type::Integer, Type::Integer)
118+
| (Type::Bool, Type::Bool)
119+
| (Type::Bool, Type::Integer)
120+
| (Type::Integer, Type::Bool) => {
108121
$instr.binop(BinaryOp::$int_op);
109122
}
110123
(Type::Float, Type::Float) => {

lib/src/tests/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,17 @@ fn test_comparison_operations() {
224224
condition_true!("1.0 == 1");
225225
condition_true!("1.0 != 1.000000000000001");
226226
condition_true!("1.0 < 1.000000000000001");
227+
228+
#[cfg(feature = "test_proto2-module")]
229+
condition_true!(
230+
r#"test_proto2.array_bool[0] == test_proto2.array_bool[0]"#
231+
);
232+
233+
#[cfg(feature = "test_proto2-module")]
234+
condition_true!(r#"test_proto2.array_bool[0] == 0 + 0"#);
235+
236+
#[cfg(feature = "test_proto2-module")]
237+
condition_true!(r#"2 - 1 == test_proto2.array_bool[1]"#);
227238
}
228239

229240
#[test]

0 commit comments

Comments
 (0)