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
4 changes: 3 additions & 1 deletion mypyc/doc/int_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ Construction
* ``u8(x: str, base: int)``

Conversions from ``int`` to a native integer type raise
``ValueError`` if the value is too large or small. Conversions from
``ValueError`` if the value is too large or small. If the value is a
constant, such as in ``u8(-1)``, this is a compile-time error instead.
Conversions from
a wider native integer type to a narrower one truncate the value and never
fail. Conversions between signed and unsigned types of the same or larger
size don't fail either: a value outside the target range wraps around
Expand Down
26 changes: 0 additions & 26 deletions mypyc/irbuild/specialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
)
from mypyc.ir.rtypes import (
RInstance,
RPrimitive,
RTuple,
RType,
RVec,
Expand Down Expand Up @@ -120,7 +119,6 @@
join_formatted_strings,
tokenizer_format_call,
)
from mypyc.irbuild.ll_builder import int_literal_value
from mypyc.irbuild.vec import (
supports_vec_to_sequence,
vec_append,
Expand Down Expand Up @@ -1285,7 +1283,6 @@ def translate_i64(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value
return builder.add(Extend(val, int64_rprimitive, signed=False, line=expr.line))
elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type):
val = builder.accept(arg)
val = truncate_literal(val, int64_rprimitive)
return builder.coerce(val, int64_rprimitive, expr.line)
return None

Expand Down Expand Up @@ -1318,7 +1315,6 @@ def translate_i32(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value
return builder.add(Extend(val, int32_rprimitive, signed=False, line=expr.line))
elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type):
val = builder.accept(arg)
val = truncate_literal(val, int32_rprimitive)
return builder.coerce(val, int32_rprimitive, expr.line)
return None

Expand Down Expand Up @@ -1351,7 +1347,6 @@ def translate_i16(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value
return builder.add(Extend(val, int16_rprimitive, signed=True, line=expr.line))
elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type):
val = builder.accept(arg)
val = truncate_literal(val, int16_rprimitive)
return builder.coerce(val, int16_rprimitive, expr.line)
return None

Expand Down Expand Up @@ -1381,7 +1376,6 @@ def translate_i8(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value |
return builder.add(Truncate(val, int8_rprimitive, line=expr.line))
elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type):
val = builder.accept(arg)
val = truncate_literal(val, int8_rprimitive)
return builder.coerce(val, int8_rprimitive, expr.line)
return None

Expand Down Expand Up @@ -1413,7 +1407,6 @@ def translate_u64(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value
return builder.add(Extend(val, uint64_rprimitive, signed=False, line=expr.line))
elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type):
val = builder.accept(arg)
val = truncate_literal(val, uint64_rprimitive)
return builder.coerce(val, uint64_rprimitive, expr.line)
return None

Expand Down Expand Up @@ -1443,7 +1436,6 @@ def translate_u32(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value
return builder.add(Truncate(val, uint32_rprimitive, line=expr.line))
elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type):
val = builder.accept(arg)
val = truncate_literal(val, uint32_rprimitive)
return builder.coerce(val, uint32_rprimitive, expr.line)
return None

Expand Down Expand Up @@ -1473,7 +1465,6 @@ def translate_u16(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value
return builder.add(Truncate(val, uint16_rprimitive, line=expr.line))
elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type):
val = builder.accept(arg)
val = truncate_literal(val, uint16_rprimitive)
return builder.coerce(val, uint16_rprimitive, expr.line)
return None

Expand All @@ -1499,27 +1490,10 @@ def translate_u8(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value |
return builder.add(Truncate(val, uint8_rprimitive, line=expr.line))
elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type):
val = builder.accept(arg)
val = truncate_literal(val, uint8_rprimitive)
return builder.coerce(val, uint8_rprimitive, expr.line)
return None


def truncate_literal(value: Value, rtype: RPrimitive) -> Value:
"""If value is an integer literal value, truncate it to given native int rtype.

For example, truncate 256 into 0 if rtype is u8.
"""
x = int_literal_value(value)
if x is None:
return value # Not a literal, nothing to do
max_unsigned = (1 << (rtype.size * 8)) - 1
x = x & max_unsigned
if rtype.is_signed and x >= (max_unsigned + 1) // 2:
# Adjust to make it a negative value
x -= max_unsigned + 1
return Integer(x, rtype)


@specialize_function("builtins.int")
def translate_int(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS:
Expand Down
30 changes: 25 additions & 5 deletions mypyc/test-data/irbuild-i16.test
Original file line number Diff line number Diff line change
Expand Up @@ -437,20 +437,16 @@ def f() -> None:
y = i16(11)
z = i16(-3)
a = i16(32767)
b = i16(32768) # Truncate
c = i16(-32768)
d = i16(-32769) # Truncate
[out]
def f():
x, y, z, a, b, c, d :: i16
x, y, z, a, c :: i16
L0:
x = 0
y = 11
z = -3
a = 32767
b = -32768
c = -32768
d = 32767
return 1

[case testI16ExplicitConversionFromVariousTypes]
Expand Down Expand Up @@ -579,3 +575,27 @@ L4:
goto L1
L5:
return 1

[case testI16DetectOutOfRangeLiterals]
from mypy_extensions import i16

def out_of_range() -> None:
a: i16 = 32768
b: i16 = -32769
f(32768)
c = i16(32768)
d = i16(-32769)
# The following are ok
e: i16 = 32767
g: i16 = -32768
f(-32768)
h = i16(32767)
i = i16(-32768)

def f(x: i16) -> None: pass
[out]
main:4: error: Value 32768 is out of range for "i16"
main:5: error: Value -32769 is out of range for "i16"
main:6: error: Value 32768 is out of range for "i16"
main:7: error: Value 32768 is out of range for "i16"
main:8: error: Value -32769 is out of range for "i16"
32 changes: 29 additions & 3 deletions mypyc/test-data/irbuild-i32.test
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,17 @@ def f() -> None:
x = i32(0)
y = i32(11)
z = i32(-3)
a = i32(2**31)
a = i32(2**31 - 1)
b = i32(-2**31)
[out]
def f():
x, y, z, a :: i32
x, y, z, a, b :: i32
L0:
x = 0
y = 11
z = -3
a = -2147483648
a = 2147483647
b = -2147483648
return 1

[case testI32ExplicitConversionFromVariousTypes_64bit]
Expand Down Expand Up @@ -596,3 +598,27 @@ L2:
keep_alive r0
L3:
return r4

[case testI32DetectOutOfRangeLiterals_64bit]
from mypy_extensions import i32

def out_of_range() -> None:
a: i32 = 2147483648
b: i32 = -2147483649
f(2147483648)
c = i32(2147483648)
d = i32(-2147483649)
# The following are ok
e: i32 = 2147483647
g: i32 = -2147483648
f(-2147483648)
h = i32(2147483647)
i = i32(-2147483648)

def f(x: i32) -> None: pass
[out]
main:4: error: Value 2147483648 is out of range for "i32"
main:5: error: Value -2147483649 is out of range for "i32"
main:6: error: Value 2147483648 is out of range for "i32"
main:7: error: Value 2147483648 is out of range for "i32"
main:8: error: Value -2147483649 is out of range for "i32"
14 changes: 4 additions & 10 deletions mypyc/test-data/irbuild-i64.test
Original file line number Diff line number Diff line change
Expand Up @@ -1659,27 +1659,17 @@ from mypy_extensions import i64
def f() -> None:
a = i64(9223372036854775807)
b = i64(-9223372036854775808)
c = i64(9223372036854775808) # Truncate
d = i64(18446744073709551621) # Truncate
[out]
def f():
r0 :: int
a :: i64
r1 :: int
b :: i64
r2 :: int
c :: i64
r3 :: int
d :: i64
L0:
r0 = object 9223372036854775807
a = 9223372036854775807
r1 = object -9223372036854775808
b = -9223372036854775808
r2 = object 9223372036854775808
c = -9223372036854775808
r3 = object 18446744073709551621
d = 5
return 1

[case testI64InitializeFromLargeLiteral_64bit]
Expand Down Expand Up @@ -1719,12 +1709,16 @@ def out_of_range() -> None:
a: i64 = 9223372036854775808
b: i64 = -9223372036854775809
g(9223372036854775808)
c = i64(9223372036854775808)
d = i64(-9223372036854775809)

def g(x: i64) -> None: pass
[out]
main:4: error: Value 9223372036854775808 is out of range for "i64"
main:5: error: Value -9223372036854775809 is out of range for "i64"
main:6: error: Value 9223372036854775808 is out of range for "i64"
main:7: error: Value 9223372036854775808 is out of range for "i64"
main:8: error: Value -9223372036854775809 is out of range for "i64"

[case testI64ForLoopOverRange]
from mypy_extensions import i64
Expand Down
24 changes: 24 additions & 0 deletions mypyc/test-data/irbuild-i8.test
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,27 @@ L4:
unreachable
L5:
return r6

[case testI8DetectOutOfRangeLiterals]
from mypy_extensions import i8

def out_of_range() -> None:
a: i8 = 128
b: i8 = -129
f(128)
c = i8(128)
d = i8(-129)
# The following are ok
e: i8 = 127
g: i8 = -128
f(-128)
h = i8(127)
i = i8(-128)

def f(x: i8) -> None: pass
[out]
main:4: error: Value 128 is out of range for "i8"
main:5: error: Value -129 is out of range for "i8"
main:6: error: Value 128 is out of range for "i8"
main:7: error: Value 128 is out of range for "i8"
main:8: error: Value -129 is out of range for "i8"
10 changes: 5 additions & 5 deletions mypyc/test-data/irbuild-u16.test
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def out_of_range() -> None:
a: u16 = 65536
b: u16 = -1
f(65536)
g = u16(65536)
h = u16(-1)
# The following are ok
c: u16 = 0
d: u16 = 65535
Expand All @@ -80,6 +82,8 @@ def f(x: u16) -> None: pass
main:4: error: Value 65536 is out of range for "u16"
main:5: error: Value -1 is out of range for "u16"
main:6: error: Value 65536 is out of range for "u16"
main:7: error: Value 65536 is out of range for "u16"
main:8: error: Value -1 is out of range for "u16"

[case testU16BoxAndUnbox]
from typing import Any
Expand Down Expand Up @@ -249,16 +253,12 @@ from mypy_extensions import u16
def f() -> None:
x = u16(0)
y = u16(11)
z = u16(-3) # Truncate
zz = u16(65538) # Truncate
a = u16(65535)
[out]
def f():
x, y, z, zz, a :: u16
x, y, a :: u16
L0:
x = 0
y = 11
z = 65533
zz = 2
a = 65535
return 1
10 changes: 5 additions & 5 deletions mypyc/test-data/irbuild-u32.test
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def out_of_range() -> None:
a: u32 = 4294967296
b: u32 = -1
f(4294967296)
g = u32(4294967296)
h = u32(-1)
# The following are ok
c: u32 = 0
d: u32 = 4294967295
Expand All @@ -80,6 +82,8 @@ def f(x: u32) -> None: pass
main:4: error: Value 4294967296 is out of range for "u32"
main:5: error: Value -1 is out of range for "u32"
main:6: error: Value 4294967296 is out of range for "u32"
main:7: error: Value 4294967296 is out of range for "u32"
main:8: error: Value -1 is out of range for "u32"

[case testU32BoxAndUnbox]
from typing import Any
Expand Down Expand Up @@ -249,17 +253,13 @@ from mypy_extensions import u32
def f() -> None:
x = u32(0)
y = u32(11)
z = u32(-3) # Truncate
zz = u32(4294967298) # Truncate
a = u32(4294967295)
[out]
def f():
x, y, z, zz, a :: u32
x, y, a :: u32
L0:
x = 0
y = 11
z = 4294967293
zz = 2
a = 4294967295
return 1

Expand Down
Loading
Loading