Don't allow negative unsigned literals

This commit is contained in:
Oli Scherer 2025-01-30 12:37:40 +00:00
parent 5e5567993d
commit a7ce15d361

View file

@ -55,7 +55,11 @@ pub(crate) fn lit_to_const<'tcx>(
let bytes = data as &[u8];
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(_)) if !neg => {
let scalar_int = trunc(n.get());
ty::ValTree::from_scalar_int(scalar_int)
}
(ast::LitKind::Int(n, _), ty::Int(_)) => {
let scalar_int =
trunc(if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() });
ty::ValTree::from_scalar_int(scalar_int)