-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Tidy up const_ops
, rename to const_arith_ops
#143949
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
base: master
Are you sure you want to change the base?
Conversation
…ibutes explicitly
r? @ibraheemdev rustbot has assigned @ibraheemdev. Use |
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
This comment has been minimized.
This comment has been minimized.
97d85a1
to
d7a8770
Compare
This comment has been minimized.
This comment has been minimized.
d7a8770
to
ad7b7c4
Compare
@@ -75,6 +76,7 @@ impl MulAssign<i64> for Wrap { | |||
} | |||
} | |||
|
|||
#[clippy::msrv = "1.88.0"] |
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.
What is this for?
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.
This hides a real issue: to know whether it can replace "a = a OP b" by "a OP= b" (where OP is, for example, "+"), Clippy looks for the const-stability of the sole associated item of the trait corresponding to "OP=". Here, AddAssign
is now identified as const-stable, and Clippy attempts to replace a = a + b;
by a += b;
in a const
context, when a
is of a generic type implementing AddAssign
.
#[diagnostic::on_unimplemented( | ||
message = "cannot add-assign `{Rhs}` to `{Self}`", | ||
label = "no implementation for `{Self} += {Rhs}`" | ||
)] | ||
#[doc(alias = "+")] | ||
#[doc(alias = "+=")] | ||
#[const_trait] |
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.
This might be what triggers the Clippy issue. It looks like the AddAssign::add_assign()
associated function is seen as being const
-stable.
Tracking issue: #143802
This is split into three commits for ease of reviewability:
forward_ref_*
macros to accept multiple attributes (in anticipation of needingrust_const_unstable
attributes) and also require attributes in these macros. Since the default attribute only helps for the initial implementations, it means it's easy to get wrong for future implementations, as shown for the saturating implementations which were incorrect before.const_ops
feature toconst_arith_ops
and adds constness and trivial implementations to the remaining traits incore::ops::arith
.A few random other notes on the implementation specifically:
macro_named_capture_groups
#142999 implemented, we can't make the constness in theforward_ref_*
macros work nicely without copy-pasting the macro body. Instead of doing that, I decided to go for a cursed extraconst
argument, which is then used for the actualconst
keyword. I explicitly mention named macro capture groups as a way of doing this better in the comments.forward_ref_*
macro calls because in some places rustfmt wanted them to be unindented, and in others it was allowed because they were themselves inside of macro bodies. I chose the consistent indenting even though I (personally) think it looks worse.As far as the actual changes go, this renames the
const_ops
feature toconst_arith_ops
(anticipating, e.g.,const_bit_ops
) and constifies the following additional traits:Neg
AddAssign
SubAssign
MulAssign
DivAssign
RemAssign
In terms of constified implementations of these traits, it adds the reference-forwarded versions of all the arithmetic operators, which are defined by the macros in
library/core/src/internal_macros.rs
. I'm not going to fully enumerate these because we'd be here all day, but sufficed to say, it effectively allows adding an&
to one or both sides of an operator for primitives.Additionally, I constified the implementations for
Wrapping
,Saturating
, andDuration
as well, since all of them forward to already-const-stable methods. Specifically forDuration
, the ref-forwarding is not present.