Closed
Description
openedon Oct 17, 2023
I tried this code: Also godbolt link https://rust.godbolt.org/z/9qs4cTP4v
pub struct MB {
addr: u8,
}
pub fn new(addr: u8) -> Option<MB> {
if !(1..=247).contains(&addr) {
return None;
}
Some(MB { addr })
}
I expected to see this happen:
I was trying this code to see what kind of assemble the compilers makes.
I tried different optimize levels.
I see with optimize levels s
and 1
, that the RangeInclusive
bits are still there but not used in the function.
function is self takes about 5 instructions and the RangeInclusive
code about 40 instruction, which is dead code.
Instead, this happened:
RangeInclusive
bits removed in all the optimize levels.
I was using a similar code on a embedded device.
FYI: It also happens with Range
. Remove the =
before 247.
Meta
I see this happen on stable and nightly both for x86_64 and thumbv6/v7.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment