diff --git a/crates/hir-ty/src/consteval/tests.rs b/crates/hir-ty/src/consteval/tests.rs index f15866106928..5421b97db238 100644 --- a/crates/hir-ty/src/consteval/tests.rs +++ b/crates/hir-ty/src/consteval/tests.rs @@ -287,6 +287,9 @@ fn floating_point_casts() { check_number(r#"const GOAL: i8 = (0./0.) as i8"#, 0); check_number(r#"const GOAL: i8 = (1./0.) as i8"#, 127); check_number(r#"const GOAL: i8 = (-1./0.) as i8"#, -128); + check_number(r#"const GOAL: u8 = (1./0.) as u8"#, 255); + check_number(r#"const GOAL: u8 = 256.0f32 as u8"#, 255); + check_number(r#"const GOAL: u16 = 1e10f32 as u16"#, 65535); check_number(r#"const GOAL: i64 = 1e18f64 as f32 as i64"#, 999999984306749440); } diff --git a/crates/hir-ty/src/mir/eval.rs b/crates/hir-ty/src/mir/eval.rs index a8879521eba6..97aeb7d41260 100644 --- a/crates/hir-ty/src/mir/eval.rs +++ b/crates/hir-ty/src/mir/eval.rs @@ -1594,7 +1594,7 @@ impl<'a, 'db: 'a> Evaluator<'a, 'db> { let max = 1i128 << (dest_bits - 1); (max - 1, -max) } else { - (1i128 << dest_bits, 0) + ((1i128 << dest_bits) - 1, 0) }; let value = (value as i128).min(max).max(min); let result = value.to_le_bytes();