Skip to content

Commit f848d27

Browse files
committed
Use tcx.hir() utils for spans in MIR building.
This corrects the `span_with_body` in the case of closures, which was incorrectly shortened to the `def_span`.
1 parent e4e4114 commit f848d27

File tree

50 files changed

+353
-299
lines changed

Some content is hidden

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

50 files changed

+353
-299
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
360360

361361
diag.span_label(upvar_span, "captured outer variable");
362362
diag.span_label(
363-
self.body.span,
363+
self.infcx.tcx.def_span(def_id),
364364
format!("captured by this `{closure_kind}` closure"),
365365
);
366366

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
973973

974974
let hir = self.infcx.tcx.hir();
975975
let closure_id = self.mir_hir_id();
976+
let closure_span = self.infcx.tcx.def_span(self.mir_def_id());
976977
let fn_call_id = hir.get_parent_node(closure_id);
977978
let node = hir.get(fn_call_id);
978979
let def_id = hir.enclosing_body_owner(fn_call_id);
@@ -1024,7 +1025,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10241025
if let Some(span) = arg {
10251026
err.span_label(span, "change this to accept `FnMut` instead of `Fn`");
10261027
err.span_label(func.span, "expects `Fn` instead of `FnMut`");
1027-
err.span_label(self.body.span, "in this closure");
1028+
err.span_label(closure_span, "in this closure");
10281029
look_at_return = false;
10291030
}
10301031
}
@@ -1050,7 +1051,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10501051
sig.decl.output.span(),
10511052
"change this to return `FnMut` instead of `Fn`",
10521053
);
1053-
err.span_label(self.body.span, "in this closure");
1054+
err.span_label(closure_span, "in this closure");
10541055
}
10551056
_ => {}
10561057
}

compiler/rustc_mir_build/src/build/mod.rs

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -67,50 +67,25 @@ fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_
6767
}
6868

6969
// Figure out what primary body this item has.
70-
let (body_id, return_ty_span, span_with_body) = match tcx.hir().get(id) {
71-
Node::Expr(hir::Expr {
72-
kind: hir::ExprKind::Closure(hir::Closure { fn_decl, body, .. }),
73-
..
74-
}) => (*body, fn_decl.output.span(), None),
75-
Node::Item(hir::Item {
76-
kind: hir::ItemKind::Fn(hir::FnSig { decl, .. }, _, body_id),
77-
span,
78-
..
79-
})
80-
| Node::ImplItem(hir::ImplItem {
81-
kind: hir::ImplItemKind::Fn(hir::FnSig { decl, .. }, body_id),
82-
span,
83-
..
84-
})
85-
| Node::TraitItem(hir::TraitItem {
86-
kind: hir::TraitItemKind::Fn(hir::FnSig { decl, .. }, hir::TraitFn::Provided(body_id)),
87-
span,
88-
..
89-
}) => {
90-
// Use the `Span` of the `Item/ImplItem/TraitItem` as the body span,
91-
// since the def span of a function does not include the body
92-
(*body_id, decl.output.span(), Some(*span))
93-
}
94-
Node::Item(hir::Item {
95-
kind: hir::ItemKind::Static(ty, _, body_id) | hir::ItemKind::Const(ty, body_id),
96-
..
97-
})
98-
| Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(ty, body_id), .. })
99-
| Node::TraitItem(hir::TraitItem {
100-
kind: hir::TraitItemKind::Const(ty, Some(body_id)),
101-
..
102-
}) => (*body_id, ty.span, None),
103-
Node::AnonConst(hir::AnonConst { body, hir_id, .. }) => {
104-
(*body, tcx.hir().span(*hir_id), None)
70+
let body_id = tcx.hir().body_owned_by(def.did);
71+
let span_with_body = tcx.hir().span_with_body(id);
72+
let return_ty_span = if let Some(fn_decl) = tcx.hir().fn_decl_by_hir_id(id) {
73+
fn_decl.output.span()
74+
} else {
75+
match tcx.hir().get(id) {
76+
Node::Item(hir::Item {
77+
kind: hir::ItemKind::Static(ty, _, _) | hir::ItemKind::Const(ty, _),
78+
..
79+
})
80+
| Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(ty, _), .. })
81+
| Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Const(ty, _), .. }) => {
82+
ty.span
83+
}
84+
Node::AnonConst(_) => tcx.def_span(def.did),
85+
_ => span_bug!(tcx.def_span(def.did), "can't build MIR for {:?}", def.did),
10586
}
106-
107-
_ => span_bug!(tcx.hir().span(id), "can't build MIR for {:?}", def.did),
10887
};
10988

110-
// If we don't have a specialized span for the body, just use the
111-
// normal def span.
112-
let span_with_body = span_with_body.unwrap_or_else(|| tcx.hir().span(id));
113-
11489
tcx.infer_ctxt().enter(|infcx| {
11590
let body = if let Some(error_reported) = typeck_results.tainted_by_errors {
11691
build::construct_error(&infcx, def, id, body_id, body_owner_kind, error_reported)
@@ -244,8 +219,6 @@ fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_
244219
// of `mir_build`, so now we can steal it
245220
let thir = thir.steal();
246221

247-
let span_with_body = span_with_body.to(tcx.hir().span(body_id.hir_id));
248-
249222
build::construct_const(
250223
&thir,
251224
&infcx,

src/test/mir-opt/const_promotion_extern_static.BAR.PromoteTemps.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
- StorageDead(_5); // scope 0 at $DIR/const-promotion-extern-static.rs:+0:43: +0:44
4141
- StorageDead(_3); // scope 0 at $DIR/const-promotion-extern-static.rs:+0:43: +0:44
4242
StorageDead(_1); // scope 0 at $DIR/const-promotion-extern-static.rs:+0:43: +0:44
43-
return; // scope 0 at $DIR/const-promotion-extern-static.rs:+0:1: +0:44
43+
return; // scope 0 at $DIR/const-promotion-extern-static.rs:+0:1: +0:45
4444
}
4545

4646
bb2 (cleanup): {
47-
resume; // scope 0 at $DIR/const-promotion-extern-static.rs:+0:1: +0:44
47+
resume; // scope 0 at $DIR/const-promotion-extern-static.rs:+0:1: +0:45
4848
}
4949
- }
5050
-

src/test/mir-opt/const_promotion_extern_static.BOP.mir_map.0.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ static BOP: &i32 = {
1212
_1 = &_2; // scope 0 at $DIR/const-promotion-extern-static.rs:+0:20: +0:23
1313
_0 = &(*_1); // scope 0 at $DIR/const-promotion-extern-static.rs:+0:20: +0:23
1414
StorageDead(_1); // scope 0 at $DIR/const-promotion-extern-static.rs:+0:22: +0:23
15-
return; // scope 0 at $DIR/const-promotion-extern-static.rs:+0:1: +0:23
15+
return; // scope 0 at $DIR/const-promotion-extern-static.rs:+0:1: +0:24
1616
}
1717
}

src/test/mir-opt/const_promotion_extern_static.FOO.PromoteTemps.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242
- StorageDead(_5); // scope 0 at $DIR/const-promotion-extern-static.rs:+0:54: +0:55
4343
- StorageDead(_3); // scope 0 at $DIR/const-promotion-extern-static.rs:+0:54: +0:55
4444
StorageDead(_1); // scope 0 at $DIR/const-promotion-extern-static.rs:+0:54: +0:55
45-
return; // scope 0 at $DIR/const-promotion-extern-static.rs:+0:1: +0:55
45+
return; // scope 0 at $DIR/const-promotion-extern-static.rs:+0:1: +0:56
4646
}
4747

4848
bb2 (cleanup): {
49-
resume; // scope 0 at $DIR/const-promotion-extern-static.rs:+0:1: +0:55
49+
resume; // scope 0 at $DIR/const-promotion-extern-static.rs:+0:1: +0:56
5050
}
5151
}
5252
-

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,70 +15,70 @@
1515
} */
1616

1717
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:+0:15: +0:17
19-
let mut _2: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:+0:15: +0:17
20-
let _3: std::string::String; // in scope 0 at $DIR/generator-drop-cleanup.rs:11:13: 11:15
21-
let _4: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:12:9: 12:14
22-
let mut _5: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:12:9: 12:14
23-
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:+0:15: +0:17
25-
let mut _8: u32; // in scope 0 at $DIR/generator-drop-cleanup.rs:+0:15: +0:17
18+
let mut _0: (); // return place in scope 0 at $DIR/generator-drop-cleanup.rs:+0:15: +3:6
19+
let mut _2: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:+0:15: +3:6
20+
let _3: std::string::String; // in scope 0 at $DIR/generator-drop-cleanup.rs:+1:13: +1:15
21+
let _4: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:+2:9: +2:14
22+
let mut _5: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:+2:9: +2:14
23+
let mut _6: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:+0:18: +0:18
24+
let mut _7: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:+0:15: +3:6
25+
let mut _8: u32; // in scope 0 at $DIR/generator-drop-cleanup.rs:+0:15: +3:6
2626
scope 1 {
27-
debug _s => (((*_1) as variant#3).0: std::string::String); // in scope 1 at $DIR/generator-drop-cleanup.rs:11:13: 11:15
27+
debug _s => (((*_1) as variant#3).0: std::string::String); // in scope 1 at $DIR/generator-drop-cleanup.rs:+1:13: +1:15
2828
}
2929

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

3535
bb1: {
36-
StorageDead(_5); // scope 1 at $DIR/generator-drop-cleanup.rs:12:13: 12:14
37-
StorageDead(_4); // scope 1 at $DIR/generator-drop-cleanup.rs:12:14: 12:15
38-
drop((((*_1) as variant#3).0: std::string::String)) -> [return: bb2, unwind: bb5]; // scope 0 at $DIR/generator-drop-cleanup.rs:13:5: 13:6
36+
StorageDead(_5); // scope 1 at $DIR/generator-drop-cleanup.rs:+2:13: +2:14
37+
StorageDead(_4); // scope 1 at $DIR/generator-drop-cleanup.rs:+2:14: +2:15
38+
drop((((*_1) as variant#3).0: std::string::String)) -> [return: bb2, unwind: bb5]; // scope 0 at $DIR/generator-drop-cleanup.rs:+3:5: +3:6
3939
}
4040

4141
bb2: {
42-
nop; // scope 0 at $DIR/generator-drop-cleanup.rs:13:5: 13:6
43-
goto -> bb8; // scope 0 at $DIR/generator-drop-cleanup.rs:13:5: 13:6
42+
nop; // scope 0 at $DIR/generator-drop-cleanup.rs:+3:5: +3:6
43+
goto -> bb8; // scope 0 at $DIR/generator-drop-cleanup.rs:+3:5: +3:6
4444
}
4545

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

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

5454
bb5 (cleanup): {
55-
nop; // scope 0 at $DIR/generator-drop-cleanup.rs:13:5: 13:6
56-
goto -> bb4; // scope 0 at $DIR/generator-drop-cleanup.rs:13:5: 13:6
55+
nop; // scope 0 at $DIR/generator-drop-cleanup.rs:+3:5: +3:6
56+
goto -> bb4; // scope 0 at $DIR/generator-drop-cleanup.rs:+3:5: +3:6
5757
}
5858

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

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

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

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

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

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

0 commit comments

Comments
 (0)