is-odd icon indicating copy to clipboard operation
is-odd copied to clipboard

`is_odd()` not marked as `const`

Open git-staus opened this issue 1 month ago • 0 comments

The following industry-standard use case

const fn unlucky() -> bool {
        surprise_data().is_odd()
}

const fn surprise_data() -> u32 {
        let compiler_present = mem::MaybeUninit::<u32>::uninit();
        // SAFETY: just trust me bro
        unsafe { compiler_present.assume_init() }
}

fails to synergise with rustc:

error[E0015]: cannot call non-const method `<u32 as IsOdd>::is_odd` in constant functions
  --> src/lib.rs:21:18
   |
21 |     surprise_data().is_odd()
   |                     ^^^^^^^^
   |
note: method `is_odd` is not const because trait `IsOdd` is not const
  --> /Users/niels/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/is-odd-1.1.1/src/lib.rs:1:1
   |
 1 | pub trait IsOdd {
   | ^^^^^^^^^^^^^^^ this trait is not const
 2 |     fn is_odd(&self) -> bool;
   |     ------------------------- this method is not const
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

For more information about this error, try `rustc --explain E0015`.

This makes for bad DX and renders this crate not very enterprise-ready. Perhaps the devs could circle back here.

git-staus avatar Nov 13 '25 21:11 git-staus