Let's leave AI out of the discussion, it is irrelevant. I hoped it came across as slightly funny but I now see that it is a distracting remark, my mistake.
An example of what I hope to detect:
const SIDE_U8: u8 = 32;
const SIDE_U16: u16 = SIDE_U8 as u16;
const SIDE_POW2_U16: u16 = SIDE_U16 * SIDE_U16;
const SIDE_POW3_U16: u16 = SIDE_POW2_U16 * SIDE_U16;
#[derive(Debug, Copy, Clone)]
pub struct Index3([u8; 3]);
impl Index3 {
#[inline]
pub fn new(indices: [u8; 3]) -> Option<Self> {
if indices.iter().copied().all(|i| i < SIDE_U8) {
Some(Self(indices))
}
else {
None
}
}
#[inline]
pub fn pack(self) -> Index {
let strides = [1, SIDE_U16, SIDE_POW2_U16];
Index::new(core::iter::zip(self.0.map(|i| i as u16), strides).map(|(i, s)| i * s).sum()).unwrap()
}
}
#[derive(Debug, Copy, Clone)]
pub struct Index(u16);
impl Index {
#[inline]
pub fn new(packed: u16) -> Option<Self> {
if packed < SIDE_POW3_U16 {
Some(Self(packed))
} else {
None
}
}
#[inline]
pub fn into_raw(self) -> u16 {
self.0
}
}
#[doc(hidden)]
#[inline(never)]
pub fn __cargo_asm_pack_an_index(index: Index3) -> Index {
index.pack()
}
The output of cargo asm on my machine for __cargo_asm_pack_an_index is:
.section .text.ex::__cargo_asm_pack_an_index,"ax",@progbits
.globl ex::__cargo_asm_pack_an_index
.p2align 4
.type ex::__cargo_asm_pack_an_index,@function
ex::__cargo_asm_pack_an_index:
.cfi_startproc
movzx eax, dil
mov ecx, edi
shr ecx, 3
and ecx, 8160
add ecx, eax
shr edi, 6
and edi, 64512
add di, cx
js .LBB0_2
mov eax, edi
ret
.LBB0_2:
push rax
.cfi_def_cfa_offset 16
lea rdi, [rip + .Lanon.1b1a26bb320d9f62de4ece743ed9d8a5.1]
call qword ptr [rip + core::option::unwrap_failed@GOTPCREL]
The symbol core::option::unwrap_failed tells me that the compiler does not (unremarkebly) recognize that the Index3 type ensures that the individual indices are less than the extent of 32, and that therefore the packed Index::new should never return None in Index3::pack.
Please be reminded that I'm not asking how to get it to compile without an error handling path that shouldn't be here, I am asking how I can create automated tests that make this type of assertion.