Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

clarfonthey
Copy link
Contributor

@clarfonthey clarfonthey commented Jul 15, 2025

Tracking issue: #143802

This is split into two commits for ease of reviewability:

  1. Updates the forward_ref_* macros to accept multiple attributes (in anticipation of needing rust_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.
  2. Actually constify the traits/impls.

A few random other notes on the implementation specifically:

  • I unindented the attributes that were passed to the 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, and NonZero 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, and NonZero, which are just wrappers over primitives.

  • Duration (arithmetic operations)
  • Ipv4Addr (bit operations)
  • Ipv6Addr (bit operations)

Note

Concerns (0 active)

Managed by @rustbot—see help for details.

@rustbot
Copy link
Collaborator

rustbot commented Jul 15, 2025

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 15, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 15, 2025

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Jul 16, 2025

Only organization members can add or resolve concerns.

@rust-log-analyzer

This comment has been minimized.

@samueltardieu
Copy link
Member

@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 rustbot added the S-waiting-on-concerns Status: Awaiting concerns to be addressed by the author label Jul 16, 2025
@samueltardieu
Copy link
Member

@rustbot resolve May break Clippy

We cleared up the misunderstanding of what the problem was.

@rustbot rustbot removed the S-waiting-on-concerns Status: Awaiting concerns to be addressed by the author label Jul 17, 2025
@rust-log-analyzer

This comment has been minimized.

@clarfonthey
Copy link
Contributor Author

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.

@samueltardieu
Copy link
Member

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 #![feature(const_ops)] in library/coretests/tests/lib.rs?

@clarfonthey
Copy link
Contributor Author

clarfonthey commented Jul 18, 2025

Didn't you just forget to replace #![feature(const_ops)] in library/coretests/tests/lib.rs?

Last time I checked this file, it didn't have it, but now it does. 🤷🏻

Literally tried rg const_ops in the library folder and got nothing last time, now it worked, so, issue fixed I guess.

@RalfJung
Copy link
Member

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.

@clarfonthey
Copy link
Contributor Author

clarfonthey commented Jul 18, 2025

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 Duration as the odd one out, and for bit operations it's Ipv[46]Addr. So, not groundbreaking.

@clarfonthey clarfonthey changed the title Tidy up const_ops, rename to const_arith_ops Constify remaining traits/impls for const_ops Jul 18, 2025
@clarfonthey
Copy link
Contributor Author

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 Index and Deref operators, which deserve separate handling.

&& !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) {
Copy link
Member

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)

Copy link
Member

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)?

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Contributor Author

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.

@fee1-dead
Copy link
Member

  • we can't make the constness in the forward_ref_* macros work nicely without copy-pasting the macro body. Instead of doing that, I decided to go for a cursed extra const argument, which is then used for the actual const keyword. I explicitly mention named macro capture groups as a way of doing this better in the comments.

Are there impls covered by these macros that should not be marked const? Because I think if not we shouldn't even include a non-const case at all.

@clarfonthey
Copy link
Contributor Author

clarfonthey commented Jul 20, 2025

That's a fair point; initially my thought process was that we would want to gradually do this, but grepping for forward_ref in the library directory shows that there's now only one usage left that isn't const. So, I'll do that instead.

I also missed the NonZero impls and will do those too. (I was checking for Add in my heuristic, but that only has Div, Rem, and Neg impls.)

@clarfonthey
Copy link
Contributor Author

I only did x check for this so I'm hoping CI doesn't find anything I missed, but now every use of ref-forwarding is marked const. I also saw that stuff like IP addresses had manual implementations of ref-forwarding, but it didn't feel particularly necessary to update them to use the macro instead, at least for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants