diff --git a/mypyc/irbuild/ll_builder.py b/mypyc/irbuild/ll_builder.py index 7e7e9e6d3758..3cd6b5d48b2a 100644 --- a/mypyc/irbuild/ll_builder.py +++ b/mypyc/irbuild/ll_builder.py @@ -176,6 +176,7 @@ int32_overflow, int64_divide_op, int64_mod_op, + int64_overflow, int64_to_int_op, int_to_int32_op, int_to_int64_op, @@ -558,6 +559,24 @@ def coerce( else: # Extend to larger type return self.add(Extend(src, target_type, target_type.is_signed, line)) + elif ( + isinstance(src_type, RPrimitive) + and isinstance(target_type, RPrimitive) + and src_type.is_native_int + and target_type.is_native_int + and not src_type.is_signed + and target_type.is_signed + ): + return self.coerce_unsigned_to_signed(src, target_type, line) + elif ( + isinstance(src_type, RPrimitive) + and isinstance(target_type, RPrimitive) + and src_type.is_native_int + and target_type.is_native_int + and src_type.is_signed + and not target_type.is_signed + ): + return self.coerce_signed_to_unsigned(src, target_type, line) elif is_int64_rprimitive(src_type) and is_c_py_ssize_t_rprimitive(target_type): return self.coerce_i64_to_ssize_t(src, line) elif is_bool_or_bit_rprimitive(src_type) and is_tagged(target_type): @@ -772,7 +791,9 @@ def coerce_tagged_to_fixed_width_with_range_check( def emit_fixed_width_overflow_error(self, target_type: RType, line: int) -> None: """Emit overflow error for fixed-width type conversion.""" - if is_int32_rprimitive(target_type): + if is_int64_rprimitive(target_type): + self.call_c(int64_overflow, [], line) + elif is_int32_rprimitive(target_type): self.call_c(int32_overflow, [], line) elif is_int16_rprimitive(target_type): self.call_c(int16_overflow, [], line) @@ -938,6 +959,53 @@ def coerce_i64_to_ssize_t(self, src: Value, line: int) -> Value: self.activate_block(end) return res + def coerce_unsigned_to_signed(self, src: Value, target_type: RPrimitive, line: int) -> Value: + """Coerce an unsigned native int to a signed native int, preserving the value. + + Raise ValueError if the value doesn't fit in the target type. + """ + assert isinstance(src.type, RPrimitive), src.type + if src.type.size < target_type.size: + # Every value fits in the target type + return self.add(Extend(src, target_type, signed=False, line=line)) + max_value = (1 << (target_type.size * 8 - 1)) - 1 + overflow, end = BasicBlock(), BasicBlock() + in_range = self.add( + ComparisonOp(src, Integer(max_value, src.type), ComparisonOp.ULE, line) + ) + self.add(Branch(in_range, end, overflow, Branch.BOOL, line)) + self.activate_block(overflow) + self.emit_fixed_width_overflow_error(target_type, line) + self.activate_block(end) + return self.add(Truncate(src, target_type, line)) + + def coerce_signed_to_unsigned(self, src: Value, target_type: RPrimitive, line: int) -> Value: + """Coerce a signed native int to an unsigned native int, preserving the value. + + Raise ValueError if the value is negative or doesn't fit in the target type. + """ + assert isinstance(src.type, RPrimitive), src.type + overflow, end = BasicBlock(), BasicBlock() + non_negative = self.add(ComparisonOp(src, Integer(0, src.type), ComparisonOp.SGE, line)) + if src.type.size > target_type.size: + # Large non-negative values don't fit in the target type + max_value = (1 << (target_type.size * 8)) - 1 + check_max = BasicBlock() + self.add(Branch(non_negative, check_max, overflow, Branch.BOOL, line)) + self.activate_block(check_max) + in_range = self.add( + ComparisonOp(src, Integer(max_value, src.type), ComparisonOp.SLE, line) + ) + self.add(Branch(in_range, end, overflow, Branch.BOOL, line)) + else: + self.add(Branch(non_negative, end, overflow, Branch.BOOL, line)) + self.activate_block(overflow) + self.emit_fixed_width_overflow_error(target_type, line) + self.activate_block(end) + if src.type.size < target_type.size: + return self.add(Extend(src, target_type, signed=False, line=line)) + return self.add(Truncate(src, target_type, line)) + def coerce_nullable(self, src: Value, target_type: RType, line: int) -> Value: """Generate a coercion from a potentially null value.""" if src.type.is_unboxed == target_type.is_unboxed and ( diff --git a/mypyc/irbuild/specialize.py b/mypyc/irbuild/specialize.py index 989a3dece443..fe527f8897a2 100644 --- a/mypyc/irbuild/specialize.py +++ b/mypyc/irbuild/specialize.py @@ -1810,16 +1810,29 @@ def translate_getitem_with_bounds_check( # Get the index argument index = builder.accept(args[0]) + # Create blocks for branching + valid_block = BasicBlock() + invalid_block = BasicBlock() + + if is_uint64_rprimitive(index.type): + # A u64 value that doesn't fit in i64 is always out of range, so raise + # IndexError (instead of the generic overflow error from coercion) + fits = builder.add( + ComparisonOp( + index, Integer(2**63 - 1, uint64_rprimitive), ComparisonOp.ULE, ctx_expr.line + ) + ) + fits_block = BasicBlock() + builder.add_bool_branch(fits, fits_block, invalid_block) + builder.activate_block(fits_block) + index = builder.add(Truncate(index, int64_rprimitive, ctx_expr.line)) + # Adjust the index (handle negative indices) adjusted_index = builder.primitive_op(adjust_index_op, [obj, index], ctx_expr.line) # Check if the adjusted index is in valid range range_check = builder.primitive_op(range_check_op, [obj, adjusted_index], ctx_expr.line) - # Create blocks for branching - valid_block = BasicBlock() - invalid_block = BasicBlock() - builder.add_bool_branch(range_check, valid_block, invalid_block) # Handle invalid index - raise IndexError diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index 139e16925723..d9f33959492f 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -229,6 +229,7 @@ PyObject *CPyBool_Str(bool b); int64_t CPyLong_AsInt64_(PyObject *o); int64_t CPyInt64_Divide(int64_t x, int64_t y); int64_t CPyInt64_Remainder(int64_t x, int64_t y); +void CPyInt64_Overflow(void); int32_t CPyLong_AsInt32_(PyObject *o); int32_t CPyInt32_Divide(int32_t x, int32_t y); int32_t CPyInt32_Remainder(int32_t x, int32_t y); diff --git a/mypyc/lib-rt/int_ops.c b/mypyc/lib-rt/int_ops.c index 9e85519fae33..36548e8adb4b 100644 --- a/mypyc/lib-rt/int_ops.c +++ b/mypyc/lib-rt/int_ops.c @@ -476,6 +476,10 @@ int64_t CPyInt64_Remainder(int64_t x, int64_t y) { return d; } +void CPyInt64_Overflow() { + PyErr_SetString(PyExc_ValueError, "int too large to convert to i64"); +} + // i32 unboxing slow path int32_t CPyLong_AsInt32_(PyObject *o) { int overflow; diff --git a/mypyc/primitives/int_ops.py b/mypyc/primitives/int_ops.py index 2bed5f1a5a91..26df5ebad2b5 100644 --- a/mypyc/primitives/int_ops.py +++ b/mypyc/primitives/int_ops.py @@ -362,6 +362,13 @@ def int_unary_op(name: str, c_function_name: str) -> PrimitiveDescription: error_kind=ERR_MAGIC_OVERLAPPING, ) +int64_overflow = custom_op( + arg_types=[], + return_type=void_rtype, + c_function_name="CPyInt64_Overflow", + error_kind=ERR_ALWAYS, +) + int32_overflow = custom_op( arg_types=[], return_type=void_rtype, diff --git a/mypyc/test-data/irbuild-u64.test b/mypyc/test-data/irbuild-u64.test index ae9e21306f8a..0cdcf1d0ac8c 100644 --- a/mypyc/test-data/irbuild-u64.test +++ b/mypyc/test-data/irbuild-u64.test @@ -457,6 +457,70 @@ L0: r1 = CPyList_SetItemUInt64(a, i, r0) return 1 +[case testU64BytesGetItem] +from mypy_extensions import u64 + +def f(b: bytes, i: u64) -> int: + return b[i] +[out] +def f(b, i): + b :: bytes + i :: u64 + r0 :: bit + r1, r2 :: i64 + r3, r4 :: bool + r5 :: int +L0: + r0 = i <= 9223372036854775807 :: unsigned + if r0 goto L1 else goto L2 :: bool +L1: + r1 = truncate i: u64 to i64 + r2 = CPyBytes_AdjustIndex(b, r1) + r3 = CPyBytes_RangeCheck(b, r2) + if r3 goto L3 else goto L2 :: bool +L2: + r4 = raise IndexError('index out of range') + unreachable +L3: + r5 = CPyBytes_GetItemUnsafe(b, r2) + return r5 + +[case testCoerceSignedToU64] +from typing import cast +from mypy_extensions import i32, i64, u64 + +def from_i64(x: i64) -> u64: + return cast(u64, x) +def from_i32(x: i32) -> u64: + return cast(u64, x) +[out] +def from_i64(x): + x :: i64 + r0 :: bit + r1 :: u64 +L0: + r0 = x >= 0 :: signed + if r0 goto L2 else goto L1 :: bool +L1: + CPyUInt64_Overflow() + unreachable +L2: + r1 = truncate x: i64 to u64 + return r1 +def from_i32(x): + x :: i32 + r0 :: bit + r1 :: u64 +L0: + r0 = x >= 0 :: signed + if r0 goto L2 else goto L1 :: bool +L1: + CPyUInt64_Overflow() + unreachable +L2: + r1 = extend x: i32 to u64 + return r1 + [case testListGetSetItemBoolIndexNotAmbiguous] # Bool matches both the i64 and u64 index ops; the i64 op must win from typing import List diff --git a/mypyc/test-data/irbuild-u8.test b/mypyc/test-data/irbuild-u8.test index 6867298c57bf..8eb97dd46b45 100644 --- a/mypyc/test-data/irbuild-u8.test +++ b/mypyc/test-data/irbuild-u8.test @@ -600,6 +600,69 @@ L4: L5: return r7 +[case testU8BytesGetItem] +from mypy_extensions import u8 + +def f(b: bytes, i: u8) -> int: + return b[i] +[out] +def f(b, i): + b :: bytes + i :: u8 + r0, r1 :: i64 + r2, r3 :: bool + r4 :: int +L0: + r0 = extend i: u8 to i64 + r1 = CPyBytes_AdjustIndex(b, r0) + r2 = CPyBytes_RangeCheck(b, r1) + if r2 goto L2 else goto L1 :: bool +L1: + r3 = raise IndexError('index out of range') + unreachable +L2: + r4 = CPyBytes_GetItemUnsafe(b, r1) + return r4 + +[case testCoerceSignedToU8] +from typing import cast +from mypy_extensions import i8, i64, u8 + +def from_i64(x: i64) -> u8: + return cast(u8, x) +def from_i8(x: i8) -> u8: + return cast(u8, x) +[out] +def from_i64(x): + x :: i64 + r0, r1 :: bit + r2 :: u8 +L0: + r0 = x >= 0 :: signed + if r0 goto L1 else goto L2 :: bool +L1: + r1 = x <= 255 :: signed + if r1 goto L3 else goto L2 :: bool +L2: + CPyUInt8_Overflow() + unreachable +L3: + r2 = truncate x: i64 to u8 + return r2 +def from_i8(x): + x :: i8 + r0 :: bit + r1 :: u8 +L0: + r0 = x >= 0 :: signed + if r0 goto L2 else goto L1 :: bool +L1: + CPyUInt8_Overflow() + unreachable +L2: + r1 = truncate x: i8 to u8 + return r1 + [case testU8ForLoopOverRangeWithStep] from mypy_extensions import u8 diff --git a/mypyc/test-data/run-u16.test b/mypyc/test-data/run-u16.test index 158f2352d226..8b9c0ab0efca 100644 --- a/mypyc/test-data/run-u16.test +++ b/mypyc/test-data/run-u16.test @@ -1,7 +1,7 @@ [case testU16BasicOps] -from typing import Any, Final, Tuple +from typing import Any, Final, Tuple, cast -from mypy_extensions import u16, i16, i32, i64, u8 +from mypy_extensions import u16, i8, i16, i32, i64, u8 from testutil import assertRaises @@ -370,3 +370,37 @@ def test_convert_u16_to_native_int() -> None: assert i16(x) == (i if i < 32768 else i - 65536) assert i32(x) == i assert i64(x) == i + +def i64_to_u16(x: i64) -> u16: + return cast(u16, x) + +def i32_to_u16(x: i32) -> u16: + return cast(u16, x) + +def i16_to_u16(x: i16) -> u16: + return cast(u16, x) + +def i8_to_u16(x: i8) -> u16: + return cast(u16, x) + +def test_coerce_from_signed_native_int() -> None: + for n in (0, 1, 255, 32767, 65535): + assert i64_to_u16(n) == n + assert i32_to_u16(n) == n + for n in (0, 1, 255, 32767): + assert i16_to_u16(n) == n + for n in (0, 1, 127): + assert i8_to_u16(n) == n + msg = "int too large or small to convert to u16" + for n in (-1, 65536, 2**63 - 1, -2**63): + with assertRaises(ValueError, msg): + i64_to_u16(n) + for n in (-1, 65536, 2**31 - 1, -2**31): + with assertRaises(ValueError, msg): + i32_to_u16(n) + for n in (-1, -32768): + with assertRaises(ValueError, msg): + i16_to_u16(n) + for n in (-1, -128): + with assertRaises(ValueError, msg): + i8_to_u16(n) diff --git a/mypyc/test-data/run-u32.test b/mypyc/test-data/run-u32.test index eb5be797d609..847fcdae7316 100644 --- a/mypyc/test-data/run-u32.test +++ b/mypyc/test-data/run-u32.test @@ -1,7 +1,7 @@ [case testU32BasicOps] -from typing import Any, Final, Tuple +from typing import Any, Final, Tuple, cast -from mypy_extensions import u32, i16, i32, i64, u8 +from mypy_extensions import u32, i8, i16, i32, i64, u8 from testutil import assertRaises @@ -367,6 +367,16 @@ def test_explicit_conversion_from_float() -> None: # One ulp below the lowest valid i64 value from_float(float(-1.0)) +def bytes_get(b: bytes, i: u32) -> int: + return b[i] + +def test_bytes_index_with_u32() -> None: + assert bytes_get(b"abc", 0) == 97 + assert bytes_get(b"abc", 2) == 99 + for n in (3, 2**31, 2**32 - 1): + with assertRaises(IndexError, "index out of range"): + bytes_get(b"abc", n) + def test_tuple_u32() -> None: a: u32 = 1 b: u32 = 2 @@ -414,3 +424,30 @@ def test_high_bit_conversions() -> None: assert u32(n32) == 2**31 assert big > 1 assert big >> 1 == 2**30 + +def i64_to_u32(x: i64) -> u32: + return cast(u32, x) + +def i32_to_u32(x: i32) -> u32: + return cast(u32, x) + +def i8_to_u32(x: i8) -> u32: + return cast(u32, x) + +def test_coerce_from_signed_native_int() -> None: + for n in (0, 1, 2**31 - 1, 2**31, 2**32 - 1): + assert i64_to_u32(n) == n + for n in (0, 1, 2**31 - 1): + assert i32_to_u32(n) == n + for n in (0, 1, 127): + assert i8_to_u32(n) == n + msg = "int too large or small to convert to u32" + for n in (-1, 2**32, 2**63 - 1, -2**63): + with assertRaises(ValueError, msg): + i64_to_u32(n) + for n in (-1, -2**31): + with assertRaises(ValueError, msg): + i32_to_u32(n) + for n in (-1, -128): + with assertRaises(ValueError, msg): + i8_to_u32(n) diff --git a/mypyc/test-data/run-u64.test b/mypyc/test-data/run-u64.test index d5ed0a548922..abb918a35afd 100644 --- a/mypyc/test-data/run-u64.test +++ b/mypyc/test-data/run-u64.test @@ -1,7 +1,7 @@ [case testU64BasicOps] -from typing import Any, Final, Tuple +from typing import Any, Final, Tuple, cast -from mypy_extensions import u64, i16, i32, i64, u8 +from mypy_extensions import u64, i8, i16, i32, i64, u8 from testutil import assertRaises @@ -397,6 +397,15 @@ def tuple_get(a: tuple[int, ...], i: u64) -> int: def str_get(s: str, i: u64) -> str: return s[i] +def bytes_get(b: bytes, i: u64) -> int: + return b[i] + +def str_ord(s: str, i: u64) -> int: + return ord(s[i]) + +def str_index_eq(s: str, i: u64) -> bool: + return s[i] == "c" + def test_index_with_u64() -> None: a = [1, 2, 3] assert list_get(a, 0) == 1 @@ -423,6 +432,18 @@ def test_index_with_u64() -> None: tuple_get((1, 2, 3), 2**63) with assertRaises(OverflowError): str_get("abc", 2**64 - 1) + assert bytes_get(b"abc", 0) == 97 + assert bytes_get(b"abc", 2) == 99 + assert str_ord("abc", 1) == 98 + assert str_index_eq("abc", 2) + assert not str_index_eq("abc", 0) + for n in (3, 2**63 - 1, 2**63, 2**64 - 3, 2**64 - 1): + with assertRaises(IndexError, "index out of range"): + bytes_get(b"abc", n) + with assertRaises(IndexError, "index out of range"): + str_ord("abc", n) + with assertRaises(IndexError, "index out of range"): + str_index_eq("abc", n) def test_tuple_u64() -> None: a: u64 = 1 @@ -442,3 +463,30 @@ def test_convert_u64_to_native_int() -> None: assert i16(x) == (i if i < 32768 else i - 18446744073709551616) assert i32(x) == i assert i64(x) == i + +def i64_to_u64(x: i64) -> u64: + return cast(u64, x) + +def i32_to_u64(x: i32) -> u64: + return cast(u64, x) + +def i8_to_u64(x: i8) -> u64: + return cast(u64, x) + +def test_coerce_from_signed_native_int() -> None: + for n in (0, 1, 2**31 - 1, 2**31, 2**63 - 1): + assert i64_to_u64(n) == n + for n in (0, 1, 2**31 - 1): + assert i32_to_u64(n) == n + for n in (0, 1, 127): + assert i8_to_u64(n) == n + msg = "int too large or small to convert to u64" + for n in (-1, -2**31, -2**63): + with assertRaises(ValueError, msg): + i64_to_u64(n) + for n in (-1, -2**31): + with assertRaises(ValueError, msg): + i32_to_u64(n) + for n in (-1, -128): + with assertRaises(ValueError, msg): + i8_to_u64(n) diff --git a/mypyc/test-data/run-u8.test b/mypyc/test-data/run-u8.test index c0e982d61d1d..5db54396f0fe 100644 --- a/mypyc/test-data/run-u8.test +++ b/mypyc/test-data/run-u8.test @@ -1,7 +1,7 @@ [case testU8BasicOps] -from typing import Any, Final, Tuple +from typing import Any, Final, Tuple, cast -from mypy_extensions import u8, i16, i32, i64 +from mypy_extensions import u8, i8, i16, i32, i64 from testutil import assertRaises @@ -349,6 +349,16 @@ def test_explicit_conversion_from_float() -> None: # One ulp below the lowest valid i64 value from_float(float(-1.0)) +def bytes_get(b: bytes, i: u8) -> int: + return b[i] + +def test_bytes_index_with_u8() -> None: + assert bytes_get(b"abc", 0) == 97 + assert bytes_get(b"abc", 2) == 99 + for n in (3, 255): + with assertRaises(IndexError, "index out of range"): + bytes_get(b"abc", n) + def test_tuple_u8() -> None: a: u8 = 1 b: u8 = 2 @@ -366,3 +376,36 @@ def test_convert_u8_to_native_int() -> None: assert i16(x) == i assert i32(x) == i assert i64(x) == i + +def i64_to_u8(x: i64) -> u8: + return cast(u8, x) + +def i32_to_u8(x: i32) -> u8: + return cast(u8, x) + +def i16_to_u8(x: i16) -> u8: + return cast(u8, x) + +def i8_to_u8(x: i8) -> u8: + return cast(u8, x) + +def test_coerce_from_signed_native_int() -> None: + for n in (0, 1, 127, 128, 255): + assert i64_to_u8(n) == n + assert i32_to_u8(n) == n + assert i16_to_u8(n) == n + for n in (0, 1, 127): + assert i8_to_u8(n) == n + msg = "int too large or small to convert to u8" + for n in (-1, 256, 2**63 - 1, -2**63): + with assertRaises(ValueError, msg): + i64_to_u8(n) + for n in (-1, 256, 2**31 - 1, -2**31): + with assertRaises(ValueError, msg): + i32_to_u8(n) + for n in (-1, 256, 32767, -32768): + with assertRaises(ValueError, msg): + i16_to_u8(n) + for n in (-1, -128): + with assertRaises(ValueError, msg): + i8_to_u8(n)