]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/const_eval.rs
Make const_eval_raw query return just an AllocId
[rust.git] / src / librustc_mir / const_eval.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Not in interpret to make sure we do not use private implementation details
12
13 use std::fmt;
14 use std::error::Error;
15 use std::borrow::{Borrow, Cow};
16 use std::hash::Hash;
17 use std::collections::hash_map::Entry;
18
19 use rustc::hir::{self, def_id::DefId};
20 use rustc::hir::def::Def;
21 use rustc::mir::interpret::{ConstEvalErr, ErrorHandled};
22 use rustc::mir;
23 use rustc::ty::{self, TyCtxt, Instance, query::TyCtxtAt};
24 use rustc::ty::layout::{self, LayoutOf, TyLayout, VariantIdx};
25 use rustc::ty::subst::Subst;
26 use rustc::traits::Reveal;
27 use rustc_data_structures::indexed_vec::IndexVec;
28 use rustc_data_structures::fx::FxHashMap;
29 use rustc::util::common::ErrorReported;
30
31 use syntax::ast::Mutability;
32 use syntax::source_map::{Span, DUMMY_SP};
33
34 use crate::interpret::{self,
35     PlaceTy, MPlaceTy, MemPlace, OpTy, Operand, Immediate, Scalar, RawConst, ConstValue, Pointer,
36     EvalResult, EvalError, EvalErrorKind, GlobalId, EvalContext, StackPopCleanup,
37     Allocation, AllocId, MemoryKind,
38     snapshot, RefTracking,
39 };
40
41 /// Number of steps until the detector even starts doing anything.
42 /// Also, a warning is shown to the user when this number is reached.
43 const STEPS_UNTIL_DETECTOR_ENABLED: isize = 1_000_000;
44 /// The number of steps between loop detector snapshots.
45 /// Should be a power of two for performance reasons.
46 const DETECTOR_SNAPSHOT_PERIOD: isize = 256;
47
48 pub fn mk_borrowck_eval_cx<'a, 'mir, 'tcx>(
49     tcx: TyCtxt<'a, 'tcx, 'tcx>,
50     instance: Instance<'tcx>,
51     mir: &'mir mir::Mir<'tcx>,
52     span: Span,
53 ) -> EvalResult<'tcx, CompileTimeEvalContext<'a, 'mir, 'tcx>> {
54     debug!("mk_borrowck_eval_cx: {:?}", instance);
55     let param_env = tcx.param_env(instance.def_id());
56     let mut ecx = EvalContext::new(tcx.at(span), param_env, CompileTimeInterpreter::new());
57     // insert a stack frame so any queries have the correct substs
58     // cannot use `push_stack_frame`; if we do `const_prop` explodes
59     ecx.stack.push(interpret::Frame {
60         block: mir::START_BLOCK,
61         locals: IndexVec::new(),
62         instance,
63         span,
64         mir,
65         return_place: None,
66         return_to_block: StackPopCleanup::Goto(None), // never pop
67         stmt: 0,
68     });
69     Ok(ecx)
70 }
71
72 pub fn mk_eval_cx<'a, 'tcx>(
73     tcx: TyCtxt<'a, 'tcx, 'tcx>,
74     instance: Instance<'tcx>,
75     param_env: ty::ParamEnv<'tcx>,
76 ) -> EvalResult<'tcx, CompileTimeEvalContext<'a, 'tcx, 'tcx>> {
77     debug!("mk_eval_cx: {:?}, {:?}", instance, param_env);
78     let span = tcx.def_span(instance.def_id());
79     let mut ecx = EvalContext::new(tcx.at(span), param_env, CompileTimeInterpreter::new());
80     let mir = ecx.load_mir(instance.def)?;
81     // insert a stack frame so any queries have the correct substs
82     ecx.push_stack_frame(
83         instance,
84         mir.span,
85         mir,
86         None,
87         StackPopCleanup::Goto(None), // never pop
88     )?;
89     Ok(ecx)
90 }
91
92 pub(crate) fn eval_promoted<'a, 'mir, 'tcx>(
93     tcx: TyCtxt<'a, 'tcx, 'tcx>,
94     cid: GlobalId<'tcx>,
95     mir: &'mir mir::Mir<'tcx>,
96     param_env: ty::ParamEnv<'tcx>,
97 ) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
98     let mut ecx = mk_borrowck_eval_cx(tcx, cid.instance, mir, DUMMY_SP).unwrap();
99     eval_body_using_ecx(&mut ecx, cid, Some(mir), param_env)
100 }
101
102 // FIXME: This thing is a bad hack. We should get rid of it.  Ideally constants are always
103 // in an allocation.
104 pub fn op_to_const<'tcx>(
105     ecx: &CompileTimeEvalContext<'_, '_, 'tcx>,
106     op: OpTy<'tcx>,
107     may_normalize: bool,
108 ) -> EvalResult<'tcx, &'tcx ty::Const<'tcx>> {
109     // We do not normalize just any data.  Only scalar layout and fat pointers.
110     let normalize = may_normalize
111         && match op.layout.abi {
112             layout::Abi::Scalar(..) => true,
113             layout::Abi::ScalarPair(..) => {
114                 // Must be a fat pointer
115                 op.layout.ty.builtin_deref(true).is_some()
116             },
117             _ => false,
118         };
119     let normalized_op = if normalize {
120         ecx.try_read_immediate(op)?
121     } else {
122         match op.op {
123             Operand::Indirect(mplace) => Err(mplace),
124             Operand::Immediate(val) => Ok(val)
125         }
126     };
127     let val = match normalized_op {
128         Err(MemPlace { ptr, align, meta }) => {
129             // extract alloc-offset pair
130             assert!(meta.is_none());
131             let ptr = ptr.to_ptr()?;
132             let alloc = ecx.memory.get(ptr.alloc_id)?;
133             assert!(alloc.align.abi() >= align.abi());
134             assert!(alloc.bytes.len() as u64 - ptr.offset.bytes() >= op.layout.size.bytes());
135             let mut alloc = alloc.clone();
136             alloc.align = align;
137             // FIXME shouldn't it be the case that `mark_static_initialized` has already
138             // interned this?  I thought that is the entire point of that `FinishStatic` stuff?
139             let alloc = ecx.tcx.intern_const_alloc(alloc);
140             ConstValue::ByRef(ptr.alloc_id, alloc, ptr.offset)
141         },
142         Ok(Immediate::Scalar(x)) =>
143             ConstValue::Scalar(x.not_undef()?),
144         Ok(Immediate::ScalarPair(a, b)) =>
145             ConstValue::ScalarPair(a.not_undef()?, b.not_undef()?),
146     };
147     Ok(ty::Const::from_const_value(ecx.tcx.tcx, val, op.layout.ty))
148 }
149
150 fn eval_body_and_ecx<'a, 'mir, 'tcx>(
151     tcx: TyCtxt<'a, 'tcx, 'tcx>,
152     cid: GlobalId<'tcx>,
153     mir: Option<&'mir mir::Mir<'tcx>>,
154     param_env: ty::ParamEnv<'tcx>,
155 ) -> (EvalResult<'tcx, MPlaceTy<'tcx>>, CompileTimeEvalContext<'a, 'mir, 'tcx>) {
156     // we start out with the best span we have
157     // and try improving it down the road when more information is available
158     let span = tcx.def_span(cid.instance.def_id());
159     let span = mir.map(|mir| mir.span).unwrap_or(span);
160     let mut ecx = EvalContext::new(tcx.at(span), param_env, CompileTimeInterpreter::new());
161     let r = eval_body_using_ecx(&mut ecx, cid, mir, param_env);
162     (r, ecx)
163 }
164
165 // Returns a pointer to where the result lives
166 fn eval_body_using_ecx<'mir, 'tcx>(
167     ecx: &mut CompileTimeEvalContext<'_, 'mir, 'tcx>,
168     cid: GlobalId<'tcx>,
169     mir: Option<&'mir mir::Mir<'tcx>>,
170     param_env: ty::ParamEnv<'tcx>,
171 ) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
172     debug!("eval_body_using_ecx: {:?}, {:?}", cid, param_env);
173     let tcx = ecx.tcx.tcx;
174     let mut mir = match mir {
175         Some(mir) => mir,
176         None => ecx.load_mir(cid.instance.def)?,
177     };
178     if let Some(index) = cid.promoted {
179         mir = &mir.promoted[index];
180     }
181     let layout = ecx.layout_of(mir.return_ty().subst(tcx, cid.instance.substs))?;
182     assert!(!layout.is_unsized());
183     let ret = ecx.allocate(layout, MemoryKind::Stack)?;
184
185     let name = ty::tls::with(|tcx| tcx.item_path_str(cid.instance.def_id()));
186     let prom = cid.promoted.map_or(String::new(), |p| format!("::promoted[{:?}]", p));
187     trace!("eval_body_using_ecx: pushing stack frame for global: {}{}", name, prom);
188     assert!(mir.arg_count == 0);
189     ecx.push_stack_frame(
190         cid.instance,
191         mir.span,
192         mir,
193         Some(ret.into()),
194         StackPopCleanup::None { cleanup: false },
195     )?;
196
197     // The main interpreter loop.
198     ecx.run()?;
199
200     // Intern the result
201     let internally_mutable = !layout.ty.is_freeze(tcx, param_env, mir.span);
202     let is_static = tcx.is_static(cid.instance.def_id());
203     let mutability = if is_static == Some(hir::Mutability::MutMutable) || internally_mutable {
204         Mutability::Mutable
205     } else {
206         Mutability::Immutable
207     };
208     ecx.memory.intern_static(ret.ptr.to_ptr()?.alloc_id, mutability)?;
209
210     debug!("eval_body_using_ecx done: {:?}", *ret);
211     Ok(ret)
212 }
213
214 impl<'tcx> Into<EvalError<'tcx>> for ConstEvalError {
215     fn into(self) -> EvalError<'tcx> {
216         EvalErrorKind::MachineError(self.to_string()).into()
217     }
218 }
219
220 #[derive(Clone, Debug)]
221 enum ConstEvalError {
222     NeedsRfc(String),
223 }
224
225 impl fmt::Display for ConstEvalError {
226     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
227         use self::ConstEvalError::*;
228         match *self {
229             NeedsRfc(ref msg) => {
230                 write!(
231                     f,
232                     "\"{}\" needs an rfc before being allowed inside constants",
233                     msg
234                 )
235             }
236         }
237     }
238 }
239
240 impl Error for ConstEvalError {
241     fn description(&self) -> &str {
242         use self::ConstEvalError::*;
243         match *self {
244             NeedsRfc(_) => "this feature needs an rfc before being allowed inside constants",
245         }
246     }
247
248     fn cause(&self) -> Option<&dyn Error> {
249         None
250     }
251 }
252
253 // Extra machine state for CTFE, and the Machine instance
254 pub struct CompileTimeInterpreter<'a, 'mir, 'tcx: 'a+'mir> {
255     /// When this value is negative, it indicates the number of interpreter
256     /// steps *until* the loop detector is enabled. When it is positive, it is
257     /// the number of steps after the detector has been enabled modulo the loop
258     /// detector period.
259     pub(super) steps_since_detector_enabled: isize,
260
261     /// Extra state to detect loops.
262     pub(super) loop_detector: snapshot::InfiniteLoopDetector<'a, 'mir, 'tcx>,
263 }
264
265 impl<'a, 'mir, 'tcx> CompileTimeInterpreter<'a, 'mir, 'tcx> {
266     fn new() -> Self {
267         CompileTimeInterpreter {
268             loop_detector: Default::default(),
269             steps_since_detector_enabled: -STEPS_UNTIL_DETECTOR_ENABLED,
270         }
271     }
272 }
273
274 impl<K: Hash + Eq, V> interpret::AllocMap<K, V> for FxHashMap<K, V> {
275     #[inline(always)]
276     fn contains_key<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> bool
277         where K: Borrow<Q>
278     {
279         FxHashMap::contains_key(self, k)
280     }
281
282     #[inline(always)]
283     fn insert(&mut self, k: K, v: V) -> Option<V>
284     {
285         FxHashMap::insert(self, k, v)
286     }
287
288     #[inline(always)]
289     fn remove<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> Option<V>
290         where K: Borrow<Q>
291     {
292         FxHashMap::remove(self, k)
293     }
294
295     #[inline(always)]
296     fn filter_map_collect<T>(&self, mut f: impl FnMut(&K, &V) -> Option<T>) -> Vec<T> {
297         self.iter()
298             .filter_map(move |(k, v)| f(k, &*v))
299             .collect()
300     }
301
302     #[inline(always)]
303     fn get_or<E>(
304         &self,
305         k: K,
306         vacant: impl FnOnce() -> Result<V, E>
307     ) -> Result<&V, E>
308     {
309         match self.get(&k) {
310             Some(v) => Ok(v),
311             None => {
312                 vacant()?;
313                 bug!("The CTFE machine shouldn't ever need to extend the alloc_map when reading")
314             }
315         }
316     }
317
318     #[inline(always)]
319     fn get_mut_or<E>(
320         &mut self,
321         k: K,
322         vacant: impl FnOnce() -> Result<V, E>
323     ) -> Result<&mut V, E>
324     {
325         match self.entry(k) {
326             Entry::Occupied(e) => Ok(e.into_mut()),
327             Entry::Vacant(e) => {
328                 let v = vacant()?;
329                 Ok(e.insert(v))
330             }
331         }
332     }
333 }
334
335 type CompileTimeEvalContext<'a, 'mir, 'tcx> =
336     EvalContext<'a, 'mir, 'tcx, CompileTimeInterpreter<'a, 'mir, 'tcx>>;
337
338 impl interpret::MayLeak for ! {
339     #[inline(always)]
340     fn may_leak(self) -> bool {
341         // `self` is uninhabited
342         self
343     }
344 }
345
346 impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
347     for CompileTimeInterpreter<'a, 'mir, 'tcx>
348 {
349     type MemoryKinds = !;
350     type AllocExtra = ();
351     type PointerTag = ();
352
353     type MemoryMap = FxHashMap<AllocId, (MemoryKind<!>, Allocation)>;
354
355     const STATIC_KIND: Option<!> = None; // no copying of statics allowed
356
357     #[inline(always)]
358     fn enforce_validity(_ecx: &EvalContext<'a, 'mir, 'tcx, Self>) -> bool {
359         false // for now, we don't enforce validity
360     }
361
362     fn find_fn(
363         ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
364         instance: ty::Instance<'tcx>,
365         args: &[OpTy<'tcx>],
366         dest: Option<PlaceTy<'tcx>>,
367         ret: Option<mir::BasicBlock>,
368     ) -> EvalResult<'tcx, Option<&'mir mir::Mir<'tcx>>> {
369         debug!("eval_fn_call: {:?}", instance);
370         // Execution might have wandered off into other crates, so we cannot to a stability-
371         // sensitive check here.  But we can at least rule out functions that are not const
372         // at all.
373         if !ecx.tcx.is_const_fn_raw(instance.def_id()) {
374             // Some functions we support even if they are non-const -- but avoid testing
375             // that for const fn!  We certainly do *not* want to actually call the fn
376             // though, so be sure we return here.
377             return if ecx.hook_fn(instance, args, dest)? {
378                 ecx.goto_block(ret)?; // fully evaluated and done
379                 Ok(None)
380             } else {
381                 err!(MachineError(format!("calling non-const function `{}`", instance)))
382             };
383         }
384         // This is a const fn. Call it.
385         Ok(Some(match ecx.load_mir(instance.def) {
386             Ok(mir) => mir,
387             Err(err) => {
388                 if let EvalErrorKind::NoMirFor(ref path) = err.kind {
389                     return Err(
390                         ConstEvalError::NeedsRfc(format!("calling extern function `{}`", path))
391                             .into(),
392                     );
393                 }
394                 return Err(err);
395             }
396         }))
397     }
398
399     fn call_intrinsic(
400         ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
401         instance: ty::Instance<'tcx>,
402         args: &[OpTy<'tcx>],
403         dest: PlaceTy<'tcx>,
404     ) -> EvalResult<'tcx> {
405         if ecx.emulate_intrinsic(instance, args, dest)? {
406             return Ok(());
407         }
408         // An intrinsic that we do not support
409         let intrinsic_name = &ecx.tcx.item_name(instance.def_id()).as_str()[..];
410         Err(
411             ConstEvalError::NeedsRfc(format!("calling intrinsic `{}`", intrinsic_name)).into()
412         )
413     }
414
415     fn ptr_op(
416         _ecx: &EvalContext<'a, 'mir, 'tcx, Self>,
417         _bin_op: mir::BinOp,
418         _left: Scalar,
419         _left_layout: TyLayout<'tcx>,
420         _right: Scalar,
421         _right_layout: TyLayout<'tcx>,
422     ) -> EvalResult<'tcx, (Scalar, bool)> {
423         Err(
424             ConstEvalError::NeedsRfc("pointer arithmetic or comparison".to_string()).into(),
425         )
426     }
427
428     fn find_foreign_static(
429         _tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
430         _def_id: DefId,
431     ) -> EvalResult<'tcx, Cow<'tcx, Allocation<Self::PointerTag>>> {
432         err!(ReadForeignStatic)
433     }
434
435     #[inline(always)]
436     fn adjust_static_allocation(
437         alloc: &'_ Allocation
438     ) -> Cow<'_, Allocation<Self::PointerTag>> {
439         // We do not use a tag so we can just cheaply forward the reference
440         Cow::Borrowed(alloc)
441     }
442
443     fn box_alloc(
444         _ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
445         _dest: PlaceTy<'tcx>,
446     ) -> EvalResult<'tcx> {
447         Err(
448             ConstEvalError::NeedsRfc("heap allocations via `box` keyword".to_string()).into(),
449         )
450     }
451
452     fn before_terminator(ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>) -> EvalResult<'tcx> {
453         {
454             let steps = &mut ecx.machine.steps_since_detector_enabled;
455
456             *steps += 1;
457             if *steps < 0 {
458                 return Ok(());
459             }
460
461             *steps %= DETECTOR_SNAPSHOT_PERIOD;
462             if *steps != 0 {
463                 return Ok(());
464             }
465         }
466
467         let span = ecx.frame().span;
468         ecx.machine.loop_detector.observe_and_analyze(
469             &ecx.tcx,
470             span,
471             &ecx.memory,
472             &ecx.stack[..],
473         )
474     }
475
476     #[inline(always)]
477     fn tag_new_allocation(
478         _ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
479         ptr: Pointer,
480         _kind: MemoryKind<Self::MemoryKinds>,
481     ) -> EvalResult<'tcx, Pointer> {
482         Ok(ptr)
483     }
484 }
485
486 /// Project to a field of a (variant of a) const
487 pub fn const_field<'a, 'tcx>(
488     tcx: TyCtxt<'a, 'tcx, 'tcx>,
489     param_env: ty::ParamEnv<'tcx>,
490     instance: ty::Instance<'tcx>,
491     variant: Option<VariantIdx>,
492     field: mir::Field,
493     value: &'tcx ty::Const<'tcx>,
494 ) -> ::rustc::mir::interpret::ConstEvalResult<'tcx> {
495     trace!("const_field: {:?}, {:?}, {:?}", instance, field, value);
496     let ecx = mk_eval_cx(tcx, instance, param_env).unwrap();
497     let result = (|| {
498         // get the operand again
499         let op = ecx.const_to_op(value)?;
500         // downcast
501         let down = match variant {
502             None => op,
503             Some(variant) => ecx.operand_downcast(op, variant)?
504         };
505         // then project
506         let field = ecx.operand_field(down, field.index() as u64)?;
507         // and finally move back to the const world, always normalizing because
508         // this is not called for statics.
509         op_to_const(&ecx, field, true)
510     })();
511     result.map_err(|error| {
512         let err = error_to_const_error(&ecx, error);
513         err.report_as_error(ecx.tcx, "could not access field of constant");
514         ErrorHandled::Reported
515     })
516 }
517
518 pub fn const_variant_index<'a, 'tcx>(
519     tcx: TyCtxt<'a, 'tcx, 'tcx>,
520     param_env: ty::ParamEnv<'tcx>,
521     instance: ty::Instance<'tcx>,
522     val: &'tcx ty::Const<'tcx>,
523 ) -> EvalResult<'tcx, VariantIdx> {
524     trace!("const_variant_index: {:?}, {:?}", instance, val);
525     let ecx = mk_eval_cx(tcx, instance, param_env).unwrap();
526     let op = ecx.const_to_op(val)?;
527     Ok(ecx.read_discriminant(op)?.1)
528 }
529
530 pub fn error_to_const_error<'a, 'mir, 'tcx>(
531     ecx: &EvalContext<'a, 'mir, 'tcx, CompileTimeInterpreter<'a, 'mir, 'tcx>>,
532     mut error: EvalError<'tcx>
533 ) -> ConstEvalErr<'tcx> {
534     error.print_backtrace();
535     let stacktrace = ecx.generate_stacktrace(None);
536     ConstEvalErr { error: error.kind, stacktrace, span: ecx.tcx.span }
537 }
538
539 fn validate_and_turn_into_const<'a, 'tcx>(
540     tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
541     constant: RawConst<'tcx>,
542     key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
543 ) -> ::rustc::mir::interpret::ConstEvalResult<'tcx> {
544     let cid = key.value;
545     let ecx = mk_eval_cx(tcx, cid.instance, key.param_env).unwrap();
546     let val = (|| {
547         let op = ecx.raw_const_to_mplace(constant)?.into();
548         // FIXME: Once the visitor infrastructure landed, change validation to
549         // work directly on `MPlaceTy`.
550         let mut ref_tracking = RefTracking::new(op);
551         while let Some((op, path)) = ref_tracking.todo.pop() {
552             ecx.validate_operand(
553                 op,
554                 path,
555                 Some(&mut ref_tracking),
556                 /* const_mode */ true,
557             )?;
558         }
559         // Now that we validated, turn this into a proper constant
560         let def_id = cid.instance.def.def_id();
561         let normalize = tcx.is_static(def_id).is_none() && cid.promoted.is_none();
562         op_to_const(&ecx, op, normalize)
563     })();
564
565     val.map_err(|error| {
566         let err = error_to_const_error(&ecx, error);
567         match err.struct_error(ecx.tcx, "it is undefined behavior to use this value") {
568             Ok(mut diag) => {
569                 diag.note("The rules on what exactly is undefined behavior aren't clear, \
570                     so this check might be overzealous. Please open an issue on the rust compiler \
571                     repository if you believe it should not be considered undefined behavior",
572                 );
573                 diag.emit();
574                 ErrorHandled::Reported
575             }
576             Err(err) => err,
577         }
578     })
579 }
580
581 pub fn const_eval_provider<'a, 'tcx>(
582     tcx: TyCtxt<'a, 'tcx, 'tcx>,
583     key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
584 ) -> ::rustc::mir::interpret::ConstEvalResult<'tcx> {
585     // see comment in const_eval_provider for what we're doing here
586     if key.param_env.reveal == Reveal::All {
587         let mut key = key.clone();
588         key.param_env.reveal = Reveal::UserFacing;
589         match tcx.const_eval(key) {
590             // try again with reveal all as requested
591             Err(ErrorHandled::TooGeneric) => {
592                 // Promoteds should never be "too generic" when getting evaluated.
593                 // They either don't get evaluated, or we are in a monomorphic context
594                 assert!(key.value.promoted.is_none());
595             },
596             // dedupliate calls
597             other => return other,
598         }
599     }
600     tcx.const_eval_raw(key).and_then(|val| {
601         validate_and_turn_into_const(tcx, val, key)
602     })
603 }
604
605 pub fn const_eval_raw_provider<'a, 'tcx>(
606     tcx: TyCtxt<'a, 'tcx, 'tcx>,
607     key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
608 ) -> ::rustc::mir::interpret::ConstEvalRawResult<'tcx> {
609     // Because the constant is computed twice (once per value of `Reveal`), we are at risk of
610     // reporting the same error twice here. To resolve this, we check whether we can evaluate the
611     // constant in the more restrictive `Reveal::UserFacing`, which most likely already was
612     // computed. For a large percentage of constants that will already have succeeded. Only
613     // associated constants of generic functions will fail due to not enough monomorphization
614     // information being available.
615
616     // In case we fail in the `UserFacing` variant, we just do the real computation.
617     if key.param_env.reveal == Reveal::All {
618         let mut key = key.clone();
619         key.param_env.reveal = Reveal::UserFacing;
620         match tcx.const_eval_raw(key) {
621             // try again with reveal all as requested
622             Err(ErrorHandled::TooGeneric) => {},
623             // dedupliate calls
624             other => return other,
625         }
626     }
627     // the first trace is for replicating an ice
628     // There's no tracking issue, but the next two lines concatenated link to the discussion on
629     // zulip. It's not really possible to test this, because it doesn't show up in diagnostics
630     // or MIR.
631     // https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/
632     // subject/anon_const_instance_printing/near/135980032
633     trace!("const eval: {}", key.value.instance);
634     trace!("const eval: {:?}", key);
635
636     let cid = key.value;
637     let def_id = cid.instance.def.def_id();
638
639     if let Some(id) = tcx.hir.as_local_node_id(def_id) {
640         let tables = tcx.typeck_tables_of(def_id);
641
642         // Do match-check before building MIR
643         if let Err(ErrorReported) = tcx.check_match(def_id) {
644             return Err(ErrorHandled::Reported)
645         }
646
647         if let hir::BodyOwnerKind::Const = tcx.hir.body_owner_kind(id) {
648             tcx.mir_const_qualif(def_id);
649         }
650
651         // Do not continue into miri if typeck errors occurred; it will fail horribly
652         if tables.tainted_by_errors {
653             return Err(ErrorHandled::Reported)
654         }
655     };
656
657     let (res, ecx) = eval_body_and_ecx(tcx, cid, None, key.param_env);
658     res.and_then(|place| {
659         Ok(RawConst {
660             alloc_id: place.to_ptr().expect("we allocated this ptr!").alloc_id,
661             ty: place.layout.ty
662         })
663     }).map_err(|error| {
664         let err = error_to_const_error(&ecx, error);
665         // errors in statics are always emitted as fatal errors
666         if tcx.is_static(def_id).is_some() {
667             let err = err.report_as_error(ecx.tcx, "could not evaluate static initializer");
668             // check that a static never produces `TooGeneric`
669             if tcx.sess.err_count() == 0 {
670                 span_bug!(ecx.tcx.span, "static eval failure didn't emit an error: {:#?}", err);
671             }
672             err
673         } else if def_id.is_local() {
674             // constant defined in this crate, we can figure out a lint level!
675             match tcx.describe_def(def_id) {
676                 // constants never produce a hard error at the definition site. Anything else is
677                 // a backwards compatibility hazard (and will break old versions of winapi for sure)
678                 //
679                 // note that validation may still cause a hard error on this very same constant,
680                 // because any code that existed before validation could not have failed validation
681                 // thus preventing such a hard error from being a backwards compatibility hazard
682                 Some(Def::Const(_)) | Some(Def::AssociatedConst(_)) => {
683                     let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
684                     err.report_as_lint(
685                         tcx.at(tcx.def_span(def_id)),
686                         "any use of this value will cause an error",
687                         node_id,
688                     )
689                 },
690                 // promoting runtime code is only allowed to error if it references broken constants
691                 // any other kind of error will be reported to the user as a deny-by-default lint
692                 _ => if let Some(p) = cid.promoted {
693                     let span = tcx.optimized_mir(def_id).promoted[p].span;
694                     if let EvalErrorKind::ReferencedConstant = err.error {
695                         err.report_as_error(
696                             tcx.at(span),
697                             "evaluation of constant expression failed",
698                         )
699                     } else {
700                         err.report_as_lint(
701                             tcx.at(span),
702                             "reaching this expression at runtime will panic or abort",
703                             tcx.hir.as_local_node_id(def_id).unwrap(),
704                         )
705                     }
706                 // anything else (array lengths, enum initializers, constant patterns) are reported
707                 // as hard errors
708                 } else {
709                     err.report_as_error(
710                         ecx.tcx,
711                         "evaluation of constant value failed",
712                     )
713                 },
714             }
715         } else {
716             // use of broken constant from other crate
717             err.report_as_error(ecx.tcx, "could not evaluate constant")
718         }
719     })
720 }