Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 (
Expand Down
21 changes: 17 additions & 4 deletions mypyc/irbuild/specialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions mypyc/lib-rt/int_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions mypyc/primitives/int_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
64 changes: 64 additions & 0 deletions mypyc/test-data/irbuild-u64.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions mypyc/test-data/irbuild-u8.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 36 additions & 2 deletions mypyc/test-data/run-u16.test
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Loading
Loading