Constify remaining traits/impls for const_ops by clarfonthey · Pull Request #143949 · rust-lang/rust (original) (raw)

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:


As far as the actual changes go, this constifies the following additional traits:

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.

Additionally, because the methods on SystemTime needed to make these operations const were not marked const, a separate tracking issue for const-stabilising those methods is #144517.

Stuff left out of this PR: