Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle lint spans after MIR inlining #76931

Merged
merged 3 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ pub trait Emitter {

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

ExpnKind::Macro(macro_kind, _) => Some(macro_kind),
}
Expand Down Expand Up @@ -356,7 +356,10 @@ pub trait Emitter {
continue;
}

if always_backtrace {
if matches!(trace.kind, ExpnKind::Inlined) {
new_labels
.push((trace.call_site, "in the inlined copy of this code".to_string()));
} else if always_backtrace {
new_labels.push((
trace.def_site,
format!(
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_middle/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ pub fn struct_lint_level<'s, 'd>(
pub fn in_external_macro(sess: &Session, span: Span) -> bool {
let expn_data = span.ctxt().outer_expn_data();
match expn_data.kind {
ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop(_)) => false,
ExpnKind::Inlined | ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop(_)) => {
false
}
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
ExpnKind::Macro(MacroKind::Bang, _) => {
// Dummy span for the `def_site` means it's an external macro.
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_mir/src/transform/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rustc_middle::mir::visit::*;
use rustc_middle::mir::*;
use rustc_middle::ty::subst::Subst;
use rustc_middle::ty::{self, ConstKind, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
use rustc_span::{hygiene::ExpnKind, ExpnData, Span};
use rustc_target::spec::abi::Abi;

use super::simplify::{remove_dead_blocks, CfgSimplifier};
Expand Down Expand Up @@ -488,6 +489,8 @@ impl Inliner<'tcx> {
cleanup_block: cleanup,
in_cleanup_block: false,
tcx: self.tcx,
callsite_span: callsite.source_info.span,
body_span: callee_body.span,
};

// Map all `Local`s, `SourceScope`s and `BasicBlock`s to new ones
Expand Down Expand Up @@ -699,6 +702,8 @@ struct Integrator<'a, 'tcx> {
cleanup_block: Option<BasicBlock>,
in_cleanup_block: bool,
tcx: TyCtxt<'tcx>,
callsite_span: Span,
body_span: Span,
}

impl<'a, 'tcx> Integrator<'a, 'tcx> {
Expand Down Expand Up @@ -743,6 +748,14 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
*scope = self.map_scope(*scope);
}

fn visit_span(&mut self, span: &mut Span) {
// Make sure that all spans track the fact that they were inlined.
*span = self.callsite_span.fresh_expansion(ExpnData {
def_site: self.body_span,
..ExpnData::default(ExpnKind::Inlined, *span, self.tcx.sess.edition(), None)
});
}

fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
// If this is the `RETURN_PLACE`, we need to rebase any projections onto it.
let dest_proj_len = self.destination.projection.len();
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_save_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,9 @@ impl<'tcx> SaveContext<'tcx> {

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

let callee_span = self.span_from_span(callee.def_site);
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_span/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,8 @@ pub enum ExpnKind {
AstPass(AstPass),
/// Desugaring done by the compiler during HIR lowering.
Desugaring(DesugaringKind),
/// MIR inlining
Inlined,
}

impl ExpnKind {
Expand All @@ -779,6 +781,7 @@ impl ExpnKind {
},
ExpnKind::AstPass(kind) => kind.descr().to_string(),
ExpnKind::Desugaring(kind) => format!("desugaring of {}", kind.descr()),
ExpnKind::Inlined => "inlined source".to_string(),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
- debug z => _3; // in scope 3 at $DIR/cycle.rs:11:9: 11:10
+ debug z => _4; // in scope 3 at $DIR/cycle.rs:11:9: 11:10
scope 4 (inlined std::mem::drop::<i32>) { // at $DIR/cycle.rs:14:5: 14:12
debug _x => _6; // in scope 4 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
debug _x => _6; // in scope 4 at $DIR/cycle.rs:14:5: 14:12
}
}
}
Expand Down Expand Up @@ -56,7 +56,7 @@
StorageLive(_6); // scope 3 at $DIR/cycle.rs:14:10: 14:11
- _6 = _1; // scope 3 at $DIR/cycle.rs:14:10: 14:11
+ _6 = _4; // scope 3 at $DIR/cycle.rs:14:10: 14:11
_5 = const (); // scope 4 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
_5 = const (); // scope 4 at $DIR/cycle.rs:14:5: 14:12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this affect the generated debuginfo? LLVM inlining preserves the original source position instead of using the position of the inlined call. The MIR inliner should do the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debuginfo part was handled in #68965

This here just addresses spans for diagnostics. These spans do preserve the original and all intermediate source positions across any level of inlining. I should probably adjust mir dumps to also print all spans instead of just the final one.

StorageDead(_6); // scope 3 at $DIR/cycle.rs:14:11: 14:12
StorageDead(_5); // scope 3 at $DIR/cycle.rs:14:12: 14:13
_0 = const (); // scope 0 at $DIR/cycle.rs:8:11: 15:2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
scope 2 {
}
scope 3 (inlined std::mem::drop::<u32>) { // at $DIR/union.rs:15:5: 15:27
debug _x => _4; // in scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
debug _x => _4; // in scope 3 at $DIR/union.rs:15:5: 15:27
}
}

Expand All @@ -31,7 +31,7 @@
StorageLive(_3); // scope 1 at $DIR/union.rs:15:5: 15:27
StorageLive(_4); // scope 1 at $DIR/union.rs:15:10: 15:26
_4 = (_1.0: u32); // scope 2 at $DIR/union.rs:15:19: 15:24
_3 = const (); // scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
_3 = const (); // scope 3 at $DIR/union.rs:15:5: 15:27
StorageDead(_4); // scope 1 at $DIR/union.rs:15:26: 15:27
StorageDead(_3); // scope 1 at $DIR/union.rs:15:27: 15:28
_0 = const (); // scope 0 at $DIR/union.rs:8:11: 16:2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ fn main::{closure#0}(_1: *mut [generator@$DIR/generator-drop-cleanup.rs:10:15: 1
debug _s => (((*_1) as variant#3).0: std::string::String); // in scope 1 at $DIR/generator-drop-cleanup.rs:11:13: 11:15
}
scope 2 (inlined String::new) { // at $DIR/generator-drop-cleanup.rs:11:18: 11:31
let mut _6: std::vec::Vec<u8>; // in scope 2 at $SRC_DIR/alloc/src/string.rs:LL:COL
scope 3 (inlined Vec::<u8>::new) { // at $SRC_DIR/alloc/src/string.rs:LL:COL
let mut _6: std::vec::Vec<u8>; // in scope 2 at $DIR/generator-drop-cleanup.rs:11:18: 11:31
scope 3 (inlined Vec::<u8>::new) { // at $DIR/generator-drop-cleanup.rs:11:18: 11:31
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/test/mir-opt/inline/inline_any_operand.bar.Inline.after.mir
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ fn bar() -> bool {
scope 1 {
debug f => _1; // in scope 1 at $DIR/inline-any-operand.rs:11:9: 11:10
scope 2 (inlined foo) { // at $DIR/inline-any-operand.rs:12:5: 12:13
debug x => _3; // in scope 2 at $DIR/inline-any-operand.rs:16:8: 16:9
debug y => _4; // in scope 2 at $DIR/inline-any-operand.rs:16:16: 16:17
let mut _5: i32; // in scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
let mut _6: i32; // in scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
debug x => _3; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
debug y => _4; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
let mut _5: i32; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
let mut _6: i32; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
}
}

Expand All @@ -28,13 +28,13 @@ fn bar() -> bool {
_3 = const 1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageLive(_4); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
_4 = const -1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageLive(_5); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
_5 = _3; // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
StorageLive(_6); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
_6 = _4; // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
_0 = Eq(move _5, move _6); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:11
StorageDead(_6); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
StorageDead(_5); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
StorageLive(_5); // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
_5 = _3; // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageLive(_6); // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
_6 = _4; // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
_0 = Eq(move _5, move _6); // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageDead(_6); // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageDead(_5); // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageDead(_4); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageDead(_3); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageDead(_2); // scope 1 at $DIR/inline-any-operand.rs:12:12: 12:13
Expand Down
6 changes: 3 additions & 3 deletions src/test/mir-opt/inline/inline_closure.foo.Inline.after.mir
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ fn foo(_1: T, _2: i32) -> i32 {
scope 1 {
debug x => _3; // in scope 1 at $DIR/inline-closure.rs:11:9: 11:10
scope 2 (inlined foo::<T>::{closure#0}) { // at $DIR/inline-closure.rs:12:5: 12:12
debug _t => _8; // in scope 2 at $DIR/inline-closure.rs:11:14: 11:16
debug _q => _9; // in scope 2 at $DIR/inline-closure.rs:11:18: 11:20
debug _t => _8; // in scope 2 at $DIR/inline-closure.rs:12:5: 12:12
debug _q => _9; // in scope 2 at $DIR/inline-closure.rs:12:5: 12:12
}
}

Expand All @@ -34,7 +34,7 @@ fn foo(_1: T, _2: i32) -> i32 {
_8 = move (_5.0: i32); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
StorageLive(_9); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
_9 = move (_5.1: i32); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
_0 = _8; // scope 2 at $DIR/inline-closure.rs:11:22: 11:24
_0 = _8; // scope 2 at $DIR/inline-closure.rs:12:5: 12:12
StorageDead(_9); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
StorageDead(_8); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
StorageDead(_7); // scope 1 at $DIR/inline-closure.rs:12:11: 12:12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ fn foo(_1: T, _2: &i32) -> i32 {
scope 1 {
debug x => _3; // in scope 1 at $DIR/inline-closure-borrows-arg.rs:12:9: 12:10
scope 2 (inlined foo::<T>::{closure#0}) { // at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
debug r => _8; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:12:14: 12:15
debug _s => _9; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:12:23: 12:25
let _10: &i32; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
debug r => _8; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
debug _s => _9; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
let _10: &i32; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
scope 3 {
debug variable => _10; // in scope 3 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
debug variable => _10; // in scope 3 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
}
}
}
Expand All @@ -38,10 +38,10 @@ fn foo(_1: T, _2: &i32) -> i32 {
_8 = move (_5.0: &i32); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
StorageLive(_9); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
_9 = move (_5.1: &i32); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
StorageLive(_10); // scope 2 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
_10 = _8; // scope 2 at $DIR/inline-closure-borrows-arg.rs:13:24: 13:27
_0 = (*_8); // scope 3 at $DIR/inline-closure-borrows-arg.rs:14:9: 14:18
StorageDead(_10); // scope 2 at $DIR/inline-closure-borrows-arg.rs:15:5: 15:6
StorageLive(_10); // scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
_10 = _8; // scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
_0 = (*_8); // scope 3 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
StorageDead(_10); // scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
StorageDead(_9); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
StorageDead(_8); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
StorageDead(_7); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:11: 16:12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
scope 1 {
debug x => _3; // in scope 1 at $DIR/inline-closure-captures.rs:11:9: 11:10
scope 2 (inlined foo::<T>::{closure#0}) { // at $DIR/inline-closure-captures.rs:12:5: 12:9
debug _q => _9; // in scope 2 at $DIR/inline-closure-captures.rs:11:14: 11:16
debug q => (*((*_6).0: &i32)); // in scope 2 at $DIR/inline-closure-captures.rs:10:23: 10:24
debug t => (*((*_6).1: &T)); // in scope 2 at $DIR/inline-closure-captures.rs:10:17: 10:18
let mut _10: T; // in scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
debug _q => _9; // in scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
debug q => (*((*_6).0: &i32)); // in scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
debug t => (*((*_6).1: &T)); // in scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
let mut _10: T; // in scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
}
}

Expand All @@ -39,11 +39,11 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
(_7.0: i32) = move _8; // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
StorageLive(_9); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
_9 = move (_7.0: i32); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
(_0.0: i32) = (*((*_6).0: &i32)); // scope 2 at $DIR/inline-closure-captures.rs:11:19: 11:20
StorageLive(_10); // scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
_10 = (*((*_6).1: &T)); // scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
(_0.1: T) = move _10; // scope 2 at $DIR/inline-closure-captures.rs:11:18: 11:24
StorageDead(_10); // scope 2 at $DIR/inline-closure-captures.rs:11:23: 11:24
(_0.0: i32) = (*((*_6).0: &i32)); // scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
StorageLive(_10); // scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
_10 = (*((*_6).1: &T)); // scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
(_0.1: T) = move _10; // scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
StorageDead(_10); // scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
StorageDead(_9); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
StorageDead(_8); // scope 1 at $DIR/inline-closure-captures.rs:12:8: 12:9
StorageDead(_7); // scope 1 at $DIR/inline-closure-captures.rs:12:8: 12:9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- }
-
- bb1: {
+ _1 = const (); // scope 1 at $DIR/inline-compatibility.rs:39:29: 39:31
+ _1 = const (); // scope 1 at $DIR/inline-compatibility.rs:25:5: 25:18
StorageDead(_1); // scope 0 at $DIR/inline-compatibility.rs:25:18: 25:19
_0 = const (); // scope 0 at $DIR/inline-compatibility.rs:24:37: 26:2
return; // scope 0 at $DIR/inline-compatibility.rs:26:2: 26:2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- }
-
- bb1: {
+ _1 = const (); // scope 1 at $DIR/inline-compatibility.rs:35:32: 35:34
+ _1 = const (); // scope 1 at $DIR/inline-compatibility.rs:14:5: 14:21
StorageDead(_1); // scope 0 at $DIR/inline-compatibility.rs:14:21: 14:22
_0 = const (); // scope 0 at $DIR/inline-compatibility.rs:13:40: 15:2
return; // scope 0 at $DIR/inline-compatibility.rs:15:2: 15:2
Expand Down
Loading