Skip to content

Commit 5cdf5b8

Browse files
committed
Auto merge of #76931 - oli-obk:const_prop_inline_lint_madness, r=wesleywiser
Properly handle lint spans after MIR inlining The first commit shows what happens when we apply mir inlining and then cause lints on the inlined MIR. The second commit fixes that. r? `@wesleywiser`
2 parents 0cd1516 + 888ef24 commit 5cdf5b8

File tree

40 files changed

+400
-350
lines changed

40 files changed

+400
-350
lines changed

compiler/rustc_errors/src/emitter.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ pub trait Emitter {
299299

300300
// Skip past non-macro entries, just in case there
301301
// are some which do actually involve macros.
302-
ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,
302+
ExpnKind::Inlined | ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,
303303

304304
ExpnKind::Macro(macro_kind, _) => Some(macro_kind),
305305
}
@@ -359,7 +359,10 @@ pub trait Emitter {
359359
continue;
360360
}
361361

362-
if always_backtrace {
362+
if matches!(trace.kind, ExpnKind::Inlined) {
363+
new_labels
364+
.push((trace.call_site, "in the inlined copy of this code".to_string()));
365+
} else if always_backtrace {
363366
new_labels.push((
364367
trace.def_site,
365368
format!(

compiler/rustc_middle/src/lint.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,9 @@ pub fn struct_lint_level<'s, 'd>(
371371
pub fn in_external_macro(sess: &Session, span: Span) -> bool {
372372
let expn_data = span.ctxt().outer_expn_data();
373373
match expn_data.kind {
374-
ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop(_)) => false,
374+
ExpnKind::Inlined | ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop(_)) => {
375+
false
376+
}
375377
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
376378
ExpnKind::Macro(MacroKind::Bang, _) => {
377379
// Dummy span for the `def_site` means it's an external macro.

compiler/rustc_mir/src/transform/inline.rs

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_middle::mir::visit::*;
88
use rustc_middle::mir::*;
99
use rustc_middle::ty::subst::Subst;
1010
use rustc_middle::ty::{self, ConstKind, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
11+
use rustc_span::{hygiene::ExpnKind, ExpnData, Span};
1112
use rustc_target::spec::abi::Abi;
1213

1314
use super::simplify::{remove_dead_blocks, CfgSimplifier};
@@ -471,6 +472,8 @@ impl Inliner<'tcx> {
471472
cleanup_block: cleanup,
472473
in_cleanup_block: false,
473474
tcx: self.tcx,
475+
callsite_span: callsite.source_info.span,
476+
body_span: callee_body.span,
474477
};
475478

476479
// Map all `Local`s, `SourceScope`s and `BasicBlock`s to new ones
@@ -682,6 +685,8 @@ struct Integrator<'a, 'tcx> {
682685
cleanup_block: Option<BasicBlock>,
683686
in_cleanup_block: bool,
684687
tcx: TyCtxt<'tcx>,
688+
callsite_span: Span,
689+
body_span: Span,
685690
}
686691

687692
impl<'a, 'tcx> Integrator<'a, 'tcx> {
@@ -726,6 +731,14 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
726731
*scope = self.map_scope(*scope);
727732
}
728733

734+
fn visit_span(&mut self, span: &mut Span) {
735+
// Make sure that all spans track the fact that they were inlined.
736+
*span = self.callsite_span.fresh_expansion(ExpnData {
737+
def_site: self.body_span,
738+
..ExpnData::default(ExpnKind::Inlined, *span, self.tcx.sess.edition(), None)
739+
});
740+
}
741+
729742
fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
730743
// If this is the `RETURN_PLACE`, we need to rebase any projections onto it.
731744
let dest_proj_len = self.destination.projection.len();

compiler/rustc_save_analysis/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,9 @@ impl<'tcx> SaveContext<'tcx> {
799799

800800
// These are not macros.
801801
// FIXME(eddyb) maybe there is a way to handle them usefully?
802-
ExpnKind::Root | ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => return None,
802+
ExpnKind::Inlined | ExpnKind::Root | ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => {
803+
return None;
804+
}
803805
};
804806

805807
let callee_span = self.span_from_span(callee.def_site);

compiler/rustc_span/src/hygiene.rs

+3
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,8 @@ pub enum ExpnKind {
766766
AstPass(AstPass),
767767
/// Desugaring done by the compiler during HIR lowering.
768768
Desugaring(DesugaringKind),
769+
/// MIR inlining
770+
Inlined,
769771
}
770772

771773
impl ExpnKind {
@@ -779,6 +781,7 @@ impl ExpnKind {
779781
},
780782
ExpnKind::AstPass(kind) => kind.descr().to_string(),
781783
ExpnKind::Desugaring(kind) => format!("desugaring of {}", kind.descr()),
784+
ExpnKind::Inlined => "inlined source".to_string(),
782785
}
783786
}
784787
}

src/test/mir-opt/dest-prop/cycle.main.DestinationPropagation.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
- debug z => _3; // in scope 3 at $DIR/cycle.rs:11:9: 11:10
2020
+ debug z => _4; // in scope 3 at $DIR/cycle.rs:11:9: 11:10
2121
scope 4 (inlined std::mem::drop::<i32>) { // at $DIR/cycle.rs:14:5: 14:12
22-
debug _x => _6; // in scope 4 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
22+
debug _x => _6; // in scope 4 at $DIR/cycle.rs:14:5: 14:12
2323
}
2424
}
2525
}
@@ -56,7 +56,7 @@
5656
StorageLive(_6); // scope 3 at $DIR/cycle.rs:14:10: 14:11
5757
- _6 = _1; // scope 3 at $DIR/cycle.rs:14:10: 14:11
5858
+ _6 = _4; // scope 3 at $DIR/cycle.rs:14:10: 14:11
59-
_5 = const (); // scope 4 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
59+
_5 = const (); // scope 4 at $DIR/cycle.rs:14:5: 14:12
6060
StorageDead(_6); // scope 3 at $DIR/cycle.rs:14:11: 14:12
6161
StorageDead(_5); // scope 3 at $DIR/cycle.rs:14:12: 14:13
6262
_0 = const (); // scope 0 at $DIR/cycle.rs:8:11: 15:2

src/test/mir-opt/dest-prop/union.main.DestinationPropagation.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
scope 2 {
1313
}
1414
scope 3 (inlined std::mem::drop::<u32>) { // at $DIR/union.rs:15:5: 15:27
15-
debug _x => _4; // in scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
15+
debug _x => _4; // in scope 3 at $DIR/union.rs:15:5: 15:27
1616
}
1717
}
1818

@@ -31,7 +31,7 @@
3131
StorageLive(_3); // scope 1 at $DIR/union.rs:15:5: 15:27
3232
StorageLive(_4); // scope 1 at $DIR/union.rs:15:10: 15:26
3333
_4 = (_1.0: u32); // scope 2 at $DIR/union.rs:15:19: 15:24
34-
_3 = const (); // scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
34+
_3 = const (); // scope 3 at $DIR/union.rs:15:5: 15:27
3535
StorageDead(_4); // scope 1 at $DIR/union.rs:15:26: 15:27
3636
StorageDead(_3); // scope 1 at $DIR/union.rs:15:27: 15:28
3737
_0 = const (); // scope 0 at $DIR/union.rs:8:11: 16:2

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ fn main::{closure#0}(_1: *mut [generator@$DIR/generator-drop-cleanup.rs:10:15: 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
scope 2 (inlined String::new) { // at $DIR/generator-drop-cleanup.rs:11:18: 11:31
30-
let mut _6: std::vec::Vec<u8>; // in scope 2 at $SRC_DIR/alloc/src/string.rs:LL:COL
31-
scope 3 (inlined Vec::<u8>::new) { // at $SRC_DIR/alloc/src/string.rs:LL:COL
30+
let mut _6: std::vec::Vec<u8>; // in scope 2 at $DIR/generator-drop-cleanup.rs:11:18: 11:31
31+
scope 3 (inlined Vec::<u8>::new) { // at $DIR/generator-drop-cleanup.rs:11:18: 11:31
3232
}
3333
}
3434

src/test/mir-opt/inline/inline_any_operand.bar.Inline.after.mir

+11-11
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ fn bar() -> bool {
99
scope 1 {
1010
debug f => _1; // in scope 1 at $DIR/inline-any-operand.rs:11:9: 11:10
1111
scope 2 (inlined foo) { // at $DIR/inline-any-operand.rs:12:5: 12:13
12-
debug x => _3; // in scope 2 at $DIR/inline-any-operand.rs:16:8: 16:9
13-
debug y => _4; // in scope 2 at $DIR/inline-any-operand.rs:16:16: 16:17
14-
let mut _5: i32; // in scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
15-
let mut _6: i32; // in scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
12+
debug x => _3; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
13+
debug y => _4; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
14+
let mut _5: i32; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
15+
let mut _6: i32; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
1616
}
1717
}
1818

@@ -28,13 +28,13 @@ fn bar() -> bool {
2828
_3 = const 1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
2929
StorageLive(_4); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
3030
_4 = const -1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
31-
StorageLive(_5); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
32-
_5 = _3; // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
33-
StorageLive(_6); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
34-
_6 = _4; // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
35-
_0 = Eq(move _5, move _6); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:11
36-
StorageDead(_6); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
37-
StorageDead(_5); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
31+
StorageLive(_5); // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
32+
_5 = _3; // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
33+
StorageLive(_6); // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
34+
_6 = _4; // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
35+
_0 = Eq(move _5, move _6); // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
36+
StorageDead(_6); // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
37+
StorageDead(_5); // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
3838
StorageDead(_4); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
3939
StorageDead(_3); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
4040
StorageDead(_2); // scope 1 at $DIR/inline-any-operand.rs:12:12: 12:13

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ fn foo(_1: T, _2: i32) -> i32 {
1414
scope 1 {
1515
debug x => _3; // in scope 1 at $DIR/inline-closure.rs:11:9: 11:10
1616
scope 2 (inlined foo::<T>::{closure#0}) { // at $DIR/inline-closure.rs:12:5: 12:12
17-
debug _t => _8; // in scope 2 at $DIR/inline-closure.rs:11:14: 11:16
18-
debug _q => _9; // in scope 2 at $DIR/inline-closure.rs:11:18: 11:20
17+
debug _t => _8; // in scope 2 at $DIR/inline-closure.rs:12:5: 12:12
18+
debug _q => _9; // in scope 2 at $DIR/inline-closure.rs:12:5: 12:12
1919
}
2020
}
2121

@@ -34,7 +34,7 @@ fn foo(_1: T, _2: i32) -> i32 {
3434
_8 = move (_5.0: i32); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
3535
StorageLive(_9); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
3636
_9 = move (_5.1: i32); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
37-
_0 = _8; // scope 2 at $DIR/inline-closure.rs:11:22: 11:24
37+
_0 = _8; // scope 2 at $DIR/inline-closure.rs:12:5: 12:12
3838
StorageDead(_9); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
3939
StorageDead(_8); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
4040
StorageDead(_7); // scope 1 at $DIR/inline-closure.rs:12:11: 12:12

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ fn foo(_1: T, _2: &i32) -> i32 {
1414
scope 1 {
1515
debug x => _3; // in scope 1 at $DIR/inline-closure-borrows-arg.rs:12:9: 12:10
1616
scope 2 (inlined foo::<T>::{closure#0}) { // at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
17-
debug r => _8; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:12:14: 12:15
18-
debug _s => _9; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:12:23: 12:25
19-
let _10: &i32; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
17+
debug r => _8; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
18+
debug _s => _9; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
19+
let _10: &i32; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
2020
scope 3 {
21-
debug variable => _10; // in scope 3 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
21+
debug variable => _10; // in scope 3 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
2222
}
2323
}
2424
}
@@ -38,10 +38,10 @@ fn foo(_1: T, _2: &i32) -> i32 {
3838
_8 = move (_5.0: &i32); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
3939
StorageLive(_9); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
4040
_9 = move (_5.1: &i32); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
41-
StorageLive(_10); // scope 2 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
42-
_10 = _8; // scope 2 at $DIR/inline-closure-borrows-arg.rs:13:24: 13:27
43-
_0 = (*_8); // scope 3 at $DIR/inline-closure-borrows-arg.rs:14:9: 14:18
44-
StorageDead(_10); // scope 2 at $DIR/inline-closure-borrows-arg.rs:15:5: 15:6
41+
StorageLive(_10); // scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
42+
_10 = _8; // scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
43+
_0 = (*_8); // scope 3 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
44+
StorageDead(_10); // scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
4545
StorageDead(_9); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
4646
StorageDead(_8); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
4747
StorageDead(_7); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:11: 16:12

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
1414
scope 1 {
1515
debug x => _3; // in scope 1 at $DIR/inline-closure-captures.rs:11:9: 11:10
1616
scope 2 (inlined foo::<T>::{closure#0}) { // at $DIR/inline-closure-captures.rs:12:5: 12:9
17-
debug _q => _9; // in scope 2 at $DIR/inline-closure-captures.rs:11:14: 11:16
18-
debug q => (*((*_6).0: &i32)); // in scope 2 at $DIR/inline-closure-captures.rs:10:23: 10:24
19-
debug t => (*((*_6).1: &T)); // in scope 2 at $DIR/inline-closure-captures.rs:10:17: 10:18
20-
let mut _10: T; // in scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
17+
debug _q => _9; // in scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
18+
debug q => (*((*_6).0: &i32)); // in scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
19+
debug t => (*((*_6).1: &T)); // in scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
20+
let mut _10: T; // in scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
2121
}
2222
}
2323

@@ -39,11 +39,11 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
3939
(_7.0: i32) = move _8; // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
4040
StorageLive(_9); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
4141
_9 = move (_7.0: i32); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
42-
(_0.0: i32) = (*((*_6).0: &i32)); // scope 2 at $DIR/inline-closure-captures.rs:11:19: 11:20
43-
StorageLive(_10); // scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
44-
_10 = (*((*_6).1: &T)); // scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
45-
(_0.1: T) = move _10; // scope 2 at $DIR/inline-closure-captures.rs:11:18: 11:24
46-
StorageDead(_10); // scope 2 at $DIR/inline-closure-captures.rs:11:23: 11:24
42+
(_0.0: i32) = (*((*_6).0: &i32)); // scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
43+
StorageLive(_10); // scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
44+
_10 = (*((*_6).1: &T)); // scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
45+
(_0.1: T) = move _10; // scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
46+
StorageDead(_10); // scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
4747
StorageDead(_9); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
4848
StorageDead(_8); // scope 1 at $DIR/inline-closure-captures.rs:12:8: 12:9
4949
StorageDead(_7); // scope 1 at $DIR/inline-closure-captures.rs:12:8: 12:9

src/test/mir-opt/inline/inline_compatibility.inlined_no_sanitize.Inline.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- }
1717
-
1818
- bb1: {
19-
+ _1 = const (); // scope 1 at $DIR/inline-compatibility.rs:39:29: 39:31
19+
+ _1 = const (); // scope 1 at $DIR/inline-compatibility.rs:25:5: 25:18
2020
StorageDead(_1); // scope 0 at $DIR/inline-compatibility.rs:25:18: 25:19
2121
_0 = const (); // scope 0 at $DIR/inline-compatibility.rs:24:37: 26:2
2222
return; // scope 0 at $DIR/inline-compatibility.rs:26:2: 26:2

src/test/mir-opt/inline/inline_compatibility.inlined_target_feature.Inline.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- }
1717
-
1818
- bb1: {
19-
+ _1 = const (); // scope 1 at $DIR/inline-compatibility.rs:35:32: 35:34
19+
+ _1 = const (); // scope 1 at $DIR/inline-compatibility.rs:14:5: 14:21
2020
StorageDead(_1); // scope 0 at $DIR/inline-compatibility.rs:14:21: 14:22
2121
_0 = const (); // scope 0 at $DIR/inline-compatibility.rs:13:40: 15:2
2222
return; // scope 0 at $DIR/inline-compatibility.rs:15:2: 15:2

0 commit comments

Comments
 (0)