Skip to content

Commit 32b6f24

Browse files
committed
Use the dedicated method for evaluating statics
This hardens the API against future changes to how we evaluate statics and avoids the explanation around `ParamEnv::reveal_all`
1 parent b443c22 commit 32b6f24

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use hir::CRATE_HIR_ID;
77
use rustc_hir::{self as hir, def_id::DefId, definitions::DefPathData};
88
use rustc_index::IndexVec;
99
use rustc_middle::mir;
10-
use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
10+
use rustc_middle::mir::interpret::{alloc_range, ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
1111
use rustc_middle::query::TyCtxtAt;
1212
use rustc_middle::ty::layout::{
1313
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOf, LayoutOfHelpers,
@@ -17,6 +17,7 @@ use rustc_middle::ty::{self, GenericArgsRef, ParamEnv, Ty, TyCtxt, TypeFoldable,
1717
use rustc_mir_dataflow::storage::always_storage_live_locals;
1818
use rustc_session::Limit;
1919
use rustc_span::Span;
20+
use rustc_target::abi::{self, Abi};
2021
use rustc_target::abi::{call::FnAbi, Align, HasDataLayout, Size, TargetDataLayout};
2122

2223
use super::{
@@ -1069,23 +1070,36 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
10691070
})
10701071
}
10711072

1072-
pub fn eval_global(
1073+
pub fn eval_global_scalar(
10731074
&self,
10741075
instance: ty::Instance<'tcx>,
1075-
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
1076+
) -> InterpResult<'tcx, Scalar<M::Provenance>> {
10761077
let gid = GlobalId { instance, promoted: None };
1077-
// For statics we pick `ParamEnv::reveal_all`, because statics don't have generics
1078-
// and thus don't care about the parameter environment. While we could just use
1079-
// `self.param_env`, that would mean we invoke the query to evaluate the static
1080-
// with different parameter environments, thus causing the static to be evaluated
1081-
// multiple times.
1082-
let param_env = if self.tcx.is_static(gid.instance.def_id()) {
1083-
ty::ParamEnv::reveal_all()
1078+
let (alloc_id, alloc) = if self.tcx.is_static(gid.instance.def_id()) {
1079+
(
1080+
self.tcx.reserve_and_set_static_alloc(gid.instance.def_id()),
1081+
self.ctfe_query(|tcx| tcx.eval_static_initializer(gid.instance.def_id()))?,
1082+
)
10841083
} else {
1085-
self.param_env
1084+
let val = self.ctfe_query(|tcx| tcx.eval_to_allocation_raw(self.param_env.and(gid)))?;
1085+
(val.alloc_id, self.tcx.global_alloc(val.alloc_id).unwrap_memory())
10861086
};
1087-
let val = self.ctfe_query(|tcx| tcx.eval_to_allocation_raw(param_env.and(gid)))?;
1088-
self.raw_const_to_mplace(val)
1087+
1088+
let ty = instance.ty(self.tcx.tcx, self.param_env);
1089+
let layout = self.layout_of(ty)?;
1090+
let read_provenance = matches!(
1091+
layout.abi,
1092+
Abi::Scalar(abi::Scalar::Initialized { value: abi::Primitive::Pointer(..), .. })
1093+
);
1094+
1095+
let scalar = alloc
1096+
.inner()
1097+
.read_scalar(self, alloc_range(Size::ZERO, layout.size), read_provenance)
1098+
.map_err(|err| err.to_interp_error(alloc_id))?;
1099+
Ok(match scalar {
1100+
Scalar::Ptr(ptr, size) => Scalar::Ptr(self.global_base_pointer(ptr)?, size),
1101+
Scalar::Int(int) => Scalar::Int(int),
1102+
})
10891103
}
10901104

10911105
pub fn eval_mir_constant(

src/tools/miri/src/helpers.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
141141
let this = self.eval_context_ref();
142142
let instance = this.resolve_path(path, Namespace::ValueNS);
143143
// We don't give a span -- this isn't actually used directly by the program anyway.
144-
let const_val = this.eval_global(instance).unwrap_or_else(|err| {
144+
this.eval_global_scalar(instance).unwrap_or_else(|err| {
145145
panic!("failed to evaluate required Rust item: {path:?}\n{err:?}")
146-
});
147-
this.read_scalar(&const_val)
148-
.unwrap_or_else(|err| panic!("failed to read required Rust item: {path:?}\n{err:?}"))
146+
})
149147
}
150148

151149
/// Helper function to get a `libc` constant as a `Scalar`.

0 commit comments

Comments
 (0)