Skip to content

Commit

Permalink
Use the dedicated method for evaluating statics
Browse files Browse the repository at this point in the history
This hardens the API against future changes to how we evaluate statics
and avoids the explanation around `ParamEnv::reveal_all`
  • Loading branch information
oli-obk committed Oct 17, 2023
1 parent b443c22 commit 32b6f24
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
40 changes: 27 additions & 13 deletions compiler/rustc_const_eval/src/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hir::CRATE_HIR_ID;
use rustc_hir::{self as hir, def_id::DefId, definitions::DefPathData};
use rustc_index::IndexVec;
use rustc_middle::mir;
use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
use rustc_middle::mir::interpret::{alloc_range, ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
use rustc_middle::query::TyCtxtAt;
use rustc_middle::ty::layout::{
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOf, LayoutOfHelpers,
Expand All @@ -17,6 +17,7 @@ use rustc_middle::ty::{self, GenericArgsRef, ParamEnv, Ty, TyCtxt, TypeFoldable,
use rustc_mir_dataflow::storage::always_storage_live_locals;
use rustc_session::Limit;
use rustc_span::Span;
use rustc_target::abi::{self, Abi};
use rustc_target::abi::{call::FnAbi, Align, HasDataLayout, Size, TargetDataLayout};

use super::{
Expand Down Expand Up @@ -1069,23 +1070,36 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
})
}

pub fn eval_global(
pub fn eval_global_scalar(
&self,
instance: ty::Instance<'tcx>,
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
) -> InterpResult<'tcx, Scalar<M::Provenance>> {
let gid = GlobalId { instance, promoted: None };
// For statics we pick `ParamEnv::reveal_all`, because statics don't have generics
// and thus don't care about the parameter environment. While we could just use
// `self.param_env`, that would mean we invoke the query to evaluate the static
// with different parameter environments, thus causing the static to be evaluated
// multiple times.
let param_env = if self.tcx.is_static(gid.instance.def_id()) {
ty::ParamEnv::reveal_all()
let (alloc_id, alloc) = if self.tcx.is_static(gid.instance.def_id()) {
(
self.tcx.reserve_and_set_static_alloc(gid.instance.def_id()),
self.ctfe_query(|tcx| tcx.eval_static_initializer(gid.instance.def_id()))?,
)
} else {
self.param_env
let val = self.ctfe_query(|tcx| tcx.eval_to_allocation_raw(self.param_env.and(gid)))?;
(val.alloc_id, self.tcx.global_alloc(val.alloc_id).unwrap_memory())
};
let val = self.ctfe_query(|tcx| tcx.eval_to_allocation_raw(param_env.and(gid)))?;
self.raw_const_to_mplace(val)

let ty = instance.ty(self.tcx.tcx, self.param_env);
let layout = self.layout_of(ty)?;
let read_provenance = matches!(
layout.abi,
Abi::Scalar(abi::Scalar::Initialized { value: abi::Primitive::Pointer(..), .. })
);

let scalar = alloc
.inner()
.read_scalar(self, alloc_range(Size::ZERO, layout.size), read_provenance)
.map_err(|err| err.to_interp_error(alloc_id))?;
Ok(match scalar {
Scalar::Ptr(ptr, size) => Scalar::Ptr(self.global_base_pointer(ptr)?, size),
Scalar::Int(int) => Scalar::Int(int),
})
}

pub fn eval_mir_constant(
Expand Down
6 changes: 2 additions & 4 deletions src/tools/miri/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let this = self.eval_context_ref();
let instance = this.resolve_path(path, Namespace::ValueNS);
// We don't give a span -- this isn't actually used directly by the program anyway.
let const_val = this.eval_global(instance).unwrap_or_else(|err| {
this.eval_global_scalar(instance).unwrap_or_else(|err| {
panic!("failed to evaluate required Rust item: {path:?}\n{err:?}")
});
this.read_scalar(&const_val)
.unwrap_or_else(|err| panic!("failed to read required Rust item: {path:?}\n{err:?}"))
})
}

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

0 comments on commit 32b6f24

Please sign in to comment.