Skip to content

[mypyc] Don't fold native int MIN // -1 to a wrapped-around value - #22558

Merged
JukkaL merged 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-native-int-floordiv-fold
Nov 26, 2026
Merged

JukkaL merged 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-native-int-floordiv-fold

Conversation

@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor

Since #22556, constant folding wraps signed native int results around, to match runtime semantics. Floor division doesn't wrap around at runtime, though: MIN // -1 raises OverflowError. So if MIN is an i64 final with value -2**63, MIN // -1 was folded to -2**63, while the same division of non-constant values raises OverflowError. Before #22556, this was a compile-time error (the folded value was out of range for i64).

Don't fold a native int floor division if the result is out of range, so that it raises OverflowError at runtime. Only signed MIN // -1 can overflow. MIN % -1 is 0 and is still folded.

Constant folding wraps signed native int results around since python#22556,
but floor division doesn't wrap around at runtime: MIN // -1 raises
OverflowError. Don't fold a floor division if the result is out of
range, so that it raises OverflowError at runtime as well.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good catch!

@JukkaL
JukkaL merged commit 62b3970 into python:master Nov 26, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants