diff --git a/compiler/rustc_const_eval/src/interpret/eval_context.rs b/compiler/rustc_const_eval/src/interpret/eval_context.rs index a82ddfb5ac5..85de5908a23 100644 --- a/compiler/rustc_const_eval/src/interpret/eval_context.rs +++ b/compiler/rustc_const_eval/src/interpret/eval_context.rs @@ -926,7 +926,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { self.param_env }; let param_env = param_env.with_const(); - let val = self.tcx.eval_to_allocation_raw(param_env.and(gid))?; + // Use a precise span for better cycle errors. + let val = self.tcx.at(self.cur_span()).eval_to_allocation_raw(param_env.and(gid))?; self.raw_const_to_mplace(val) } diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs index 9c032c55fe5..17b8ab8742a 100644 --- a/compiler/rustc_const_eval/src/interpret/memory.rs +++ b/compiler/rustc_const_eval/src/interpret/memory.rs @@ -504,7 +504,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { throw_unsup!(ReadExternStatic(def_id)); } - (self.tcx.eval_static_initializer(def_id)?, Some(def_id)) + // Use a precise span for better cycle errors. + (self.tcx.at(self.cur_span()).eval_static_initializer(def_id)?, Some(def_id)) } }; M::before_access_global(*self.tcx, &self.machine, id, alloc, def_id, is_write)?; diff --git a/compiler/rustc_const_eval/src/interpret/step.rs b/compiler/rustc_const_eval/src/interpret/step.rs index c511581371b..98f69456e49 100644 --- a/compiler/rustc_const_eval/src/interpret/step.rs +++ b/compiler/rustc_const_eval/src/interpret/step.rs @@ -55,7 +55,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { }; let basic_block = &self.body().basic_blocks()[loc.block]; - if let Some(stmt) = basic_block.statements.get(loc.statement_index) { let old_frames = self.frame_idx(); self.statement(stmt)?; diff --git a/compiler/rustc_middle/src/mir/interpret/queries.rs b/compiler/rustc_middle/src/mir/interpret/queries.rs index 5fb8e911124..8fc957cf49c 100644 --- a/compiler/rustc_middle/src/mir/interpret/queries.rs +++ b/compiler/rustc_middle/src/mir/interpret/queries.rs @@ -3,9 +3,9 @@ use super::{ErrorHandled, EvalToConstValueResult, GlobalId}; use crate::mir; use crate::ty::fold::TypeFoldable; use crate::ty::subst::InternalSubsts; -use crate::ty::{self, TyCtxt}; +use crate::ty::{self, query::TyCtxtAt, TyCtxt}; use rustc_hir::def_id::DefId; -use rustc_span::Span; +use rustc_span::{Span, DUMMY_SP}; impl<'tcx> TyCtxt<'tcx> { /// Evaluates a constant without providing any substitutions. This is useful to evaluate consts @@ -86,6 +86,17 @@ impl<'tcx> TyCtxt<'tcx> { } } + /// Evaluate a static's initializer, returning the allocation of the initializer's memory. + #[inline(always)] + pub fn eval_static_initializer( + self, + def_id: DefId, + ) -> Result<mir::ConstAllocation<'tcx>, ErrorHandled> { + self.at(DUMMY_SP).eval_static_initializer(def_id) + } +} + +impl<'tcx> TyCtxtAt<'tcx> { /// Evaluate a static's initializer, returning the allocation of the initializer's memory. pub fn eval_static_initializer( self, @@ -93,7 +104,7 @@ impl<'tcx> TyCtxt<'tcx> { ) -> Result<mir::ConstAllocation<'tcx>, ErrorHandled> { trace!("eval_static_initializer: Need to compute {:?}", def_id); assert!(self.is_static(def_id)); - let instance = ty::Instance::mono(self, def_id); + let instance = ty::Instance::mono(*self, def_id); let gid = GlobalId { instance, promoted: None }; self.eval_to_allocation(gid, ty::ParamEnv::reveal_all()) } @@ -109,7 +120,9 @@ impl<'tcx> TyCtxt<'tcx> { let raw_const = self.eval_to_allocation_raw(param_env.and(gid))?; Ok(self.global_alloc(raw_const.alloc_id).unwrap_memory()) } +} +impl<'tcx> TyCtxt<'tcx> { /// Destructure a type-level constant ADT or array into its variant index and its field values. /// Panics if the destructuring fails, use `try_destructure_const` for fallible version. pub fn destructure_const( diff --git a/src/test/ui/consts/recursive-zst-static.default.stderr b/src/test/ui/consts/recursive-zst-static.default.stderr index 03f8f5c5a0e..2a4ad5825ec 100644 --- a/src/test/ui/consts/recursive-zst-static.default.stderr +++ b/src/test/ui/consts/recursive-zst-static.default.stderr @@ -5,10 +5,10 @@ LL | static FOO: () = FOO; | ^^^^^^^^^^^^^^^^^^^^^ | note: ...which requires const-evaluating + checking `FOO`... - --> $DIR/recursive-zst-static.rs:10:1 + --> $DIR/recursive-zst-static.rs:10:18 | LL | static FOO: () = FOO; - | ^^^^^^^^^^^^^^^^^^^^^ + | ^^^ = note: ...which again requires const-evaluating + checking `FOO`, completing the cycle = note: cycle used when running analysis passes on this crate diff --git a/src/test/ui/consts/recursive-zst-static.unleash.stderr b/src/test/ui/consts/recursive-zst-static.unleash.stderr index 03f8f5c5a0e..2a4ad5825ec 100644 --- a/src/test/ui/consts/recursive-zst-static.unleash.stderr +++ b/src/test/ui/consts/recursive-zst-static.unleash.stderr @@ -5,10 +5,10 @@ LL | static FOO: () = FOO; | ^^^^^^^^^^^^^^^^^^^^^ | note: ...which requires const-evaluating + checking `FOO`... - --> $DIR/recursive-zst-static.rs:10:1 + --> $DIR/recursive-zst-static.rs:10:18 | LL | static FOO: () = FOO; - | ^^^^^^^^^^^^^^^^^^^^^ + | ^^^ = note: ...which again requires const-evaluating + checking `FOO`, completing the cycle = note: cycle used when running analysis passes on this crate diff --git a/src/test/ui/consts/write-to-static-mut-in-static.stderr b/src/test/ui/consts/write-to-static-mut-in-static.stderr index 789919bd166..ab4b8844e5b 100644 --- a/src/test/ui/consts/write-to-static-mut-in-static.stderr +++ b/src/test/ui/consts/write-to-static-mut-in-static.stderr @@ -11,10 +11,10 @@ LL | pub static mut C: u32 = unsafe { C = 1; 0 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: ...which requires const-evaluating + checking `C`... - --> $DIR/write-to-static-mut-in-static.rs:5:1 + --> $DIR/write-to-static-mut-in-static.rs:5:34 | LL | pub static mut C: u32 = unsafe { C = 1; 0 }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^ = note: ...which again requires const-evaluating + checking `C`, completing the cycle = note: cycle used when running analysis passes on this crate diff --git a/src/test/ui/recursion/recursive-static-definition.stderr b/src/test/ui/recursion/recursive-static-definition.stderr index ee73b026a0b..be4f09f9286 100644 --- a/src/test/ui/recursion/recursive-static-definition.stderr +++ b/src/test/ui/recursion/recursive-static-definition.stderr @@ -5,10 +5,10 @@ LL | pub static FOO: u32 = FOO; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: ...which requires const-evaluating + checking `FOO`... - --> $DIR/recursive-static-definition.rs:1:1 + --> $DIR/recursive-static-definition.rs:1:23 | LL | pub static FOO: u32 = FOO; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^ = note: ...which again requires const-evaluating + checking `FOO`, completing the cycle = note: cycle used when running analysis passes on this crate