-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Make closure capturing have consistent and correct behaviour around patterns #138961
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
Merged
+809
−342
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9cb47c6
ExprUseVisitor: properly report discriminant reads
meithecatte e25803a
ExprUseVisitor: remove maybe_read_scrutinee
meithecatte 1cc7e7f
Add test case for issue 138973
meithecatte 8800f95
Avoid using #[derive] in test
meithecatte 462b51a
Add debug logging in hir_typeck::upvar
meithecatte 19f6bc4
Add miri tests for new closure capture behavior
meithecatte e6c5999
add a comment: MatchPair and ExprUseVisitor must stay in sync
meithecatte 3d5d1d7
Mark crash 140011 as fixed
meithecatte a2e249c
ExprUseVisitor: resolve a FIXME – it's fine as is
meithecatte 4f7915b
Bless miri tests
traviscross d05b1d7
Add a test for deref projections in new pattern capture behavior
meithecatte 2443fba
Rewrite the comment on is_multivariant_adt
meithecatte ae19274
Add more variations from the PR thread
meithecatte 9e98885
re-bless miri tests
meithecatte de65837
`search_is_some`: move to nursery
meithecatte 55bbe05
Re-bless tests
meithecatte 011c5ed
Skip rust-analyzer closure layout tests for now
meithecatte 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| #![warn(clippy::search_is_some)] | ||
| pub struct Thing; | ||
| //@no-rustfix | ||
| pub fn has_thing(things: &[Thing]) -> bool { | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // This test serves to document the change in semantics introduced by | ||
| // rust-lang/rust#138961. | ||
| // | ||
| // A corollary of partial-pattern.rs: while the tuple access testcase makes | ||
| // it clear why these semantics are useful, it is actually the dereference | ||
| // being performed by the pattern that matters. | ||
|
|
||
| fn main() { | ||
| // the inner reference is dangling | ||
| let x: &&u32 = unsafe { | ||
| let x: u32 = 42; | ||
| &&* &raw const x | ||
| }; | ||
|
|
||
| let _ = || { //~ ERROR: encountered a dangling reference | ||
| match x { | ||
| &&_y => {}, | ||
| } | ||
| }; | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/tools/miri/tests/fail/closures/deref-in-pattern.stderr
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,18 @@ | ||
| error: Undefined Behavior: constructing invalid value: encountered a dangling reference (use-after-free) | ||
| --> tests/fail/closures/deref-in-pattern.rs:LL:CC | ||
| | | ||
| LL | let _ = || { | ||
| | _____________^ | ||
| LL | | match x { | ||
| LL | | &&_y => {}, | ||
| LL | | } | ||
| LL | | }; | ||
| | |_____^ Undefined Behavior occurred here | ||
| | | ||
| = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
| = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
|
|
||
| note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
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,28 @@ | ||
| // This test serves to document the change in semantics introduced by | ||
| // rust-lang/rust#138961. | ||
| // | ||
| // Previously, the closure would capture the entirety of x, and access *(*x).0 | ||
| // when called. Now, the closure only captures *(*x).0, which means that | ||
| // a &*(*x).0 reborrow happens when the closure is constructed. | ||
| // | ||
| // Hence, if one of the references is dangling, this constitutes newly introduced UB | ||
| // in the case where the closure doesn't get called. This isn't a big deal, | ||
| // because while opsem only now considers this to be UB, the unsafe code | ||
| // guidelines have long recommended against any handling of dangling references. | ||
|
|
||
| fn main() { | ||
| // the inner references are dangling | ||
| let x: &(&u32, &u32) = unsafe { | ||
| let a = 21; | ||
| let b = 37; | ||
| let ra = &* &raw const a; | ||
| let rb = &* &raw const b; | ||
| &(ra, rb) | ||
| }; | ||
|
|
||
| let _ = || { //~ ERROR: encountered a dangling reference | ||
| match x { | ||
| (&_y, _) => {}, | ||
| } | ||
| }; | ||
| } |
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,18 @@ | ||
| error: Undefined Behavior: constructing invalid value: encountered a dangling reference (use-after-free) | ||
| --> tests/fail/closures/partial-pattern.rs:LL:CC | ||
| | | ||
| LL | let _ = || { | ||
| | _____________^ | ||
| LL | | match x { | ||
| LL | | (&_y, _) => {}, | ||
| LL | | } | ||
| LL | | }; | ||
| | |_____^ Undefined Behavior occurred here | ||
| | | ||
| = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
| = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
|
|
||
| note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
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,31 @@ | ||
| // Motivated by rust-lang/rust#138961, this shows how invalid discriminants interact with | ||
| // closure captures. | ||
| #![feature(never_type)] | ||
|
|
||
| #[repr(C)] | ||
| #[allow(dead_code)] | ||
| enum E { | ||
| V0, // discriminant: 0 | ||
| V1, // 1 | ||
| V2(!), // 2 | ||
| } | ||
|
|
||
| fn main() { | ||
| assert_eq!(std::mem::size_of::<E>(), 4); | ||
|
|
||
| let val = 2u32; | ||
| let ptr = (&raw const val).cast::<E>(); | ||
| let r = unsafe { &*ptr }; | ||
| let f = || { | ||
| // After rust-lang/rust#138961, constructing the closure performs a reborrow of r. | ||
| // Nevertheless, the discriminant is only actually inspected when the closure | ||
| // is called. | ||
| match r { //~ ERROR: read discriminant of an uninhabited enum variant | ||
| E::V0 => {} | ||
| E::V1 => {} | ||
| E::V2(_) => {} | ||
| } | ||
| }; | ||
|
|
||
| f(); | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/tools/miri/tests/fail/closures/uninhabited-variant.stderr
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,20 @@ | ||
| error: Undefined Behavior: read discriminant of an uninhabited enum variant | ||
| --> tests/fail/closures/uninhabited-variant.rs:LL:CC | ||
| | | ||
| LL | match r { | ||
| | ^ Undefined Behavior occurred here | ||
| | | ||
| = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
| = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
| = note: BACKTRACE: | ||
| = note: inside closure at tests/fail/closures/uninhabited-variant.rs:LL:CC | ||
| note: inside `main` | ||
| --> tests/fail/closures/uninhabited-variant.rs:LL:CC | ||
| | | ||
| LL | f(); | ||
| | ^^^ | ||
|
|
||
| note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
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
File renamed without changes.
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,15 @@ | ||
| //@ known-bug: #119786 | ||
| //@ edition:2021 | ||
|
|
||
| fn enum_upvar() { | ||
| type T = impl Copy; | ||
| let foo: T = Some((1u32, 2u32)); | ||
| let x = move || { | ||
| match foo { | ||
| None => (), | ||
| Some(_) => (), | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| pub fn main() {} |
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,15 @@ | ||
| //@ known-bug: #119786 | ||
| //@ edition:2021 | ||
|
|
||
| fn enum_upvar() { | ||
| type T = impl Copy; | ||
| let foo: T = Some((1u32, 2u32)); | ||
| let x = move || { | ||
| match foo { | ||
| None => (), | ||
| Some((a, b)) => (), | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| pub fn main() {} |
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.
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.
huh, was this signed off by the clippy team?
cc @rust-lang/clippy
I grepped through the comments and didn't find any discussion around this
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.
It was. See #138961 (comment) and rust-lang/rust-clippy#16086.
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.
ah good, thanks!