-
Notifications
You must be signed in to change notification settings - Fork 1.6k
RFC: implement Rem for all equally signed integers where RHS < LHS #2643
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
Conversation
This will break/change type inference in some instances, I think. While historically that isn’t considered a breaking change that must never be done, this would have scope larger than usual, I think. |
@nagisa I do not think that it is going to be that impactful, as the return type and the second argument have the same type. So we only need one of them to be used in a different operation. |
I've tested the rust compiler with |
This has same issue as previous proposals to implement comparisons for heterogeneous operands (#2021). Performing heterogeneous operations on integers correctly, if they are actually intended, is a real issue though. |
To demonstrate the breakage that nagisa mentioned: use std::ops::Rem;
struct Local<T>(T);
// What we have today
impl Rem for Local<i16> {
type Output = Self;
fn rem(self, x: Self) -> Self { Local(self.0 % x.0) }
}
// Adding this new one breaks the `main` below
impl Rem<Local<i8>> for Local<i16> {
type Output = Local<i8>;
fn rem(self, x: Local<i8>) -> Local<i8> { Local((self.0 % x.0 as i16) as i8) }
}
fn main() {
let _ = Local(1_i16) % Local(4);
// ERROR: ^ no implementation for `Local<i16> % Local<i32>`
} https://play.rust-lang.org/?edition=2018&gist=e7d759acb7e30d0b762a920520366f91 |
I'd like to face such problem in a more principled and more general solution compared to this, that's based on a value range analysis instead. |
In my opinion separate library functions could actually be enough, as long as they are part of |
rendered
Unresolved questions
Rem<usize/isize>
be implemented for.