rust/tests/ui/traits/wrong-mul-method-signature.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

69 lines
1.3 KiB
Rust
Raw Normal View History

// This test is to make sure we don't just ICE if the trait
// method for an operator is not implemented properly.
// (In this case the mul method should take &f64 and not f64)
// See: #11450
use std::ops::Mul;
struct Vec1 {
x: f64
}
2014-12-01 22:52:10 +00:00
// Expecting value in input signature
2014-12-31 20:45:13 +00:00
impl Mul<f64> for Vec1 {
type Output = Vec1;
2014-12-01 22:52:10 +00:00
fn mul(self, s: &f64) -> Vec1 {
2015-01-12 06:01:44 +00:00
//~^ ERROR method `mul` has an incompatible type for trait
Vec1 {
2014-12-01 22:52:10 +00:00
x: self.x * *s
}
}
}
struct Vec2 {
x: f64,
y: f64
}
// Wrong type parameter ordering
2014-12-31 20:45:13 +00:00
impl Mul<Vec2> for Vec2 {
type Output = f64;
2014-12-01 22:52:10 +00:00
fn mul(self, s: f64) -> Vec2 {
2015-01-12 06:01:44 +00:00
//~^ ERROR method `mul` has an incompatible type for trait
Vec2 {
x: self.x * s,
y: self.y * s
}
}
}
struct Vec3 {
x: f64,
y: f64,
z: f64
}
// Unexpected return type
2014-12-31 20:45:13 +00:00
impl Mul<f64> for Vec3 {
type Output = i32;
2014-12-01 22:52:10 +00:00
fn mul(self, s: f64) -> f64 {
2015-01-12 06:01:44 +00:00
//~^ ERROR method `mul` has an incompatible type for trait
2014-12-01 22:52:10 +00:00
s
}
}
pub fn main() {
// Check that the usage goes from the trait declaration:
let x: Vec1 = Vec1 { x: 1.0 } * 2.0; // this is OK
let x: Vec2 = Vec2 { x: 1.0, y: 2.0 } * 2.0; // trait had reversed order
//~^ ERROR mismatched types
//~| ERROR mismatched types
let x: i32 = Vec3 { x: 1.0, y: 2.0, z: 3.0 } * 2.0;
}