Skip to content

Commit 467e0f4

Browse files
committed
use precise spans for recursive const evaluation
1 parent 9ab4f87 commit 467e0f4

File tree

8 files changed

+28
-14
lines changed

8 files changed

+28
-14
lines changed

compiler/rustc_const_eval/src/interpret/eval_context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
926926
self.param_env
927927
};
928928
let param_env = param_env.with_const();
929-
let val = self.tcx.eval_to_allocation_raw(param_env.and(gid))?;
929+
// Use a precise span for better cycle errors.
930+
let val = self.tcx.at(self.cur_span()).eval_to_allocation_raw(param_env.and(gid))?;
930931
self.raw_const_to_mplace(val)
931932
}
932933

compiler/rustc_const_eval/src/interpret/memory.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
504504
throw_unsup!(ReadExternStatic(def_id));
505505
}
506506

507-
(self.tcx.eval_static_initializer(def_id)?, Some(def_id))
507+
// Use a precise span for better cycle errors.
508+
(self.tcx.at(self.cur_span()).eval_static_initializer(def_id)?, Some(def_id))
508509
}
509510
};
510511
M::before_access_global(*self.tcx, &self.machine, id, alloc, def_id, is_write)?;

compiler/rustc_const_eval/src/interpret/step.rs

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
5555
};
5656
let basic_block = &self.body().basic_blocks()[loc.block];
5757

58-
5958
if let Some(stmt) = basic_block.statements.get(loc.statement_index) {
6059
let old_frames = self.frame_idx();
6160
self.statement(stmt)?;

compiler/rustc_middle/src/mir/interpret/queries.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use super::{ErrorHandled, EvalToConstValueResult, GlobalId};
33
use crate::mir;
44
use crate::ty::fold::TypeFoldable;
55
use crate::ty::subst::InternalSubsts;
6-
use crate::ty::{self, TyCtxt};
6+
use crate::ty::{self, query::TyCtxtAt, TyCtxt};
77
use rustc_hir::def_id::DefId;
8-
use rustc_span::Span;
8+
use rustc_span::{Span, DUMMY_SP};
99

1010
impl<'tcx> TyCtxt<'tcx> {
1111
/// Evaluates a constant without providing any substitutions. This is useful to evaluate consts
@@ -86,14 +86,25 @@ impl<'tcx> TyCtxt<'tcx> {
8686
}
8787
}
8888

89+
/// Evaluate a static's initializer, returning the allocation of the initializer's memory.
90+
#[inline(always)]
91+
pub fn eval_static_initializer(
92+
self,
93+
def_id: DefId,
94+
) -> Result<mir::ConstAllocation<'tcx>, ErrorHandled> {
95+
self.at(DUMMY_SP).eval_static_initializer(def_id)
96+
}
97+
}
98+
99+
impl<'tcx> TyCtxtAt<'tcx> {
89100
/// Evaluate a static's initializer, returning the allocation of the initializer's memory.
90101
pub fn eval_static_initializer(
91102
self,
92103
def_id: DefId,
93104
) -> Result<mir::ConstAllocation<'tcx>, ErrorHandled> {
94105
trace!("eval_static_initializer: Need to compute {:?}", def_id);
95106
assert!(self.is_static(def_id));
96-
let instance = ty::Instance::mono(self, def_id);
107+
let instance = ty::Instance::mono(*self, def_id);
97108
let gid = GlobalId { instance, promoted: None };
98109
self.eval_to_allocation(gid, ty::ParamEnv::reveal_all())
99110
}
@@ -109,7 +120,9 @@ impl<'tcx> TyCtxt<'tcx> {
109120
let raw_const = self.eval_to_allocation_raw(param_env.and(gid))?;
110121
Ok(self.global_alloc(raw_const.alloc_id).unwrap_memory())
111122
}
123+
}
112124

125+
impl<'tcx> TyCtxt<'tcx> {
113126
/// Destructure a type-level constant ADT or array into its variant index and its field values.
114127
/// Panics if the destructuring fails, use `try_destructure_const` for fallible version.
115128
pub fn destructure_const(

src/test/ui/consts/recursive-zst-static.default.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | static FOO: () = FOO;
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: ...which requires const-evaluating + checking `FOO`...
8-
--> $DIR/recursive-zst-static.rs:10:1
8+
--> $DIR/recursive-zst-static.rs:10:18
99
|
1010
LL | static FOO: () = FOO;
11-
| ^^^^^^^^^^^^^^^^^^^^^
11+
| ^^^
1212
= note: ...which again requires const-evaluating + checking `FOO`, completing the cycle
1313
= note: cycle used when running analysis passes on this crate
1414

src/test/ui/consts/recursive-zst-static.unleash.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | static FOO: () = FOO;
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: ...which requires const-evaluating + checking `FOO`...
8-
--> $DIR/recursive-zst-static.rs:10:1
8+
--> $DIR/recursive-zst-static.rs:10:18
99
|
1010
LL | static FOO: () = FOO;
11-
| ^^^^^^^^^^^^^^^^^^^^^
11+
| ^^^
1212
= note: ...which again requires const-evaluating + checking `FOO`, completing the cycle
1313
= note: cycle used when running analysis passes on this crate
1414

src/test/ui/consts/write-to-static-mut-in-static.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ LL | pub static mut C: u32 = unsafe { C = 1; 0 };
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
|
1313
note: ...which requires const-evaluating + checking `C`...
14-
--> $DIR/write-to-static-mut-in-static.rs:5:1
14+
--> $DIR/write-to-static-mut-in-static.rs:5:34
1515
|
1616
LL | pub static mut C: u32 = unsafe { C = 1; 0 };
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
| ^^^^^
1818
= note: ...which again requires const-evaluating + checking `C`, completing the cycle
1919
= note: cycle used when running analysis passes on this crate
2020

src/test/ui/recursion/recursive-static-definition.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | pub static FOO: u32 = FOO;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: ...which requires const-evaluating + checking `FOO`...
8-
--> $DIR/recursive-static-definition.rs:1:1
8+
--> $DIR/recursive-static-definition.rs:1:23
99
|
1010
LL | pub static FOO: u32 = FOO;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
| ^^^
1212
= note: ...which again requires const-evaluating + checking `FOO`, completing the cycle
1313
= note: cycle used when running analysis passes on this crate
1414

0 commit comments

Comments
 (0)