1
Fork 0

Auto merge of #136302 - oli-obk:push-vvqmwzunxsrk, r=compiler-errors

Avoid calling the layout_of query in lit_to_const

We got all the information available locally
This commit is contained in:
bors 2025-02-05 15:10:28 +00:00
commit d4bdd1ed55
2 changed files with 28 additions and 21 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,9 +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(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => { (ast::LitKind::Int(n, _), ty::Uint(ui)) if !neg => {
let scalar_int = let scalar_int = trunc(n.get(), *ui);
trunc(if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() }); ty::ValTree::from_scalar_int(scalar_int)
}
(ast::LitKind::Int(n, _), ty::Int(i)) => {
let scalar_int = trunc(
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()),