Skip to content

Commit 2cf14f4

Browse files
authored
Merge pull request #22430 from shulaoda/05-23-fix_hir-ty_saturate_float-to-uint_cast_in_const_eval
fix(hir-ty): saturate float-to-uint cast in const eval
2 parents 4a32bbe + 0bb9a2b commit 2cf14f4

2 files changed

Lines changed: 4 additions & 1 deletion

File tree

crates/hir-ty/src/consteval/tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ fn floating_point_casts() {
287287
check_number(r#"const GOAL: i8 = (0./0.) as i8"#, 0);
288288
check_number(r#"const GOAL: i8 = (1./0.) as i8"#, 127);
289289
check_number(r#"const GOAL: i8 = (-1./0.) as i8"#, -128);
290+
check_number(r#"const GOAL: u8 = (1./0.) as u8"#, 255);
291+
check_number(r#"const GOAL: u8 = 256.0f32 as u8"#, 255);
292+
check_number(r#"const GOAL: u16 = 1e10f32 as u16"#, 65535);
290293
check_number(r#"const GOAL: i64 = 1e18f64 as f32 as i64"#, 999999984306749440);
291294
}
292295

crates/hir-ty/src/mir/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,7 @@ impl<'a, 'db: 'a> Evaluator<'a, 'db> {
15941594
let max = 1i128 << (dest_bits - 1);
15951595
(max - 1, -max)
15961596
} else {
1597-
(1i128 << dest_bits, 0)
1597+
((1i128 << dest_bits) - 1, 0)
15981598
};
15991599
let value = (value as i128).min(max).max(min);
16001600
let result = value.to_le_bytes();

0 commit comments

Comments
 (0)