File: wrong-mul-method-signature.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (68 lines) | stat: -rw-r--r-- 1,374 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 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
}

// Expecting value in input signature
impl Mul<f64> for Vec1 {
    type Output = Vec1;

    fn mul(self, s: &f64) -> Vec1 {
    //~^ ERROR method `mul` has an incompatible type for trait
        Vec1 {
            x: self.x * *s
        }
    }
}

struct Vec2 {
    x: f64,
    y: f64
}

// Wrong type parameter ordering
impl Mul<Vec2> for Vec2 {
    type Output = f64;

    fn mul(self, s: f64) -> Vec2 {
    //~^ 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
impl Mul<f64> for Vec3 {
    type Output = i32;

    fn mul(self, s: f64) -> f64 {
    //~^ ERROR method `mul` has an incompatible type for trait
        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;
}