From 0919ec3eccfc06534dcf392b91b616a70d0bf8b1 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 24 May 2023 20:26:24 +0000 Subject: [PATCH 1/2] Remove ExpnKind::Inlined. --- compiler/rustc_codegen_cranelift/src/common.rs | 6 +----- compiler/rustc_codegen_ssa/src/mir/block.rs | 6 +----- .../interpret/intrinsics/caller_location.rs | 6 +----- compiler/rustc_errors/src/emitter.rs | 5 ++--- compiler/rustc_middle/src/lint.rs | 3 +-- compiler/rustc_middle/src/ty/mod.rs | 4 +--- compiler/rustc_mir_transform/src/inline.rs | 18 ------------------ compiler/rustc_span/src/hygiene.rs | 4 ---- compiler/rustc_span/src/lib.rs | 6 ------ 9 files changed, 7 insertions(+), 51 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/src/common.rs b/compiler/rustc_codegen_cranelift/src/common.rs index ccb3a0c4f27..5eaa988dd09 100644 --- a/compiler/rustc_codegen_cranelift/src/common.rs +++ b/compiler/rustc_codegen_cranelift/src/common.rs @@ -413,11 +413,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> { // Note: must be kept in sync with get_caller_location from cg_ssa pub(crate) fn get_caller_location(&mut self, mut source_info: mir::SourceInfo) -> CValue<'tcx> { - let span_to_caller_location = |fx: &mut FunctionCx<'_, '_, 'tcx>, mut span: Span| { - // Remove `Inlined` marks as they pollute `expansion_cause`. - while span.is_inlined() { - span.remove_mark(); - } + let span_to_caller_location = |fx: &mut FunctionCx<'_, '_, 'tcx>, span: Span| { let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span); let caller = fx.tcx.sess.source_map().lookup_char_pos(topmost.lo()); let const_loc = fx.tcx.const_caller_location(( diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index 1f5f5b69d4d..d516ac4ebb7 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -1450,11 +1450,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { ) -> OperandRef<'tcx, Bx::Value> { let tcx = bx.tcx(); - let mut span_to_caller_location = |mut span: Span| { - // Remove `Inlined` marks as they pollute `expansion_cause`. - while span.is_inlined() { - span.remove_mark(); - } + let mut span_to_caller_location = |span: Span| { let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span); let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo()); let const_loc = tcx.const_caller_location(( diff --git a/compiler/rustc_const_eval/src/interpret/intrinsics/caller_location.rs b/compiler/rustc_const_eval/src/interpret/intrinsics/caller_location.rs index 3701eb93ec8..df5b581000b 100644 --- a/compiler/rustc_const_eval/src/interpret/intrinsics/caller_location.rs +++ b/compiler/rustc_const_eval/src/interpret/intrinsics/caller_location.rs @@ -111,11 +111,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { location } - pub(crate) fn location_triple_for_span(&self, mut span: Span) -> (Symbol, u32, u32) { - // Remove `Inlined` marks as they pollute `expansion_cause`. - while span.is_inlined() { - span.remove_mark(); - } + pub(crate) fn location_triple_for_span(&self, span: Span) -> (Symbol, u32, u32) { let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span); let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo()); ( diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 6d944e51314..e8cd7eaa60f 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -332,7 +332,7 @@ pub trait Emitter: Translate { // Skip past non-macro entries, just in case there // are some which do actually involve macros. - ExpnKind::Inlined | ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None, + ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None, ExpnKind::Macro(macro_kind, name) => Some((macro_kind, name)), } @@ -403,7 +403,7 @@ pub trait Emitter: Translate { continue; } - if always_backtrace && !matches!(trace.kind, ExpnKind::Inlined) { + if always_backtrace { new_labels.push(( trace.def_site, format!( @@ -442,7 +442,6 @@ pub trait Emitter: Translate { "this derive macro expansion".into() } ExpnKind::Macro(MacroKind::Bang, _) => "this macro invocation".into(), - ExpnKind::Inlined => "this inlined function call".into(), ExpnKind::Root => "the crate root".into(), ExpnKind::AstPass(kind) => kind.descr().into(), ExpnKind::Desugaring(kind) => { diff --git a/compiler/rustc_middle/src/lint.rs b/compiler/rustc_middle/src/lint.rs index c266584ac28..14343ac1108 100644 --- a/compiler/rustc_middle/src/lint.rs +++ b/compiler/rustc_middle/src/lint.rs @@ -468,8 +468,7 @@ pub fn struct_lint_level( pub fn in_external_macro(sess: &Session, span: Span) -> bool { let expn_data = span.ctxt().outer_expn_data(); match expn_data.kind { - ExpnKind::Inlined - | ExpnKind::Root + ExpnKind::Root | ExpnKind::Desugaring( DesugaringKind::ForLoop | DesugaringKind::WhileLoop | DesugaringKind::OpaqueTy, ) => false, diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 73f435d4840..a8d0dca37ff 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -2488,9 +2488,7 @@ impl<'tcx> TyCtxt<'tcx> { && if self.features().collapse_debuginfo { span.in_macro_expansion_with_collapse_debuginfo() } else { - // Inlined spans should not be collapsed as that leads to all of the - // inlined code being attributed to the inline callsite. - span.from_expansion() && !span.is_inlined() + span.from_expansion() } } diff --git a/compiler/rustc_mir_transform/src/inline.rs b/compiler/rustc_mir_transform/src/inline.rs index 6c2e22a70b9..1748b1bf4a0 100644 --- a/compiler/rustc_mir_transform/src/inline.rs +++ b/compiler/rustc_mir_transform/src/inline.rs @@ -10,7 +10,6 @@ use rustc_middle::mir::*; use rustc_middle::ty::TypeVisitableExt; use rustc_middle::ty::{self, Instance, InstanceDef, ParamEnv, Ty, TyCtxt}; use rustc_session::config::OptLevel; -use rustc_span::{hygiene::ExpnKind, ExpnData, LocalExpnId, Span}; use rustc_target::abi::{FieldIdx, FIRST_VARIANT}; use rustc_target::spec::abi::Abi; @@ -551,16 +550,6 @@ impl<'tcx> Inliner<'tcx> { // Copy the arguments if needed. let args: Vec<_> = self.make_call_args(args, &callsite, caller_body, &callee_body); - let mut expn_data = ExpnData::default( - ExpnKind::Inlined, - callsite.source_info.span, - self.tcx.sess.edition(), - None, - None, - ); - expn_data.def_site = callee_body.span; - let expn_data = - self.tcx.with_stable_hashing_context(|hcx| LocalExpnId::fresh(expn_data, hcx)); let mut integrator = Integrator { args: &args, new_locals: Local::new(caller_body.local_decls.len()).., @@ -572,7 +561,6 @@ impl<'tcx> Inliner<'tcx> { cleanup_block: unwind, in_cleanup_block: false, tcx: self.tcx, - expn_data, always_live_locals: BitSet::new_filled(callee_body.local_decls.len()), }; @@ -956,7 +944,6 @@ struct Integrator<'a, 'tcx> { cleanup_block: UnwindAction, in_cleanup_block: bool, tcx: TyCtxt<'tcx>, - expn_data: LocalExpnId, always_live_locals: BitSet, } @@ -1042,11 +1029,6 @@ impl<'tcx> MutVisitor<'tcx> for Integrator<'_, '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 = span.fresh_expansion(self.expn_data); - } - fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) { self.in_cleanup_block = data.is_cleanup; self.super_basic_block_data(block, data); diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index c669b64dd2c..6755657c727 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -320,7 +320,6 @@ impl ExpnId { // Stop going up the backtrace once include! is encountered if expn_data.is_root() || expn_data.kind == ExpnKind::Macro(MacroKind::Bang, sym::include) - || expn_data.kind == ExpnKind::Inlined { break; } @@ -1058,8 +1057,6 @@ pub enum ExpnKind { AstPass(AstPass), /// Desugaring done by the compiler during HIR lowering. Desugaring(DesugaringKind), - /// MIR inlining - Inlined, } impl ExpnKind { @@ -1073,7 +1070,6 @@ impl ExpnKind { }, ExpnKind::AstPass(kind) => kind.descr().to_string(), ExpnKind::Desugaring(kind) => format!("desugaring of {}", kind.descr()), - ExpnKind::Inlined => "inlined source".to_string(), } } } diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 3d3833dfdab..eae3f0fa041 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -594,12 +594,6 @@ impl Span { matches!(outer_expn.kind, ExpnKind::Macro(..)) && outer_expn.collapse_debuginfo } - /// Returns `true` if this span comes from MIR inlining. - pub fn is_inlined(self) -> bool { - let outer_expn = self.ctxt().outer_expn_data(); - matches!(outer_expn.kind, ExpnKind::Inlined) - } - /// Returns `true` if `span` originates in a derive-macro's expansion. pub fn in_derive_expansion(self) -> bool { matches!(self.ctxt().outer_expn_data().kind, ExpnKind::Macro(MacroKind::Derive, _)) From 3a423c3feb9b4827ff8b3dd1e18307df700320f2 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 24 May 2023 21:12:30 +0000 Subject: [PATCH 2/2] Manually add inlined frames in the interpreter stacktrace. --- .../src/interpret/eval_context.rs | 15 ++++++++++++++- src/tools/miri/tests/fail/terminate-terminator.rs | 2 +- .../miri/tests/fail/terminate-terminator.stderr | 11 +++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/eval_context.rs b/compiler/rustc_const_eval/src/interpret/eval_context.rs index 040eba10eb4..7e94578003e 100644 --- a/compiler/rustc_const_eval/src/interpret/eval_context.rs +++ b/compiler/rustc_const_eval/src/interpret/eval_context.rs @@ -949,7 +949,20 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // This deliberately does *not* honor `requires_caller_location` since it is used for much // more than just panics. for frame in stack.iter().rev() { - let span = frame.current_span(); + let span = match frame.loc { + Left(loc) => { + // If the stacktrace passes through MIR-inlined source scopes, add them. + let mir::SourceInfo { mut span, scope } = *frame.body.source_info(loc); + let mut scope_data = &frame.body.source_scopes[scope]; + while let Some((instance, call_span)) = scope_data.inlined { + frames.push(FrameInfo { span, instance }); + span = call_span; + scope_data = &frame.body.source_scopes[scope_data.parent_scope.unwrap()]; + } + span + } + Right(span) => span, + }; frames.push(FrameInfo { span, instance: frame.instance }); } trace!("generate stacktrace: {:#?}", frames); diff --git a/src/tools/miri/tests/fail/terminate-terminator.rs b/src/tools/miri/tests/fail/terminate-terminator.rs index 22ffa1b2711..b9199cff079 100644 --- a/src/tools/miri/tests/fail/terminate-terminator.rs +++ b/src/tools/miri/tests/fail/terminate-terminator.rs @@ -12,13 +12,13 @@ impl Drop for Foo { #[inline(always)] fn has_cleanup() { + //~^ ERROR: panic in a function that cannot unwind let _f = Foo; panic!(); } extern "C" fn panic_abort() { has_cleanup(); - //~^ ERROR: panic in a function that cannot unwind } fn main() { diff --git a/src/tools/miri/tests/fail/terminate-terminator.stderr b/src/tools/miri/tests/fail/terminate-terminator.stderr index 8ce4bb7cbb5..d73e23a53d0 100644 --- a/src/tools/miri/tests/fail/terminate-terminator.stderr +++ b/src/tools/miri/tests/fail/terminate-terminator.stderr @@ -6,15 +6,18 @@ error: abnormal termination: panic in a function that cannot unwind --> $DIR/terminate-terminator.rs:LL:CC | LL | / fn has_cleanup() { +LL | | LL | | let _f = Foo; LL | | panic!(); LL | | } | |_^ panic in a function that cannot unwind -... -LL | has_cleanup(); - | ------------- in this inlined function call | - = note: inside `panic_abort` at $DIR/terminate-terminator.rs:LL:CC + = note: inside `has_cleanup` at $DIR/terminate-terminator.rs:LL:CC +note: inside `panic_abort` + --> $DIR/terminate-terminator.rs:LL:CC + | +LL | has_cleanup(); + | ^^^^^^^^^^^^^ note: inside `main` --> $DIR/terminate-terminator.rs:LL:CC |