From 522fa4f18505acd8cf7ca0f4e34dfc5264dd9440 Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Sat, 14 Nov 2026 15:59:42 +0500 Subject: [PATCH 1/4] [mypyc] Fix negation of unsigned native int literals Unary minus on an integer literal is constant folded in LowLevelIRBuilder.unary_minus, but the result wasn't wrapped around for unsigned native int types. For example, -u64(1) was folded into the u64 constant -1 instead of 2**64 - 1, so int(-u64(1)) and str(-u64(1)) produced -1, and comparisons such as x == -u64(1) emitted C code that failed to compile with -Werror=sign-compare. The same happened with u32, u16 and u8. Wrap the folded value modulo 2**bits for unsigned types, which matches the behavior of negating a non-literal unsigned value at runtime. --- mypyc/irbuild/ll_builder.py | 8 ++++++-- mypyc/test-data/irbuild-u64.test | 16 +++++++++++++++ mypyc/test-data/irbuild-u8.test | 16 +++++++++++++++ mypyc/test-data/run-u16.test | 35 ++++++++++++++++++++++++++++++++ mypyc/test-data/run-u32.test | 35 ++++++++++++++++++++++++++++++++ mypyc/test-data/run-u64.test | 35 ++++++++++++++++++++++++++++++++ mypyc/test-data/run-u8.test | 35 ++++++++++++++++++++++++++++++++ 7 files changed, 178 insertions(+), 2 deletions(-) diff --git a/mypyc/irbuild/ll_builder.py b/mypyc/irbuild/ll_builder.py index 5fa291a6ae8ec..2df22277b9812 100644 --- a/mypyc/irbuild/ll_builder.py +++ b/mypyc/irbuild/ll_builder.py @@ -2180,8 +2180,12 @@ def unary_minus(self, value: Value, line: int) -> Value: """Perform unary '-'.""" typ = value.type if isinstance(value, Integer): - # TODO: Overflow? Unsigned? - return Integer(-value.numeric_value(), typ, line) + # TODO: Signed overflow? + result = -value.numeric_value() + if is_fixed_width_rtype(typ) and not typ.is_signed: + # Unsigned negation wraps around, like at runtime + result &= (1 << (typ.size * 8)) - 1 + return Integer(result, typ, line) elif isinstance(value, Float): return Float(-value.value, line) elif is_fixed_width_rtype(typ): diff --git a/mypyc/test-data/irbuild-u64.test b/mypyc/test-data/irbuild-u64.test index 765192e9390fd..858f04e8354a2 100644 --- a/mypyc/test-data/irbuild-u64.test +++ b/mypyc/test-data/irbuild-u64.test @@ -319,6 +319,22 @@ L0: c = 1 return 1 +[case testU64NegateLiteral] +from mypy_extensions import u64 + +def f() -> None: + a = -u64(0) + b = -u64(1) + c = -u64(5) +[out] +def f(): + a, b, c :: u64 +L0: + a = 0 + b = 18446744073709551615 + c = 18446744073709551611 + return 1 + [case testU64FromShortInt_64bit] from mypy_extensions import u64 diff --git a/mypyc/test-data/irbuild-u8.test b/mypyc/test-data/irbuild-u8.test index 7ed94757c6f18..8d184ce6130be 100644 --- a/mypyc/test-data/irbuild-u8.test +++ b/mypyc/test-data/irbuild-u8.test @@ -81,6 +81,22 @@ L0: y = x return y +[case testU8NegateLiteral] +from mypy_extensions import u8 + +def f() -> None: + a = -u8(0) + b = -u8(1) + c = -u8(5) +[out] +def f(): + a, b, c :: u8 +L0: + a = 0 + b = 255 + c = 251 + return 1 + [case testU8DivisionByConstant] from mypy_extensions import u8 diff --git a/mypyc/test-data/run-u16.test b/mypyc/test-data/run-u16.test index 7278cd8d5b1a7..b350b99dfc4fb 100644 --- a/mypyc/test-data/run-u16.test +++ b/mypyc/test-data/run-u16.test @@ -249,6 +249,41 @@ def test_u16_from_large_small_literal() -> None: x = u16(0) assert x == 0 +def neg_literal_to_int() -> int: + return -u16(1) + +def eq_neg_literal(x: u16) -> bool: + return x == -u16(1) + +def gt_neg_literal(x: u16) -> bool: + return x > -u16(5) + +def and_neg_literal(x: u16) -> u16: + return x & -u16(8) + +def div_neg_literal(x: u16) -> u16: + return x // -u16(5) + +def test_negate_literal() -> None: + assert -u16(0) == 0 + assert -u16(1) == 65535 + assert int(-u16(1)) == 2**16 - 1 + assert neg_literal_to_int() == 2**16 - 1 + assert str(-u16(1)) == "65535" + assert -u16(65535) == 1 + assert 0 - u16(1) == 65535 + assert ~u16(0) == 65535 + one: u16 = 1 + int() + assert -u16(1) == -one + assert -u16(5) == -(one * 5) + assert eq_neg_literal(65535) + assert not eq_neg_literal(1) + assert gt_neg_literal(65535) + assert not gt_neg_literal(65531) + assert and_neg_literal(65535) == 65528 + assert div_neg_literal(65535) == 1 + assert div_neg_literal(5) == 0 + def test_u16_truncate_from_i64() -> None: large = i64(2**32 + 65536 + 157 + int()) x = u16(large) diff --git a/mypyc/test-data/run-u32.test b/mypyc/test-data/run-u32.test index 8f7b727aeb122..1036ac549cbc8 100644 --- a/mypyc/test-data/run-u32.test +++ b/mypyc/test-data/run-u32.test @@ -264,6 +264,41 @@ def test_u32_from_large_small_literal() -> None: x = u32(0) assert x == 0 +def neg_literal_to_int() -> int: + return -u32(1) + +def eq_neg_literal(x: u32) -> bool: + return x == -u32(1) + +def gt_neg_literal(x: u32) -> bool: + return x > -u32(5) + +def and_neg_literal(x: u32) -> u32: + return x & -u32(8) + +def div_neg_literal(x: u32) -> u32: + return x // -u32(5) + +def test_negate_literal() -> None: + assert -u32(0) == 0 + assert -u32(1) == 4294967295 + assert int(-u32(1)) == 2**32 - 1 + assert neg_literal_to_int() == 2**32 - 1 + assert str(-u32(1)) == "4294967295" + assert -u32(4294967295) == 1 + assert 0 - u32(1) == 4294967295 + assert ~u32(0) == 4294967295 + one: u32 = 1 + int() + assert -u32(1) == -one + assert -u32(5) == -(one * 5) + assert eq_neg_literal(4294967295) + assert not eq_neg_literal(1) + assert gt_neg_literal(4294967295) + assert not gt_neg_literal(4294967291) + assert and_neg_literal(4294967295) == 4294967288 + assert div_neg_literal(4294967295) == 1 + assert div_neg_literal(5) == 0 + def test_u32_truncate_from_i64() -> None: large = i64(2**32 + 4294967296 + 157 + int()) x = u32(large) diff --git a/mypyc/test-data/run-u64.test b/mypyc/test-data/run-u64.test index 012b26815c47e..196532621c662 100644 --- a/mypyc/test-data/run-u64.test +++ b/mypyc/test-data/run-u64.test @@ -278,6 +278,41 @@ def test_u64_large_literal_implicit_coercion() -> None: def u64_identity(x: u64) -> u64: return x +def neg_literal_to_int() -> int: + return -u64(1) + +def eq_neg_literal(x: u64) -> bool: + return x == -u64(1) + +def gt_neg_literal(x: u64) -> bool: + return x > -u64(5) + +def and_neg_literal(x: u64) -> u64: + return x & -u64(8) + +def div_neg_literal(x: u64) -> u64: + return x // -u64(5) + +def test_negate_literal() -> None: + assert -u64(0) == 0 + assert -u64(1) == 18446744073709551615 + assert int(-u64(1)) == 2**64 - 1 + assert neg_literal_to_int() == 2**64 - 1 + assert str(-u64(1)) == "18446744073709551615" + assert -u64(18446744073709551615) == 1 + assert 0 - u64(1) == 18446744073709551615 + assert ~u64(0) == 18446744073709551615 + one: u64 = 1 + int() + assert -u64(1) == -one + assert -u64(5) == -(one * 5) + assert eq_neg_literal(18446744073709551615) + assert not eq_neg_literal(1) + assert gt_neg_literal(18446744073709551615) + assert not gt_neg_literal(18446744073709551611) + assert and_neg_literal(18446744073709551615) == 18446744073709551608 + assert div_neg_literal(18446744073709551615) == 1 + assert div_neg_literal(5) == 0 + def test_u64_truncate_from_i64() -> None: large = i64(2**62 + 157 + int()) x = u64(large) diff --git a/mypyc/test-data/run-u8.test b/mypyc/test-data/run-u8.test index 05844de8ecff7..ee51c10e399b8 100644 --- a/mypyc/test-data/run-u8.test +++ b/mypyc/test-data/run-u8.test @@ -246,6 +246,41 @@ def test_u8_from_large_small_literal() -> None: x = u8(0) assert x == 0 +def neg_literal_to_int() -> int: + return -u8(1) + +def eq_neg_literal(x: u8) -> bool: + return x == -u8(1) + +def gt_neg_literal(x: u8) -> bool: + return x > -u8(5) + +def and_neg_literal(x: u8) -> u8: + return x & -u8(8) + +def div_neg_literal(x: u8) -> u8: + return x // -u8(5) + +def test_negate_literal() -> None: + assert -u8(0) == 0 + assert -u8(1) == 255 + assert int(-u8(1)) == 2**8 - 1 + assert neg_literal_to_int() == 2**8 - 1 + assert str(-u8(1)) == "255" + assert -u8(255) == 1 + assert 0 - u8(1) == 255 + assert ~u8(0) == 255 + one: u8 = 1 + int() + assert -u8(1) == -one + assert -u8(5) == -(one * 5) + assert eq_neg_literal(255) + assert not eq_neg_literal(1) + assert gt_neg_literal(255) + assert not gt_neg_literal(251) + assert and_neg_literal(255) == 248 + assert div_neg_literal(255) == 1 + assert div_neg_literal(5) == 0 + def test_u8_truncate_from_i64() -> None: large = i64(2**32 + 256 + 157 + int()) x = u8(large) From df6eed2e4f262a830cbd9bbab952dcf5a8bef24b Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Sat, 14 Nov 2026 16:59:26 +0500 Subject: [PATCH 2/4] [mypyc] Wrap around constant folded unsigned native int Final values Constant folding evaluated operations on Final native int values using arbitrary-precision int semantics, so expressions such as "-X", "X - 2" or "~X" with "X: Final[u64] = 1" produced a negative value that was then rejected with 'Value -1 is out of range for "u64"'. Similar errors happened for u8/u16/u32 (e.g. "X + 1" with "X: Final[u8] = 255"), and "%d" % (-X,) silently produced "-1". At runtime unsigned native int operations wrap around. Wrap each folded int result around based on the native int type of the expression, so that intermediate results also behave like at runtime (e.g. (X - 2) // 2). mypy also folds Final initializers like "Y: Final[u64] = -X" into the out-of-range final value -1, so also wrap final values when loading them. Signed native int overflow is undefined behavior, so folded expressions that overflow a signed type are still reported as errors. --- mypyc/ir/rtypes.py | 12 ++++++++ mypyc/irbuild/builder.py | 8 +++++- mypyc/irbuild/constant_fold.py | 10 +++++++ mypyc/irbuild/ll_builder.py | 7 ++--- mypyc/test-data/irbuild-u64.test | 47 ++++++++++++++++++++++++++++++++ mypyc/test-data/irbuild-u8.test | 33 ++++++++++++++++++++++ mypyc/test-data/run-u16.test | 28 +++++++++++++++++++ mypyc/test-data/run-u32.test | 28 +++++++++++++++++++ mypyc/test-data/run-u64.test | 28 +++++++++++++++++++ mypyc/test-data/run-u8.test | 28 +++++++++++++++++++ 10 files changed, 223 insertions(+), 6 deletions(-) diff --git a/mypyc/ir/rtypes.py b/mypyc/ir/rtypes.py index 18b37f0fcb641..6f89e0e4337dc 100644 --- a/mypyc/ir/rtypes.py +++ b/mypyc/ir/rtypes.py @@ -1416,6 +1416,18 @@ def check_native_int_range(rtype: RPrimitive, n: int) -> bool: return -limit <= n < limit +def wrap_unsigned_native_int(rtype: RType, n: int) -> int: + """Wrap n around to the range of rtype if it's an unsigned native int type. + + Unsigned native int arithmetic wraps around on overflow at runtime, so + compile-time evaluation must do the same (e.g. -u64(1) is 2**64 - 1). + Return n unchanged for other types. + """ + if is_fixed_width_rtype(rtype) and not rtype.is_signed: + return n & ((1 << (8 * rtype.size)) - 1) + return n + + # Buffers for vec item types that have a packed representation # # Note that the 'items' fields are variable-length arrays, and mypyc IR isn't diff --git a/mypyc/irbuild/builder.py b/mypyc/irbuild/builder.py index 409fe4a0501dc..234de60f285f0 100644 --- a/mypyc/irbuild/builder.py +++ b/mypyc/irbuild/builder.py @@ -133,6 +133,7 @@ object_pointer_rprimitive, object_rprimitive, str_rprimitive, + wrap_unsigned_native_int, ) from mypyc.irbuild.constant_fold import constant_fold_expr from mypyc.irbuild.context import FuncInfo, ImplicitClass @@ -1403,7 +1404,12 @@ def emit_load_final( is_local: whether the name is a local variable """ if final_var.final_value is not None: # this is safe even for non-native names - return self.load_literal_value(final_var.final_value, line) + value = final_var.final_value + if isinstance(value, int): + # mypy evaluates Final initializers such as "-X" using int semantics, + # but unsigned native ints wrap around + value = wrap_unsigned_native_int(self.type_to_rtype(typ), value) + return self.load_literal_value(value, line) elif is_native and not is_local and module_prefix(self.graph, fullname): # A local final variable isn't stored in a static (its fullname is just the # bare name, which could also be the name of a module), but a literal value diff --git a/mypyc/irbuild/constant_fold.py b/mypyc/irbuild/constant_fold.py index 53274dd3f971e..8d02e145ce691 100644 --- a/mypyc/irbuild/constant_fold.py +++ b/mypyc/irbuild/constant_fold.py @@ -26,6 +26,7 @@ UnaryExpr, Var, ) +from mypyc.ir.rtypes import wrap_unsigned_native_int from mypyc.irbuild.util import bytes_from_str if TYPE_CHECKING: @@ -41,6 +42,15 @@ def constant_fold_expr(builder: IRBuilder, expr: Expression) -> ConstantValue | Return None otherwise. """ + value = constant_fold_expr_inner(builder, expr) + if isinstance(value, int) and not isinstance(expr, IntExpr): + # Evaluate each operation using native int semantics if needed, since + # unsigned native ints wrap around on overflow (e.g. -X if X is a u64) + value = wrap_unsigned_native_int(builder.node_type(expr), value) + return value + + +def constant_fold_expr_inner(builder: IRBuilder, expr: Expression) -> ConstantValue | None: if isinstance(expr, IntExpr): return expr.value if isinstance(expr, FloatExpr): diff --git a/mypyc/irbuild/ll_builder.py b/mypyc/irbuild/ll_builder.py index 2df22277b9812..e942a8ef78ff9 100644 --- a/mypyc/irbuild/ll_builder.py +++ b/mypyc/irbuild/ll_builder.py @@ -134,6 +134,7 @@ pointer_rprimitive, short_int_rprimitive, str_rprimitive, + wrap_unsigned_native_int, ) from mypyc.irbuild.util import concrete_arg_kind from mypyc.irbuild.vec import vec_contains, vec_get_item, vec_len_native @@ -2181,11 +2182,7 @@ def unary_minus(self, value: Value, line: int) -> Value: typ = value.type if isinstance(value, Integer): # TODO: Signed overflow? - result = -value.numeric_value() - if is_fixed_width_rtype(typ) and not typ.is_signed: - # Unsigned negation wraps around, like at runtime - result &= (1 << (typ.size * 8)) - 1 - return Integer(result, typ, line) + return Integer(wrap_unsigned_native_int(typ, -value.numeric_value()), typ, line) elif isinstance(value, Float): return Float(-value.value, line) elif is_fixed_width_rtype(typ): diff --git a/mypyc/test-data/irbuild-u64.test b/mypyc/test-data/irbuild-u64.test index 858f04e8354a2..eb63b311c01d7 100644 --- a/mypyc/test-data/irbuild-u64.test +++ b/mypyc/test-data/irbuild-u64.test @@ -335,6 +335,53 @@ L0: c = 18446744073709551611 return 1 +[case testU64FinalWraparound] +from typing import Final +from mypy_extensions import u64 + +X: Final[u64] = 1 +Y: Final[u64] = -X + +def f() -> None: + a = -X + b = X - 2 + c = ~X + d = (X - 2) // 2 + e = Y +def g() -> str: + return "%d" % (-X,) +[typing fixtures/typing-full.pyi] +[out] +def f(): + r0 :: int + a :: u64 + r1 :: int + b :: u64 + r2 :: int + c :: u64 + r3 :: int + d :: u64 + r4 :: int + e :: u64 +L0: + r0 = object 18446744073709551615 + a = 18446744073709551615 + r1 = object 18446744073709551615 + b = 18446744073709551615 + r2 = object 18446744073709551614 + c = 18446744073709551614 + r3 = object 9223372036854775807 + d = 9223372036854775807 + r4 = object 18446744073709551615 + e = 18446744073709551615 + return 1 +def g(): + r0, r1 :: str +L0: + r0 = '18446744073709551615' + r1 = CPyStr_Build(1, r0) + return r1 + [case testU64FromShortInt_64bit] from mypy_extensions import u64 diff --git a/mypyc/test-data/irbuild-u8.test b/mypyc/test-data/irbuild-u8.test index 8d184ce6130be..b5133956c1ba3 100644 --- a/mypyc/test-data/irbuild-u8.test +++ b/mypyc/test-data/irbuild-u8.test @@ -97,6 +97,39 @@ L0: c = 251 return 1 +[case testU8FinalWraparound] +from typing import Final +from mypy_extensions import u8 + +X: Final[u8] = 1 +Y: Final[u8] = -X + +def f() -> None: + a = -X + b = X - 2 + c = ~X + d = (X - 2) // 2 + e = Y +def g() -> str: + return "%d" % (-X,) +[typing fixtures/typing-full.pyi] +[out] +def f(): + a, b, c, d, e :: u8 +L0: + a = 255 + b = 255 + c = 254 + d = 127 + e = 255 + return 1 +def g(): + r0, r1 :: str +L0: + r0 = '255' + r1 = CPyStr_Build(1, r0) + return r1 + [case testU8DivisionByConstant] from mypy_extensions import u8 diff --git a/mypyc/test-data/run-u16.test b/mypyc/test-data/run-u16.test index b350b99dfc4fb..c4d5d077894aa 100644 --- a/mypyc/test-data/run-u16.test +++ b/mypyc/test-data/run-u16.test @@ -284,6 +284,34 @@ def test_negate_literal() -> None: assert div_neg_literal(65535) == 1 assert div_neg_literal(5) == 0 +ONE_FINAL: Final[u16] = 1 +MAX_FINAL: Final[u16] = 65535 +NEG_ONE_FINAL: Final[u16] = -ONE_FINAL + +class FinalAttrs: + TWO: Final[u16] = 2 + +def neg_final() -> u16: + return -ONE_FINAL + +def neg_final_to_int() -> int: + return -ONE_FINAL + +def test_final_wraparound() -> None: + one: u16 = 1 + int() + max_val: u16 = 65535 + int() + assert -ONE_FINAL == -one == 65535 + assert neg_final() == 65535 + assert neg_final_to_int() == 65535 + assert ONE_FINAL - 2 == one - 2 == 65535 + assert ~ONE_FINAL == ~one == 65534 + assert MAX_FINAL + 1 == max_val + 1 == 0 + assert MAX_FINAL * 2 == max_val * 2 == 65534 + assert (ONE_FINAL - 2) // 2 == (one - 2) // 2 == 32767 + assert -ONE_FINAL >> 1 == -one >> 1 == 32767 + assert -FinalAttrs.TWO == -(one * 2) == 65534 + assert NEG_ONE_FINAL == 65535 + def test_u16_truncate_from_i64() -> None: large = i64(2**32 + 65536 + 157 + int()) x = u16(large) diff --git a/mypyc/test-data/run-u32.test b/mypyc/test-data/run-u32.test index 1036ac549cbc8..13d1ac632a403 100644 --- a/mypyc/test-data/run-u32.test +++ b/mypyc/test-data/run-u32.test @@ -299,6 +299,34 @@ def test_negate_literal() -> None: assert div_neg_literal(4294967295) == 1 assert div_neg_literal(5) == 0 +ONE_FINAL: Final[u32] = 1 +MAX_FINAL: Final[u32] = 4294967295 +NEG_ONE_FINAL: Final[u32] = -ONE_FINAL + +class FinalAttrs: + TWO: Final[u32] = 2 + +def neg_final() -> u32: + return -ONE_FINAL + +def neg_final_to_int() -> int: + return -ONE_FINAL + +def test_final_wraparound() -> None: + one: u32 = 1 + int() + max_val: u32 = 4294967295 + int() + assert -ONE_FINAL == -one == 4294967295 + assert neg_final() == 4294967295 + assert neg_final_to_int() == 4294967295 + assert ONE_FINAL - 2 == one - 2 == 4294967295 + assert ~ONE_FINAL == ~one == 4294967294 + assert MAX_FINAL + 1 == max_val + 1 == 0 + assert MAX_FINAL * 2 == max_val * 2 == 4294967294 + assert (ONE_FINAL - 2) // 2 == (one - 2) // 2 == 2147483647 + assert -ONE_FINAL >> 1 == -one >> 1 == 2147483647 + assert -FinalAttrs.TWO == -(one * 2) == 4294967294 + assert NEG_ONE_FINAL == 4294967295 + def test_u32_truncate_from_i64() -> None: large = i64(2**32 + 4294967296 + 157 + int()) x = u32(large) diff --git a/mypyc/test-data/run-u64.test b/mypyc/test-data/run-u64.test index 196532621c662..89ddae598d629 100644 --- a/mypyc/test-data/run-u64.test +++ b/mypyc/test-data/run-u64.test @@ -313,6 +313,34 @@ def test_negate_literal() -> None: assert div_neg_literal(18446744073709551615) == 1 assert div_neg_literal(5) == 0 +ONE_FINAL: Final[u64] = 1 +MAX_FINAL: Final[u64] = 18446744073709551615 +NEG_ONE_FINAL: Final[u64] = -ONE_FINAL + +class FinalAttrs: + TWO: Final[u64] = 2 + +def neg_final() -> u64: + return -ONE_FINAL + +def neg_final_to_int() -> int: + return -ONE_FINAL + +def test_final_wraparound() -> None: + one: u64 = 1 + int() + max_val: u64 = 18446744073709551615 + int() + assert -ONE_FINAL == -one == 18446744073709551615 + assert neg_final() == 18446744073709551615 + assert neg_final_to_int() == 18446744073709551615 + assert ONE_FINAL - 2 == one - 2 == 18446744073709551615 + assert ~ONE_FINAL == ~one == 18446744073709551614 + assert MAX_FINAL + 1 == max_val + 1 == 0 + assert MAX_FINAL * 2 == max_val * 2 == 18446744073709551614 + assert (ONE_FINAL - 2) // 2 == (one - 2) // 2 == 9223372036854775807 + assert -ONE_FINAL >> 1 == -one >> 1 == 9223372036854775807 + assert -FinalAttrs.TWO == -(one * 2) == 18446744073709551614 + assert NEG_ONE_FINAL == 18446744073709551615 + def test_u64_truncate_from_i64() -> None: large = i64(2**62 + 157 + int()) x = u64(large) diff --git a/mypyc/test-data/run-u8.test b/mypyc/test-data/run-u8.test index ee51c10e399b8..f0a55cce08013 100644 --- a/mypyc/test-data/run-u8.test +++ b/mypyc/test-data/run-u8.test @@ -281,6 +281,34 @@ def test_negate_literal() -> None: assert div_neg_literal(255) == 1 assert div_neg_literal(5) == 0 +ONE_FINAL: Final[u8] = 1 +MAX_FINAL: Final[u8] = 255 +NEG_ONE_FINAL: Final[u8] = -ONE_FINAL + +class FinalAttrs: + TWO: Final[u8] = 2 + +def neg_final() -> u8: + return -ONE_FINAL + +def neg_final_to_int() -> int: + return -ONE_FINAL + +def test_final_wraparound() -> None: + one: u8 = 1 + int() + max_val: u8 = 255 + int() + assert -ONE_FINAL == -one == 255 + assert neg_final() == 255 + assert neg_final_to_int() == 255 + assert ONE_FINAL - 2 == one - 2 == 255 + assert ~ONE_FINAL == ~one == 254 + assert MAX_FINAL + 1 == max_val + 1 == 0 + assert MAX_FINAL * 2 == max_val * 2 == 254 + assert (ONE_FINAL - 2) // 2 == (one - 2) // 2 == 127 + assert -ONE_FINAL >> 1 == -one >> 1 == 127 + assert -FinalAttrs.TWO == -(one * 2) == 254 + assert NEG_ONE_FINAL == 255 + def test_u8_truncate_from_i64() -> None: large = i64(2**32 + 256 + 157 + int()) x = u8(large) From e5c68b51bc8a230cf7987f873d39d2029e919206 Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Sat, 14 Nov 2026 17:29:50 +0500 Subject: [PATCH 3/4] [mypyc] Fix converting large unsigned native int constants to int on 32-bit Converting a native int constant that doesn't fit in a short int, such as -u32(1) or -u64(1), to int generated an unreachable fast path that truncated or shifted the constant. On 32-bit platforms the C compiler rejected this with -Werror=overflow. Load such constants as int literals instead. --- mypyc/irbuild/ll_builder.py | 7 +++++++ mypyc/test-data/irbuild-u64.test | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/mypyc/irbuild/ll_builder.py b/mypyc/irbuild/ll_builder.py index e942a8ef78ff9..d3fccb52a4ce3 100644 --- a/mypyc/irbuild/ll_builder.py +++ b/mypyc/irbuild/ll_builder.py @@ -827,6 +827,13 @@ def coerce_short_int_to_fixed_width(self, src: Value, target_type: RType, line: return res def coerce_fixed_width_to_int(self, src: Value, line: int) -> Value: + if isinstance(src, Integer): + value = src.numeric_value() + if not (MIN_SHORT_INT <= value <= MAX_SHORT_INT): + # The constant doesn't fit in a short int, so load it as an int literal. + # The fast path below would be unreachable, and C compilers may reject + # it, since it would truncate the constant (e.g. u64 on 32-bit platforms). + return self.load_int(value, line) if ( (is_int32_rprimitive(src.type) and PLATFORM_SIZE == 8) or is_int16_rprimitive(src.type) diff --git a/mypyc/test-data/irbuild-u64.test b/mypyc/test-data/irbuild-u64.test index eb63b311c01d7..ae9e21306f8ab 100644 --- a/mypyc/test-data/irbuild-u64.test +++ b/mypyc/test-data/irbuild-u64.test @@ -335,6 +335,25 @@ L0: c = 18446744073709551611 return 1 +[case testU64LargeConstantToInt_64bit] +from mypy_extensions import u64 + +def f() -> int: + return -u64(1) +def g() -> int: + return -u64(5) +[out] +def f(): + r0 :: int +L0: + r0 = object 18446744073709551615 + return r0 +def g(): + r0 :: int +L0: + r0 = object 18446744073709551611 + return r0 + [case testU64FinalWraparound] from typing import Final from mypy_extensions import u64 From f96ea300d67e72add9cb531b79858bf9955a642d Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Sun, 15 Nov 2026 02:07:17 +0500 Subject: [PATCH 4/4] [mypyc] Don't wrap bool constants when folding unsigned native ints When mypyc is compiled, passing True to wrap_unsigned_native_int (which takes and returns int) produces the int 1, so loading a bool Final value generated a boxed int that was unboxed as bool. --- mypyc/irbuild/builder.py | 5 +++-- mypyc/irbuild/constant_fold.py | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/mypyc/irbuild/builder.py b/mypyc/irbuild/builder.py index 234de60f285f0..ef0ab899d088b 100644 --- a/mypyc/irbuild/builder.py +++ b/mypyc/irbuild/builder.py @@ -1405,9 +1405,10 @@ def emit_load_final( """ if final_var.final_value is not None: # this is safe even for non-native names value = final_var.final_value - if isinstance(value, int): + if isinstance(value, int) and not isinstance(value, bool): # mypy evaluates Final initializers such as "-X" using int semantics, - # but unsigned native ints wrap around + # but unsigned native ints wrap around. (Skip bools, since the helper + # returns a plain int when compiled.) value = wrap_unsigned_native_int(self.type_to_rtype(typ), value) return self.load_literal_value(value, line) elif is_native and not is_local and module_prefix(self.graph, fullname): diff --git a/mypyc/irbuild/constant_fold.py b/mypyc/irbuild/constant_fold.py index 8d02e145ce691..69a3b9893d2de 100644 --- a/mypyc/irbuild/constant_fold.py +++ b/mypyc/irbuild/constant_fold.py @@ -43,9 +43,10 @@ def constant_fold_expr(builder: IRBuilder, expr: Expression) -> ConstantValue | Return None otherwise. """ value = constant_fold_expr_inner(builder, expr) - if isinstance(value, int) and not isinstance(expr, IntExpr): + if isinstance(value, int) and not isinstance(value, bool) and not isinstance(expr, IntExpr): # Evaluate each operation using native int semantics if needed, since - # unsigned native ints wrap around on overflow (e.g. -X if X is a u64) + # unsigned native ints wrap around on overflow (e.g. -X if X is a u64). + # Bools are skipped, since the helper returns a plain int when compiled. value = wrap_unsigned_native_int(builder.node_type(expr), value) return value