Skip to content

Commit eba361a

Browse files
committed
Auto merge of #98482 - cjgillot:short-struct-span-closure, r=estebank
Shorten def_span of closures to just their header Continuation of #93967.
2 parents 9b21131 + d43fa93 commit eba361a

File tree

175 files changed

+978
-1362
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

175 files changed

+978
-1362
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
861861
let arg_pos = args
862862
.iter()
863863
.enumerate()
864-
.filter(|(_, arg)| arg.span == self.body.span)
864+
.filter(|(_, arg)| arg.hir_id == closure_id)
865865
.map(|(pos, _)| pos)
866866
.next();
867867
let def_id = hir.local_def_id(item_id);
@@ -903,9 +903,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
903903
if let Some(span) = arg {
904904
err.span_label(span, "change this to accept `FnMut` instead of `Fn`");
905905
err.span_label(func.span, "expects `Fn` instead of `FnMut`");
906-
if self.infcx.tcx.sess.source_map().is_multiline(self.body.span) {
907-
err.span_label(self.body.span, "in this closure");
908-
}
906+
err.span_label(self.body.span, "in this closure");
909907
look_at_return = false;
910908
}
911909
}

compiler/rustc_middle/src/hir/map/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@ impl<'hir> Map<'hir> {
10211021
_ => named_span(item.span, item.ident, None),
10221022
},
10231023
Node::Ctor(_) => return self.opt_span(self.get_parent_node(hir_id)),
1024+
Node::Expr(Expr { kind: ExprKind::Closure { fn_decl_span, .. }, .. }) => *fn_decl_span,
10241025
_ => self.span_with_body(hir_id),
10251026
};
10261027
Some(span)

compiler/rustc_typeck/src/check/upvar.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -747,14 +747,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
747747
let (migration_string, migrated_variables_concat) =
748748
migration_suggestion_for_2229(self.tcx, &need_migrations);
749749

750-
let local_def_id = closure_def_id.expect_local();
751-
let closure_hir_id = self.tcx.hir().local_def_id_to_hir_id(local_def_id);
752-
let closure_span = self.tcx.hir().span(closure_hir_id);
753-
let closure_head_span = self.tcx.sess.source_map().guess_head_span(closure_span);
750+
let closure_hir_id =
751+
self.tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local());
752+
let closure_head_span = self.tcx.def_span(closure_def_id);
754753
self.tcx.struct_span_lint_hir(
755754
lint::builtin::RUST_2021_INCOMPATIBLE_CLOSURE_CAPTURES,
756755
closure_hir_id,
757-
closure_head_span,
756+
closure_head_span,
758757
|lint| {
759758
let mut diagnostics_builder = lint.build(
760759
&reasons.migration_message(),
@@ -827,12 +826,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
827826
migrated_variables_concat
828827
);
829828

829+
let closure_span = self.tcx.hir().span_with_body(closure_hir_id);
830830
let mut closure_body_span = {
831831
// If the body was entirely expanded from a macro
832832
// invocation, i.e. the body is not contained inside the
833833
// closure span, then we walk up the expansion until we
834834
// find the span before the expansion.
835-
let s = self.tcx.hir().span(body_id.hir_id);
835+
let s = self.tcx.hir().span_with_body(body_id.hir_id);
836836
s.find_ancestor_inside(closure_span).unwrap_or(s)
837837
};
838838

src/test/codegen/generator-debug-msvc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ fn generator_test() -> impl Generator<Yield = i32, Return = ()> {
2727
// CHECK-NOT: flags: DIFlagArtificial
2828
// CHECK-SAME: )
2929
// CHECK: {{!.*}} = !DIDerivedType(tag: DW_TAG_member, name: "variant1", scope: [[GEN]],
30-
// CHECK-SAME: file: [[FILE]], line: 18,
30+
// CHECK-SAME: file: [[FILE]], line: 14,
3131
// CHECK-NOT: flags: DIFlagArtificial
3232
// CHECK-SAME: )
3333
// CHECK: {{!.*}} = !DIDerivedType(tag: DW_TAG_member, name: "variant2", scope: [[GEN]],
34-
// CHECK-SAME: file: [[FILE]], line: 18,
34+
// CHECK-SAME: file: [[FILE]], line: 14,
3535
// CHECK-NOT: flags: DIFlagArtificial
3636
// CHECK-SAME: )
3737
// CHECK: {{!.*}} = !DIDerivedType(tag: DW_TAG_member, name: "variant3", scope: [[GEN]],

src/test/codegen/generator-debug.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ fn generator_test() -> impl Generator<Yield = i32, Return = ()> {
3333
// CHECK-NOT: flags: DIFlagArtificial
3434
// CHECK-SAME: )
3535
// CHECK: {{!.*}} = !DIDerivedType(tag: DW_TAG_member, name: "1", scope: [[VARIANT]],
36-
// CHECK-SAME: file: [[FILE]], line: 18,
36+
// CHECK-SAME: file: [[FILE]], line: 14,
3737
// CHECK-NOT: flags: DIFlagArtificial
3838
// CHECK-SAME: )
3939
// CHECK: {{!.*}} = !DIDerivedType(tag: DW_TAG_member, name: "2", scope: [[VARIANT]],
40-
// CHECK-SAME: file: [[FILE]], line: 18,
40+
// CHECK-SAME: file: [[FILE]], line: 14,
4141
// CHECK-NOT: flags: DIFlagArtificial
4242
// CHECK-SAME: )
4343
// CHECK: {{!.*}} = !DIDerivedType(tag: DW_TAG_member, name: "3", scope: [[VARIANT]],

src/test/mir-opt/generator_drop_cleanup.main-{closure#0}.generator_drop.0.mir

+16-16
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414
},
1515
} */
1616

17-
fn main::{closure#0}(_1: *mut [generator@$DIR/generator-drop-cleanup.rs:10:15: 13:6]) -> () {
18-
let mut _0: (); // return place in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
19-
let mut _2: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
17+
fn main::{closure#0}(_1: *mut [generator@$DIR/generator-drop-cleanup.rs:10:15: 10:17]) -> () {
18+
let mut _0: (); // return place in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
19+
let mut _2: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
2020
let _3: std::string::String; // in scope 0 at $DIR/generator-drop-cleanup.rs:11:13: 11:15
2121
let _4: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:12:9: 12:14
2222
let mut _5: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:12:9: 12:14
2323
let mut _6: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:10:18: 10:18
24-
let mut _7: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
25-
let mut _8: u32; // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
24+
let mut _7: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
25+
let mut _8: u32; // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
2626
scope 1 {
2727
debug _s => (((*_1) as variant#3).0: std::string::String); // in scope 1 at $DIR/generator-drop-cleanup.rs:11:13: 11:15
2828
}
2929

3030
bb0: {
31-
_8 = discriminant((*_1)); // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
32-
switchInt(move _8) -> [0_u32: bb7, 3_u32: bb10, otherwise: bb11]; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
31+
_8 = discriminant((*_1)); // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
32+
switchInt(move _8) -> [0_u32: bb7, 3_u32: bb10, otherwise: bb11]; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
3333
}
3434

3535
bb1: {
@@ -44,11 +44,11 @@ fn main::{closure#0}(_1: *mut [generator@$DIR/generator-drop-cleanup.rs:10:15: 1
4444
}
4545

4646
bb3: {
47-
return; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
47+
return; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
4848
}
4949

5050
bb4 (cleanup): {
51-
resume; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
51+
resume; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
5252
}
5353

5454
bb5 (cleanup): {
@@ -57,28 +57,28 @@ fn main::{closure#0}(_1: *mut [generator@$DIR/generator-drop-cleanup.rs:10:15: 1
5757
}
5858

5959
bb6: {
60-
return; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
60+
return; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
6161
}
6262

6363
bb7: {
64-
goto -> bb9; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
64+
goto -> bb9; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
6565
}
6666

6767
bb8: {
6868
goto -> bb3; // scope 0 at $DIR/generator-drop-cleanup.rs:13:5: 13:6
6969
}
7070

7171
bb9: {
72-
goto -> bb6; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
72+
goto -> bb6; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
7373
}
7474

7575
bb10: {
76-
StorageLive(_4); // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
77-
StorageLive(_5); // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
78-
goto -> bb1; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
76+
StorageLive(_4); // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
77+
StorageLive(_5); // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
78+
goto -> bb1; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
7979
}
8080

8181
bb11: {
82-
return; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
82+
return; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
8383
}
8484
}

src/test/mir-opt/generator_storage_dead_unwind.main-{closure#0}.StateTransform.before.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// MIR for `main::{closure#0}` before StateTransform
22

3-
fn main::{closure#0}(_1: [generator@$DIR/generator-storage-dead-unwind.rs:22:16: 28:6], _2: ()) -> ()
3+
fn main::{closure#0}(_1: [generator@$DIR/generator-storage-dead-unwind.rs:22:16: 22:18], _2: ()) -> ()
44
yields ()
55
{
66
let mut _0: (); // return place in scope 0 at $DIR/generator-storage-dead-unwind.rs:22:19: 22:19
@@ -66,7 +66,7 @@ yields ()
6666
}
6767

6868
bb4: {
69-
return; // scope 0 at $DIR/generator-storage-dead-unwind.rs:28:6: 28:6
69+
return; // scope 0 at $DIR/generator-storage-dead-unwind.rs:22:18: 22:18
7070
}
7171

7272
bb5: {
@@ -82,7 +82,7 @@ yields ()
8282
}
8383

8484
bb7: {
85-
generator_drop; // scope 0 at $DIR/generator-storage-dead-unwind.rs:22:16: 28:6
85+
generator_drop; // scope 0 at $DIR/generator-storage-dead-unwind.rs:22:16: 22:18
8686
}
8787

8888
bb8 (cleanup): {
@@ -104,7 +104,7 @@ yields ()
104104
}
105105

106106
bb11 (cleanup): {
107-
resume; // scope 0 at $DIR/generator-storage-dead-unwind.rs:22:16: 28:6
107+
resume; // scope 0 at $DIR/generator-storage-dead-unwind.rs:22:16: 22:18
108108
}
109109

110110
bb12 (cleanup): {

src/test/mir-opt/generator_tiny.main-{closure#0}.generator_resume.0.mir

+16-16
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,31 @@
1414
},
1515
} */
1616

17-
fn main::{closure#0}(_1: Pin<&mut [generator@$DIR/generator-tiny.rs:19:16: 25:6]>, _2: u8) -> GeneratorState<(), ()> {
17+
fn main::{closure#0}(_1: Pin<&mut [generator@$DIR/generator-tiny.rs:19:16: 19:24]>, _2: u8) -> GeneratorState<(), ()> {
1818
debug _x => _10; // in scope 0 at $DIR/generator-tiny.rs:19:17: 19:19
19-
let mut _0: std::ops::GeneratorState<(), ()>; // return place in scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
19+
let mut _0: std::ops::GeneratorState<(), ()>; // return place in scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
2020
let _3: HasDrop; // in scope 0 at $DIR/generator-tiny.rs:20:13: 20:15
2121
let mut _4: !; // in scope 0 at $DIR/generator-tiny.rs:21:9: 24:10
22-
let mut _5: (); // in scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
22+
let mut _5: (); // in scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
2323
let _6: u8; // in scope 0 at $DIR/generator-tiny.rs:22:13: 22:18
2424
let mut _7: (); // in scope 0 at $DIR/generator-tiny.rs:22:13: 22:18
2525
let _8: (); // in scope 0 at $DIR/generator-tiny.rs:23:13: 23:21
2626
let mut _9: (); // in scope 0 at $DIR/generator-tiny.rs:19:25: 19:25
2727
let _10: u8; // in scope 0 at $DIR/generator-tiny.rs:19:17: 19:19
28-
let mut _11: u32; // in scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
28+
let mut _11: u32; // in scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
2929
scope 1 {
30-
debug _d => (((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 25:6])) as variant#3).0: HasDrop); // in scope 1 at $DIR/generator-tiny.rs:20:13: 20:15
30+
debug _d => (((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 19:24])) as variant#3).0: HasDrop); // in scope 1 at $DIR/generator-tiny.rs:20:13: 20:15
3131
}
3232

3333
bb0: {
34-
_11 = discriminant((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 25:6]))); // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
35-
switchInt(move _11) -> [0_u32: bb1, 3_u32: bb5, otherwise: bb6]; // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
34+
_11 = discriminant((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 19:24]))); // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
35+
switchInt(move _11) -> [0_u32: bb1, 3_u32: bb5, otherwise: bb6]; // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
3636
}
3737

3838
bb1: {
39-
_10 = move _2; // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
39+
_10 = move _2; // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
4040
nop; // scope 0 at $DIR/generator-tiny.rs:20:13: 20:15
41-
Deinit((((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 25:6])) as variant#3).0: HasDrop)); // scope 0 at $DIR/generator-tiny.rs:20:18: 20:25
41+
Deinit((((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 19:24])) as variant#3).0: HasDrop)); // scope 0 at $DIR/generator-tiny.rs:20:18: 20:25
4242
StorageLive(_4); // scope 1 at $DIR/generator-tiny.rs:21:9: 24:10
4343
goto -> bb2; // scope 1 at $DIR/generator-tiny.rs:21:9: 24:10
4444
}
@@ -50,7 +50,7 @@ fn main::{closure#0}(_1: Pin<&mut [generator@$DIR/generator-tiny.rs:19:16: 25:6]
5050
Deinit(_0); // scope 1 at $DIR/generator-tiny.rs:22:13: 22:18
5151
((_0 as Yielded).0: ()) = move _7; // scope 1 at $DIR/generator-tiny.rs:22:13: 22:18
5252
discriminant(_0) = 0; // scope 1 at $DIR/generator-tiny.rs:22:13: 22:18
53-
discriminant((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 25:6]))) = 3; // scope 1 at $DIR/generator-tiny.rs:22:13: 22:18
53+
discriminant((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 19:24]))) = 3; // scope 1 at $DIR/generator-tiny.rs:22:13: 22:18
5454
return; // scope 1 at $DIR/generator-tiny.rs:22:13: 22:18
5555
}
5656

@@ -71,14 +71,14 @@ fn main::{closure#0}(_1: Pin<&mut [generator@$DIR/generator-tiny.rs:19:16: 25:6]
7171
}
7272

7373
bb5: {
74-
StorageLive(_4); // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
75-
StorageLive(_6); // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
76-
StorageLive(_7); // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
77-
_6 = move _2; // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
78-
goto -> bb3; // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
74+
StorageLive(_4); // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
75+
StorageLive(_6); // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
76+
StorageLive(_7); // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
77+
_6 = move _2; // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
78+
goto -> bb3; // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
7979
}
8080

8181
bb6: {
82-
unreachable; // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
82+
unreachable; // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
8383
}
8484
}

src/test/mir-opt/inline/inline_closure_captures.foo.Inline.after.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
1919
debug t => (*((*_6).1: &T)); // in scope 2 at $DIR/inline-closure-captures.rs:10:17: 10:18
2020
let mut _10: i32; // in scope 2 at $DIR/inline-closure-captures.rs:11:19: 11:20
2121
let mut _11: T; // in scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
22-
let mut _12: &i32; // in scope 2 at $DIR/inline-closure-captures.rs:11:13: 11:24
23-
let mut _13: &T; // in scope 2 at $DIR/inline-closure-captures.rs:11:13: 11:24
22+
let mut _12: &i32; // in scope 2 at $DIR/inline-closure-captures.rs:11:13: 11:17
23+
let mut _13: &T; // in scope 2 at $DIR/inline-closure-captures.rs:11:13: 11:17
2424
}
2525
}
2626

@@ -33,8 +33,8 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
3333
Deinit(_3); // scope 0 at $DIR/inline-closure-captures.rs:11:13: 11:24
3434
(_3.0: &i32) = move _4; // scope 0 at $DIR/inline-closure-captures.rs:11:13: 11:24
3535
(_3.1: &T) = move _5; // scope 0 at $DIR/inline-closure-captures.rs:11:13: 11:24
36-
StorageDead(_5); // scope 0 at $DIR/inline-closure-captures.rs:11:23: 11:24
37-
StorageDead(_4); // scope 0 at $DIR/inline-closure-captures.rs:11:23: 11:24
36+
StorageDead(_5); // scope 0 at $DIR/inline-closure-captures.rs:11:16: 11:17
37+
StorageDead(_4); // scope 0 at $DIR/inline-closure-captures.rs:11:16: 11:17
3838
StorageLive(_6); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:6
3939
_6 = &_3; // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:6
4040
StorageLive(_7); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9

0 commit comments

Comments
 (0)