Maintain range information on get for optimisation purposes - #29
Conversation
This seems like a good change. But is there any way we can add a test for this, something that can run in CI? Maybe with iai, or with cargo asm or something? Unfortunately I don’t know of a tool that makes this sort of thing easy…
|
Kestrer
commented
Sep 23, 2025
Btw, I fixed the CI failures, so you can rebase on/merge with main. |
Kestrer
commented
Sep 23, 2025
Nevermind, someone pointed out to me a better solution. Make another library crate in the workspace and put in its [lib]
crate-type = ["cdylib"]Add this to its unsafeextern"C"{
safe fnshould_be_optimized_out() -> !;}fnassert(b:bool){if !b {should_be_optimized_out();}}In CI, run the command #[unsafe(no_mangle)]pubfnsimple_range(i:&BoundedU8<3,5>){assert(3 <= i.get());assert(i.get() <= 5);} |
clarfonthey
commented
Sep 23, 2025
Also worth noting that I think this statement is only necessary for the struct definitions, as the |
I like the idea! I ran the automated tests under Miri, which is usually pretty good at finding undef. Great idea to use linker failures to handle this though! EDIT: I also quickly manually tested that the asserts are optimised out using |
fc453b0 to
7364647Compare…1.90.0, even though it seems like it should
…ilds, add `miri test` for additional checks for undef
eira-fransham
commented
Sep 24, 2025
Ok, I added tests to ensure that the range checks are optimized out as expected. |
| "uses": "actions-rs/cargo@v1", | ||
| "with": { | ||
| "command": "miri", | ||
| # `miri` does not support the `macros` feature as it uses IO. |
There was a problem hiding this comment.
Wait, really? cargo +nightly miri test --workspace --all-features works for me.
There was a problem hiding this comment.
Interesting! I’ll investigate tomorrow
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
eira-fransham
commented
Oct 1, 2025
Made some changes based on your review. The name I went with in the end was |
eira-fransham
commented
Oct 17, 2025
@Kestrer Hey, any movement on this? |
Kestrer
commented
Oct 17, 2025
Sorry for the delay. I didn’t forget but I’ve been rather sick… |
Small PR to ensure that the range information is persisted when getting the value. This allows
bounded-integerto be used not just for correctness, but for optimisation too.