2 use rustc::mir::interpret::{ConstEvalErr};
4 use rustc::ty::{self, TyCtxt, Ty, Instance};
5 use rustc::ty::layout::{self, LayoutOf, Primitive};
6 use rustc::ty::subst::Subst;
8 use syntax::ast::Mutability;
9 use syntax::codemap::Span;
10 use syntax::codemap::DUMMY_SP;
12 use rustc::mir::interpret::{
13 EvalResult, EvalError, EvalErrorKind, GlobalId,
14 Value, Scalar, AllocId, Allocation, ConstValue,
16 use super::{Place, EvalContext, StackPopCleanup, ValTy, PlaceExtra, Memory, MemoryKind};
19 use std::error::Error;
21 pub fn mk_borrowck_eval_cx<'a, 'mir, 'tcx>(
22 tcx: TyCtxt<'a, 'tcx, 'tcx>,
23 instance: Instance<'tcx>,
24 mir: &'mir mir::Mir<'tcx>,
26 ) -> EvalResult<'tcx, EvalContext<'a, 'mir, 'tcx, CompileTimeEvaluator>> {
27 debug!("mk_borrowck_eval_cx: {:?}", instance);
28 let param_env = tcx.param_env(instance.def_id());
29 let mut ecx = EvalContext::new(tcx.at(span), param_env, CompileTimeEvaluator, ());
30 // insert a stack frame so any queries have the correct substs
36 StackPopCleanup::None,
41 pub fn mk_eval_cx<'a, 'tcx>(
42 tcx: TyCtxt<'a, 'tcx, 'tcx>,
43 instance: Instance<'tcx>,
44 param_env: ty::ParamEnv<'tcx>,
45 ) -> EvalResult<'tcx, EvalContext<'a, 'tcx, 'tcx, CompileTimeEvaluator>> {
46 debug!("mk_eval_cx: {:?}, {:?}", instance, param_env);
47 let span = tcx.def_span(instance.def_id());
48 let mut ecx = EvalContext::new(tcx.at(span), param_env, CompileTimeEvaluator, ());
49 let mir = ecx.load_mir(instance.def)?;
50 // insert a stack frame so any queries have the correct substs
56 StackPopCleanup::None,
61 pub fn eval_promoted<'a, 'mir, 'tcx>(
62 ecx: &mut EvalContext<'a, 'mir, 'tcx, CompileTimeEvaluator>,
64 mir: &'mir mir::Mir<'tcx>,
65 param_env: ty::ParamEnv<'tcx>,
66 ) -> EvalResult<'tcx, (Value, Scalar, Ty<'tcx>)> {
67 ecx.with_fresh_body(|ecx| {
68 eval_body_using_ecx(ecx, cid, Some(mir), param_env)
72 pub fn value_to_const_value<'tcx>(
73 ecx: &EvalContext<'_, '_, 'tcx, CompileTimeEvaluator>,
76 ) -> &'tcx ty::Const<'tcx> {
77 let layout = ecx.layout_of(ty).unwrap();
78 match (val, &layout.abi) {
79 (Value::Scalar(Scalar::Bits { defined: 0, ..}), _) if layout.is_zst() => {},
80 (Value::ByRef(..), _) |
81 (Value::Scalar(_), &layout::Abi::Scalar(_)) |
82 (Value::ScalarPair(..), &layout::Abi::ScalarPair(..)) => {},
83 _ => bug!("bad value/layout combo: {:#?}, {:#?}", val, layout),
87 Value::Scalar(val) => Ok(ConstValue::Scalar(val)),
88 Value::ScalarPair(a, b) => Ok(ConstValue::ScalarPair(a, b)),
89 Value::ByRef(ptr, align) => {
90 let ptr = ptr.to_ptr().unwrap();
91 let alloc = ecx.memory.get(ptr.alloc_id)?;
92 assert!(alloc.align.abi() >= align.abi());
93 assert!(alloc.bytes.len() as u64 - ptr.offset.bytes() >= layout.size.bytes());
94 let mut alloc = alloc.clone();
96 let alloc = ecx.tcx.intern_const_alloc(alloc);
97 Ok(ConstValue::ByRef(alloc, ptr.offset))
102 Ok(val) => ty::Const::from_const_value(ecx.tcx.tcx, val, ty),
104 let (frames, span) = ecx.generate_stacktrace(None);
105 let err = ConstEvalErr {
112 "failed to convert Value to ConstValue, this is a bug",
114 span_bug!(span, "miri error occured when converting Value to ConstValue")
119 fn eval_body_and_ecx<'a, 'mir, 'tcx>(
120 tcx: TyCtxt<'a, 'tcx, 'tcx>,
122 mir: Option<&'mir mir::Mir<'tcx>>,
123 param_env: ty::ParamEnv<'tcx>,
124 ) -> (EvalResult<'tcx, (Value, Scalar, Ty<'tcx>)>, EvalContext<'a, 'mir, 'tcx, CompileTimeEvaluator>) {
125 debug!("eval_body_and_ecx: {:?}, {:?}", cid, param_env);
126 // we start out with the best span we have
127 // and try improving it down the road when more information is available
128 let span = tcx.def_span(cid.instance.def_id());
129 let span = mir.map(|mir| mir.span).unwrap_or(span);
130 let mut ecx = EvalContext::new(tcx.at(span), param_env, CompileTimeEvaluator, ());
131 let r = eval_body_using_ecx(&mut ecx, cid, mir, param_env);
135 fn eval_body_using_ecx<'a, 'mir, 'tcx>(
136 ecx: &mut EvalContext<'a, 'mir, 'tcx, CompileTimeEvaluator>,
138 mir: Option<&'mir mir::Mir<'tcx>>,
139 param_env: ty::ParamEnv<'tcx>,
140 ) -> EvalResult<'tcx, (Value, Scalar, Ty<'tcx>)> {
141 debug!("eval_body: {:?}, {:?}", cid, param_env);
142 let tcx = ecx.tcx.tcx;
143 let mut mir = match mir {
145 None => ecx.load_mir(cid.instance.def)?,
147 if let Some(index) = cid.promoted {
148 mir = &mir.promoted[index];
150 let layout = ecx.layout_of(mir.return_ty().subst(tcx, cid.instance.substs))?;
151 assert!(!layout.is_unsized());
152 let ptr = ecx.memory.allocate(
157 let internally_mutable = !layout.ty.is_freeze(tcx, param_env, mir.span);
158 let is_static = tcx.is_static(cid.instance.def_id());
159 let mutability = if is_static == Some(hir::Mutability::MutMutable) || internally_mutable {
162 Mutability::Immutable
164 let cleanup = StackPopCleanup::MarkStatic(mutability);
165 let name = ty::tls::with(|tcx| tcx.item_path_str(cid.instance.def_id()));
166 let prom = cid.promoted.map_or(String::new(), |p| format!("::promoted[{:?}]", p));
167 trace!("const_eval: pushing stack frame for global: {}{}", name, prom);
168 assert!(mir.arg_count == 0);
169 ecx.push_stack_frame(
173 Place::from_ptr(ptr, layout.align),
178 let ptr = ptr.into();
179 // always try to read the value and report errors
180 let value = match ecx.try_read_value(ptr, layout.align, layout.ty)? {
181 Some(val) if is_static.is_none() => val,
182 // point at the allocation
183 _ => Value::ByRef(ptr, layout.align),
185 Ok((value, ptr, layout.ty))
188 pub struct CompileTimeEvaluator;
190 impl<'tcx> Into<EvalError<'tcx>> for ConstEvalError {
191 fn into(self) -> EvalError<'tcx> {
192 EvalErrorKind::MachineError(self.to_string()).into()
196 #[derive(Clone, Debug)]
197 enum ConstEvalError {
202 impl fmt::Display for ConstEvalError {
203 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
204 use self::ConstEvalError::*;
206 NeedsRfc(ref msg) => {
209 "\"{}\" needs an rfc before being allowed inside constants",
213 NotConst(ref msg) => write!(f, "{}", msg),
218 impl Error for ConstEvalError {
219 fn description(&self) -> &str {
220 use self::ConstEvalError::*;
222 NeedsRfc(_) => "this feature needs an rfc before being allowed inside constants",
223 NotConst(_) => "this feature is not compatible with constant evaluation",
227 fn cause(&self) -> Option<&dyn Error> {
232 impl<'mir, 'tcx> super::Machine<'mir, 'tcx> for CompileTimeEvaluator {
233 type MemoryData = ();
234 type MemoryKinds = !;
236 ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
237 instance: ty::Instance<'tcx>,
238 destination: Option<(Place, mir::BasicBlock)>,
239 args: &[ValTy<'tcx>],
241 sig: ty::FnSig<'tcx>,
242 ) -> EvalResult<'tcx, bool> {
243 debug!("eval_fn_call: {:?}", instance);
244 if !ecx.tcx.is_const_fn(instance.def_id()) {
245 let def_id = instance.def_id();
246 let (op, oflo) = if let Some(op) = ecx.tcx.is_binop_lang_item(def_id) {
250 ConstEvalError::NotConst(format!("calling non-const fn `{}`", instance)).into(),
253 let (dest, bb) = destination.expect("128 lowerings can't diverge");
254 let dest_ty = sig.output();
256 ecx.intrinsic_with_overflow(op, args[0], args[1], dest, dest_ty)?;
258 ecx.intrinsic_overflowing(op, args[0], args[1], dest, dest_ty)?;
263 let mir = match ecx.load_mir(instance.def) {
266 if let EvalErrorKind::NoMirFor(ref path) = err.kind {
268 ConstEvalError::NeedsRfc(format!("calling extern function `{}`", path))
275 let (return_place, return_to_block) = match destination {
276 Some((place, block)) => (place, StackPopCleanup::Goto(block)),
277 None => (Place::undef(), StackPopCleanup::None),
280 ecx.push_stack_frame(
292 fn call_intrinsic<'a>(
293 ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
294 instance: ty::Instance<'tcx>,
295 args: &[ValTy<'tcx>],
297 dest_layout: layout::TyLayout<'tcx>,
298 target: mir::BasicBlock,
299 ) -> EvalResult<'tcx> {
300 let substs = instance.substs;
302 let intrinsic_name = &ecx.tcx.item_name(instance.def_id()).as_str()[..];
303 match intrinsic_name {
305 let elem_ty = substs.type_at(0);
306 let elem_align = ecx.layout_of(elem_ty)?.align.abi();
307 let align_val = Scalar::Bits {
308 bits: elem_align as u128,
309 defined: dest_layout.size.bits() as u8,
311 ecx.write_scalar(dest, align_val, dest_layout.ty)?;
315 let ty = substs.type_at(0);
316 let size = ecx.layout_of(ty)?.size.bytes() as u128;
317 let size_val = Scalar::Bits {
319 defined: dest_layout.size.bits() as u8,
321 ecx.write_scalar(dest, size_val, dest_layout.ty)?;
325 let ty = substs.type_at(0);
326 let type_id = ecx.tcx.type_id_hash(ty) as u128;
327 let id_val = Scalar::Bits {
329 defined: dest_layout.size.bits() as u8,
331 ecx.write_scalar(dest, id_val, dest_layout.ty)?;
333 "ctpop" | "cttz" | "cttz_nonzero" | "ctlz" | "ctlz_nonzero" | "bswap" => {
334 let ty = substs.type_at(0);
335 let layout_of = ecx.layout_of(ty)?;
336 let bits = ecx.value_to_scalar(args[0])?.to_bits(layout_of.size)?;
337 let kind = match layout_of.abi {
338 ty::layout::Abi::Scalar(ref scalar) => scalar.value,
339 _ => Err(::rustc::mir::interpret::EvalErrorKind::TypeNotPrimitive(ty))?,
341 let out_val = if intrinsic_name.ends_with("_nonzero") {
343 return err!(Intrinsic(format!("{} called on 0", intrinsic_name)));
345 numeric_intrinsic(intrinsic_name.trim_right_matches("_nonzero"), bits, kind)?
347 numeric_intrinsic(intrinsic_name, bits, kind)?
349 ecx.write_scalar(dest, out_val, ty)?;
353 ConstEvalError::NeedsRfc(format!("calling intrinsic `{}`", name)).into()
357 ecx.goto_block(target);
359 // Since we pushed no stack frame, the main loop will act
360 // as if the call just completed and it's returning to the
366 _ecx: &EvalContext<'a, 'mir, 'tcx, Self>,
372 ) -> EvalResult<'tcx, Option<(Scalar, bool)>> {
373 if left.is_bits() && right.is_bits() {
377 ConstEvalError::NeedsRfc("pointer arithmetic or comparison".to_string()).into(),
382 fn mark_static_initialized<'a>(
383 _mem: &mut Memory<'a, 'mir, 'tcx, Self>,
385 _mutability: Mutability,
386 ) -> EvalResult<'tcx, bool> {
391 ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
393 ) -> EvalResult<'tcx, AllocId> {
398 .intern_static(cid.instance.def_id()))
402 _ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
405 ) -> EvalResult<'tcx> {
407 ConstEvalError::NeedsRfc("heap allocations via `box` keyword".to_string()).into(),
411 fn global_item_with_linkage<'a>(
412 _ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
413 _instance: ty::Instance<'tcx>,
414 _mutability: Mutability,
415 ) -> EvalResult<'tcx> {
417 ConstEvalError::NotConst("statics with `linkage` attribute".to_string()).into(),
422 pub fn const_val_field<'a, 'tcx>(
423 tcx: TyCtxt<'a, 'tcx, 'tcx>,
424 param_env: ty::ParamEnv<'tcx>,
425 instance: ty::Instance<'tcx>,
426 variant: Option<usize>,
428 value: &'tcx ty::Const<'tcx>,
429 ) -> ::rustc::mir::interpret::ConstEvalResult<'tcx> {
430 trace!("const_val_field: {:?}, {:?}, {:?}", instance, field, value);
431 let mut ecx = mk_eval_cx(tcx, instance, param_env).unwrap();
434 let value = ecx.const_to_value(value.val)?;
435 let layout = ecx.layout_of(ty)?;
436 let (ptr, align) = match value {
437 Value::ByRef(ptr, align) => (ptr, align),
438 Value::ScalarPair(..) | Value::Scalar(_) => {
439 let ptr = ecx.alloc_ptr(ty)?.into();
440 ecx.write_value_to_ptr(value, ptr, layout.align, ty)?;
444 let place = Place::Ptr {
447 extra: variant.map_or(PlaceExtra::None, PlaceExtra::DowncastVariant),
449 let (place, layout) = ecx.place_field(place, field, layout)?;
450 let (ptr, align) = place.to_ptr_align();
451 let mut new_value = Value::ByRef(ptr, align);
452 new_value = ecx.try_read_by_ref(new_value, layout.ty)?;
453 use rustc_data_structures::indexed_vec::Idx;
454 match (value, new_value) {
455 (Value::Scalar(_), Value::ByRef(..)) |
456 (Value::ScalarPair(..), Value::ByRef(..)) |
457 (Value::Scalar(_), Value::ScalarPair(..)) => bug!(
458 "field {} of {:?} yielded {:?}",
465 Ok(value_to_const_value(&ecx, new_value, layout.ty))
467 result.map_err(|err| {
468 let (trace, span) = ecx.generate_stacktrace(None);
477 pub fn const_variant_index<'a, 'tcx>(
478 tcx: TyCtxt<'a, 'tcx, 'tcx>,
479 param_env: ty::ParamEnv<'tcx>,
480 instance: ty::Instance<'tcx>,
481 val: &'tcx ty::Const<'tcx>,
482 ) -> EvalResult<'tcx, usize> {
483 trace!("const_variant_index: {:?}, {:?}", instance, val);
484 let mut ecx = mk_eval_cx(tcx, instance, param_env).unwrap();
485 let value = ecx.const_to_value(val.val)?;
486 let (ptr, align) = match value {
487 Value::ScalarPair(..) | Value::Scalar(_) => {
488 let layout = ecx.layout_of(val.ty)?;
489 let ptr = ecx.memory.allocate(layout.size, layout.align, Some(MemoryKind::Stack))?.into();
490 ecx.write_value_to_ptr(value, ptr, layout.align, val.ty)?;
493 Value::ByRef(ptr, align) => (ptr, align),
495 let place = Place::from_scalar_ptr(ptr, align);
496 ecx.read_discriminant_as_variant_index(place, val.ty)
499 pub fn const_value_to_allocation_provider<'a, 'tcx>(
500 tcx: TyCtxt<'a, 'tcx, 'tcx>,
501 val: &'tcx ty::Const<'tcx>,
502 ) -> &'tcx Allocation {
504 ConstValue::ByRef(alloc, offset) => {
505 assert_eq!(offset.bytes(), 0);
510 let result = || -> EvalResult<'tcx, &'tcx Allocation> {
511 let mut ecx = EvalContext::new(
513 ty::ParamEnv::reveal_all(),
514 CompileTimeEvaluator,
516 let value = ecx.const_to_value(val.val)?;
517 let layout = ecx.layout_of(val.ty)?;
518 let ptr = ecx.memory.allocate(layout.size, layout.align, Some(MemoryKind::Stack))?;
519 ecx.write_value_to_ptr(value, ptr.into(), layout.align, val.ty)?;
520 let alloc = ecx.memory.get(ptr.alloc_id)?;
521 Ok(tcx.intern_const_alloc(alloc.clone()))
523 result().expect("unable to convert ConstValue to Allocation")
526 pub fn const_eval_provider<'a, 'tcx>(
527 tcx: TyCtxt<'a, 'tcx, 'tcx>,
528 key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
529 ) -> ::rustc::mir::interpret::ConstEvalResult<'tcx> {
530 trace!("const eval: {:?}", key);
532 let def_id = cid.instance.def.def_id();
534 if let Some(id) = tcx.hir.as_local_node_id(def_id) {
535 let tables = tcx.typeck_tables_of(def_id);
536 let span = tcx.def_span(def_id);
538 // Do match-check before building MIR
539 if tcx.check_match(def_id).is_err() {
540 return Err(ConstEvalErr {
541 error: EvalErrorKind::CheckMatchError.into(),
547 if let hir::BodyOwnerKind::Const = tcx.hir.body_owner_kind(id) {
548 tcx.mir_const_qualif(def_id);
551 // Do not continue into miri if typeck errors occurred; it will fail horribly
552 if tables.tainted_by_errors {
553 return Err(ConstEvalErr {
554 error: EvalErrorKind::CheckMatchError.into(),
561 let (res, ecx) = eval_body_and_ecx(tcx, cid, None, key.param_env);
562 res.and_then(|(mut val, _, miri_ty)| {
563 if tcx.is_static(def_id).is_none() {
564 val = ecx.try_read_by_ref(val, miri_ty)?;
566 Ok(value_to_const_value(&ecx, val, miri_ty))
568 let (trace, span) = ecx.generate_stacktrace(None);
569 let err = ConstEvalErr {
574 if tcx.is_static(def_id).is_some() {
575 err.report_as_error(ecx.tcx, "could not evaluate static initializer");
581 fn numeric_intrinsic<'tcx>(
585 ) -> EvalResult<'tcx, Scalar> {
586 let defined = match kind {
587 Primitive::Int(integer, _) => integer.size().bits() as u8,
588 _ => bug!("invalid `{}` argument: {:?}", name, bits),
590 let extra = 128 - defined as u128;
591 let bits_out = match name {
592 "ctpop" => bits.count_ones() as u128,
593 "ctlz" => bits.leading_zeros() as u128 - extra,
594 "cttz" => (bits << extra).trailing_zeros() as u128 - extra,
595 "bswap" => (bits << extra).swap_bytes(),
596 _ => bug!("not a numeric intrinsic: {}", name),
598 Ok(Scalar::Bits { bits: bits_out, defined })