[ty] Infer precise types for bit-shift operations on integer literals#23301
Merged
AlexWaygood merged 2 commits intomainfrom Feb 16, 2026
Merged
[ty] Infer precise types for bit-shift operations on integer literals#23301AlexWaygood merged 2 commits intomainfrom
AlexWaygood merged 2 commits intomainfrom
Conversation
…rflow - Support literal int inference for `<<` and `>>` operators - Use `checked_neg()` for unary negation to handle i64::MIN overflow - Add overflow detection for left shifts into the sign bit using a leading-bits headroom check: count redundant sign-extension bits to determine how far a value can be shifted without overflowing i64 - Add tests for bitshift arithmetic with booleans and integers https://claude.ai/code/session_01MPdchMiqZsZQ2Le3JV9aWf
Typing conformance resultsNo changes detected ✅ |
|
Memory usage reportMemory usage unchanged ✅ |
carljm
approved these changes
Feb 16, 2026
Contributor
There was a problem hiding this comment.
Looks good!
Codex notes two edge-case precision issues; I'm not sure we care, but in case you want to fix them up:
0 << nis inferred asintfor large non-negative n, but Python result is always exactly 0.- Very large right-shift counts (> u32::MAX) lose literal precision:
- ty:
reveal_type(1 >> 5000000000) => int,reveal_type(-1 >> 5000000000) => int - Python:
1 >> 5000000000 => 0,-1 >> 5000000000 => -1
- ty:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes astral-sh/ty#517.
This PR revives #18329 / #19281, but without the controversial bit of those PRs (the new diagnostic). Rather than adding a new diagnostic if an integer is left-shifted or right-shifted by a negative number, we just ignore that and infer
int. We can always add a new diagnostic later, if we find consensus on it.Along the way, Claude also discovered that
reveal_type(-(-9223372036854775807 - 1))currently revealsLiteral[-9223372036854775808]onmain, which is... not correct. So this PR fixes that too.Test Coverage
mdtests
Co-authored-by: Brandt Bucher [email protected]