-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Simple operator overloading does not work #22743
Comments
Seems to be a bug, this code works use std::ops::Mul;
pub struct Foo {
x: f64,
}
impl Mul<Foo> for f64 {
type Output = Foo;
fn mul(self, rhs: Foo) -> Foo {
println!("Multiplying!");
rhs
}
}
pub fn main() {
let f: Foo = Foo { x: 5.0 };
let val: f64 = 3.0;
let f2: Foo = Mul::mul(val, f);
} |
Yup, looks like that works, so looks like some sort of error in the desugaring... |
Just FYI, operator overloading isn't actually desguared, since desugaring happens before typechecking and we don't know if it's an overloaded operator until after then. Anyway, looks like this is a bug if the type checking for operations on primitive types. I think the fix is actually #19434, which was closed while #20749 was being resolved. Seeing as that issue has now been resolved, this bug may be fixable. I don't know enough details to be sure (specifically, whether or not this bug is indeed fixable, though the fact that implementation is valid suggests that it is). |
Fixes rust-lang#22743. Fixes rust-lang#19035. Fixes rust-lang#22099. (Those all seem to be exactly the same scenario.)
Apologies if this has been covered already or this is the wrong place to file this. This may be a bug or may simply be a lack of documentation, I'm not sure.
I have the following simple code:
which results in the error message
This usage seems in line with the documentation and the tutorials. It may be related to #21188 and maybe #20749, but I am not sure.
How do I fix this or what am I doing wrong or why is this not working?
Version:
The text was updated successfully, but these errors were encountered: