Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #22770 from qti3e/p/cttz-zero
fix: clamp cttz const-eval result to type width
| -rw-r--r-- | crates/hir-ty/src/consteval/tests/intrinsics.rs | 27 | ||||
| -rw-r--r-- | crates/hir-ty/src/mir/eval/shim.rs | 4 |
2 files changed, 30 insertions, 1 deletions
diff --git a/crates/hir-ty/src/consteval/tests/intrinsics.rs b/crates/hir-ty/src/consteval/tests/intrinsics.rs index 85e917fe1a..1772e3c172 100644 --- a/crates/hir-ty/src/consteval/tests/intrinsics.rs +++ b/crates/hir-ty/src/consteval/tests/intrinsics.rs @@ -691,6 +691,33 @@ fn cttz() { "#, 3, ); + check_number( + r#" + #[rustc_intrinsic] + pub fn cttz<T: Copy>(x: T) -> T; + + const GOAL: i8 = cttz(0i8); + "#, + 8, + ); + check_number( + r#" + #[rustc_intrinsic] + pub fn cttz<T: Copy>(x: T) -> T; + + const GOAL: u32 = cttz(0u32); + "#, + 32, + ); + check_number( + r#" + #[rustc_intrinsic] + pub fn cttz<T: Copy>(x: T) -> T; + + const GOAL: u64 = cttz(0u64); + "#, + 64, + ); } #[test] diff --git a/crates/hir-ty/src/mir/eval/shim.rs b/crates/hir-ty/src/mir/eval/shim.rs index b59d6c1cfb..7a6cbb8ca3 100644 --- a/crates/hir-ty/src/mir/eval/shim.rs +++ b/crates/hir-ty/src/mir/eval/shim.rs @@ -1100,7 +1100,9 @@ impl<'a, 'db: 'a> Evaluator<'a, 'db> { let [arg] = args else { return Err(MirEvalError::InternalError("cttz arg is not provided".into())); }; - let result = u128::from_le_bytes(pad16(arg.get(self)?, false)).trailing_zeros(); + let arg: &[u8] = arg.get(self)?; + let bit_count = arg.len() as u32 * 8; + let result = u128::from_le_bytes(pad16(arg, false)).trailing_zeros().min(bit_count); destination .write_from_bytes(self, &(result as u128).to_le_bytes()[0..destination.size]) } |