Skip to content

Commit 0799a8a

Browse files
authored
[mypyc] Fix unions of bools and ints (#15066)
Fixes mypyc/mypyc#982
1 parent 4276308 commit 0799a8a

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

Diff for: mypyc/rt_subtype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def visit_rinstance(self, left: RInstance) -> bool:
5151
return is_subtype(left, self.right)
5252

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

5656
def visit_rprimitive(self, left: RPrimitive) -> bool:
5757
if is_short_int_rprimitive(left) and is_int_rprimitive(self.right):

Diff for: mypyc/test-data/run-bools.test

+6
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,9 @@ def test_mixed_comparisons_i64() -> None:
221221
assert neq_mixed_i64(n, x) == (n != int(x))
222222
assert lt_mixed_i64(x, n) == (int(x) < n)
223223
assert gt_mixed_i64(n, x) == (n > int(x))
224+
225+
[case testBoolMixInt]
226+
y = False
227+
print((y or 0) and True)
228+
[out]
229+
0

Diff for: mypyc/test/test_typeops.py

+7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ def test_bool(self) -> None:
5454
assert not is_runtime_subtype(bool_rprimitive, bit_rprimitive)
5555
assert not is_runtime_subtype(bool_rprimitive, int_rprimitive)
5656

57+
def test_union(self) -> None:
58+
bool_int_mix = RUnion([bool_rprimitive, int_rprimitive])
59+
assert not is_runtime_subtype(bool_int_mix, short_int_rprimitive)
60+
assert not is_runtime_subtype(bool_int_mix, int_rprimitive)
61+
assert not is_runtime_subtype(short_int_rprimitive, bool_int_mix)
62+
assert not is_runtime_subtype(int_rprimitive, bool_int_mix)
63+
5764

5865
class TestUnionSimplification(unittest.TestCase):
5966
def test_simple_type_result(self) -> None:

0 commit comments

Comments
 (0)