diff options
| author | Oli Scherer <github333195615777966@oli-obk.de> | 2025-01-30 12:37:40 +0000 |
|---|---|---|
| committer | Oli Scherer <github333195615777966@oli-obk.de> | 2025-01-30 14:33:58 +0000 |
| commit | a7ce15d361ca3d774981e35c3c03d8dcccf0ed88 (patch) | |
| tree | 13e09c493f510e6d27f7e87208fa4f797becec37 | |
| parent | 5e5567993d8ee5f8b260cebe96f1714d134d1d96 (diff) | |
Don't allow negative unsigned literals
| -rw-r--r-- | compiler/rustc_mir_build/src/thir/constant.rs | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/compiler/rustc_mir_build/src/thir/constant.rs b/compiler/rustc_mir_build/src/thir/constant.rs index 49db522cf0e..98edd3fea47 100644 --- a/compiler/rustc_mir_build/src/thir/constant.rs +++ b/compiler/rustc_mir_build/src/thir/constant.rs @@ -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) |
