forked from programble/bounded-integer
-
Notifications
You must be signed in to change notification settings - Fork 10
Maintain range information on get for optimisation purposes #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Kestrer
merged 6 commits into
Kestrer:main
from
eira-fransham:maintain-range-information-on-get
Oct 17, 2025
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7a8c517
Maintain range information on get for optimisation purposes
eira-fransham 7364647
Update trybuild output to match v1.90.0-stable
eira-fransham 243af12
It seems like `must_use_candidate` isn't triggering in `prim_int` on …
eira-fransham 42a8e39
Add tests to ensure that range checks are optimized out in release bu…
eira-fransham 01d7474
Clean up tests based on review
eira-fransham b27ef97
Rename to `optimizer_assert_guaranteed`
eira-fransham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| [package] | ||
| name = "optimization-tests" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
| bounded-integer.path = ".." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| //! Checking compilation with optimizations for the `assert_unchecked` range tests in `unsafe_api`. | ||
| //! | ||
| //! > *TODO*: Rust seems to ignore optimization for doctests even if they are specified in the profile, | ||
eira-fransham marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! > and even if running `cargo test --doc --release`. The `compile_fail` test _does_ correctly fail | ||
| //! > to compile, but that is probably true even for out-of-range comparisons. | ||
| //! | ||
| //! ```rust,compile_fail | ||
| //! const LOWER_BOUND: usize = 15; | ||
| //! | ||
| //! let bounded = bounded_integer::BoundedUsize::<LOWER_BOUND, 20>::new_saturating(15); | ||
| //! let bounded = core::hint::black_box(bounded); | ||
| //! | ||
| //! optimization_tests::assert_optimized_out!( | ||
| //! bounded.get() <= LOWER_BOUND | ||
| //! ) | ||
| //! optimization_tests::assert_optimized_out!( | ||
| //! *bounded.get_ref() <= LOWER_BOUND | ||
| //! ) | ||
| //! optimization_tests::assert_optimized_out!( | ||
| //! *unsafe { bounded.get_mut() } <= LOWER_BOUND | ||
| //! ) | ||
| //! ``` | ||
|
|
||
| unsafe extern "C" { | ||
| // This function should fail to link if range checks are not optimized out as expected. | ||
| pub safe fn should_be_optimized_out() -> !; | ||
| } | ||
|
|
||
| #[macro_export] | ||
eira-fransham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| macro_rules! assert_optimized_out { | ||
Kestrer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ($cond:expr) => { | ||
| if $cond { | ||
| $crate::should_be_optimized_out(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use bounded_integer::BoundedUsize; | ||
| use crate::assert_optimized_out; | ||
|
|
||
| fn range_check_optimized_out_usize<const LO: usize, const HI: usize>(expected: usize) { | ||
| let i = core::hint::black_box(BoundedUsize::<LO, HI>::new(expected).unwrap()); | ||
| assert_optimized_out!(i.get() < LO || i.get() > HI); | ||
| let i = core::hint::black_box(i); | ||
| assert_optimized_out!(*i.get_ref() < LO || i.get() > HI); | ||
Kestrer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let mut i = core::hint::black_box(i); | ||
| assert_optimized_out!(*unsafe { i.get_mut() } < LO || *unsafe { i.get_mut() } > HI); | ||
|
|
||
| assert_eq!(core::hint::black_box(i.get()), core::hint::black_box(expected)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn range_check_optimized_out() { | ||
| range_check_optimized_out_usize::<10, 20>(15); | ||
| range_check_optimized_out_usize::<20, 20>(20); | ||
| range_check_optimized_out_usize::<1, { usize::MAX - 1 }>(usize::MAX - 1); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| #![expect(clippy::must_use_candidate)] | ||
|
|
||
| use core::fmt::{self, Display, Formatter}; | ||
| use core::num::NonZero; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, really?
cargo +nightly miri test --workspace --all-featuresworks for me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! I’ll investigate tomorrow