Skip to content

Commit a1a583b

Browse files
committed
stop unnecessarily passing around span argument for Miri function calls
1 parent abe143a commit a1a583b

File tree

6 files changed

+25
-42
lines changed

6 files changed

+25
-42
lines changed

src/librustc_mir/const_eval/machine.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use std::hash::Hash;
88
use rustc_data_structures::fx::FxHashMap;
99

1010
use rustc_ast::ast::Mutability;
11+
use rustc_hir::def_id::DefId;
1112
use rustc_middle::mir::AssertMessage;
1213
use rustc_span::symbol::Symbol;
13-
use rustc_span::{def_id::DefId, Span};
1414

1515
use crate::interpret::{
1616
self, AllocId, Allocation, GlobalId, ImmTy, InterpCx, InterpResult, Memory, MemoryKind, OpTy,
@@ -64,7 +64,6 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter> {
6464
/// If this returns successfully (`Ok`), the function should just be evaluated normally.
6565
fn hook_panic_fn(
6666
&mut self,
67-
span: Span,
6867
instance: ty::Instance<'tcx>,
6968
args: &[OpTy<'tcx>],
7069
) -> InterpResult<'tcx> {
@@ -77,7 +76,7 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter> {
7776

7877
let msg_place = self.deref_operand(args[0])?;
7978
let msg = Symbol::intern(self.read_str(msg_place)?);
80-
let span = self.find_closest_untracked_caller_location().unwrap_or(span);
79+
let span = self.find_closest_untracked_caller_location();
8180
let (file, line, col) = self.location_triple_for_span(span);
8281
Err(ConstEvalErrKind::Panic { msg, file, line, col }.into())
8382
} else {
@@ -191,7 +190,6 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
191190

192191
fn find_mir_or_eval_fn(
193192
ecx: &mut InterpCx<'mir, 'tcx, Self>,
194-
span: Span,
195193
instance: ty::Instance<'tcx>,
196194
args: &[OpTy<'tcx>],
197195
ret: Option<(PlaceTy<'tcx>, mir::BasicBlock)>,
@@ -213,7 +211,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
213211
} else {
214212
// Some functions we support even if they are non-const -- but avoid testing
215213
// that for const fn!
216-
ecx.hook_panic_fn(span, instance, args)?;
214+
ecx.hook_panic_fn(instance, args)?;
217215
// We certainly do *not* want to actually call the fn
218216
// though, so be sure we return here.
219217
throw_unsup_format!("calling non-const function `{}`", instance)
@@ -248,13 +246,12 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
248246

249247
fn call_intrinsic(
250248
ecx: &mut InterpCx<'mir, 'tcx, Self>,
251-
span: Span,
252249
instance: ty::Instance<'tcx>,
253250
args: &[OpTy<'tcx>],
254251
ret: Option<(PlaceTy<'tcx>, mir::BasicBlock)>,
255252
_unwind: Option<mir::BasicBlock>,
256253
) -> InterpResult<'tcx> {
257-
if ecx.emulate_intrinsic(span, instance, args, ret)? {
254+
if ecx.emulate_intrinsic(instance, args, ret)? {
258255
return Ok(());
259256
}
260257
// An intrinsic that we do not support

src/librustc_mir/interpret/intrinsics.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc_middle::ty::layout::{LayoutOf, Primitive, Size};
1515
use rustc_middle::ty::subst::SubstsRef;
1616
use rustc_middle::ty::TyCtxt;
1717
use rustc_span::symbol::{sym, Symbol};
18-
use rustc_span::Span;
1918

2019
use super::{ImmTy, InterpCx, Machine, OpTy, PlaceTy};
2120

@@ -78,7 +77,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
7877
/// Returns `true` if emulation happened.
7978
pub fn emulate_intrinsic(
8079
&mut self,
81-
span: Span,
8280
instance: ty::Instance<'tcx>,
8381
args: &[OpTy<'tcx, M::PointerTag>],
8482
ret: Option<(PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>,
@@ -101,7 +99,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
10199
// `src/librustc_middle/ty/constness.rs`
102100
match intrinsic_name {
103101
sym::caller_location => {
104-
let span = self.find_closest_untracked_caller_location().unwrap_or(span);
102+
let span = self.find_closest_untracked_caller_location();
105103
let location = self.alloc_caller_location_for_span(span);
106104
self.write_scalar(location.ptr, dest)?;
107105
}
@@ -118,7 +116,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
118116
sym::needs_drop => self.tcx.types.bool,
119117
sym::type_id => self.tcx.types.u64,
120118
sym::type_name => self.tcx.mk_static_str(),
121-
_ => span_bug!(span, "Already checked for nullary intrinsics"),
119+
_ => bug!("already checked for nullary intrinsics"),
122120
};
123121
let val = self.const_eval(gid, ty)?;
124122
self.copy_op(val, dest)?;

src/librustc_mir/interpret/intrinsics/caller_location.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
1414
/// Walks up the callstack from the intrinsic's callsite, searching for the first callsite in a
1515
/// frame which is not `#[track_caller]`. If the first frame found lacks `#[track_caller]`, then
1616
/// `None` is returned and the callsite of the function invocation itself should be used.
17-
crate fn find_closest_untracked_caller_location(&self) -> Option<Span> {
18-
let mut caller_span = None;
19-
for next_caller in self.stack.iter().rev() {
20-
caller_span = next_caller.current_source_info().map(|si| si.span).or_else(|| caller_span);
21-
if !next_caller.instance.def.requires_caller_location(*self.tcx) {
22-
return caller_span;
23-
}
24-
}
25-
26-
caller_span
17+
crate fn find_closest_untracked_caller_location(&self) -> Span {
18+
self.stack
19+
.iter()
20+
.rev()
21+
// Skip `#[track_caller]` frames.
22+
.skip_while(|frame| frame.instance.def.requires_caller_location(*self.tcx))
23+
// Find next frame with source info.
24+
.find_map(|frame| frame.current_source_info())
25+
.map(|si| si.span)
26+
// Fallback to current frame. That one has to have source_info as only
27+
// currently unwinding frames without cleanup do *not* have it -- and those
28+
// frames do not call this intrinsic.
29+
.unwrap_or_else(|| self.frame().current_source_info().unwrap().span)
2730
}
2831

2932
/// Allocate a `const core::panic::Location` with the provided filename and line/column numbers.

src/librustc_mir/interpret/machine.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::hash::Hash;
77

88
use rustc_middle::mir;
99
use rustc_middle::ty::{self, Ty};
10-
use rustc_span::{def_id::DefId, Span};
10+
use rustc_span::def_id::DefId;
1111

1212
use super::{
1313
AllocId, Allocation, AllocationExtra, Frame, ImmTy, InterpCx, InterpResult, Memory, MemoryKind,
@@ -135,7 +135,6 @@ pub trait Machine<'mir, 'tcx>: Sized {
135135
/// was used.
136136
fn find_mir_or_eval_fn(
137137
ecx: &mut InterpCx<'mir, 'tcx, Self>,
138-
span: Span,
139138
instance: ty::Instance<'tcx>,
140139
args: &[OpTy<'tcx, Self::PointerTag>],
141140
ret: Option<(PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>,
@@ -156,7 +155,6 @@ pub trait Machine<'mir, 'tcx>: Sized {
156155
/// responsibility to advance the instruction pointer as appropriate.
157156
fn call_intrinsic(
158157
ecx: &mut InterpCx<'mir, 'tcx, Self>,
159-
span: Span,
160158
instance: ty::Instance<'tcx>,
161159
args: &[OpTy<'tcx, Self::PointerTag>],
162160
ret: Option<(PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>,

src/librustc_mir/interpret/terminator.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::convert::TryFrom;
44
use rustc_middle::ty::layout::{self, LayoutOf, TyAndLayout};
55
use rustc_middle::ty::Instance;
66
use rustc_middle::{mir, ty};
7-
use rustc_span::source_map::Span;
87
use rustc_target::spec::abi::Abi;
98

109
use super::{
@@ -71,14 +70,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
7170
Some((dest, ret)) => Some((self.eval_place(dest)?, *ret)),
7271
None => None,
7372
};
74-
self.eval_fn_call(
75-
fn_val,
76-
terminator.source_info.span,
77-
abi,
78-
&args[..],
79-
ret,
80-
*cleanup,
81-
)?;
73+
self.eval_fn_call(fn_val, abi, &args[..], ret, *cleanup)?;
8274
}
8375

8476
Drop { ref location, target, unwind } => {
@@ -88,7 +80,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
8880
trace!("TerminatorKind::drop: {:?}, type {}", location, ty);
8981

9082
let instance = Instance::resolve_drop_in_place(*self.tcx, ty);
91-
self.drop_in_place(place, instance, terminator.source_info.span, target, unwind)?;
83+
self.drop_in_place(place, instance, target, unwind)?;
9284
}
9385

9486
Assert { ref cond, expected, ref msg, target, cleanup } => {
@@ -196,7 +188,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
196188
fn eval_fn_call(
197189
&mut self,
198190
fn_val: FnVal<'tcx, M::ExtraFnVal>,
199-
span: Span,
200191
caller_abi: Abi,
201192
args: &[OpTy<'tcx, M::PointerTag>],
202193
ret: Option<(PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>,
@@ -242,7 +233,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
242233
match instance.def {
243234
ty::InstanceDef::Intrinsic(..) => {
244235
assert!(caller_abi == Abi::RustIntrinsic || caller_abi == Abi::PlatformIntrinsic);
245-
M::call_intrinsic(self, span, instance, args, ret, unwind)
236+
M::call_intrinsic(self, instance, args, ret, unwind)
246237
}
247238
ty::InstanceDef::VtableShim(..)
248239
| ty::InstanceDef::ReifyShim(..)
@@ -252,7 +243,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
252243
| ty::InstanceDef::CloneShim(..)
253244
| ty::InstanceDef::Item(_) => {
254245
// We need MIR for this fn
255-
let body = match M::find_mir_or_eval_fn(self, span, instance, args, ret, unwind)? {
246+
let body = match M::find_mir_or_eval_fn(self, instance, args, ret, unwind)? {
256247
Some(body) => body,
257248
None => return Ok(()),
258249
};
@@ -406,7 +397,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
406397
OpTy::from(ImmTy { layout: this_receiver_ptr, imm: receiver_place.ptr.into() });
407398
trace!("Patched self operand to {:#?}", args[0]);
408399
// recurse with concrete function
409-
self.eval_fn_call(drop_fn, span, caller_abi, &args, ret, unwind)
400+
self.eval_fn_call(drop_fn, caller_abi, &args, ret, unwind)
410401
}
411402
}
412403
}
@@ -415,7 +406,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
415406
&mut self,
416407
place: PlaceTy<'tcx, M::PointerTag>,
417408
instance: ty::Instance<'tcx>,
418-
span: Span,
419409
target: mir::BasicBlock,
420410
unwind: Option<mir::BasicBlock>,
421411
) -> InterpResult<'tcx> {
@@ -443,7 +433,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
443433

444434
self.eval_fn_call(
445435
FnVal::Instance(instance),
446-
span,
447436
Abi::Rust,
448437
&[arg.into()],
449438
Some((dest.into(), target)),

src/librustc_mir/transform/const_prop.rs

-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
183183

184184
fn find_mir_or_eval_fn(
185185
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
186-
_span: Span,
187186
_instance: ty::Instance<'tcx>,
188187
_args: &[OpTy<'tcx>],
189188
_ret: Option<(PlaceTy<'tcx>, BasicBlock)>,
@@ -204,7 +203,6 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
204203

205204
fn call_intrinsic(
206205
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
207-
_span: Span,
208206
_instance: ty::Instance<'tcx>,
209207
_args: &[OpTy<'tcx>],
210208
_ret: Option<(PlaceTy<'tcx>, BasicBlock)>,

0 commit comments

Comments
 (0)