Skip to content

Commit 66d64ba

Browse files
committed
simplify const_to_allocation_provider because it is used for statics only
1 parent 286fc5c commit 66d64ba

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

src/librustc_mir/interpret/const_eval.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use rustc_data_structures::indexed_vec::{IndexVec, Idx};
2121

2222
use syntax::ast::Mutability;
2323
use syntax::source_map::Span;
24-
use syntax::source_map::DUMMY_SP;
2524

2625
use rustc::mir::interpret::{
2726
EvalResult, EvalError, EvalErrorKind, GlobalId,
@@ -390,30 +389,19 @@ pub fn const_variant_index<'a, 'tcx>(
390389
}
391390

392391
pub fn const_to_allocation_provider<'a, 'tcx>(
393-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
392+
_tcx: TyCtxt<'a, 'tcx, 'tcx>,
394393
val: &'tcx ty::Const<'tcx>,
395394
) -> &'tcx Allocation {
395+
// FIXME: This really does not need to be a query. Instead, we should have a query for statics
396+
// that returns an allocation directly (or an `AllocId`?), after doing a sanity check of the
397+
// value and centralizing error reporting.
396398
match val.val {
397399
ConstValue::ByRef(_, alloc, offset) => {
398400
assert_eq!(offset.bytes(), 0);
399401
return alloc;
400402
},
401-
_ => ()
403+
_ => bug!("const_to_allocation called on non-static"),
402404
}
403-
let result = || -> EvalResult<'tcx, &'tcx Allocation> {
404-
let mut ecx = EvalContext::new(
405-
tcx.at(DUMMY_SP),
406-
ty::ParamEnv::reveal_all(),
407-
CompileTimeEvaluator,
408-
());
409-
let op = const_to_op(&mut ecx, val)?;
410-
// Make a new allocation, copy things there
411-
let ptr = ecx.allocate(op.layout, MemoryKind::Stack)?;
412-
ecx.copy_op(op, ptr.into())?;
413-
let alloc = ecx.memory.get(ptr.to_ptr()?.alloc_id)?;
414-
Ok(tcx.intern_const_alloc(alloc.clone()))
415-
};
416-
result().expect("unable to convert ConstValue to Allocation")
417405
}
418406

419407
pub fn const_eval_provider<'a, 'tcx>(

0 commit comments

Comments
 (0)