]> git.lizzy.rs Git - rust.git/commitdiff
simplify const_to_allocation_provider because it is used for statics only
authorRalf Jung <post@ralfj.de>
Thu, 23 Aug 2018 21:38:47 +0000 (23:38 +0200)
committerRalf Jung <post@ralfj.de>
Mon, 27 Aug 2018 16:12:49 +0000 (18:12 +0200)
src/librustc_mir/interpret/const_eval.rs

index 9840fb2cfd7dbc04358870bef91eddbd899d89aa..ec3e46f7067e63ed8911d11ec10d0dca2349faef 100644 (file)
@@ -21,7 +21,6 @@
 
 use syntax::ast::Mutability;
 use syntax::source_map::Span;
-use syntax::source_map::DUMMY_SP;
 
 use rustc::mir::interpret::{
     EvalResult, EvalError, EvalErrorKind, GlobalId,
@@ -390,30 +389,19 @@ pub fn const_variant_index<'a, 'tcx>(
 }
 
 pub fn const_to_allocation_provider<'a, 'tcx>(
-    tcx: TyCtxt<'a, 'tcx, 'tcx>,
+    _tcx: TyCtxt<'a, 'tcx, 'tcx>,
     val: &'tcx ty::Const<'tcx>,
 ) -> &'tcx Allocation {
+    // FIXME: This really does not need to be a query.  Instead, we should have a query for statics
+    // that returns an allocation directly (or an `AllocId`?), after doing a sanity check of the
+    // value and centralizing error reporting.
     match val.val {
         ConstValue::ByRef(_, alloc, offset) => {
             assert_eq!(offset.bytes(), 0);
             return alloc;
         },
-        _ => ()
+        _ => bug!("const_to_allocation called on non-static"),
     }
-    let result = || -> EvalResult<'tcx, &'tcx Allocation> {
-        let mut ecx = EvalContext::new(
-            tcx.at(DUMMY_SP),
-            ty::ParamEnv::reveal_all(),
-            CompileTimeEvaluator,
-            ());
-        let op = const_to_op(&mut ecx, val)?;
-        // Make a new allocation, copy things there
-        let ptr = ecx.allocate(op.layout, MemoryKind::Stack)?;
-        ecx.copy_op(op, ptr.into())?;
-        let alloc = ecx.memory.get(ptr.to_ptr()?.alloc_id)?;
-        Ok(tcx.intern_const_alloc(alloc.clone()))
-    };
-    result().expect("unable to convert ConstValue to Allocation")
 }
 
 pub fn const_eval_provider<'a, 'tcx>(