-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
patterns: reject raw pointers that are not just integers #116930
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bec88ad
patterns: reject raw pointers that are not just integers
RalfJung af6c7e0
also lint against fn ptr and raw ptr nested inside the const
RalfJung 70a8e15
make pointer_structural_match warn-by-default
RalfJung 03b24f2
remove some dead code
RalfJung 3058865
avoid unnecessary nested conditionals
RalfJung 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 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 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 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 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 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 |
---|---|---|
|
@@ -247,6 +247,7 @@ marker_impls! { | |
/// | ||
/// const CFN: Wrap<fn(&())> = Wrap(higher_order); | ||
/// | ||
/// #[allow(pointer_structural_match)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW these entire trait docs are outdated; that's tracked in #115881. |
||
/// fn main() { | ||
/// match CFN { | ||
/// CFN => {} | ||
|
This file contains 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
12 changes: 12 additions & 0 deletions
12
tests/ui/closures/2229_closure_analysis/match/match-edge-cases_1.stderr
This file contains 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,12 @@ | ||
warning: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details. | ||
--> $DIR/match-edge-cases_1.rs:29:13 | ||
| | ||
LL | NUMBER_POINTER => (), | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/70861> | ||
= note: `#[warn(pointer_structural_match)]` on by default | ||
|
||
warning: 1 warning emitted | ||
|
32 changes: 32 additions & 0 deletions
32
tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.rs
This file contains 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,32 @@ | ||
#![deny(pointer_structural_match)] | ||
#![allow(dead_code)] | ||
|
||
const C: *const u8 = &0; | ||
// Make sure we also find pointers nested in other types. | ||
const C_INNER: (*const u8, u8) = (C, 0); | ||
|
||
fn foo(x: *const u8) { | ||
match x { | ||
C => {} //~ERROR: behave unpredictably | ||
//~| previously accepted | ||
_ => {} | ||
} | ||
} | ||
|
||
fn foo2(x: *const u8) { | ||
match (x, 1) { | ||
C_INNER => {} //~ERROR: behave unpredictably | ||
//~| previously accepted | ||
_ => {} | ||
} | ||
} | ||
|
||
const D: *const [u8; 4] = b"abcd"; | ||
|
||
fn main() { | ||
match D { | ||
D => {} //~ERROR: behave unpredictably | ||
//~| previously accepted | ||
_ => {} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.stderr
This file contains 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,34 @@ | ||
error: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details. | ||
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:10:9 | ||
| | ||
LL | C => {} | ||
| ^ | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/70861> | ||
note: the lint level is defined here | ||
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:1:9 | ||
| | ||
LL | #![deny(pointer_structural_match)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details. | ||
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:18:9 | ||
| | ||
LL | C_INNER => {} | ||
| ^^^^^^^ | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/70861> | ||
|
||
error: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details. | ||
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:28:9 | ||
| | ||
LL | D => {} | ||
| ^ | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/70861> | ||
|
||
error: aborting due to 3 previous errors | ||
|
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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.
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.
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.
I didn't realize this lint is still allow-by-default oO. Seems high time we make it warn-by-default?