-
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
Fix closure migration suggestion when the body is a macro. #87956
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
945a4b1
Fix closure migration suggestion when the body is a macro.
m-ou-se 99a0477
Add test for closure migration with a macro body.
m-ou-se cd7f960
Improve comment in closure migration code.
m-ou-se 26c590d
Improve fallback span for closure migration lint.
m-ou-se 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
16 changes: 16 additions & 0 deletions
16
src/test/ui/closures/2229_closure_analysis/migrations/macro.fixed
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,16 @@ | ||
// run-rustfix | ||
|
||
// See https://github.com/rust-lang/rust/issues/87955 | ||
|
||
#![deny(rust_2021_incompatible_closure_captures)] | ||
//~^ NOTE: the lint level is defined here | ||
|
||
fn main() { | ||
let a = ("hey".to_string(), "123".to_string()); | ||
let _ = || { let _ = &a; dbg!(a.0) }; | ||
//~^ ERROR: drop order | ||
//~| NOTE: only captures `a.0` | ||
//~| NOTE: for more information, see | ||
//~| HELP: add a dummy let to cause `a` to be fully captured | ||
} | ||
//~^ NOTE: dropped here |
16 changes: 16 additions & 0 deletions
16
src/test/ui/closures/2229_closure_analysis/migrations/macro.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,16 @@ | ||
// run-rustfix | ||
|
||
// See https://github.com/rust-lang/rust/issues/87955 | ||
|
||
#![deny(rust_2021_incompatible_closure_captures)] | ||
//~^ NOTE: the lint level is defined here | ||
|
||
fn main() { | ||
let a = ("hey".to_string(), "123".to_string()); | ||
let _ = || dbg!(a.0); | ||
//~^ ERROR: drop order | ||
//~| NOTE: only captures `a.0` | ||
//~| NOTE: for more information, see | ||
//~| HELP: add a dummy let to cause `a` to be fully captured | ||
} | ||
//~^ NOTE: dropped here |
24 changes: 24 additions & 0 deletions
24
src/test/ui/closures/2229_closure_analysis/migrations/macro.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,24 @@ | ||
error: changes to closure capture in Rust 2021 will affect drop order | ||
--> $DIR/macro.rs:10:13 | ||
| | ||
LL | let _ = || dbg!(a.0); | ||
| ^^^^^^^^---^ | ||
| | | ||
| in Rust 2018, closure captures all of `a`, but in Rust 2021, it only captures `a.0` | ||
... | ||
LL | } | ||
| - in Rust 2018, `a` would be dropped here, but in Rust 2021, only `a.0` would be dropped here alongside the closure | ||
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. Pre-existing: I think saying 'as part as the closure' would make more sense than 'alongside the closure'. 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. |
||
| | ||
note: the lint level is defined here | ||
--> $DIR/macro.rs:5:9 | ||
| | ||
LL | #![deny(rust_2021_incompatible_closure_captures)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/disjoint-capture-in-closures.html> | ||
help: add a dummy let to cause `a` to be fully captured | ||
| | ||
LL | let _ = || { let _ = &a; dbg!(a.0) }; | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: aborting due to previous error | ||
|
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.
If we end with a dummy span, then the overall diagnostic will end up worse than it currently is. Can you restore
closure_body_span
to its original value if the loop ends up setting it toDUMMY_SP
?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 kept the dummy span for the suggestion part. Otherwise
.span_to_snippet
is going to return the wrong snippet and the suggestion will be wrong and confusing. The fallback span now uses the entire closure, which chas the highest chance of pointing to the place where modifications need to be made.