-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Emit range assume after IntToInt cast in MatchBranchSimplification #155206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
53 changes: 53 additions & 0 deletions
53
tests/codegen-llvm/exhaustive-match-bounds-check-issue-149480.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| //@ compile-flags: -O | ||
| // Regression test for https://github.com/rust-lang/rust/issues/149480: | ||
| // the bounds check should be eliminated when indexing an array with | ||
| // the result of an exhaustive match over nested enums. The range | ||
| // assume emitted by MatchBranchSimplification after the IntToInt cast | ||
| // allows LLVM to prove the index is in-bounds. | ||
|
|
||
| #![crate_type = "lib"] | ||
|
|
||
| pub enum Foo { | ||
| A(A), | ||
| B(B), | ||
| } | ||
| pub enum A { | ||
| A0, | ||
| A1, | ||
| A2, | ||
| } | ||
| pub enum B { | ||
| B0, | ||
| B1, | ||
| } | ||
|
|
||
| // CHECK-LABEL: @bar | ||
| #[no_mangle] | ||
| pub fn bar(foo: Foo, arr: &[u8; 5]) -> u8 { | ||
| let offset: usize = match foo { | ||
| Foo::A(A::A0) => 0, | ||
| Foo::A(A::A1) => 1, | ||
| Foo::A(A::A2) => 2, | ||
| Foo::B(B::B0) => 3, | ||
| Foo::B(B::B1) => 4, | ||
| }; | ||
| // The bounds check must be eliminated. | ||
| // CHECK-NOT: panic_bounds_check | ||
| // Positive check: the indexing must lower to a plain load from `arr`, | ||
| // so the test cannot pass accidentally if `bar` is optimized into | ||
| // another kind of panicking path or if `panic_bounds_check` is | ||
| // renamed. | ||
| // CHECK: load i8, ptr | ||
| // CHECK: ret i8 | ||
| arr[offset] | ||
| } | ||
|
|
||
| // Sanity check: make sure `panic_bounds_check` is still the symbol LLVM | ||
| // emits for a non-elidable out-of-bounds index, so the `CHECK-NOT` above | ||
| // is guarding against something real. | ||
| // CHECK-LABEL: @test_check | ||
| #[no_mangle] | ||
| pub fn test_check(arr: &[u8], i: usize) -> u8 { | ||
| // CHECK: panic_bounds_check | ||
| arr[i] | ||
| } | ||
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
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
View changes since the review
It's good to have a negative check but without a positive test as well in here (checking for the IR sequence that should appear) this test could incorrectly pass if the function is renamed or if it's optimized to some other kind of panicking.
You should be able to draw inspiration from other codegen tests. Not having a positive check is a common flaw in the existing test suite, but it's not universal :)