num-traits
num-traits copied to clipboard
Implementation of Signed::abs is not correct in regard to the doc
doc is: https://github.com/rust-num/num-traits/blob/d394467906aab26065b5e518f4449aa982c6dcb1/src/sign.rs#L13-L14
but implementation is https://github.com/rust-num/num-traits/blob/d394467906aab26065b5e518f4449aa982c6dcb1/src/sign.rs#L48-L50
the implementation doesn't return MIN but actually panic with overflow, see:
fn abs(t: &i32) -> i32 {
if t.is_negative() { -*t } else { *t }
}
fn main() {
println!("{}", abs(&i32::min_value()))
}
It's actually more nuanced than that, because in release mode overflow silently wraps. We should probably be forwarding this implementation to std anyway, and the docs there explain:
The absolute value of
i32::min_value()cannot be represented as ani32, and attempting to calculate it will cause an overflow. This means that code in debug mode will trigger a panic on this case and optimized code will returni32::min_value()without a panic.