Skip to content

Commit 2210abe

Browse files
committed
keep root_span and tcx together
1 parent c6512fd commit 2210abe

File tree

10 files changed

+60
-72
lines changed

10 files changed

+60
-72
lines changed

src/librustc_mir/const_eval/eval_queries.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
2727
body: &'mir mir::Body<'tcx>,
2828
) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
2929
debug!("eval_body_using_ecx: {:?}, {:?}", cid, ecx.param_env);
30-
let tcx = ecx.tcx;
30+
let tcx = *ecx.tcx;
3131
let layout = ecx.layout_of(body.return_ty().subst(tcx, cid.instance.substs))?;
3232
assert!(!layout.is_unsized());
3333
let ret = ecx.allocate(layout, MemoryKind::Stack);
@@ -214,7 +214,7 @@ fn validate_and_turn_into_const<'tcx>(
214214

215215
val.map_err(|error| {
216216
let err = error_to_const_error(&ecx, error, None);
217-
err.struct_error(ecx.tcx_at(), "it is undefined behavior to use this value", |mut diag| {
217+
err.struct_error(ecx.tcx, "it is undefined behavior to use this value", |mut diag| {
218218
diag.note(note_on_undefined_behavior_error());
219219
diag.emit();
220220
})

src/librustc_mir/interpret/cast.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
5656
}
5757

5858
let instance = ty::Instance::resolve_for_fn_ptr(
59-
self.tcx,
59+
*self.tcx,
6060
self.param_env,
6161
def_id,
6262
substs,
@@ -91,7 +91,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
9191
}
9292

9393
let instance = ty::Instance::resolve_closure(
94-
self.tcx,
94+
*self.tcx,
9595
def_id,
9696
substs,
9797
ty::ClosureKind::FnOnce,
@@ -140,7 +140,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
140140
// Handle cast from a univariant (ZST) enum.
141141
match src.layout.variants {
142142
Variants::Single { index } => {
143-
if let Some(discr) = src.layout.ty.discriminant_for_variant(self.tcx, index) {
143+
if let Some(discr) = src.layout.ty.discriminant_for_variant(*self.tcx, index) {
144144
assert!(src.layout.is_zst());
145145
let discr_layout = self.layout_of(discr.ty)?;
146146
return Ok(self.cast_from_scalar(discr.val, discr_layout, cast_ty).into());
@@ -269,7 +269,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
269269
let ptr = self.read_immediate(src)?.to_scalar()?;
270270
// u64 cast is from usize to u64, which is always good
271271
let val =
272-
Immediate::new_slice(ptr, length.eval_usize(self.tcx, self.param_env), self);
272+
Immediate::new_slice(ptr, length.eval_usize(*self.tcx, self.param_env), self);
273273
self.write_immediate(val, dest)
274274
}
275275
(&ty::Dynamic(..), &ty::Dynamic(..)) => {

src/librustc_mir/interpret/eval_context.rs

+18-29
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ pub struct InterpCx<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
3333
pub machine: M,
3434

3535
/// The results of the type checker, from rustc.
36-
pub tcx: TyCtxt<'tcx>,
37-
38-
/// The span of the "root" of the evaluation, i.e., the const
36+
/// The span in this is the "root" of the evaluation, i.e., the const
3937
/// we are evaluating (if this is CTFE).
40-
pub(super) root_span: Span,
38+
pub tcx: TyCtxtAt<'tcx>,
4139

4240
/// Bounds in scope for polymorphic evaluations.
4341
pub(crate) param_env: ty::ParamEnv<'tcx>,
@@ -200,7 +198,7 @@ where
200198
{
201199
#[inline]
202200
fn tcx(&self) -> TyCtxt<'tcx> {
203-
self.tcx
201+
*self.tcx
204202
}
205203
}
206204

@@ -219,7 +217,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> LayoutOf for InterpCx<'mir, 'tcx,
219217

220218
#[inline]
221219
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
222-
self.tcx_at()
220+
self.tcx
223221
.layout_of(self.param_env.and(ty))
224222
.map_err(|layout| err_inval!(Layout(layout)).into())
225223
}
@@ -304,8 +302,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
304302
) -> Self {
305303
InterpCx {
306304
machine,
307-
tcx,
308-
root_span,
305+
tcx: tcx.at(root_span),
309306
param_env,
310307
memory: Memory::new(tcx, memory_extra),
311308
vtables: FxHashMap::default(),
@@ -318,14 +315,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
318315
.last()
319316
.and_then(|f| f.current_source_info())
320317
.map(|si| si.span)
321-
.unwrap_or(self.root_span)
322-
}
323-
324-
#[inline(always)]
325-
pub fn tcx_at(&self) -> TyCtxtAt<'tcx> {
326-
// Computing the current span has a non-trivial cost, and for cycle errors
327-
// the "root span" is good enough.
328-
self.tcx.at(self.root_span)
318+
.unwrap_or(self.tcx.span)
329319
}
330320

331321
#[inline(always)]
@@ -403,12 +393,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
403393

404394
#[inline]
405395
pub fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
406-
ty.is_sized(self.tcx_at(), self.param_env)
396+
ty.is_sized(self.tcx, self.param_env)
407397
}
408398

409399
#[inline]
410400
pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
411-
ty.is_freeze(self.tcx, self.param_env, self.root_span)
401+
ty.is_freeze(*self.tcx, self.param_env, self.tcx.span)
412402
}
413403

414404
pub fn load_mir(
@@ -419,21 +409,20 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
419409
// do not continue if typeck errors occurred (can only occur in local crate)
420410
let did = instance.def_id();
421411
if let Some(did) = did.as_local() {
422-
if self.tcx_at().has_typeck_tables(did) {
423-
if let Some(error_reported) = self.tcx_at().typeck_tables_of(did).tainted_by_errors
424-
{
412+
if self.tcx.has_typeck_tables(did) {
413+
if let Some(error_reported) = self.tcx.typeck_tables_of(did).tainted_by_errors {
425414
throw_inval!(TypeckError(error_reported))
426415
}
427416
}
428417
}
429418
trace!("load mir(instance={:?}, promoted={:?})", instance, promoted);
430419
if let Some(promoted) = promoted {
431-
return Ok(&self.tcx_at().promoted_mir(did)[promoted]);
420+
return Ok(&self.tcx.promoted_mir(did)[promoted]);
432421
}
433422
match instance {
434423
ty::InstanceDef::Item(def_id) => {
435-
if self.tcx_at().is_mir_available(did) {
436-
Ok(self.tcx_at().optimized_mir(did))
424+
if self.tcx.is_mir_available(did) {
425+
Ok(self.tcx.optimized_mir(did))
437426
} else {
438427
throw_unsup!(NoMirFor(def_id))
439428
}
@@ -474,7 +463,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
474463
trace!("resolve: {:?}, {:#?}", def_id, substs);
475464
trace!("param_env: {:#?}", self.param_env);
476465
trace!("substs: {:#?}", substs);
477-
match ty::Instance::resolve(self.tcx, self.param_env, def_id, substs) {
466+
match ty::Instance::resolve(*self.tcx, self.param_env, def_id, substs) {
478467
Ok(Some(instance)) => Ok(instance),
479468
Ok(None) => throw_inval!(TooGeneric),
480469

@@ -493,7 +482,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
493482
// have to support that case (mostly by skipping all caching).
494483
match frame.locals.get(local).and_then(|state| state.layout.get()) {
495484
None => {
496-
let layout = from_known_layout(self.tcx_at(), layout, || {
485+
let layout = from_known_layout(self.tcx, layout, || {
497486
let local_ty = frame.body.local_decls[local].ty;
498487
let local_ty =
499488
self.subst_from_frame_and_normalize_erasing_regions(frame, local_ty);
@@ -645,7 +634,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
645634
let mut locals = IndexVec::from_elem(dummy, &body.local_decls);
646635

647636
// Now mark those locals as dead that we do not want to initialize
648-
match self.tcx_at().def_kind(instance.def_id()) {
637+
match self.tcx.def_kind(instance.def_id()) {
649638
// statics and constants don't have `Storage*` statements, no need to look for them
650639
//
651640
// FIXME: The above is likely untrue. See
@@ -860,7 +849,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
860849
} else {
861850
self.param_env
862851
};
863-
let val = self.tcx.const_eval_global_id(param_env, gid, Some(self.root_span))?;
852+
let val = self.tcx.const_eval_global_id(param_env, gid, Some(self.tcx.span))?;
864853

865854
// Even though `ecx.const_eval` is called from `eval_const_to_op` we can never have a
866855
// recursion deeper than one level, because the `tcx.const_eval` above is guaranteed to not
@@ -891,7 +880,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
891880
// FIXME: We can hit delay_span_bug if this is an invalid const, interning finds
892881
// that problem, but we never run validation to show an error. Can we ensure
893882
// this does not happen?
894-
let val = self.tcx_at().const_eval_raw(param_env.and(gid))?;
883+
let val = self.tcx.const_eval_raw(param_env.and(gid))?;
895884
self.raw_const_to_mplace(val)
896885
}
897886

src/librustc_mir/interpret/intern.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx>>(
9393
// in the value the dangling reference lies.
9494
// The `delay_span_bug` ensures that we don't forget such a check in validation.
9595
if tcx.get_global_alloc(alloc_id).is_none() {
96-
tcx.sess.delay_span_bug(ecx.root_span, "tried to intern dangling pointer");
96+
tcx.sess.delay_span_bug(ecx.tcx.span, "tried to intern dangling pointer");
9797
}
9898
// treat dangling pointers like other statics
9999
// just to stop trying to recurse into them
@@ -111,7 +111,7 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx>>(
111111
if let InternMode::Static(mutability) = mode {
112112
// For this, we need to take into account `UnsafeCell`. When `ty` is `None`, we assume
113113
// no interior mutability.
114-
let frozen = ty.map_or(true, |ty| ty.is_freeze(ecx.tcx, ecx.param_env, ecx.root_span));
114+
let frozen = ty.map_or(true, |ty| ty.is_freeze(*ecx.tcx, ecx.param_env, ecx.tcx.span));
115115
// For statics, allocation mutability is the combination of the place mutability and
116116
// the type mutability.
117117
// The entire allocation needs to be mutable if it contains an `UnsafeCell` anywhere.
@@ -174,7 +174,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx>> ValueVisitor<'mir
174174
// they caused. It also helps us to find cases where const-checking
175175
// failed to prevent an `UnsafeCell` (but as `ignore_interior_mut_in_const`
176176
// shows that part is not airtight).
177-
mutable_memory_in_const(self.ecx.tcx_at(), "`UnsafeCell`");
177+
mutable_memory_in_const(self.ecx.tcx, "`UnsafeCell`");
178178
}
179179
// We are crossing over an `UnsafeCell`, we can mutate again. This means that
180180
// References we encounter inside here are interned as pointing to mutable
@@ -192,7 +192,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx>> ValueVisitor<'mir
192192
fn visit_value(&mut self, mplace: MPlaceTy<'tcx>) -> InterpResult<'tcx> {
193193
// Handle Reference types, as these are the only relocations supported by const eval.
194194
// Raw pointers (and boxes) are handled by the `leftover_relocations` logic.
195-
let tcx = self.ecx.tcx.at(self.ecx.root_span);
195+
let tcx = self.ecx.tcx;
196196
let ty = mplace.layout.ty;
197197
if let ty::Ref(_, referenced_ty, ref_mutability) = ty.kind {
198198
let value = self.ecx.read_immediate(mplace.into())?;
@@ -253,8 +253,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx>> ValueVisitor<'mir
253253
// caused (by somehow getting a mutable reference in a `const`).
254254
if ref_mutability == Mutability::Mut {
255255
match referenced_ty.kind {
256-
ty::Array(_, n)
257-
if n.eval_usize(self.ecx.tcx, self.ecx.param_env) == 0 => {}
256+
ty::Array(_, n) if n.eval_usize(*tcx, self.ecx.param_env) == 0 => {}
258257
ty::Slice(_)
259258
if mplace.meta.unwrap_meta().to_machine_usize(self.ecx)?
260259
== 0 => {}
@@ -358,7 +357,7 @@ pub fn intern_const_alloc_recursive<M: CompileTimeMachine<'mir, 'tcx>>(
358357
Ok(()) => {}
359358
Err(error) => {
360359
ecx.tcx.sess.delay_span_bug(
361-
ecx.root_span,
360+
ecx.tcx.span,
362361
"error during interning should later cause validation failure",
363362
);
364363
// Some errors shouldn't come up because creating them causes
@@ -407,7 +406,7 @@ pub fn intern_const_alloc_recursive<M: CompileTimeMachine<'mir, 'tcx>>(
407406
// such as `const CONST_RAW: *const Vec<i32> = &Vec::new() as *const _;`.
408407
ecx.tcx
409408
.sess
410-
.span_err(ecx.root_span, "untyped pointers are not allowed in constant");
409+
.span_err(ecx.tcx.span, "untyped pointers are not allowed in constant");
411410
// For better errors later, mark the allocation as immutable.
412411
alloc.mutability = Mutability::Not;
413412
}
@@ -422,11 +421,11 @@ pub fn intern_const_alloc_recursive<M: CompileTimeMachine<'mir, 'tcx>>(
422421
} else if ecx.memory.dead_alloc_map.contains_key(&alloc_id) {
423422
// Codegen does not like dangling pointers, and generally `tcx` assumes that
424423
// all allocations referenced anywhere actually exist. So, make sure we error here.
425-
ecx.tcx.sess.span_err(ecx.root_span, "encountered dangling pointer in final constant");
424+
ecx.tcx.sess.span_err(ecx.tcx.span, "encountered dangling pointer in final constant");
426425
} else if ecx.tcx.get_global_alloc(alloc_id).is_none() {
427426
// We have hit an `AllocId` that is neither in local or global memory and isn't
428427
// marked as dangling by local memory. That should be impossible.
429-
span_bug!(ecx.root_span, "encountered unknown alloc id {:?}", alloc_id);
428+
span_bug!(ecx.tcx.span, "encountered unknown alloc id {:?}", alloc_id);
430429
}
431430
}
432431
}

src/librustc_mir/interpret/intrinsics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
347347
let index = u64::from(self.read_scalar(args[1])?.to_u32()?);
348348
let elem = args[2];
349349
let input = args[0];
350-
let (len, e_ty) = input.layout.ty.simd_size_and_type(self.tcx);
350+
let (len, e_ty) = input.layout.ty.simd_size_and_type(*self.tcx);
351351
assert!(
352352
index < len,
353353
"Index `{}` must be in bounds of vector type `{}`: `[0, {})`",
@@ -374,7 +374,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
374374
}
375375
sym::simd_extract => {
376376
let index = u64::from(self.read_scalar(args[1])?.to_u32()?);
377-
let (len, e_ty) = args[0].layout.ty.simd_size_and_type(self.tcx);
377+
let (len, e_ty) = args[0].layout.ty.simd_size_and_type(*self.tcx);
378378
assert!(
379379
index < len,
380380
"index `{}` is out-of-bounds of vector type `{}` with length `{}`",

src/librustc_mir/interpret/intrinsics/caller_location.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
2525
"find_closest_untracked_caller_location: checking frame {:?}",
2626
frame.instance
2727
);
28-
!frame.instance.def.requires_caller_location(self.tcx)
28+
!frame.instance.def.requires_caller_location(*self.tcx)
2929
})
3030
// Assert that there is always such a frame.
3131
.unwrap();
@@ -58,7 +58,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
5858
let loc_ty = self
5959
.tcx
6060
.type_of(self.tcx.require_lang_item(PanicLocationLangItem, None))
61-
.subst(self.tcx, self.tcx.mk_substs([self.tcx.lifetimes.re_erased.into()].iter()));
61+
.subst(*self.tcx, self.tcx.mk_substs([self.tcx.lifetimes.re_erased.into()].iter()));
6262
let loc_layout = self.layout_of(loc_ty).unwrap();
6363
let location = self.allocate(loc_layout, MemoryKind::CallerLocation);
6464

0 commit comments

Comments
 (0)