GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||
GeezerVison is showing github.com — [mypyc] Fix native int shifts by negative or too large counts by Jahongir-Qurbonov · Pull Request #22419 · python/mypy Conversation
Contributor
Shifts of native ints ( from mypy_extensions import i64, u32 def rshift(x: i64, n: i64) -> i64: return x >> n def lshift(x: u32, n: u32) -> u32: return x << n rshift(-8, 64) # -8, expected -1 rshift(5, 64) # 5, expected 0 rshift(8, -1) # 0, expected ValueError: negative shift count lshift(1, 32) # 1, expected 0 With a constant count (e.g. Now the plain shift is only used when the count is in range:
This also fixes two related codegen issues:
Added run tests for all native int types. They cover counts 0, width-1, width, width+1, 64, 100 and negative counts, both constant and variable.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsShifts of native ints (i64, i32, i16, u32, u16, u8) were compiled to a plain C shift, which is undefined behavior if the count is negative or at least the bit width. In practice the count was masked, so e.g. i64 `5 >> 64` gave 5 and `8 >> -1` gave 0, and a constant count such as `x >> 64` failed the C build with -Werror. Use the plain shift only if the count is in range. Otherwise a negative count raises ValueError, a left shift gives 0 and a right shift gives 0, or -1 for a negative signed value. Constant counts are resolved at compile time. The common case needs one extra unsigned comparison. Also cast an integer literal left operand to the native int type in C, since the C shift type follows the promoted left operand (`3 << n` for i64 was a 32-bit shift), and add the missing uint16_t cast for unsigned comparisons of i16 values.
Collaborator
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Contributor
Author
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ReviewersNo reviewsUh oh!
There was an error while loading. Please reload this page. AssigneesNo one assignedLabels
None yet
ProjectsMilestoneNo milestoneDevelopmentSuccessfully merging this pull request may close these issues. Uh oh!
There was an error while loading. Please reload this page. 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. | ||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |