use rustc_ast as ast;
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
use rustc_middle::ty::{self, ParamEnv, ScalarInt, TyCtxt};
+use rustc_span::DUMMY_SP;
pub(crate) fn lit_to_const<'tcx>(
tcx: TyCtxt<'tcx>,
let trunc = |n| {
let param_ty = ParamEnv::reveal_all().and(ty);
- let width = tcx.layout_of(param_ty).map_err(|_| LitToConstError::Reported)?.size;
+ let width = tcx
+ .layout_of(param_ty)
+ .map_err(|_| {
+ LitToConstError::Reported(tcx.sess.delay_span_bug(
+ DUMMY_SP,
+ format!("couldn't compute width of literal: {:?}", lit_input.lit),
+ ))
+ })?
+ .size;
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
let result = width.truncate(n);
trace!("trunc result: {}", result);
}
(ast::LitKind::Bool(b), ty::Bool) => ty::ValTree::from_scalar_int((*b).into()),
(ast::LitKind::Char(c), ty::Char) => ty::ValTree::from_scalar_int((*c).into()),
- (ast::LitKind::Err, _) => return Err(LitToConstError::Reported),
+ (ast::LitKind::Err, _) => {
+ return Err(LitToConstError::Reported(
+ tcx.sess.delay_span_bug(DUMMY_SP, "encountered LitKind::Err during mir build"),
+ ));
+ }
_ => return Err(LitToConstError::TypeError),
};
- Ok(ty::Const::from_value(tcx, valtree, ty))
+ Ok(tcx.mk_const(valtree, ty))
}