Skip to content

Improve formatting of closure capture migration suggestion for multi-line closures. #87953

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
merged 2 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions compiler/rustc_typeck/src/check/upvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,11 +649,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
match self.tcx.sess.source_map().span_to_snippet(closure_body_span) {
Ok(s) => {
let trimmed = s.trim_start();
let mut lines = trimmed.lines();
let line1 = lines.next().unwrap_or_default();

// If the closure contains a block then replace the opening brace
// with "{ let _ = (..); "
let sugg = if let Some('{') = trimmed.chars().next() {
format!("{{ {}; {}", migration_string, &trimmed[1..])
let sugg = if line1.trim_end() == "{" {
// This is a multi-line closure with just a `{` on the first line,
// so we put the `let` on its own line.
// We take the indentation from the next non-empty line.
let line2 = lines.filter(|line| !line.is_empty()).next().unwrap_or_default();
let indent = line2.split_once(|c: char| !c.is_whitespace()).unwrap_or_default().0;
format!("{{\n{}{};{}", indent, migration_string, &trimmed[line1.len()..])
} else if line1.starts_with('{') {
format!("{{ {}; {}", migration_string, &trimmed[1..].trim_start())
} else {
format!("{{ {}; {} }}", migration_string, s)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ impl Clone for U {

fn test_clone_trait() {
let f = U(S(String::from("Hello World")), T(0));
let c = || { let _ = &f;
let c = || {
let _ = &f;
//~^ ERROR: `Clone` trait implementation for closure and drop order
//~| NOTE: in Rust 2018, this closure would implement `Clone` as `f` implements `Clone`, but in Rust 2021, this closure would no longer implement `Clone` as `f.1` does not implement `Clone`
//~| NOTE: for more information, see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ LL | }
= 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 `f` to be fully captured
|
LL ~ let c = || { let _ = &f;
LL ~ let c = || {
LL + let _ = &f;
LL +
LL +
LL +
LL +
LL + let f_1 = f.1;
...

error: aborting due to 3 previous errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ fn test1_all_need_migration() {
let t1 = (String::new(), String::new());
let t2 = (String::new(), String::new());

let c = || { let _ = (&t, &t1, &t2);
let c = || {
let _ = (&t, &t1, &t2);
//~^ ERROR: drop order
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `t`, `t1`, `t2` to be fully captured
Expand All @@ -38,7 +39,8 @@ fn test2_only_precise_paths_need_migration() {
let t1 = (String::new(), String::new());
let t2 = (String::new(), String::new());

let c = || { let _ = (&t, &t1);
let c = || {
let _ = (&t, &t1);
//~^ ERROR: drop order
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `t`, `t1` to be fully captured
Expand All @@ -59,7 +61,8 @@ fn test2_only_precise_paths_need_migration() {
fn test3_only_by_value_need_migration() {
let t = (String::new(), String::new());
let t1 = (String::new(), String::new());
let c = || { let _ = &t;
let c = || {
let _ = &t;
//~^ ERROR: drop order
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `t` to be fully captured
Expand All @@ -80,7 +83,8 @@ fn test4_only_non_copy_types_need_migration() {
// `t1` is Copy because all of its elements are Copy
let t1 = (0i32, 0i32);

let c = || { let _ = &t;
let c = || {
let _ = &t;
//~^ ERROR: drop order
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `t` to be fully captured
Expand All @@ -101,7 +105,8 @@ fn test5_only_drop_types_need_migration() {
// `s` doesn't implement Drop or any elements within it, and doesn't need migration
let s = S(0i32, 0i32);

let c = || { let _ = &t;
let c = || {
let _ = &t;
//~^ ERROR: drop order
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `t` to be fully captured
Expand All @@ -119,7 +124,8 @@ fn test5_only_drop_types_need_migration() {
fn test6_move_closures_non_copy_types_might_need_migration() {
let t = (String::new(), String::new());
let t1 = (String::new(), String::new());
let c = move || { let _ = (&t1, &t);
let c = move || {
let _ = (&t1, &t);
//~^ ERROR: drop order
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `t1`, `t` to be fully captured
Expand All @@ -139,7 +145,8 @@ fn test6_move_closures_non_copy_types_might_need_migration() {
fn test7_drop_non_drop_aggregate_need_migration() {
let t = (String::new(), String::new(), 0i32);

let c = || { let _ = &t;
let c = || {
let _ = &t;
//~^ ERROR: drop order
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `t` to be fully captured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ 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 `t`, `t1`, `t2` to be fully captured
|
LL ~ let c = || { let _ = (&t, &t1, &t2);
LL ~ let c = || {
LL + let _ = (&t, &t1, &t2);
LL +
LL +
LL +
LL +
LL + let _t = t.0;
...

error: changes to closure capture in Rust 2021 will affect drop order
Expand All @@ -57,12 +57,12 @@ LL | }
= 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 `t`, `t1` to be fully captured
|
LL ~ let c = || { let _ = (&t, &t1);
LL ~ let c = || {
LL + let _ = (&t, &t1);
LL +
LL +
LL +
LL + let _t = t.0;
LL +
...

error: changes to closure capture in Rust 2021 will affect drop order
Expand All @@ -80,12 +80,12 @@ LL | }
= 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 `t` to be fully captured
|
LL ~ let c = || { let _ = &t;
LL ~ let c = || {
LL + let _ = &t;
LL +
LL +
LL +
LL + let _t = t.0;
LL +
...

error: changes to closure capture in Rust 2021 will affect drop order
Expand All @@ -103,12 +103,12 @@ LL | }
= 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 `t` to be fully captured
|
LL ~ let c = || { let _ = &t;
LL ~ let c = || {
LL + let _ = &t;
LL +
LL +
LL +
LL + let _t = t.0;
LL +
...

error: changes to closure capture in Rust 2021 will affect drop order
Expand All @@ -126,12 +126,12 @@ LL | }
= 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 `t` to be fully captured
|
LL ~ let c = || { let _ = &t;
LL ~ let c = || {
LL + let _ = &t;
LL +
LL +
LL +
LL + let _t = t.0;
LL +
...

error: changes to closure capture in Rust 2021 will affect drop order
Expand All @@ -154,12 +154,12 @@ LL | }
= 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 `t1`, `t` to be fully captured
|
LL ~ let c = move || { let _ = (&t1, &t);
LL ~ let c = move || {
LL + let _ = (&t1, &t);
LL +
LL +
LL +
LL + println!("{} {}", t1.1, t.1);
LL +
...

error: changes to closure capture in Rust 2021 will affect drop order
Expand All @@ -177,12 +177,12 @@ LL | }
= 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 `t` to be fully captured
|
LL ~ let c = || { let _ = &t;
LL ~ let c = || {
LL + let _ = &t;
LL +
LL +
LL +
LL + let _t = t.0;
LL +
...

error: aborting due to 7 previous errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ impl<T> Drop for GenericStruct<T> {
fn significant_drop_needs_migration() {
let t = (SigDrop {}, SigDrop {});

let c = || { let _ = &t;
let c = || {
let _ = &t;
//~^ ERROR: drop order
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `t` to be fully captured
Expand All @@ -54,7 +55,8 @@ fn generic_struct_with_significant_drop_needs_migration() {
let t = Wrapper(GenericStruct(SigDrop {}, SigDrop {}), 5);

// move is used to force i32 to be copied instead of being a ref
let c = move || { let _ = &t;
let c = move || {
let _ = &t;
//~^ ERROR: drop order
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `t` to be fully captured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ 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 `t` to be fully captured
|
LL ~ let c = || { let _ = &t;
LL ~ let c = || {
LL + let _ = &t;
LL +
LL +
LL +
LL + let _t = t.0;
LL +
...

error: changes to closure capture in Rust 2021 will affect drop order
Expand All @@ -41,12 +41,12 @@ LL | }
= 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 `t` to be fully captured
|
LL ~ let c = move || { let _ = &t;
LL ~ let c = move || {
LL + let _ = &t;
LL +
LL +
LL +
LL + let _t = t.1;
LL +
...

error: aborting due to 2 previous errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ impl Drop for Foo {

fn closure_contains_block() {
let t = (Foo(0), Foo(0));
let c = || { let _ = &t;
let c = || {
let _ = &t;
//~^ ERROR: drop order
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `t` to be fully captured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ 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 `t` to be fully captured
|
LL ~ let c = || { let _ = &t;
LL ~ let c = || {
LL + let _ = &t;
LL +
LL +
LL +
LL + let _t = t.0;
LL +
...

error: changes to closure capture in Rust 2021 will affect drop order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ where
F: FnOnce(),
{
let f = panic::AssertUnwindSafe(f);
let result = panic::catch_unwind(move || { let _ = &f;
let result = panic::catch_unwind(move || {
let _ = &f;
//~^ ERROR: `UnwindSafe`, `RefUnwindSafe` trait implementation for closure
//~| NOTE: in Rust 2018, this closure would implement `UnwindSafe`, `RefUnwindSafe` as `f` implements `UnwindSafe`, `RefUnwindSafe`, but in Rust 2021, this closure would no longer implement `UnwindSafe`, `RefUnwindSafe` as `f.0` does not implement `UnwindSafe`, `RefUnwindSafe`
//~| NOTE: for more information, see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ 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 `f` to be fully captured
|
LL ~ let result = panic::catch_unwind(move || { let _ = &f;
LL ~ let result = panic::catch_unwind(move || {
LL + let _ = &f;
LL +
LL +
LL +
LL +
LL + f.0()
...

error: aborting due to previous error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ impl Clone for U {
fn test_multi_issues() {
let f1 = U(S(String::from("foo")), T(0));
let f2 = U(S(String::from("bar")), T(0));
let c = || { let _ = (&f1, &f2);
let c = || {
let _ = (&f1, &f2);
//~^ ERROR: `Clone` trait implementation for closure and drop order
//~| NOTE: in Rust 2018, this closure would implement `Clone` as `f1` implements `Clone`, but in Rust 2021, this closure would no longer implement `Clone` as `f1.0` does not implement `Clone`
//~| NOTE: for more information, see
Expand All @@ -39,7 +40,8 @@ fn test_multi_issues() {

fn test_capturing_all_disjoint_fields_individually() {
let f1 = U(S(String::from("foo")), T(0));
let c = || { let _ = &f1;
let c = || {
let _ = &f1;
//~^ ERROR: `Clone` trait implementation for closure
//~| NOTE: in Rust 2018, this closure would implement `Clone` as `f1` implements `Clone`, but in Rust 2021, this closure would no longer implement `Clone` as `f1.0` does not implement `Clone`
//~| NOTE: for more information, see
Expand All @@ -64,7 +66,8 @@ impl Clone for U1 {

fn test_capturing_several_disjoint_fields_individually_1() {
let f1 = U1(S(String::from("foo")), T(0), S(String::from("bar")));
let c = || { let _ = &f1;
let c = || {
let _ = &f1;
//~^ ERROR: `Clone` trait implementation for closure
//~| NOTE: in Rust 2018, this closure would implement `Clone` as `f1` implements `Clone`, but in Rust 2021, this closure would no longer implement `Clone` as `f1.0` does not implement `Clone`
//~| NOTE: in Rust 2018, this closure would implement `Clone` as `f1` implements `Clone`, but in Rust 2021, this closure would no longer implement `Clone` as `f1.2` does not implement `Clone`
Expand All @@ -83,7 +86,8 @@ fn test_capturing_several_disjoint_fields_individually_1() {

fn test_capturing_several_disjoint_fields_individually_2() {
let f1 = U1(S(String::from("foo")), T(0), S(String::from("bar")));
let c = || { let _ = &f1;
let c = || {
let _ = &f1;
//~^ ERROR: `Clone` trait implementation for closure and drop order
//~| NOTE: in Rust 2018, this closure would implement `Clone` as `f1` implements `Clone`, but in Rust 2021, this closure would no longer implement `Clone` as `f1.0` does not implement `Clone`
//~| NOTE: for more information, see
Expand Down
Loading