-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Constify remaining traits/impls for const_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
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
ad7b7c4
to
071b5f0
Compare
Only organization members can add or resolve concerns. |
This comment has been minimized.
This comment has been minimized.
@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) |
@rustbot resolve May break Clippy We cleared up the misunderstanding of what the problem was. |
071b5f0
to
ed0722b
Compare
This comment has been minimized.
This comment has been minimized.
Yeah, I'll be honest, I have genuinely no idea what those tests are doing. I could just add the feature flag, but it wasn't necessary before, and I don't know why. |
Didn't you just forget to replace |
Last time I checked this file, it didn't have it, but now it does. 🤷🏻 Literally tried |
ed0722b
to
8e2f97b
Compare
TBH I think it'd be fine to have arith and bit ops in the same feature gate. We don't need a dozen of them, and it seems likely we'd stabilize these together. |
That's fair. I could combine the bit ops I've already gotten working into this PR, I just figured that it made more sense to separate them. Part of my reason was to just simplify the review, but I guess that the compiler is used to 100-file changes, so… 🤷🏻 I also thought there would be more implementations to constify but for the arithmetic side it's just |
const_ops
, rename to const_arith_ops
const_ops
Okay, I went for it and just incorporated the bit operations in this PR and undid the rename. After this, "all" of the operators are constified. I say "all" because I'm excluding the |
8e2f97b
to
c4fe6d7
Compare
&& !is_stable_const_fn(cx, *binop_id, msrv) | ||
{ | ||
// FIXME: reintroduce a better check after this is merged back into Clippy | ||
if is_in_const_context(cx) { |
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.
Wouldn't we want to check for tcx.features().const_ops()
? (I'm not being able to find const_ops
on Features
)
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.
Have you seen the discussion starting at #143949 (comment)?
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.
Note, the proper fix should be done on the clippy repo later. The issue is that the bug is entirely unrelated to this change, but the changes make the existing test no longer pass, so, something has to give, and this was the minimal fix to at least get rid of some invalid suggestions.
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.
const_ops
is a library feature so tcx.features().const_ops()
doesn't exist. tcx.features().enabled(...)
should work though.
Cc @flip1995
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.
FWIW, there shouldn't be any feature checking here since both the assignment version of the operators and the non-assignment version are gated behind the same features, so, if you can use the non-assignment version, you will be able to use the assignment version.
The issue is that this isn't checking specifically for the bounds on both operators, since [const] Add
doesn't guarantee [const] AddAssign
. It does check that AddAssign
is a bound when suggesting it, but it isn't also checking for [const] AddAssign
when you're calling it in a const context.
c4fe6d7
to
102866c
Compare
…ibutes explicitly
102866c
to
c58f64c
Compare
Are there |
That's a fair point; initially my thought process was that we would want to gradually do this, but grepping for I also missed the |
c58f64c
to
207d52a
Compare
I only did |
Tracking issue: #143802
This is split into two 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.A few random other notes on the implementation specifically:
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 constifies the following additional traits:
Neg
Not
BitAnd
BitOr
BitXor
Shl
Shr
AddAssign
SubAssign
MulAssign
DivAssign
RemAssign
BitAndAssign
BitOrAssign
BitXorAssign
ShlAssign
ShrAssign
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
, andNonZero
as well, since all of them forward to already-const-stable methods. (potentially via intrinsics, to avoid extra overhead)There are three "non-primitive" types which implement these traits, listed below. Note that I put "non-primitive" in quotes since I'm including
Wrapping
,Saturating
, andNonZero
, which are just wrappers over primitives.Duration
(arithmetic operations)Ipv4Addr
(bit operations)Ipv6Addr
(bit operations)Note
Concerns (0 active)
May break Clippyresolved in this commentManaged by
@rustbot
—see help for details.