Skip to content

Commit 81b062a

Browse files
committed
Fix wording
1 parent 36eb544 commit 81b062a

File tree

12 files changed

+250
-397
lines changed

12 files changed

+250
-397
lines changed

compiler/rustc_typeck/src/check/upvar.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
498498

499499
let local_def_id = closure_def_id.expect_local();
500500
let closure_hir_id = self.tcx.hir().local_def_id_to_hir_id(local_def_id);
501+
let closure_span = self.tcx.hir().span(closure_hir_id);
502+
let closure_head_span = self.tcx.sess.source_map().guess_head_span(closure_span);
501503
self.tcx.struct_span_lint_hir(
502504
lint::builtin::RUST_2021_INCOMPATIBLE_CLOSURE_CAPTURES,
503505
closure_hir_id,
504-
span,
506+
closure_head_span,
505507
|lint| {
506508
let mut diagnostics_builder = lint.build(
507509
format!(
@@ -512,6 +514,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
512514
);
513515
for (var_hir_id, diagnostics_info) in need_migrations.iter() {
514516
let mut captured_names = format!("");
517+
// Label every Span which are responsible for the captured values
515518
for (captured_hir_id, captured_name) in diagnostics_info.iter() {
516519
if let Some(captured_hir_id) = captured_hir_id {
517520
let cause_span = self.tcx.hir().span(*captured_hir_id);
@@ -527,6 +530,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
527530
}
528531
}
529532

533+
// Add a label pointing to where a closure and it's captured variables affected by drop order are dropped
530534
if reasons.contains("drop order") {
531535
let drop_location_span = drop_location_span(self.tcx, &closure_hir_id);
532536

@@ -536,13 +540,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
536540
));
537541
}
538542

539-
if reasons.contains("closure trait implementation") {
540-
let closure_body_span = self.tcx.hir().span(body_id.hir_id);
541-
let closure_ending_span = self.tcx.sess.source_map().guess_head_span(closure_body_span).shrink_to_lo();
543+
// Add a label explaining why a closure no longer implements a trait
544+
if reasons.contains("trait implementation") {
545+
let missing_trait = &reasons[..reasons.find("trait implementation").unwrap() - 1];
542546

543-
let missing_trait = &reasons[..reasons.find("closure trait implementation").unwrap() - 1];
544-
545-
diagnostics_builder.span_label(closure_ending_span, format!("in Rust 2018, this closure would implement {} as `{}` implements {}, but in Rust 2021, this closure will no longer implement {} as {} does not implement {}",
547+
diagnostics_builder.span_label(closure_head_span, format!("in Rust 2018, this closure would implement {} as `{}` implements {}, but in Rust 2021, this closure would no longer implement {} as {} does not implement {}",
546548
missing_trait,
547549
self.tcx.hir().name(*var_hir_id),
548550
missing_trait,
@@ -598,7 +600,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
598600

599601
if auto_trait_reasons.len() > 0 {
600602
reasons = format!(
601-
"{} closure trait implementation",
603+
"{} trait implementation for closure",
602604
auto_trait_reasons.clone().into_iter().collect::<Vec<&str>>().join(", ")
603605
);
604606
}
@@ -1386,24 +1388,19 @@ fn drop_location_span(tcx: TyCtxt<'tcx>, hir_id: &hir::HirId) -> Span {
13861388
let owner_id = tcx.hir().get_enclosing_scope(*hir_id).unwrap();
13871389

13881390
let owner_node = tcx.hir().get(owner_id);
1389-
match owner_node {
1391+
let owner_span = match owner_node {
13901392
hir::Node::Item(item) => match item.kind {
1391-
hir::ItemKind::Fn(_, _, owner_id) => {
1392-
let owner_span = tcx.hir().span(owner_id.hir_id);
1393-
tcx.sess.source_map().end_point(owner_span)
1394-
}
1393+
hir::ItemKind::Fn(_, _, owner_id) => tcx.hir().span(owner_id.hir_id),
13951394
_ => {
13961395
bug!("Drop location span error: need to handle more ItemKind {:?}", item.kind);
13971396
}
13981397
},
1399-
hir::Node::Block(block) => {
1400-
let owner_span = tcx.hir().span(block.hir_id);
1401-
tcx.sess.source_map().end_point(owner_span)
1402-
}
1398+
hir::Node::Block(block) => tcx.hir().span(block.hir_id),
14031399
_ => {
14041400
bug!("Drop location span error: need to handle more Node {:?}", owner_node);
14051401
}
1406-
}
1402+
};
1403+
tcx.sess.source_map().end_point(owner_span)
14071404
}
14081405

14091406
struct InferBorrowKind<'a, 'tcx> {

src/test/ui/closures/2229_closure_analysis/migrations/auto_traits.fixed

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fn test_send_trait() {
1212
let mut f = 10;
1313
let fptr = SendPointer(&mut f as *mut i32);
1414
thread::spawn(move || { let _ = &fptr; unsafe {
15-
//~^ ERROR: `Send` closure trait implementation
16-
//~| NOTE: in Rust 2018, this closure would implement `Send` as `fptr` implements `Send`, but in Rust 2021, this closure will no longer implement `Send` as `fptr.0` does not implement `Send`
15+
//~^ ERROR: `Send` trait implementation for closure
16+
//~| NOTE: in Rust 2018, this closure would implement `Send` as `fptr` implements `Send`, but in Rust 2021, this closure would no longer implement `Send` as `fptr.0` does not implement `Send`
1717
//~| NOTE: for more information, see
1818
//~| HELP: add a dummy let to cause `fptr` to be fully captured
1919
*fptr.0 = 20;
@@ -32,8 +32,8 @@ fn test_sync_trait() {
3232
let f = CustomInt(&mut f as *mut i32);
3333
let fptr = SyncPointer(f);
3434
thread::spawn(move || { let _ = &fptr; unsafe {
35-
//~^ ERROR: `Sync`, `Send` closure trait implementation
36-
//~| NOTE: in Rust 2018, this closure would implement `Sync`, `Send` as `fptr` implements `Sync`, `Send`, but in Rust 2021, this closure will no longer implement `Sync`, `Send` as `fptr.0.0` does not implement `Sync`, `Send`
35+
//~^ ERROR: `Sync`, `Send` trait implementation for closure
36+
//~| NOTE: in Rust 2018, this closure would implement `Sync`, `Send` as `fptr` implements `Sync`, `Send`, but in Rust 2021, this closure would no longer implement `Sync`, `Send` as `fptr.0.0` does not implement `Sync`, `Send`
3737
//~| NOTE: for more information, see
3838
//~| HELP: add a dummy let to cause `fptr` to be fully captured
3939
*fptr.0.0 = 20;
@@ -56,8 +56,8 @@ impl Clone for U {
5656
fn test_clone_trait() {
5757
let f = U(S(String::from("Hello World")), T(0));
5858
let c = || { let _ = &f;
59-
//~^ ERROR: `Clone` closure trait implementation, and drop order
60-
//~| NOTE: in Rust 2018, this closure would implement `Clone` as `f` implements `Clone`, but in Rust 2021, this closure will no longer implement `Clone` as `f.1` does not implement `Clone`
59+
//~^ ERROR: `Clone` trait implementation for closure, and drop order
60+
//~| 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`
6161
//~| NOTE: for more information, see
6262
//~| HELP: add a dummy let to cause `f` to be fully captured
6363
let f_1 = f.1;

src/test/ui/closures/2229_closure_analysis/migrations/auto_traits.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fn test_send_trait() {
1212
let mut f = 10;
1313
let fptr = SendPointer(&mut f as *mut i32);
1414
thread::spawn(move || unsafe {
15-
//~^ ERROR: `Send` closure trait implementation
16-
//~| NOTE: in Rust 2018, this closure would implement `Send` as `fptr` implements `Send`, but in Rust 2021, this closure will no longer implement `Send` as `fptr.0` does not implement `Send`
15+
//~^ ERROR: `Send` trait implementation for closure
16+
//~| NOTE: in Rust 2018, this closure would implement `Send` as `fptr` implements `Send`, but in Rust 2021, this closure would no longer implement `Send` as `fptr.0` does not implement `Send`
1717
//~| NOTE: for more information, see
1818
//~| HELP: add a dummy let to cause `fptr` to be fully captured
1919
*fptr.0 = 20;
@@ -32,8 +32,8 @@ fn test_sync_trait() {
3232
let f = CustomInt(&mut f as *mut i32);
3333
let fptr = SyncPointer(f);
3434
thread::spawn(move || unsafe {
35-
//~^ ERROR: `Sync`, `Send` closure trait implementation
36-
//~| NOTE: in Rust 2018, this closure would implement `Sync`, `Send` as `fptr` implements `Sync`, `Send`, but in Rust 2021, this closure will no longer implement `Sync`, `Send` as `fptr.0.0` does not implement `Sync`, `Send`
35+
//~^ ERROR: `Sync`, `Send` trait implementation for closure
36+
//~| NOTE: in Rust 2018, this closure would implement `Sync`, `Send` as `fptr` implements `Sync`, `Send`, but in Rust 2021, this closure would no longer implement `Sync`, `Send` as `fptr.0.0` does not implement `Sync`, `Send`
3737
//~| NOTE: for more information, see
3838
//~| HELP: add a dummy let to cause `fptr` to be fully captured
3939
*fptr.0.0 = 20;
@@ -56,8 +56,8 @@ impl Clone for U {
5656
fn test_clone_trait() {
5757
let f = U(S(String::from("Hello World")), T(0));
5858
let c = || {
59-
//~^ ERROR: `Clone` closure trait implementation, and drop order
60-
//~| NOTE: in Rust 2018, this closure would implement `Clone` as `f` implements `Clone`, but in Rust 2021, this closure will no longer implement `Clone` as `f.1` does not implement `Clone`
59+
//~^ ERROR: `Clone` trait implementation for closure, and drop order
60+
//~| 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`
6161
//~| NOTE: for more information, see
6262
//~| HELP: add a dummy let to cause `f` to be fully captured
6363
let f_1 = f.1;

src/test/ui/closures/2229_closure_analysis/migrations/auto_traits.stderr

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
error: changes to closure capture in Rust 2021 will affect `Send` closure trait implementation
1+
error: changes to closure capture in Rust 2021 will affect `Send` trait implementation for closure
22
--> $DIR/auto_traits.rs:14:19
33
|
4-
LL | thread::spawn(move || unsafe {
5-
| ^ - in Rust 2018, this closure would implement `Send` as `fptr` implements `Send`, but in Rust 2021, this closure will no longer implement `Send` as `fptr.0` does not implement `Send`
6-
| ___________________|
7-
| |
8-
LL | |
9-
LL | |
10-
LL | |
11-
LL | |
12-
LL | | *fptr.0 = 20;
13-
| | ------- in Rust 2018, closure captures all of `fptr`, but in Rust 2021, it only captures `fptr.0`
14-
LL | |
15-
LL | | });
16-
| |_____^
4+
LL | thread::spawn(move || unsafe {
5+
| ^^^^^^^^^^^^^^ in Rust 2018, this closure would implement `Send` as `fptr` implements `Send`, but in Rust 2021, this closure would no longer implement `Send` as `fptr.0` does not implement `Send`
6+
...
7+
LL | *fptr.0 = 20;
8+
| ------- in Rust 2018, closure captures all of `fptr`, but in Rust 2021, it only captures `fptr.0`
179
|
1810
note: the lint level is defined here
1911
--> $DIR/auto_traits.rs:2:9
@@ -31,22 +23,14 @@ LL |
3123
LL | *fptr.0 = 20;
3224
...
3325

34-
error: changes to closure capture in Rust 2021 will affect `Sync`, `Send` closure trait implementation
26+
error: changes to closure capture in Rust 2021 will affect `Sync`, `Send` trait implementation for closure
3527
--> $DIR/auto_traits.rs:34:19
3628
|
37-
LL | thread::spawn(move || unsafe {
38-
| ^ - in Rust 2018, this closure would implement `Sync`, `Send` as `fptr` implements `Sync`, `Send`, but in Rust 2021, this closure will no longer implement `Sync`, `Send` as `fptr.0.0` does not implement `Sync`, `Send`
39-
| ___________________|
40-
| |
41-
LL | |
42-
LL | |
43-
LL | |
44-
LL | |
45-
LL | | *fptr.0.0 = 20;
46-
| | --------- in Rust 2018, closure captures all of `fptr`, but in Rust 2021, it only captures `fptr.0.0`
47-
LL | |
48-
LL | | });
49-
| |_____^
29+
LL | thread::spawn(move || unsafe {
30+
| ^^^^^^^^^^^^^^ in Rust 2018, this closure would implement `Sync`, `Send` as `fptr` implements `Sync`, `Send`, but in Rust 2021, this closure would no longer implement `Sync`, `Send` as `fptr.0.0` does not implement `Sync`, `Send`
31+
...
32+
LL | *fptr.0.0 = 20;
33+
| --------- in Rust 2018, closure captures all of `fptr`, but in Rust 2021, it only captures `fptr.0.0`
5034
|
5135
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/disjoint-capture-in-closures.html>
5236
help: add a dummy let to cause `fptr` to be fully captured
@@ -59,26 +43,17 @@ LL |
5943
LL | *fptr.0.0 = 20;
6044
...
6145

62-
error: changes to closure capture in Rust 2021 will affect `Clone` closure trait implementation, and drop order
46+
error: changes to closure capture in Rust 2021 will affect `Clone` trait implementation for closure, and drop order
6347
--> $DIR/auto_traits.rs:58:13
6448
|
65-
LL | let c = || {
66-
| ^ - in Rust 2018, this closure would implement `Clone` as `f` implements `Clone`, but in Rust 2021, this closure will no longer implement `Clone` as `f.1` does not implement `Clone`
67-
| _____________|
68-
| |
69-
LL | |
70-
LL | |
71-
LL | |
72-
LL | |
73-
LL | | let f_1 = f.1;
74-
| | --- in Rust 2018, closure captures all of `f`, but in Rust 2021, it only captures `f.1`
75-
LL | |
76-
LL | | println!("{:?}", f_1.0);
77-
LL | | };
78-
| |_____^
49+
LL | let c = || {
50+
| ^^ 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`
51+
...
52+
LL | let f_1 = f.1;
53+
| --- in Rust 2018, closure captures all of `f`, but in Rust 2021, it only captures `f.1`
7954
...
80-
LL | }
81-
| - in Rust 2018, `f` would be dropped here, but in Rust 2021, only `f.1` would be dropped here alongside the closure
55+
LL | }
56+
| - in Rust 2018, `f` would be dropped here, but in Rust 2021, only `f.1` would be dropped here alongside the closure
8257
|
8358
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/disjoint-capture-in-closures.html>
8459
help: add a dummy let to cause `f` to be fully captured

0 commit comments

Comments
 (0)