From 99f743cc0babe8af7f65bbbd8b3da7f9afe56db0 Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Sat, 14 Nov 2026 16:04:17 +0500 Subject: [PATCH 1/3] [mypyc] Coerce unsigned native ints to signed ones without boxing LowLevelIRBuilder.coerce only had a direct path between native ints of the same signedness. Coercing an unsigned native int (u8, u16, u32, u64) to a signed one, for example when indexing bytes with a u64 (the index primitives take an i64), fell through to the generic box/unbox path and allocated a Python int every time. Zero-extend when the target type is wider. Otherwise range check the value against the target's maximum and truncate. Values that don't fit raise the same ValueError as before; add CPyInt64_Overflow for the u64 -> i64 case, with the same message as i64 unboxing. --- mypyc/irbuild/ll_builder.py | 34 +++++++++++++++++++++++++++++++- mypyc/lib-rt/CPy.h | 1 + mypyc/lib-rt/int_ops.c | 4 ++++ mypyc/primitives/int_ops.py | 7 +++++++ mypyc/test-data/irbuild-u64.test | 31 +++++++++++++++++++++++++++++ mypyc/test-data/irbuild-u8.test | 24 ++++++++++++++++++++++ mypyc/test-data/run-u32.test | 10 ++++++++++ mypyc/test-data/run-u64.test | 11 +++++++++++ mypyc/test-data/run-u8.test | 10 ++++++++++ 9 files changed, 131 insertions(+), 1 deletion(-) diff --git a/mypyc/irbuild/ll_builder.py b/mypyc/irbuild/ll_builder.py index 5fa291a6ae8ec..6131b82b0db6e 100644 --- a/mypyc/irbuild/ll_builder.py +++ b/mypyc/irbuild/ll_builder.py @@ -174,6 +174,7 @@ int32_overflow, int64_divide_op, int64_mod_op, + int64_overflow, int64_to_int_op, int_to_int32_op, int_to_int64_op, @@ -547,6 +548,15 @@ 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 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): @@ -767,7 +777,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) @@ -926,6 +938,26 @@ 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_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/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index 5151bb7cd745e..e5014bffafc11 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 9e85519fae338..36548e8adb4bf 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 997b19e49693b..2f10930a81f38 100644 --- a/mypyc/primitives/int_ops.py +++ b/mypyc/primitives/int_ops.py @@ -352,6 +352,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 765192e9390fd..964ff6da558cd 100644 --- a/mypyc/test-data/irbuild-u64.test +++ b/mypyc/test-data/irbuild-u64.test @@ -375,6 +375,37 @@ 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 L2 else goto L1 :: bool +L1: + CPyInt64_Overflow() + unreachable +L2: + r1 = truncate i: u64 to i64 + r2 = CPyBytes_AdjustIndex(b, r1) + r3 = CPyBytes_RangeCheck(b, r2) + if r3 goto L4 else goto L3 :: bool +L3: + r4 = raise IndexError('index out of range') + unreachable +L4: + r5 = CPyBytes_GetItemUnsafe(b, r2) + return r5 + [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 7ed94757c6f18..43d97f06d02e2 100644 --- a/mypyc/test-data/irbuild-u8.test +++ b/mypyc/test-data/irbuild-u8.test @@ -550,3 +550,27 @@ L4: unreachable 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 diff --git a/mypyc/test-data/run-u32.test b/mypyc/test-data/run-u32.test index 8f7b727aeb122..15968b73475a7 100644 --- a/mypyc/test-data/run-u32.test +++ b/mypyc/test-data/run-u32.test @@ -301,6 +301,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 diff --git a/mypyc/test-data/run-u64.test b/mypyc/test-data/run-u64.test index 012b26815c47e..f838f6ff8ace8 100644 --- a/mypyc/test-data/run-u64.test +++ b/mypyc/test-data/run-u64.test @@ -331,6 +331,9 @@ 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 test_index_with_u64() -> None: a = [1, 2, 3] assert list_get(a, 0) == 1 @@ -357,6 +360,14 @@ 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 + for n in (3, 2**63 - 1): + with assertRaises(IndexError, "index out of range"): + bytes_get(b"abc", n) + for n in (2**63, 2**64 - 1): + with assertRaises(ValueError, "int too large to convert to i64"): + bytes_get(b"abc", n) def test_tuple_u64() -> None: a: u64 = 1 diff --git a/mypyc/test-data/run-u8.test b/mypyc/test-data/run-u8.test index 05844de8ecff7..d73f78f3276f8 100644 --- a/mypyc/test-data/run-u8.test +++ b/mypyc/test-data/run-u8.test @@ -283,6 +283,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 From dc7f62a7f93901dab660c5e4d76b68169cf6eac4 Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Sat, 14 Nov 2026 17:00:26 +0500 Subject: [PATCH 2/3] [mypyc] Coerce signed native ints to unsigned ones without boxing Coercing a signed native int (i8, i16, i32, i64) to an unsigned one, for example via cast(u64, x) where x is an i64, still went through the generic box/unbox path and allocated a Python int every time. Add the reverse of the unsigned -> signed direct path: check that the value is non-negative and, if the target type is narrower than the source type, that it doesn't exceed the target's maximum. Then zero-extend or truncate. Values that don't fit raise the same ValueError as unboxing ("int too large or small to convert to uN"). Signed comparisons are used, since the C backend doesn't emit unsigned casts for i8/i16 operands of unsigned comparison ops. --- mypyc/irbuild/ll_builder.py | 36 +++++++++++++++++++++++++++++ mypyc/test-data/irbuild-u64.test | 36 +++++++++++++++++++++++++++++ mypyc/test-data/irbuild-u8.test | 39 ++++++++++++++++++++++++++++++++ mypyc/test-data/run-u16.test | 38 +++++++++++++++++++++++++++++-- mypyc/test-data/run-u32.test | 31 +++++++++++++++++++++++-- mypyc/test-data/run-u64.test | 31 +++++++++++++++++++++++-- mypyc/test-data/run-u8.test | 37 ++++++++++++++++++++++++++++-- 7 files changed, 240 insertions(+), 8 deletions(-) diff --git a/mypyc/irbuild/ll_builder.py b/mypyc/irbuild/ll_builder.py index 6131b82b0db6e..02bdc82cfaaaa 100644 --- a/mypyc/irbuild/ll_builder.py +++ b/mypyc/irbuild/ll_builder.py @@ -557,6 +557,15 @@ def coerce( 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): @@ -958,6 +967,33 @@ def coerce_unsigned_to_signed(self, src: Value, target_type: RPrimitive, line: i 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/test-data/irbuild-u64.test b/mypyc/test-data/irbuild-u64.test index 964ff6da558cd..8073da0fd3936 100644 --- a/mypyc/test-data/irbuild-u64.test +++ b/mypyc/test-data/irbuild-u64.test @@ -406,6 +406,42 @@ L4: 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 43d97f06d02e2..ed81a43875628 100644 --- a/mypyc/test-data/irbuild-u8.test +++ b/mypyc/test-data/irbuild-u8.test @@ -574,3 +574,42 @@ L1: 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 diff --git a/mypyc/test-data/run-u16.test b/mypyc/test-data/run-u16.test index 7278cd8d5b1a7..87147ebdeebed 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 @@ -304,3 +304,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 15968b73475a7..0a6bf528330d8 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 @@ -358,3 +358,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 f838f6ff8ace8..fddc7701f9cb9 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 @@ -387,3 +387,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 d73f78f3276f8..ec101e4fc8670 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 @@ -310,3 +310,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) From d2e5652085abf06c857d4cd7eff529eda9a083dd Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Sat, 14 Nov 2026 17:01:51 +0500 Subject: [PATCH 3/3] [mypyc] Raise IndexError for too large u64 index into bytes and str Indexing bytes, or str in `ord(s[i])` and `s[i] == "x"`, uses specialized primitives that take an i64 index. A u64 index was coerced to i64, so a value >= 2**63 raised "ValueError: int too large to convert to i64", while CPython raises IndexError. Such a value is always out of range, so branch to the existing IndexError block instead of coercing. This replaces the overflow check emitted by the coercion, so there is no additional overhead. Smaller unsigned types always fit in i64 and are unaffected. --- mypyc/irbuild/specialize.py | 21 +++++++++++++++++---- mypyc/test-data/irbuild-u64.test | 11 ++++------- mypyc/test-data/run-u64.test | 18 ++++++++++++++---- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/mypyc/irbuild/specialize.py b/mypyc/irbuild/specialize.py index d33f15a68beec..1c7a08c704080 100644 --- a/mypyc/irbuild/specialize.py +++ b/mypyc/irbuild/specialize.py @@ -1769,16 +1769,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/test-data/irbuild-u64.test b/mypyc/test-data/irbuild-u64.test index 8073da0fd3936..ada2dd07582b9 100644 --- a/mypyc/test-data/irbuild-u64.test +++ b/mypyc/test-data/irbuild-u64.test @@ -390,19 +390,16 @@ def f(b, i): r5 :: int L0: r0 = i <= 9223372036854775807 :: unsigned - if r0 goto L2 else goto L1 :: bool + if r0 goto L1 else goto L2 :: bool L1: - CPyInt64_Overflow() - unreachable -L2: r1 = truncate i: u64 to i64 r2 = CPyBytes_AdjustIndex(b, r1) r3 = CPyBytes_RangeCheck(b, r2) - if r3 goto L4 else goto L3 :: bool -L3: + if r3 goto L3 else goto L2 :: bool +L2: r4 = raise IndexError('index out of range') unreachable -L4: +L3: r5 = CPyBytes_GetItemUnsafe(b, r2) return r5 diff --git a/mypyc/test-data/run-u64.test b/mypyc/test-data/run-u64.test index fddc7701f9cb9..4e5d5a96e2a5e 100644 --- a/mypyc/test-data/run-u64.test +++ b/mypyc/test-data/run-u64.test @@ -334,6 +334,12 @@ def str_get(s: str, i: u64) -> str: 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 @@ -362,12 +368,16 @@ def test_index_with_u64() -> None: str_get("abc", 2**64 - 1) assert bytes_get(b"abc", 0) == 97 assert bytes_get(b"abc", 2) == 99 - for n in (3, 2**63 - 1): + 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) - for n in (2**63, 2**64 - 1): - with assertRaises(ValueError, "int too large to convert to i64"): - 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