Skip to content

Commit

Permalink
[mypyc] Fix unions of bools and ints (#15066)
Browse files Browse the repository at this point in the history
  • Loading branch information
r3m0t committed Apr 17, 2023
1 parent 4276308 commit 0799a8a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypyc/rt_subtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def visit_rinstance(self, left: RInstance) -> bool:
return is_subtype(left, self.right)

def visit_runion(self, left: RUnion) -> bool:
return is_subtype(left, self.right)
return not self.right.is_unboxed and is_subtype(left, self.right)

def visit_rprimitive(self, left: RPrimitive) -> bool:
if is_short_int_rprimitive(left) and is_int_rprimitive(self.right):
Expand Down
6 changes: 6 additions & 0 deletions mypyc/test-data/run-bools.test
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,9 @@ def test_mixed_comparisons_i64() -> None:
assert neq_mixed_i64(n, x) == (n != int(x))
assert lt_mixed_i64(x, n) == (int(x) < n)
assert gt_mixed_i64(n, x) == (n > int(x))

[case testBoolMixInt]
y = False
print((y or 0) and True)
[out]
0
7 changes: 7 additions & 0 deletions mypyc/test/test_typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def test_bool(self) -> None:
assert not is_runtime_subtype(bool_rprimitive, bit_rprimitive)
assert not is_runtime_subtype(bool_rprimitive, int_rprimitive)

def test_union(self) -> None:
bool_int_mix = RUnion([bool_rprimitive, int_rprimitive])
assert not is_runtime_subtype(bool_int_mix, short_int_rprimitive)
assert not is_runtime_subtype(bool_int_mix, int_rprimitive)
assert not is_runtime_subtype(short_int_rprimitive, bool_int_mix)
assert not is_runtime_subtype(int_rprimitive, bool_int_mix)


class TestUnionSimplification(unittest.TestCase):
def test_simple_type_result(self) -> None:
Expand Down

0 comments on commit 0799a8a

Please sign in to comment.