Skip to content

Commit 5a34dbf

Browse files
committed
Improve spans in custom mir
1 parent 52ce1f7 commit 5a34dbf

10 files changed

+47
-39
lines changed

compiler/rustc_mir_build/src/build/custom/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub(super) fn build_custom_mir<'tcx>(
7474
let mut pctxt = ParseCtxt {
7575
tcx,
7676
thir,
77-
source_info: SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE },
77+
source_scope: OUTERMOST_SOURCE_SCOPE,
7878
body: &mut body,
7979
local_map: FxHashMap::default(),
8080
block_map: FxHashMap::default(),
@@ -128,7 +128,7 @@ fn parse_attribute(attr: &Attribute) -> MirPhase {
128128
struct ParseCtxt<'tcx, 'body> {
129129
tcx: TyCtxt<'tcx>,
130130
thir: &'body Thir<'tcx>,
131-
source_info: SourceInfo,
131+
source_scope: SourceScope,
132132

133133
body: &'body mut Body<'tcx>,
134134
local_map: FxHashMap<LocalVarId, Local>,

compiler/rustc_mir_build/src/build/custom/parse.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,23 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
233233
let mut data = BasicBlockData::new(None);
234234
for stmt_id in &*block.stmts {
235235
let stmt = self.statement_as_expr(*stmt_id)?;
236+
let span = self.thir[stmt].span;
236237
let statement = self.parse_statement(stmt)?;
237-
data.statements.push(Statement { source_info: self.source_info, kind: statement });
238+
data.statements.push(Statement {
239+
source_info: SourceInfo { span, scope: self.source_scope },
240+
kind: statement,
241+
});
238242
}
239243

240244
let Some(trailing) = block.expr else {
241245
return Err(self.expr_error(expr_id, "terminator"))
242246
};
247+
let span = self.thir[trailing].span;
243248
let terminator = self.parse_terminator(trailing)?;
244-
data.terminator = Some(Terminator { source_info: self.source_info, kind: terminator });
249+
data.terminator = Some(Terminator {
250+
source_info: SourceInfo { span, scope: self.source_scope },
251+
kind: terminator,
252+
});
245253

246254
Ok(data)
247255
}

compiler/rustc_mir_build/src/build/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ fn construct_fn<'tcx>(
492492
arguments,
493493
return_ty,
494494
return_ty_span,
495-
span,
495+
span_with_body,
496496
custom_mir_attr,
497497
);
498498
}

src/test/mir-opt/building/custom/arbitrary_let.arbitrary_let.built.after.mir

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ fn arbitrary_let(_1: i32) -> i32 {
66
let mut _3: i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
77

88
bb0: {
9-
_2 = _1; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32
10-
goto -> bb2; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32
9+
_2 = _1; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
10+
goto -> bb2; // scope 0 at $DIR/arbitrary_let.rs:+4:13: +4:25
1111
}
1212

1313
bb1: {
14-
_0 = _3; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32
15-
return; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32
14+
_0 = _3; // scope 0 at $DIR/arbitrary_let.rs:+7:13: +7:20
15+
return; // scope 0 at $DIR/arbitrary_let.rs:+8:13: +8:21
1616
}
1717

1818
bb2: {
19-
_3 = _2; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32
20-
goto -> bb1; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32
19+
_3 = _2; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
20+
goto -> bb1; // scope 0 at $DIR/arbitrary_let.rs:+12:13: +12:24
2121
}
2222
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
// MIR for `consts` after built
22

33
fn consts() -> () {
4-
let mut _0: (); // return place in scope 0 at $DIR/consts.rs:10:27: 10:27
4+
let mut _0: (); // return place in scope 0 at $DIR/consts.rs:+0:27: +0:27
55
let mut _1: u8; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
66
let mut _2: i8; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
77
let mut _3: u32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
88
let mut _4: i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
99
let mut _5: fn() {consts::<10>}; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
1010

1111
bb0: {
12-
_1 = const 5_u8; // scope 0 at $DIR/consts.rs:+0:1: +0:26
13-
_2 = const _; // scope 0 at $DIR/consts.rs:+0:1: +0:26
14-
_3 = const C; // scope 0 at $DIR/consts.rs:+0:1: +0:26
15-
_4 = const _; // scope 0 at $DIR/consts.rs:+0:1: +0:26
16-
_5 = consts::<10>; // scope 0 at $DIR/consts.rs:+0:1: +0:26
12+
_1 = const 5_u8; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
13+
_2 = const _; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
14+
_3 = const C; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
15+
_4 = const _; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
16+
_5 = consts::<10>; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
1717
// mir::Constant
1818
// + span: $DIR/consts.rs:16:18: 16:30
1919
// + literal: Const { ty: fn() {consts::<10>}, val: Value(<ZST>) }
20-
return; // scope 0 at $DIR/consts.rs:+0:1: +0:26
20+
return; // scope 0 at $DIR/consts.rs:+7:9: +7:17
2121
}
2222
}

src/test/mir-opt/building/custom/consts.statics.built.after.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
// MIR for `statics` after built
22

33
fn statics() -> () {
4-
let mut _0: (); // return place in scope 0 at $DIR/consts.rs:25:14: 25:14
4+
let mut _0: (); // return place in scope 0 at $DIR/consts.rs:+0:14: +0:14
55
let mut _1: &i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
66
let mut _2: *mut i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
77

88
bb0: {
9-
_1 = const {alloc1: &i32}; // scope 0 at $DIR/consts.rs:+0:1: +0:13
9+
_1 = const {alloc1: &i32}; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
1010
// mir::Constant
1111
// + span: $DIR/consts.rs:27:31: 27:32
1212
// + literal: Const { ty: &i32, val: Value(Scalar(alloc1)) }
13-
_2 = const {alloc2: *mut i32}; // scope 0 at $DIR/consts.rs:+0:1: +0:13
13+
_2 = const {alloc2: *mut i32}; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
1414
// mir::Constant
1515
// + span: $DIR/consts.rs:28:38: 28:39
1616
// + literal: Const { ty: *mut i32, val: Value(Scalar(alloc2)) }
17-
return; // scope 0 at $DIR/consts.rs:+0:1: +0:13
17+
return; // scope 0 at $DIR/consts.rs:+4:9: +4:17
1818
}
1919
}
2020

src/test/mir-opt/building/custom/references.immut_ref.built.after.mir

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ fn immut_ref(_1: &i32) -> &i32 {
55
let mut _2: *const i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
66

77
bb0: {
8-
_2 = &raw const (*_1); // scope 0 at $DIR/references.rs:+0:1: +0:34
9-
Retag([raw] _2); // scope 0 at $DIR/references.rs:+0:1: +0:34
10-
_0 = &(*_2); // scope 0 at $DIR/references.rs:+0:1: +0:34
11-
Retag(_0); // scope 0 at $DIR/references.rs:+0:1: +0:34
12-
return; // scope 0 at $DIR/references.rs:+0:1: +0:34
8+
_2 = &raw const (*_1); // scope 0 at $DIR/references.rs:+5:13: +5:29
9+
Retag([raw] _2); // scope 0 at $DIR/references.rs:+6:13: +6:24
10+
_0 = &(*_2); // scope 0 at $DIR/references.rs:+7:13: +7:23
11+
Retag(_0); // scope 0 at $DIR/references.rs:+8:13: +8:23
12+
return; // scope 0 at $DIR/references.rs:+9:13: +9:21
1313
}
1414
}

src/test/mir-opt/building/custom/references.mut_ref.built.after.mir

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ fn mut_ref(_1: &mut i32) -> &mut i32 {
55
let mut _2: *mut i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
66

77
bb0: {
8-
_2 = &raw mut (*_1); // scope 0 at $DIR/references.rs:+0:1: +0:40
9-
Retag([raw] _2); // scope 0 at $DIR/references.rs:+0:1: +0:40
10-
_0 = &mut (*_2); // scope 0 at $DIR/references.rs:+0:1: +0:40
11-
Retag(_0); // scope 0 at $DIR/references.rs:+0:1: +0:40
12-
return; // scope 0 at $DIR/references.rs:+0:1: +0:40
8+
_2 = &raw mut (*_1); // scope 0 at $DIR/references.rs:+5:13: +5:33
9+
Retag([raw] _2); // scope 0 at $DIR/references.rs:+6:13: +6:24
10+
_0 = &mut (*_2); // scope 0 at $DIR/references.rs:+7:13: +7:26
11+
Retag(_0); // scope 0 at $DIR/references.rs:+8:13: +8:23
12+
return; // scope 0 at $DIR/references.rs:+9:13: +9:21
1313
}
1414
}

src/test/mir-opt/building/custom/simple_assign.simple.built.after.mir

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ fn simple(_1: i32) -> i32 {
66
let mut _3: i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
77

88
bb0: {
9-
_2 = _1; // scope 0 at $DIR/simple_assign.rs:+0:1: +0:29
10-
goto -> bb1; // scope 0 at $DIR/simple_assign.rs:+0:1: +0:29
9+
_2 = _1; // scope 0 at $DIR/simple_assign.rs:+6:13: +6:22
10+
goto -> bb1; // scope 0 at $DIR/simple_assign.rs:+7:13: +7:23
1111
}
1212

1313
bb1: {
14-
_3 = move _2; // scope 0 at $DIR/simple_assign.rs:+0:1: +0:29
15-
_0 = _3; // scope 0 at $DIR/simple_assign.rs:+0:1: +0:29
16-
return; // scope 0 at $DIR/simple_assign.rs:+0:1: +0:29
14+
_3 = move _2; // scope 0 at $DIR/simple_assign.rs:+11:13: +11:32
15+
_0 = _3; // scope 0 at $DIR/simple_assign.rs:+12:13: +12:24
16+
return; // scope 0 at $DIR/simple_assign.rs:+13:13: +13:21
1717
}
1818
}

src/test/mir-opt/building/custom/simple_assign.simple_ref.built.after.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn simple_ref(_1: &mut i32) -> &mut i32 {
44
let mut _0: &mut i32; // return place in scope 0 at $DIR/simple_assign.rs:+0:35: +0:43
55

66
bb0: {
7-
_0 = move _1; // scope 0 at $DIR/simple_assign.rs:+0:1: +0:43
8-
return; // scope 0 at $DIR/simple_assign.rs:+0:1: +0:43
7+
_0 = move _1; // scope 0 at $DIR/simple_assign.rs:+2:9: +2:22
8+
return; // scope 0 at $DIR/simple_assign.rs:+3:9: +3:17
99
}
1010
}

0 commit comments

Comments
 (0)