1
Fork 0

Avoid calling the layout_of query in lit_to_const

This commit is contained in:
Oli Scherer 2025-01-30 14:34:50 +00:00
parent a7ce15d361
commit 0cd51863ff
2 changed files with 26 additions and 23 deletions

View file

@ -105,13 +105,12 @@ fn lit_to_mir_constant<'tcx>(tcx: TyCtxt<'tcx>, lit_input: LitToConstInput<'tcx>
return Const::Ty(Ty::new_error(tcx, guar), ty::Const::new_error(tcx, guar)); return Const::Ty(Ty::new_error(tcx, guar), ty::Const::new_error(tcx, guar));
} }
let trunc = |n| { let trunc = |n, width: ty::UintTy| {
let width = match tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty)) { let width = width
Ok(layout) => layout.size, .normalize(tcx.data_layout.pointer_size.bits().try_into().unwrap())
Err(_) => { .bit_width()
tcx.dcx().bug(format!("couldn't compute width of literal: {:?}", lit_input.lit)) .unwrap();
} let width = Size::from_bits(width);
};
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits()); trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
let result = width.truncate(n); let result = width.truncate(n);
trace!("trunc result: {}", result); trace!("trunc result: {}", result);
@ -145,9 +144,11 @@ fn lit_to_mir_constant<'tcx>(tcx: TyCtxt<'tcx>, lit_input: LitToConstInput<'tcx>
(ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => { (ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1))) ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1)))
} }
(ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => { (ast::LitKind::Int(n, _), ty::Uint(ui)) if !neg => trunc(n.get(), *ui),
trunc(if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() }) (ast::LitKind::Int(n, _), ty::Int(i)) => trunc(
} if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() },
i.to_unsigned(),
),
(ast::LitKind::Float(n, _), ty::Float(fty)) => { (ast::LitKind::Float(n, _), ty::Float(fty)) => {
parse_float_into_constval(*n, *fty, neg).unwrap() parse_float_into_constval(*n, *fty, neg).unwrap()
} }

View file

@ -1,4 +1,5 @@
use rustc_ast as ast; use rustc_abi::Size;
use rustc_ast::{self as ast};
use rustc_hir::LangItem; use rustc_hir::LangItem;
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::mir::interpret::LitToConstInput; use rustc_middle::mir::interpret::LitToConstInput;
@ -17,13 +18,12 @@ pub(crate) fn lit_to_const<'tcx>(
return ty::Const::new_error(tcx, guar); return ty::Const::new_error(tcx, guar);
} }
let trunc = |n| { let trunc = |n, width: ty::UintTy| {
let width = match tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty)) { let width = width
Ok(layout) => layout.size, .normalize(tcx.data_layout.pointer_size.bits().try_into().unwrap())
Err(_) => { .bit_width()
tcx.dcx().bug(format!("couldn't compute width of literal: {:?}", lit_input.lit)) .unwrap();
} let width = Size::from_bits(width);
};
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits()); trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
let result = width.truncate(n); let result = width.truncate(n);
trace!("trunc result: {}", result); trace!("trunc result: {}", result);
@ -55,13 +55,15 @@ pub(crate) fn lit_to_const<'tcx>(
let bytes = data as &[u8]; let bytes = data as &[u8];
ty::ValTree::from_raw_bytes(tcx, bytes) ty::ValTree::from_raw_bytes(tcx, bytes)
} }
(ast::LitKind::Int(n, _), ty::Uint(_)) if !neg => { (ast::LitKind::Int(n, _), ty::Uint(ui)) if !neg => {
let scalar_int = trunc(n.get()); let scalar_int = trunc(n.get(), *ui);
ty::ValTree::from_scalar_int(scalar_int) ty::ValTree::from_scalar_int(scalar_int)
} }
(ast::LitKind::Int(n, _), ty::Int(_)) => { (ast::LitKind::Int(n, _), ty::Int(i)) => {
let scalar_int = let scalar_int = trunc(
trunc(if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() }); if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() },
i.to_unsigned(),
);
ty::ValTree::from_scalar_int(scalar_int) ty::ValTree::from_scalar_int(scalar_int)
} }
(ast::LitKind::Bool(b), ty::Bool) => ty::ValTree::from_scalar_int((*b).into()), (ast::LitKind::Bool(b), ty::Bool) => ty::ValTree::from_scalar_int((*b).into()),