Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ac4bb7f

Browse files
committedMar 12, 2025
Simplify lit_to_mir_constant a bit
1 parent 381e3a6 commit ac4bb7f

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed
 

‎compiler/rustc_mir_build/src/builder/expr/as_constant.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,8 @@ fn lit_to_mir_constant<'tcx>(tcx: TyCtxt<'tcx>, lit_input: LitToConstInput<'tcx>
105105
return Const::Ty(Ty::new_error(tcx, guar), ty::Const::new_error(tcx, guar));
106106
}
107107

108-
let trunc = |n, width: ty::UintTy| {
109-
let width = width
110-
.normalize(tcx.data_layout.pointer_size.bits().try_into().unwrap())
111-
.bit_width()
112-
.unwrap();
113-
let width = Size::from_bits(width);
108+
let trunc = |n| {
109+
let width = ty.primitive_size(tcx);
114110
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
115111
let result = width.truncate(n);
116112
trace!("trunc result: {}", result);
@@ -144,11 +140,10 @@ fn lit_to_mir_constant<'tcx>(tcx: TyCtxt<'tcx>, lit_input: LitToConstInput<'tcx>
144140
(ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
145141
ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1)))
146142
}
147-
(ast::LitKind::Int(n, _), ty::Uint(ui)) if !neg => trunc(n.get(), *ui),
148-
(ast::LitKind::Int(n, _), ty::Int(i)) => trunc(
149-
if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() },
150-
i.to_unsigned(),
151-
),
143+
(ast::LitKind::Int(n, _), ty::Uint(_)) if !neg => trunc(n.get()),
144+
(ast::LitKind::Int(n, _), ty::Int(_)) => {
145+
trunc(if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() })
146+
}
152147
(ast::LitKind::Float(n, _), ty::Float(fty)) => {
153148
parse_float_into_constval(*n, *fty, neg).unwrap()
154149
}

0 commit comments

Comments
 (0)
Failed to load comments.