-
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
.
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.
Yes: I'm not quite sure whether this code should even remain on Clippy's side after this PR, since I'm pretty sure that "MSRV" doesn't account for nightly compilers, and we definitely won't want to ever stabilise an operator without its corresponding assign version. I chose the easiest approach to get it to pass without making too much of a modification, since it makes more sense to edit this in the clippy repo directly instead of via the subtree.
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.
Filed rust-lang/rust-clippy#15285 to keep track of this.
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.
In the meantime, you could go with https://rfc1149.pastes.sh/patch-assign-op-pattern.diff and remove the msrv
attribute, and we'll take care of reintroducing the proper code in Clippy.
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.
@rustbot concern Breaks clippy
The problem in commenting the test code but letting the code in Clippy as-is is that starting from the nightly after the PR is merged, Clippy (as part of the nightly release of Rust) will start giving bogus suggestions in const
contexts, including in CI runs for people using nightly. And this will stay for at least two sync cycles (up to one month) and may even enter the next Rust beta release.
Applying the patch I referenced above will not only prevent Clippy from giving bad suggestions, but it will also still pass all the tests without altering them.
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 exactly do you mean as far as bogus suggestions? It actually is correct; if you enable the const_trait_impls
feature and the const_arith_ops
feature, as are required for the add operator to work in const context, then it will correctly suggest that you can replace x = x + y
with x += y
. I'm a bit confused what the issue is, here.
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.
In the test, res = res + Self::ONE;
cannot be replaced by res += Self::ONE
in a const
context because T
(the type of res
and Self::ONE
) implements AddAssign
, not [const] AddAssign
(and it implements [const] Add
, hence the valid +
operation in the const
context). But Clippy, if it stays unpatched, will suggest it because it only checks the constness of the AddAssign
trait declaration, not its implementation.
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.
If I am mistaken and this suggestion is valid, then instead of commenting the test, just add a //~^ assign_op_pattern
below the res = res + Self::ONE;
line and bless the results, but I think you'll get an invalid .fixed
file.
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.
Oh, you're totally correct here; I was mistaken. So, this is a genuine bug, but it's unrelated to the actual issue at hand, IMHO: it should also suggest to replace [const] Add
with [const] AddAssign
, or add it. Or at least make it not an automatically applicable thing. It has nothing to do with the const stability of the trait, but the const bound on the trait.
ad7b7c4
to
071b5f0
Compare
Only organization members can add or resolve concerns. |
The job Click to see the possible cause of the failure (guessed by this bot)
|
@rustbot concern May break Clippy (checking if concerns work as top-level comments only, real concern was written here, and discussion about the error message is on Zulip) |
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.Caution
Concerns (1 active)
Managed by
@rustbot
—see help for details.