const and NonZero impl for clamp_magnitude() - #163279
Conversation
|
Thanks for the pull request, and welcome! The Rust Project has assigned @Darksonn (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks. Please see the contribution instructions and our LLM policy for more information. Why was this reviewer chosen?The reviewer was selected based on:
|
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Address the missing const-stability gates, documentation issue, and required compile-time/runtime test coverage.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 3
Open (5)
What changed in this PR
This PR makes clamp_magnitude const across numeric types and adds signed NonZero support.
Changes:
- Makes integer and floating-point implementations
const. - Adds
NonZero<signed>::clamp_magnitude. - Updates floating-point panic-message tests.
| File | Summary |
|---|---|
library/coretests/tests/num/clamp_magnitude.rs |
Updates panic expectations; add compile-time coverage for const and NonZero APIs. |
library/core/src/num/nonzero.rs |
Adds signed NonZero support and documentation; fix the equivalence example and add coverage. |
library/core/src/num/int_macros.rs |
Makes signed integer methods const; add compile-time assertions. |
library/core/src/num/f64.rs |
Makes the method const; add an explicit const-stability gate. |
library/core/src/num/f32.rs |
Makes the method const; add an explicit const-stability gate. |
library/core/src/num/f16.rs |
Makes the method const. |
library/core/src/num/f128.rs |
Makes the method const. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Adding tests addressing the changes in coretests/tests/num/clamp_magnitude.rs
|
Currently the clamp_magnitude.rs file in coretests is not included in mod.rs, therefore the (additional and existing) tests are not run with the rest of the suite. Since this has to be deliberate: what is the reason? |
| #[must_use = "method returns a new number and does not mutate the original value"] | ||
| #[rustc_const_unstable(feature = "clamp_magnitude", issue = "148519")] | ||
| #[unstable(feature = "clamp_magnitude", issue = "148519")] | ||
| pub const fn clamp_magnitude(self, limit: $UnsignedT) -> Self { | ||
| if let Ok(limit) = core::convert::TryInto::<$SelfT>::try_into(limit) { |
There was a problem hiding this comment.
You're making this const. Note that this implementation depends on traits, so stabilization as const may be an issue.
There was a problem hiding this comment.
You are right. const impl Ord is gated behind #[rustc_const_unstable(feature = "const_cmp", issue = "143800")], so const fn clamp_magnitude should be too. I will address this.
There was a problem hiding this comment.
Gating it behind const_cmp seems wrong/weird to me ... most likely it should just be implemented without using the const trait, or it should wait for said const trait to stabilize.
There was a problem hiding this comment.
Clamp is part of the Ord trait. Generally const functions that depend on functions in the ord trait seem to have rustc_const_unstable gated behind "const_cmp". Look for example to the total_cmp() functions in the float types that are stable, but gated rustc_const_unstable behind "const_cmp" since they depend on cmp(). The only reason why clamp_magnitude() is not itself part of Ord is that it requires Neg (and therefore is only applicable to signed types). I also don't see how I would implement clamp_magnitude() without Ord, that seems like it would result in a lot of redundant code. If that does not convince you I am fine removing the const in int_macros.rs and nonzero.rs for now, however that partially defeats the purpose of this PR.
|
Reminder, once the PR becomes ready for a review, use |
There was a problem hiding this comment.
You removed this for NonZero, but I think it could make sense to translate it to cases using 1 as the limit.
There was a problem hiding this comment.
Not sure if that would really make sense. The special case in zero for signed integers is that a value clamped to the magnitude of zero should always be zero, regardless of whether that value was positive or negative. For NonZero values that would not be the case, therefore I fail to see how cases using 1 as limit provide any additional coverage compared to the tests using 50 as limit.
Adding mod clamp_magnitude in coretests/num/mod.rs Activating feature(clamp_magnitude) in coretests/lib.rs Adding test coverage for f16, f128, and NaN as well as zero cases Modifying nonzero and int_macros documentation for better readablity
|
@rustbot ready |
|
@rustbot ready |
|
The failure was (presumably) due to MinGW not having real f16 and f128 support. Gating the relevant tests behind @rustbot ready |
|
You could have just linked to zulip, so that your reviewer doesn't just have to take your word for it ^^ |
|
@bors try jobs=dist-various-1,test-various,test-x86_64-gnu-aux,test-x86_64-gnu-llvm-21-3,test-x86_64-msvc-1,test-aarch64-apple-1,test-aarch64-apple-2,test-x86_64-mingw-1,test-i686-msvc,test-armhf-gnu |
…try> const and NonZero impl for clamp_magnitude() try-job: dist-various-1 try-job: test-various try-job: test-x86_64-gnu-aux try-job: test-x86_64-gnu-llvm-21-3 try-job: test-x86_64-msvc-1 try-job: test-aarch64-apple-1 try-job: test-aarch64-apple-2 try-job: test-x86_64-mingw-1 try-job: test-i686-msvc try-job: test-armhf-gnu
This comment has been minimized.
This comment has been minimized.
|
@bors r+ rollup=iffy |
…-limit, r=Darksonn const and NonZero impl for clamp_magnitude() This PR is implementing the latest proposed changes in Issue rust-lang#148519, making the clamp_magnitude() functions const for all floating point and integer types, as well as adding a clamp_magnitude() to NonZero<$Int>, taking a NonZero<$UInt> as limit parameter.
…-limit, r=Darksonn const and NonZero impl for clamp_magnitude() This PR is implementing the latest proposed changes in Issue rust-lang#148519, making the clamp_magnitude() functions const for all floating point and integer types, as well as adding a clamp_magnitude() to NonZero<$Int>, taking a NonZero<$UInt> as limit parameter.
…-limit, r=Darksonn const and NonZero impl for clamp_magnitude() This PR is implementing the latest proposed changes in Issue rust-lang#148519, making the clamp_magnitude() functions const for all floating point and integer types, as well as adding a clamp_magnitude() to NonZero<$Int>, taking a NonZero<$UInt> as limit parameter.
…uwer Rollup of 18 pull requests Successful merges: - #163532 (rustc_codegen_cranelift subtree update) - #163534 (miri subtree update) - #163279 (const and NonZero impl for clamp_magnitude()) - #162900 (Some refactorings around metadata encoding) - #163455 (Don't use the metadata based crate_hash for rustdoc runs) - #163483 (Bump bootstrap compiler to 1.100.0 beta) - #159798 (Attribute documentation for cfg_attr) - #163368 (Don't build format string suggestions from `concat!` offsets) - #163375 (Update expect messages in library/std/src/os/unix/net/ following Rust's `expect` guidance) - #163405 (Remove some #[linkage] options) - #163470 (Miscellaneous attr error stuff) - #163489 (expose Rc::is_unique) - #163492 (x86 and x86_64: cleanup some callconv code) - #163496 (cycle handling: mirror old solver) - #163509 (Use more default field values in `Resolver`) - #163519 (Forbid `Reborrow` impls for types with destructors) - #163520 (Cast cleanups) - #163524 (Document `Result` case for the `arena_cache` query modifier) Failed merges: - #163547 ([beta] rustfmt backport)
…-limit, r=Darksonn const and NonZero impl for clamp_magnitude() This PR is implementing the latest proposed changes in Issue rust-lang#148519, making the clamp_magnitude() functions const for all floating point and integer types, as well as adding a clamp_magnitude() to NonZero<$Int>, taking a NonZero<$UInt> as limit parameter.
…uwer Rollup of 18 pull requests Successful merges: - #163532 (rustc_codegen_cranelift subtree update) - #163534 (miri subtree update) - #163279 (const and NonZero impl for clamp_magnitude()) - #162900 (Some refactorings around metadata encoding) - #163455 (Don't use the metadata based crate_hash for rustdoc runs) - #159798 (Attribute documentation for cfg_attr) - #162921 (make mips64 `Complex` GCC-compatible) - #163368 (Don't build format string suggestions from `concat!` offsets) - #163375 (Update expect messages in library/std/src/os/unix/net/ following Rust's `expect` guidance) - #163405 (Remove some #[linkage] options) - #163470 (Miscellaneous attr error stuff) - #163489 (expose Rc::is_unique) - #163492 (x86 and x86_64: cleanup some callconv code) - #163496 (cycle handling: mirror old solver) - #163509 (Use more default field values in `Resolver`) - #163519 (Forbid `Reborrow` impls for types with destructors) - #163520 (Cast cleanups) - #163524 (Document `Result` case for the `arena_cache` query modifier)
…uwer Rollup of 18 pull requests Successful merges: - #163532 (rustc_codegen_cranelift subtree update) - #163534 (miri subtree update) - #163279 (const and NonZero impl for clamp_magnitude()) - #162900 (Some refactorings around metadata encoding) - #163455 (Don't use the metadata based crate_hash for rustdoc runs) - #159798 (Attribute documentation for cfg_attr) - #162921 (make mips64 `Complex` GCC-compatible) - #163368 (Don't build format string suggestions from `concat!` offsets) - #163375 (Update expect messages in library/std/src/os/unix/net/ following Rust's `expect` guidance) - #163405 (Remove some #[linkage] options) - #163470 (Miscellaneous attr error stuff) - #163489 (expose Rc::is_unique) - #163492 (x86 and x86_64: cleanup some callconv code) - #163496 (cycle handling: mirror old solver) - #163509 (Use more default field values in `Resolver`) - #163519 (Forbid `Reborrow` impls for types with destructors) - #163520 (Cast cleanups) - #163524 (Document `Result` case for the `arena_cache` query modifier)
Rollup of 20 pull requests Successful merges: - #163279 (const and NonZero impl for clamp_magnitude()) - #163081 (Provide more context on "not general enough" error) - #163455 (Don't use the metadata based crate_hash for rustdoc runs) - #159798 (Attribute documentation for cfg_attr) - #162921 (make mips64 `Complex` GCC-compatible) - #163368 (Don't build format string suggestions from `concat!` offsets) - #163375 (Update expect messages in library/std/src/os/unix/net/ following Rust's `expect` guidance) - #163470 (Miscellaneous attr error stuff) - #163489 (expose Rc::is_unique) - #163492 (x86 and x86_64: cleanup some callconv code) - #163496 (cycle handling: mirror old solver) - #163509 (Use more default field values in `Resolver`) - #163519 (Forbid `Reborrow` impls for types with destructors) - #163520 (Cast cleanups) - #163524 (Document `Result` case for the `arena_cache` query modifier) - #163546 (PassWrapper: adapt for new PassPlugin load method) - #163551 (Add libs-nominated triagebot config) - #163559 (Suggest `#[unsafe(no_mangle)]` for entry points in `no_std` binaries) - #163564 (rustc-dev-guide subtree update) - #163568 (remove dead cfg_select! arm)
Rollup merge of #163279 - Eleocraft:clamp-magnitude-unsigned-limit, r=Darksonn const and NonZero impl for clamp_magnitude() This PR is implementing the latest proposed changes in Issue #148519, making the clamp_magnitude() functions const for all floating point and integer types, as well as adding a clamp_magnitude() to NonZero<$Int>, taking a NonZero<$UInt> as limit parameter.
Rollup of 20 pull requests Successful merges: - rust-lang/rust#163279 (const and NonZero impl for clamp_magnitude()) - rust-lang/rust#163081 (Provide more context on "not general enough" error) - rust-lang/rust#163455 (Don't use the metadata based crate_hash for rustdoc runs) - rust-lang/rust#159798 (Attribute documentation for cfg_attr) - rust-lang/rust#162921 (make mips64 `Complex` GCC-compatible) - rust-lang/rust#163368 (Don't build format string suggestions from `concat!` offsets) - rust-lang/rust#163375 (Update expect messages in library/std/src/os/unix/net/ following Rust's `expect` guidance) - rust-lang/rust#163470 (Miscellaneous attr error stuff) - rust-lang/rust#163489 (expose Rc::is_unique) - rust-lang/rust#163492 (x86 and x86_64: cleanup some callconv code) - rust-lang/rust#163496 (cycle handling: mirror old solver) - rust-lang/rust#163509 (Use more default field values in `Resolver`) - rust-lang/rust#163519 (Forbid `Reborrow` impls for types with destructors) - rust-lang/rust#163520 (Cast cleanups) - rust-lang/rust#163524 (Document `Result` case for the `arena_cache` query modifier) - rust-lang/rust#163546 (PassWrapper: adapt for new PassPlugin load method) - rust-lang/rust#163551 (Add libs-nominated triagebot config) - rust-lang/rust#163559 (Suggest `#[unsafe(no_mangle)]` for entry points in `no_std` binaries) - rust-lang/rust#163564 (rustc-dev-guide subtree update) - rust-lang/rust#163568 (remove dead cfg_select! arm)


View all comments
This PR is implementing the latest proposed changes in Issue #148519, making the clamp_magnitude() functions const for all floating point and integer types, as well as adding a clamp_magnitude() to NonZero<$Int>, taking a NonZero<$UInt> as limit parameter.