Skip to content
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

Impl {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}Assign<$t> for Wrapping<$t> for rust 1.60.0 #93208

Merged
merged 2 commits into from
Feb 7, 2022

Conversation

kellerkindt
Copy link
Contributor

@kellerkindt kellerkindt commented Jan 22, 2022

Tracking issue #93204

This is about adding basic integer operations to the Wrapping type:

let mut value = Wrapping(2u8);
value += 3u8;
value -= 1u8;
value *= 2u8;
value /= 2u8;
value %= 2u8;
value ^= 255u8;
value |= 123u8;
value &= 2u8;

Because this adds stable impls on a stable type, it runs into the following issue if an #[unstable(...)] attribute is used:

an `#[unstable]` annotation here has no effect
note: see issue #55436 <https://github.com/rust-lang/rust/issues/55436> for more information

This means - if I understood this correctly - the new impls have to be stabilized instantly.
Which in turn means, this PR has to kick of an FCP on the tracking issue as well?

This impl is analog to 1c0dc18 #92356 for the Saturating type @dtolnay @Mark-Simulacrum

@rust-highfive
Copy link
Collaborator

r? @m-ou-se

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 22, 2022
@m-ou-se m-ou-se added needs-fcp This change is insta-stable, so needs a completed FCP to proceed. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jan 22, 2022
@m-ou-se
Copy link
Member

m-ou-se commented Jan 22, 2022

I feel a bit conflicted about these. I agree Wrapping can be annoying to work with, and this is a big improvement to that. But this also adds some inconcistencies that we might not be able to fix:

  • This adds Add<i32> for Wrapping<i32> (etc.), but do we also want Add<Wrapping<i32>> for i32? Having w + 1 work but not 1 + w seems bad. But adding even more implementations on i32 doesn't seem great either.
  • Should we also add Add<&i32> for Wrapping<i32> (etc.), just like we have Add<&Wrapping> for Wrapping? It would be consistent, but it would technically be a breaking change since Wrapping(1) + &Default::default() would no longer compile.
  • How about PartialEq/PartialOrd? We can't add those as it'd be a breaking change, but having w + 1 work, while still requiring an explicit w == Wrapping(1) also doesn't feel great.
  • What if someone wants a non-wrapping result type? E.g. they write let b: i32 = w + 1; and forget the .0 in w.0 to get the non-wrapping type. Today, they get an error that they can't add i32 to Wrapping<i32> and they need to pick either the wrapping behaviour or the non-wrapping behaviour explicitly, by wrapping the 1 in Wrapping(1) or by unwrapping the w with .0. But with this change, the addition would happen in a wrapping way, and then give the an error about assigning a Wrapping<i32> to a i32. Or not give any error if : i32 wasn't explicit.

This makes me think it might be better if we only do this for the *Assign traits. wrapping_i32 += 1; clearly follows the behaviour of the wrapping_i32 variable, and some_i32 += Wrapping(1); won't compile.

These are just some initial thoughts after thinking about this for a few minutes. I'd love to hear some input from the rest of @rust-lang/libs-api.

This means - if I understood this correctly - the new impls have to be stabilized instantly. Which in turn means, this PR has to kick of an FCP on the tracking issue as well?

In such cases we usually do the FCP on the PR itself.

@m-ou-se m-ou-se added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Jan 22, 2022
@jackh726
Copy link
Member

This makes me think it might be better if we only do this for the *Assign traits. wrapping_i32 += 1; clearly follows the behaviour of the wrapping_i32 variable, and some_i32 += Wrapping(1); won't compile.

Just a fly on the wall here, but I do want to say that I really like this idea. And a reminder to make sure to change the Saturating impls if this changes!

@kellerkindt
Copy link
Contributor Author

This makes me think it might be better if we only do this for the *Assign traits. wrapping_i32 += 1; clearly follows the behaviour of the wrapping_i32 variable, and some_i32 += Wrapping(1); won't compile.

This was my initial intention before I thought the other impls would be nice to have as well. Thanks for pointing out the issues. I have updated the commit and I will post a PR to remove the impls from the Saturating type once this PR is merged.

@yaahc
Copy link
Member

yaahc commented Jan 24, 2022

These are just some initial thoughts after thinking about this for a few minutes. I'd love to hear some input from the rest of @rust-lang/libs-api.

I very much like and support your suggestion of starting with just the Assign traits since they're much easier to reason about here.

@kellerkindt kellerkindt changed the title Add and Stabilize {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}{,Assign}<$t> to Wrapping<$t> for rust 1.61.0 Impl {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}<$t> for Wrapping<$t> for rust 1.61.0 Jan 25, 2022
@kellerkindt kellerkindt changed the title Impl {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}<$t> for Wrapping<$t> for rust 1.61.0 Impl {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}Assign<$t> for Wrapping<$t> for rust 1.61.0 Jan 25, 2022
@joshtriplett
Copy link
Member

We discussed this in today's @rust-lang/libs-api meeting, and given the change to only impl the *Assign traits, we'd like to merge this:

@rfcbot merge

Also, we'd like to be explicit that this should not be taken as precedent for adding methods to do normal_num += wrapping, only wrapping += normal_num. Similarly, while we should enable saturating += normal_num, we should not enable saturating += wrapping or wrapping += saturating.

@rfcbot
Copy link

rfcbot commented Jan 26, 2022

Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jan 26, 2022
@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Jan 26, 2022
@rfcbot
Copy link

rfcbot commented Jan 26, 2022

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Jan 26, 2022
kellerkindt added a commit to kellerkindt/rust that referenced this pull request Jan 26, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 28, 2022
…mpl, r=joshtriplett

Unimpl {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}<$t> for Saturating<$t>

Tracking issue rust-lang#92354

Analog to 9648b31 rust-lang#93208 reduce `saturating_int_assign_impl` (rust-lang#93208) to:

```rust
let mut value = Saturating(2u8);
value += 3u8;
value -= 1u8;
value *= 2u8;
value /= 2u8;
value %= 2u8;
value ^= 255u8;
value |= 123u8;
value &= 2u8;
```

See rust-lang#93208 (comment)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 28, 2022
…mpl, r=joshtriplett

Unimpl {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}<$t> for Saturating<$t>

Tracking issue rust-lang#92354

Analog to 9648b31 rust-lang#93208 reduce `saturating_int_assign_impl` (rust-lang#93208) to:

```rust
let mut value = Saturating(2u8);
value += 3u8;
value -= 1u8;
value *= 2u8;
value /= 2u8;
value %= 2u8;
value ^= 255u8;
value |= 123u8;
value &= 2u8;
```

See rust-lang#93208 (comment)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 28, 2022
…mpl, r=joshtriplett

Unimpl {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}<$t> for Saturating<$t>

Tracking issue rust-lang#92354

Analog to 9648b31 rust-lang#93208 reduce `saturating_int_assign_impl` (rust-lang#93208) to:

```rust
let mut value = Saturating(2u8);
value += 3u8;
value -= 1u8;
value *= 2u8;
value /= 2u8;
value %= 2u8;
value ^= 255u8;
value |= 123u8;
value &= 2u8;
```

See rust-lang#93208 (comment)
@m-ou-se m-ou-se removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Feb 2, 2022
@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Feb 5, 2022
@rfcbot
Copy link

rfcbot commented Feb 5, 2022

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@m-ou-se m-ou-se changed the title Impl {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}Assign<$t> for Wrapping<$t> for rust 1.61.0 Impl {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}Assign<$t> for Wrapping<$t> for rust 1.60.0 Feb 7, 2022
@m-ou-se
Copy link
Member

m-ou-se commented Feb 7, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Feb 7, 2022

📌 Commit 14ff58c has been approved by m-ou-se

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 7, 2022
m-ou-se added a commit to m-ou-se/rust that referenced this pull request Feb 7, 2022
…l, r=m-ou-se

Impl {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}Assign<$t> for Wrapping<$t> for rust 1.60.0

Tracking issue rust-lang#93204

This is about adding basic integer operations to the `Wrapping` type:

```rust
let mut value = Wrapping(2u8);
value += 3u8;
value -= 1u8;
value *= 2u8;
value /= 2u8;
value %= 2u8;
value ^= 255u8;
value |= 123u8;
value &= 2u8;
```

Because this adds stable impls on a stable type, it runs into the following issue if an `#[unstable(...)]` attribute is used:

```
an `#[unstable]` annotation here has no effect
note: see issue rust-lang#55436 <rust-lang#55436> for more information
```

This means - if I understood this correctly - the new impls have to be stabilized instantly.
Which in turn means, this PR has to kick of an FCP on the tracking issue as well?

This impl is analog to 1c0dc18 rust-lang#92356 for the `Saturating` type `@dtolnay`  `@Mark-Simulacrum`
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 7, 2022
Rollup of 13 pull requests

Successful merges:

 - rust-lang#88313 (Make the pre-commit script pre-push instead)
 - rust-lang#91530 (Suggest 1-tuple parentheses on exprs without existing parens)
 - rust-lang#92724 (Cleanup c_str.rs)
 - rust-lang#93208 (Impl {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}Assign<$t> for Wrapping<$t> for rust 1.60.0)
 - rust-lang#93394 (Don't allow {} to refer to implicit captures in format_args.)
 - rust-lang#93416 (remove `allow_fail` test flag)
 - rust-lang#93487 (Fix linking stage1 toolchain in `./x.py setup`)
 - rust-lang#93673 (Linkify sidebar headings for sibling items)
 - rust-lang#93680 (Drop json::from_reader)
 - rust-lang#93682 (Update tracking issue for `const_fn_trait_bound`)
 - rust-lang#93722 (Use shallow clones for submodules managed by rustbuild, not just bootstrap.py)
 - rust-lang#93723 (Rerun bootstrap's build script when RUSTC changes)
 - rust-lang#93737 (bootstrap: prefer using '--config' over 'RUST_BOOTSTRAP_CONFIG')

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit e3c972e into rust-lang:master Feb 7, 2022
@rustbot rustbot added this to the 1.60.0 milestone Feb 7, 2022
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants