-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
tests encoding current behavior for various cases of "binding" to _. #69573
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6e70849
tests encoding current behavior for various cases of "binding" to _.
pnkfelix ee7a035
Update src/test/ui/binding/issue-53114-borrow-checks.rs
pnkfelix 192d533
Update src/test/ui/binding/issue-53114-safety-checks.rs
pnkfelix 1ff99b7
copy test cases to `if let` as well.
pnkfelix 22ea3a4
--bless you
Centril 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Issue #53114: NLL's borrow check had some deviations from the old borrow | ||
// checker, and both had some deviations from our ideal state. This test | ||
// captures the behavior of how `_` bindings are handled with respect to how we | ||
// flag expressions that are meant to request unsafe blocks. | ||
#![allow(irrefutable_let_patterns)] | ||
struct M; | ||
|
||
fn let_wild_gets_moved_expr() { | ||
let m = M; | ||
drop(m); | ||
let _ = m; // accepted, and want it to continue to be | ||
|
||
let mm = (M, M); // variation on above with `_` in substructure | ||
let (_x, _) = mm; | ||
let (_, _y) = mm; | ||
let (_, _) = mm; | ||
} | ||
|
||
fn match_moved_expr_to_wild() { | ||
let m = M; | ||
drop(m); | ||
match m { _ => { } } // #53114: should eventually be accepted too | ||
//~^ ERROR [E0382] | ||
|
||
let mm = (M, M); // variation on above with `_` in substructure | ||
match mm { (_x, _) => { } } | ||
match mm { (_, _y) => { } } | ||
//~^ ERROR [E0382] | ||
match mm { (_, _) => { } } | ||
//~^ ERROR [E0382] | ||
} | ||
|
||
fn if_let_moved_expr_to_wild() { | ||
let m = M; | ||
drop(m); | ||
if let _ = m { } // #53114: should eventually be accepted too | ||
//~^ ERROR [E0382] | ||
|
||
let mm = (M, M); // variation on above with `_` in substructure | ||
if let (_x, _) = mm { } | ||
if let (_, _y) = mm { } | ||
//~^ ERROR [E0382] | ||
if let (_, _) = mm { } | ||
//~^ ERROR [E0382] | ||
} | ||
|
||
fn let_wild_gets_borrowed_expr() { | ||
let mut m = M; | ||
let r = &mut m; | ||
let _ = m; // accepted, and want it to continue to be | ||
// let _x = m; // (compare with this error.) | ||
drop(r); | ||
|
||
let mut mm = (M, M); // variation on above with `_` in substructure | ||
let (r1, r2) = (&mut mm.0, &mut mm.1); | ||
let (_, _) = mm; | ||
drop((r1, r2)); | ||
} | ||
|
||
fn match_borrowed_expr_to_wild() { | ||
let mut m = M; | ||
let r = &mut m; | ||
match m { _ => {} } ; // accepted, and want it to continue to be | ||
drop(r); | ||
|
||
let mut mm = (M, M); // variation on above with `_` in substructure | ||
let (r1, r2) = (&mut mm.0, &mut mm.1); | ||
match mm { (_, _) => { } } | ||
drop((r1, r2)); | ||
} | ||
|
||
fn if_let_borrowed_expr_to_wild() { | ||
let mut m = M; | ||
let r = &mut m; | ||
if let _ = m { } // accepted, and want it to continue to be | ||
drop(r); | ||
|
||
let mut mm = (M, M); // variation on above with `_` in substructure | ||
let (r1, r2) = (&mut mm.0, &mut mm.1); | ||
if let (_, _) = mm { } | ||
drop((r1, r2)); | ||
} | ||
|
||
fn main() { } |
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,65 @@ | ||
error[E0382]: use of moved value: `m` | ||
--> $DIR/issue-53114-borrow-checks.rs:22:11 | ||
| | ||
LL | let m = M; | ||
| - move occurs because `m` has type `M`, which does not implement the `Copy` trait | ||
LL | drop(m); | ||
| - value moved here | ||
LL | match m { _ => { } } // #53114: should eventually be accepted too | ||
| ^ value used here after move | ||
|
||
error[E0382]: use of moved value: `mm` | ||
--> $DIR/issue-53114-borrow-checks.rs:27:11 | ||
| | ||
LL | match mm { (_x, _) => { } } | ||
| -- value moved here | ||
LL | match mm { (_, _y) => { } } | ||
| ^^ value used here after partial move | ||
| | ||
= note: move occurs because `mm.0` has type `M`, which does not implement the `Copy` trait | ||
|
||
error[E0382]: use of moved value: `mm` | ||
--> $DIR/issue-53114-borrow-checks.rs:29:11 | ||
| | ||
LL | match mm { (_, _y) => { } } | ||
| -- value moved here | ||
LL | | ||
LL | match mm { (_, _) => { } } | ||
| ^^ value used here after partial move | ||
| | ||
= note: move occurs because `mm.1` has type `M`, which does not implement the `Copy` trait | ||
|
||
error[E0382]: use of moved value: `m` | ||
--> $DIR/issue-53114-borrow-checks.rs:36:16 | ||
| | ||
LL | let m = M; | ||
| - move occurs because `m` has type `M`, which does not implement the `Copy` trait | ||
LL | drop(m); | ||
| - value moved here | ||
LL | if let _ = m { } // #53114: should eventually be accepted too | ||
| ^ value used here after move | ||
|
||
error[E0382]: use of moved value: `mm` | ||
--> $DIR/issue-53114-borrow-checks.rs:41:22 | ||
| | ||
LL | if let (_x, _) = mm { } | ||
| -- value moved here | ||
LL | if let (_, _y) = mm { } | ||
| ^^ value used here after partial move | ||
| | ||
= note: move occurs because `mm.0` has type `M`, which does not implement the `Copy` trait | ||
|
||
error[E0382]: use of moved value: `mm` | ||
--> $DIR/issue-53114-borrow-checks.rs:43:21 | ||
| | ||
LL | if let (_, _y) = mm { } | ||
| -- value moved here | ||
LL | | ||
LL | if let (_, _) = mm { } | ||
| ^^ value used here after partial move | ||
| | ||
= note: move occurs because `mm.1` has type `M`, which does not implement the `Copy` trait | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0382`. |
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,51 @@ | ||
// Issue #53114: NLL's borrow check had some deviations from the old borrow | ||
// checker, and both had some deviations from our ideal state. This test | ||
// captures the behavior of how `_` bindings are handled with respect to how we | ||
// flag expressions that are meant to request unsafe blocks. | ||
|
||
#![feature(untagged_unions)] | ||
|
||
struct I(i64); | ||
struct F(f64); | ||
|
||
union U { a: I, b: F } | ||
|
||
#[repr(packed)] | ||
struct P { | ||
a: &'static i8, | ||
b: &'static u32, | ||
} | ||
|
||
fn let_wild_gets_unsafe_field() { | ||
let u1 = U { a: I(0) }; | ||
let u2 = U { a: I(1) }; | ||
let p = P { a: &2, b: &3 }; | ||
let _ = &p.b; //~ WARN E0133 | ||
//~^ WARN will become a hard error | ||
let _ = u1.a; // #53114: should eventually signal error as well | ||
let _ = &u2.a; //~ ERROR [E0133] | ||
|
||
// variation on above with `_` in substructure | ||
let (_,) = (&p.b,); //~ WARN E0133 | ||
//~^ WARN will become a hard error | ||
let (_,) = (u1.a,); //~ ERROR [E0133] | ||
let (_,) = (&u2.a,); //~ ERROR [E0133] | ||
} | ||
|
||
fn match_unsafe_field_to_wild() { | ||
let u1 = U { a: I(0) }; | ||
let u2 = U { a: I(1) }; | ||
let p = P { a: &2, b: &3 }; | ||
match &p.b { _ => { } } //~ WARN E0133 | ||
//~^ WARN will become a hard error | ||
match u1.a { _ => { } } //~ ERROR [E0133] | ||
match &u2.a { _ => { } } //~ ERROR [E0133] | ||
|
||
// variation on above with `_` in substructure | ||
match (&p.b,) { (_,) => { } } //~ WARN E0133 | ||
//~^ WARN will become a hard error | ||
match (u1.a,) { (_,) => { } } //~ ERROR [E0133] | ||
match (&u2.a,) { (_,) => { } } //~ ERROR [E0133] | ||
} | ||
|
||
fn main() { } |
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,100 @@ | ||
warning: borrow of packed field is unsafe and requires unsafe function or block (error E0133) | ||
--> $DIR/issue-53114-safety-checks.rs:23:13 | ||
| | ||
LL | let _ = &p.b; | ||
| ^^^^ | ||
| | ||
= note: `#[warn(safe_packed_borrows)]` on by default | ||
= 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 #46043 <https://github.com/rust-lang/rust/issues/46043> | ||
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/issue-53114-safety-checks.rs:26:13 | ||
| | ||
LL | let _ = &u2.a; | ||
| ^^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
warning: borrow of packed field is unsafe and requires unsafe function or block (error E0133) | ||
--> $DIR/issue-53114-safety-checks.rs:29:17 | ||
| | ||
LL | let (_,) = (&p.b,); | ||
| ^^^^ | ||
| | ||
= 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 #46043 <https://github.com/rust-lang/rust/issues/46043> | ||
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/issue-53114-safety-checks.rs:31:17 | ||
| | ||
LL | let (_,) = (u1.a,); | ||
| ^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/issue-53114-safety-checks.rs:32:17 | ||
| | ||
LL | let (_,) = (&u2.a,); | ||
| ^^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
warning: borrow of packed field is unsafe and requires unsafe function or block (error E0133) | ||
--> $DIR/issue-53114-safety-checks.rs:39:11 | ||
| | ||
LL | match &p.b { _ => { } } | ||
| ^^^^ | ||
| | ||
= 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 #46043 <https://github.com/rust-lang/rust/issues/46043> | ||
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/issue-53114-safety-checks.rs:41:11 | ||
| | ||
LL | match u1.a { _ => { } } | ||
| ^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/issue-53114-safety-checks.rs:42:11 | ||
| | ||
LL | match &u2.a { _ => { } } | ||
| ^^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
warning: borrow of packed field is unsafe and requires unsafe function or block (error E0133) | ||
--> $DIR/issue-53114-safety-checks.rs:45:12 | ||
| | ||
LL | match (&p.b,) { (_,) => { } } | ||
| ^^^^ | ||
| | ||
= 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 #46043 <https://github.com/rust-lang/rust/issues/46043> | ||
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/issue-53114-safety-checks.rs:47:12 | ||
| | ||
LL | match (u1.a,) { (_,) => { } } | ||
| ^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/issue-53114-safety-checks.rs:48:12 | ||
| | ||
LL | match (&u2.a,) { (_,) => { } } | ||
| ^^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error: aborting due to 7 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0133`. |
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.
General note: Would be good to also test
if let
as it might diverge somewhat in the future in terms of MIR building.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.
hmm. interesting the most obvious "ports" of these tests as written into
if let
form runs afoul of "warning: irrefutable if-let pattern"Which makes total sense.
But also leads me to wonder whether I should be testing them in different fashion. I'm not sure these tests translate over to refutable patterns, because if the data has been moved away, then you won't have a discriminant to inspect.
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.
Perhaps just inserting an
Option<_>
layer would be good for the match/if-let tests? iirc I did that in a somewhat similar test somewhere. Presumably_
should work below sum types as well.