Skip to content

Commit ece7178

Browse files
committed
Retire GlobalId.
1 parent bfdeb2e commit ece7178

File tree

11 files changed

+63
-120
lines changed

11 files changed

+63
-120
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+22-39
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_target::abi::{self, Abi};
1717
use super::{CompileTimeEvalContext, CompileTimeInterpreter, ConstEvalErr};
1818
use crate::interpret::eval_nullary_intrinsic;
1919
use crate::interpret::{
20-
intern_const_alloc_recursive, Allocation, ConstAlloc, ConstValue, CtfeValidationMode, GlobalId,
20+
intern_const_alloc_recursive, Allocation, ConstAlloc, ConstValue, CtfeValidationMode,
2121
Immediate, InternKind, InterpCx, InterpError, InterpResult, MPlaceTy, MemoryKind, OpTy,
2222
RefTracking, StackPopCleanup,
2323
};
@@ -29,14 +29,14 @@ const NOTE_ON_UNDEFINED_BEHAVIOR_ERROR: &str = "The rules on what exactly is und
2929
// Returns a pointer to where the result lives
3030
fn eval_body_using_ecx<'mir, 'tcx>(
3131
ecx: &mut CompileTimeEvalContext<'mir, 'tcx>,
32-
cid: GlobalId<'tcx>,
32+
instance: ty::Instance<'tcx>,
3333
body: &'mir mir::Body<'tcx>,
3434
) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
35-
debug!("eval_body_using_ecx: {:?}, {:?}", cid, ecx.param_env);
35+
debug!("eval_body_using_ecx: {:?}, {:?}", instance, ecx.param_env);
3636
let tcx = *ecx.tcx;
3737
assert!(
3838
matches!(
39-
ecx.tcx.def_kind(cid.instance.def_id()),
39+
ecx.tcx.def_kind(instance.def_id()),
4040
DefKind::Const
4141
| DefKind::Static(_)
4242
| DefKind::ConstParam
@@ -46,29 +46,21 @@ fn eval_body_using_ecx<'mir, 'tcx>(
4646
| DefKind::Promoted
4747
),
4848
"Unexpected DefKind: {:?}",
49-
ecx.tcx.def_kind(cid.instance.def_id())
49+
ecx.tcx.def_kind(instance.def_id())
5050
);
51-
let layout = ecx.layout_of(body.bound_return_ty().subst(tcx, cid.instance.substs))?;
51+
let layout = ecx.layout_of(body.bound_return_ty().subst(tcx, instance.substs))?;
5252
assert!(layout.is_sized());
5353
let ret = ecx.allocate(layout, MemoryKind::Stack)?;
5454

55-
trace!(
56-
"eval_body_using_ecx: pushing stack frame for global: {}",
57-
with_no_trimmed_paths!(ecx.tcx.def_path_str(cid.instance.def_id())),
58-
);
55+
trace!("eval_body_using_ecx: pushing stack frame for global: {:?}", instance.def_id());
5956

60-
ecx.push_stack_frame(
61-
cid.instance,
62-
body,
63-
&ret.into(),
64-
StackPopCleanup::Root { cleanup: false },
65-
)?;
57+
ecx.push_stack_frame(instance, body, &ret.into(), StackPopCleanup::Root { cleanup: false })?;
6658

6759
// The main interpreter loop.
6860
while ecx.step()? {}
6961

7062
// Intern the result
71-
let intern_kind = match tcx.def_kind(cid.instance.def_id()) {
63+
let intern_kind = match tcx.def_kind(instance.def_id()) {
7264
DefKind::Promoted => InternKind::Promoted,
7365
DefKind::Static(m) => InternKind::Static(m),
7466
_ => InternKind::Constant,
@@ -196,15 +188,15 @@ pub(super) fn op_to_const<'tcx>(
196188
pub(crate) fn turn_into_const_value<'tcx>(
197189
tcx: TyCtxt<'tcx>,
198190
constant: ConstAlloc<'tcx>,
199-
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
191+
key: ty::ParamEnvAnd<'tcx, ty::Instance<'tcx>>,
200192
) -> ConstValue<'tcx> {
201-
let cid = key.value;
202-
let def_id = cid.instance.def.def_id();
193+
let instance = key.value;
194+
let def_id = instance.def.def_id();
203195
let is_static = tcx.is_static(def_id);
204196
// This is just accessing an already computed constant, so no need to check alignment here.
205197
let ecx = mk_eval_cx(
206198
tcx,
207-
tcx.def_span(key.value.instance.def_id()),
199+
tcx.def_span(def_id),
208200
key.param_env,
209201
/*can_access_statics:*/ is_static,
210202
);
@@ -225,7 +217,7 @@ pub(crate) fn turn_into_const_value<'tcx>(
225217
#[instrument(skip(tcx), level = "debug")]
226218
pub fn eval_to_const_value_raw_provider<'tcx>(
227219
tcx: TyCtxt<'tcx>,
228-
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
220+
key: ty::ParamEnvAnd<'tcx, ty::Instance<'tcx>>,
229221
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> {
230222
assert!(key.param_env.is_const());
231223
// see comment in eval_to_allocation_raw_provider for what we're doing here
@@ -242,8 +234,8 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
242234

243235
// We call `const_eval` for zero arg intrinsics, too, in order to cache their value.
244236
// Catch such calls and evaluate them instead of trying to load a constant's MIR.
245-
if let ty::InstanceDef::Intrinsic(def_id) = key.value.instance.def {
246-
let ty = key.value.instance.ty(tcx, key.param_env);
237+
if let ty::InstanceDef::Intrinsic(def_id) = key.value.def {
238+
let ty = key.value.ty(tcx, key.param_env);
247239
let ty::FnDef(_, substs) = ty.kind() else {
248240
bug!("intrinsic with type {:?}", ty);
249241
};
@@ -260,7 +252,7 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
260252
#[instrument(skip(tcx), level = "debug")]
261253
pub fn eval_to_allocation_raw_provider<'tcx>(
262254
tcx: TyCtxt<'tcx>,
263-
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
255+
key: ty::ParamEnvAnd<'tcx, ty::Instance<'tcx>>,
264256
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
265257
assert!(key.param_env.is_const());
266258
// Because the constant is computed twice (once per value of `Reveal`), we are at risk of
@@ -281,18 +273,10 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
281273
other => return other,
282274
}
283275
}
284-
if cfg!(debug_assertions) {
285-
// Make sure we format the instance even if we do not print it.
286-
// This serves as a regression test against an ICE on printing.
287-
// The next two lines concatenated contain some discussion:
288-
// https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/
289-
// subject/anon_const_instance_printing/near/135980032
290-
let instance = with_no_trimmed_paths!(key.value.instance.to_string());
291-
trace!("const eval: {:?} ({})", key, instance);
292-
}
276+
trace!("const eval: {:?}", key);
293277

294-
let cid = key.value;
295-
let def = cid.instance.def.def_id();
278+
let instance = key.value;
279+
let def = instance.def.def_id();
296280
let within_static = match tcx.def_kind(def) {
297281
DefKind::Promoted => tcx.is_static(tcx.parent(def)),
298282
DefKind::Static(_) => true,
@@ -316,8 +300,8 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
316300
),
317301
);
318302

319-
let res = ecx.load_mir(cid.instance.def);
320-
match res.and_then(|body| eval_body_using_ecx(&mut ecx, cid, &body)) {
303+
let res = ecx.load_mir(instance.def);
304+
match res.and_then(|body| eval_body_using_ecx(&mut ecx, instance, &body)) {
321305
Err(error) => {
322306
let err = ConstEvalErr::new(&ecx, error, None);
323307
let msg = if within_static {
@@ -326,7 +310,6 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
326310
// If the current item has generics, we'd like to enrich the message with the
327311
// instance and its substs: to show the actual compile-time values, in addition to
328312
// the expression, leading to the const eval error.
329-
let instance = &key.value.instance;
330313
if !instance.substs.is_empty() {
331314
let instance = with_no_trimmed_paths!(instance.to_string());
332315
let msg = format!("evaluation of `{}` failed", instance);

compiler/rustc_const_eval/src/const_eval/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::interpret::{
77
};
88
use rustc_hir::Mutability;
99
use rustc_middle::mir;
10-
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId};
10+
use rustc_middle::mir::interpret::EvalToValTreeResult;
1111
use rustc_middle::ty::{self, TyCtxt};
1212
use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
1313

@@ -51,9 +51,9 @@ pub(crate) type ValTreeCreationResult<'tcx> = Result<ty::ValTree<'tcx>, ValTreeC
5151
pub(crate) fn eval_to_valtree<'tcx>(
5252
tcx: TyCtxt<'tcx>,
5353
param_env: ty::ParamEnv<'tcx>,
54-
cid: GlobalId<'tcx>,
54+
instance: ty::Instance<'tcx>,
5555
) -> EvalToValTreeResult<'tcx> {
56-
let const_alloc = tcx.eval_to_allocation_raw(param_env.and(cid))?;
56+
let const_alloc = tcx.eval_to_allocation_raw(param_env.and(instance))?;
5757

5858
// FIXME Need to provide a span to `eval_to_valtree`
5959
let ecx = mk_eval_cx(
@@ -71,10 +71,10 @@ pub(crate) fn eval_to_valtree<'tcx>(
7171
match valtree_result {
7272
Ok(valtree) => Ok(Some(valtree)),
7373
Err(err) => {
74-
let did = cid.instance.def_id();
75-
let global_const_id = cid.display(tcx);
74+
let did = instance.def_id();
7675
match err {
7776
ValTreeCreationError::NodesOverflow => {
77+
let global_const_id = tcx.def_path_str(did);
7878
let msg = format!(
7979
"maximum number of nodes exceeded in constant {}",
8080
&global_const_id

compiler/rustc_const_eval/src/interpret/eval_context.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ use rustc_span::Span;
2020
use rustc_target::abi::{call::FnAbi, Align, HasDataLayout, Size, TargetDataLayout};
2121

2222
use super::{
23-
AllocId, GlobalId, Immediate, InterpErrorInfo, InterpResult, MPlaceTy, Machine, MemPlace,
24-
MemPlaceMeta, Memory, MemoryKind, Operand, Place, PlaceTy, PointerArithmetic, Provenance,
25-
Scalar, StackPopJump,
23+
AllocId, Immediate, InterpErrorInfo, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta,
24+
Memory, MemoryKind, Operand, Place, PlaceTy, PointerArithmetic, Provenance, Scalar,
25+
StackPopJump,
2626
};
2727
use crate::util;
2828

@@ -911,21 +911,22 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
911911

912912
pub fn eval_global(
913913
&self,
914-
gid: GlobalId<'tcx>,
914+
instance: ty::Instance<'tcx>,
915915
span: Option<Span>,
916916
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
917917
// For statics we pick `ParamEnv::reveal_all`, because statics don't have generics
918918
// and thus don't care about the parameter environment. While we could just use
919919
// `self.param_env`, that would mean we invoke the query to evaluate the static
920920
// with different parameter environments, thus causing the static to be evaluated
921921
// multiple times.
922-
let param_env = if self.tcx.is_static(gid.instance.def_id()) {
922+
let param_env = if self.tcx.is_static(instance.def_id()) {
923923
ty::ParamEnv::reveal_all()
924924
} else {
925925
self.param_env
926926
};
927927
let param_env = param_env.with_const();
928-
let val = self.ctfe_query(span, |tcx| tcx.eval_to_allocation_raw(param_env.and(gid)))?;
928+
let val =
929+
self.ctfe_query(span, |tcx| tcx.eval_to_allocation_raw(param_env.and(instance)))?;
929930
self.raw_const_to_mplace(val)
930931
}
931932

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use rustc_hir::def_id::DefId;
66
use rustc_middle::mir::{
77
self,
8-
interpret::{
9-
Allocation, ConstAllocation, ConstValue, GlobalId, InterpResult, PointerArithmetic, Scalar,
10-
},
8+
interpret::{Allocation, ConstAllocation, ConstValue, InterpResult, PointerArithmetic, Scalar},
119
BinOp, NonDivergingIntrinsic,
1210
};
1311
use rustc_middle::ty;
@@ -163,7 +161,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
163161
| sym::type_id
164162
| sym::type_name
165163
| sym::variant_count => {
166-
let gid = GlobalId { instance };
167164
let ty = match intrinsic_name {
168165
sym::pref_align_of | sym::variant_count => self.tcx.types.usize,
169166
sym::needs_drop => self.tcx.types.bool,
@@ -172,7 +169,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
172169
_ => bug!(),
173170
};
174171
let val = self.ctfe_query(None, |tcx| {
175-
tcx.const_eval_global_id(self.param_env, gid, Some(tcx.span))
172+
tcx.const_eval_global_id(self.param_env, instance, Some(tcx.span))
176173
})?;
177174
let val = self.const_val_to_op(val, ty, Some(dest.layout))?;
178175
self.copy_op(&val, dest, /*allow_transmute*/ false)?;

compiler/rustc_const_eval/src/interpret/operand.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use rustc_span::Span;
1212
use rustc_target::abi::{self, Abi, Align, HasDataLayout, Size};
1313

1414
use super::{
15-
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, Frame, GlobalId,
16-
InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Place, PlaceTy, Pointer,
17-
Provenance, Scalar,
15+
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, Frame, InterpCx,
16+
InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Place, PlaceTy, Pointer, Provenance,
17+
Scalar,
1818
};
1919

2020
/// An `Immediate` represents a single immediate self-contained Rust value.
@@ -599,9 +599,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
599599
}
600600
ty::ConstKind::Unevaluated(uv) => {
601601
let instance = self.resolve(uv.def, uv.substs)?;
602-
let cid = GlobalId { instance };
603602
self.ctfe_query(span, |tcx| {
604-
tcx.eval_to_valtree(self.param_env.with_const().and(cid))
603+
tcx.eval_to_valtree(self.param_env.with_const().and(instance))
605604
})?
606605
.unwrap_or_else(|| bug!("unable to create ValTree for {uv:?}"))
607606
}
@@ -628,7 +627,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
628627
mir::ConstantKind::Val(val, ty) => self.const_val_to_op(val, ty, layout),
629628
mir::ConstantKind::Unevaluated(uv, _) => {
630629
let instance = self.resolve(uv.def, uv.substs)?;
631-
Ok(self.eval_global(GlobalId { instance }, span)?.into())
630+
Ok(self.eval_global(instance, span)?.into())
632631
}
633632
}
634633
}

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

-18
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ use rustc_data_structures::tiny_list::TinyList;
108108
use rustc_errors::ErrorGuaranteed;
109109
use rustc_hir::def_id::DefId;
110110
use rustc_macros::HashStable;
111-
use rustc_middle::ty::print::with_no_trimmed_paths;
112111
use rustc_serialize::{Decodable, Encodable};
113112
use rustc_target::abi::{AddressSpace, Endian, HasDataLayout};
114113

@@ -132,23 +131,6 @@ pub use self::allocation::{
132131

133132
pub use self::pointer::{Pointer, PointerArithmetic, Provenance};
134133

135-
/// Uniquely identifies one of the following:
136-
/// - A constant
137-
/// - A static
138-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, TyEncodable, TyDecodable)]
139-
#[derive(HashStable, Lift, TypeFoldable, TypeVisitable)]
140-
pub struct GlobalId<'tcx> {
141-
/// For a constant or static, the `Instance` of the item itself.
142-
/// For a promoted global, the `Instance` of the function they belong to.
143-
pub instance: ty::Instance<'tcx>,
144-
}
145-
146-
impl<'tcx> GlobalId<'tcx> {
147-
pub fn display(self, tcx: TyCtxt<'tcx>) -> String {
148-
with_no_trimmed_paths!(tcx.def_path_str(self.instance.def.def_id()))
149-
}
150-
}
151-
152134
/// Input argument for `tcx.lit_to_const`.
153135
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, HashStable)]
154136
pub struct LitToConstInput<'tcx> {

0 commit comments

Comments
 (0)