-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
fix: fix the capture behavior of if let in closures
#154210
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
Open
Embers-of-the-Fire
wants to merge
6
commits into
rust-lang:main
Choose a base branch
from
Embers-of-the-Fire:feat/if-let-no-full-capture
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
63c15e4
test: cover current if let closure capture behavior
Embers-of-the-Fire 923ef1f
fix: drop eager if let scrutinee borrow during capture analysis
Embers-of-the-Fire 665416d
test(ui): add regression for if-let closure capture size
Embers-of-the-Fire 2aadcee
feat: remove callback param for walk_local method
Embers-of-the-Fire 1f81a55
test: add if let test for partial capture
Embers-of-the-Fire f491a6c
test(miri): add if-let closure capture UB smoke tests
Embers-of-the-Fire 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
25 changes: 25 additions & 0 deletions
25
src/tools/miri/tests/fail/match/closures/if-let-deref-in-pattern.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,25 @@ | ||
| // This test serves to document the change in semantics introduced by | ||
| // rust-lang/rust#138961, extended to `if let` closure captures. | ||
| // | ||
| // 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. | ||
| // | ||
| // Before rust-lang/rust#154210, `if let` in closures captured all of `x`, so | ||
| // this test did not fail because the closure is never called. | ||
| //@normalize-stderr-test: "constructing invalid value of type [^:]+:" -> "constructing invalid value:" | ||
|
|
||
| #![allow(irrefutable_let_patterns)] | ||
|
|
||
| fn main() { | ||
| // the inner reference is dangling | ||
| let x: &&u32 = unsafe { | ||
| let x: u32 = 42; | ||
| &&*&raw const x | ||
| }; | ||
|
|
||
| //~v ERROR: encountered a dangling reference | ||
| let _ = || { | ||
| if let &&_y = x {} | ||
| }; | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/tools/miri/tests/fail/match/closures/if-let-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,16 @@ | ||
| error: Undefined Behavior: constructing invalid value: encountered a dangling reference (use-after-free) | ||
| --> tests/fail/match/closures/if-let-deref-in-pattern.rs:LL:CC | ||
| | | ||
| LL | let _ = || { | ||
| | _____________^ | ||
| LL | | if let &&_y = x {} | ||
| 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 | ||
|
|
33 changes: 33 additions & 0 deletions
33
src/tools/miri/tests/fail/match/closures/if-let-partial-pattern.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,33 @@ | ||
| // This test serves to document the change in semantics introduced by | ||
| // rust-lang/rust#138961, extended to `if let` closure captures. | ||
| // | ||
| // 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. | ||
| // | ||
| // Before rust-lang/rust#154210, `if let` in closures captured all of `x`, so | ||
| // this test did not fail because the closure is never called. | ||
| //@normalize-stderr-test: "constructing invalid value of type [^:]+:" -> "constructing invalid value:" | ||
|
|
||
| #![allow(irrefutable_let_patterns)] | ||
|
|
||
| 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) | ||
| }; | ||
|
|
||
| //~v ERROR: encountered a dangling reference | ||
| let _ = || { | ||
| if let &(&_y, _) = x {} | ||
| }; | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/tools/miri/tests/fail/match/closures/if-let-partial-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,16 @@ | ||
| error: Undefined Behavior: constructing invalid value: encountered a dangling reference (use-after-free) | ||
| --> tests/fail/match/closures/if-let-partial-pattern.rs:LL:CC | ||
| | | ||
| LL | let _ = || { | ||
| | _____________^ | ||
| LL | | if let &(&_y, _) = x {} | ||
| 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
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
54 changes: 54 additions & 0 deletions
54
tests/ui/closures/2229_closure_analysis/if-let-patterns-capture-analysis.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,54 @@ | ||
| //@ edition:2021 | ||
|
|
||
| #![feature(rustc_attrs)] | ||
| #![feature(stmt_expr_attributes)] | ||
| #![allow(irrefutable_let_patterns)] | ||
|
|
||
| enum SingleVariant { | ||
| Pair(i32, String), | ||
| } | ||
|
|
||
| fn if_let_closure() { | ||
| let variant = SingleVariant::Pair(1, "hello".into()); | ||
|
|
||
| let c = #[rustc_capture_analysis] | ||
| || { | ||
| //~^ ERROR First Pass analysis includes: | ||
| //~| ERROR Min Capture analysis includes: | ||
| if let SingleVariant::Pair(ref n, s) = variant { | ||
| //~^ NOTE: Capturing variant[(0, 0)] -> Immutable | ||
| //~| NOTE: Capturing variant[(1, 0)] -> ByValue | ||
| //~| NOTE: Min Capture variant[(0, 0)] -> Immutable | ||
| //~| NOTE: Min Capture variant[(1, 0)] -> ByValue | ||
| let _ = (n, s); | ||
| } | ||
| }; | ||
|
|
||
| c(); | ||
| } | ||
|
|
||
| fn match_closure() { | ||
| let variant = SingleVariant::Pair(1, "hello".into()); | ||
|
|
||
| let c = #[rustc_capture_analysis] | ||
| || { | ||
| //~^ ERROR First Pass analysis includes: | ||
| //~| ERROR Min Capture analysis includes: | ||
| match variant { | ||
| //~^ NOTE: Capturing variant[(0, 0)] -> Immutable | ||
| //~| NOTE: Capturing variant[(1, 0)] -> ByValue | ||
| //~| NOTE: Min Capture variant[(0, 0)] -> Immutable | ||
| //~| NOTE: Min Capture variant[(1, 0)] -> ByValue | ||
| SingleVariant::Pair(ref n, s) => { | ||
| let _ = (n, s); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| c(); | ||
| } | ||
|
|
||
| fn main() { | ||
| if_let_closure(); | ||
| match_closure(); | ||
| } |
90 changes: 90 additions & 0 deletions
90
tests/ui/closures/2229_closure_analysis/if-let-patterns-capture-analysis.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,90 @@ | ||
| error: First Pass analysis includes: | ||
| --> $DIR/if-let-patterns-capture-analysis.rs:15:5 | ||
| | | ||
| LL | / || { | ||
| LL | | | ||
| LL | | | ||
| LL | | if let SingleVariant::Pair(ref n, s) = variant { | ||
| ... | | ||
| LL | | }; | ||
| | |_____^ | ||
| | | ||
| note: Capturing variant[(0, 0)] -> Immutable | ||
| --> $DIR/if-let-patterns-capture-analysis.rs:18:48 | ||
| | | ||
| LL | if let SingleVariant::Pair(ref n, s) = variant { | ||
| | ^^^^^^^ | ||
| note: Capturing variant[(1, 0)] -> ByValue | ||
| --> $DIR/if-let-patterns-capture-analysis.rs:18:48 | ||
| | | ||
| LL | if let SingleVariant::Pair(ref n, s) = variant { | ||
| | ^^^^^^^ | ||
|
|
||
| error: Min Capture analysis includes: | ||
| --> $DIR/if-let-patterns-capture-analysis.rs:15:5 | ||
| | | ||
| LL | / || { | ||
| LL | | | ||
| LL | | | ||
| LL | | if let SingleVariant::Pair(ref n, s) = variant { | ||
| ... | | ||
| LL | | }; | ||
| | |_____^ | ||
| | | ||
| note: Min Capture variant[(0, 0)] -> Immutable | ||
| --> $DIR/if-let-patterns-capture-analysis.rs:18:48 | ||
| | | ||
| LL | if let SingleVariant::Pair(ref n, s) = variant { | ||
| | ^^^^^^^ | ||
| note: Min Capture variant[(1, 0)] -> ByValue | ||
| --> $DIR/if-let-patterns-capture-analysis.rs:18:48 | ||
| | | ||
| LL | if let SingleVariant::Pair(ref n, s) = variant { | ||
| | ^^^^^^^ | ||
|
|
||
| error: First Pass analysis includes: | ||
| --> $DIR/if-let-patterns-capture-analysis.rs:34:5 | ||
| | | ||
| LL | / || { | ||
| LL | | | ||
| LL | | | ||
| LL | | match variant { | ||
| ... | | ||
| LL | | }; | ||
| | |_____^ | ||
| | | ||
| note: Capturing variant[(0, 0)] -> Immutable | ||
| --> $DIR/if-let-patterns-capture-analysis.rs:37:15 | ||
| | | ||
| LL | match variant { | ||
| | ^^^^^^^ | ||
| note: Capturing variant[(1, 0)] -> ByValue | ||
| --> $DIR/if-let-patterns-capture-analysis.rs:37:15 | ||
| | | ||
| LL | match variant { | ||
| | ^^^^^^^ | ||
|
|
||
| error: Min Capture analysis includes: | ||
| --> $DIR/if-let-patterns-capture-analysis.rs:34:5 | ||
| | | ||
| LL | / || { | ||
| LL | | | ||
| LL | | | ||
| LL | | match variant { | ||
| ... | | ||
| LL | | }; | ||
| | |_____^ | ||
| | | ||
| note: Min Capture variant[(0, 0)] -> Immutable | ||
| --> $DIR/if-let-patterns-capture-analysis.rs:37:15 | ||
| | | ||
| LL | match variant { | ||
| | ^^^^^^^ | ||
| note: Min Capture variant[(1, 0)] -> ByValue | ||
| --> $DIR/if-let-patterns-capture-analysis.rs:37:15 | ||
| | | ||
| LL | match variant { | ||
| | ^^^^^^^ | ||
|
|
||
| error: aborting due to 4 previous errors | ||
|
|
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.