forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#116930 - RalfJung:raw-ptr-match, r=davidtwco
patterns: reject raw pointers that are not just integers Matching against `0 as *const i32` is fine, matching against `&42 as *const i32` is not. This extends the existing check against function pointers and wide pointers: we now uniformly reject all these pointer types during valtree construction, and then later lint because of that. See [here](rust-lang#116930 (comment)) for some more explanation and context. Also fixes rust-lang#116929. Cc `@oli-obk` `@lcnr`
- Loading branch information
Showing
18 changed files
with
375 additions
and
98 deletions.
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
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.