]> git.lizzy.rs Git - rust.git/blob - src/test/ui/wrong-mul-method-signature.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / wrong-mul-method-signature.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // This test is to make sure we don't just ICE if the trait
12 // method for an operator is not implemented properly.
13 // (In this case the mul method should take &f64 and not f64)
14 // See: #11450
15
16 use std::ops::Mul;
17
18 struct Vec1 {
19     x: f64
20 }
21
22 // Expecting value in input signature
23 impl Mul<f64> for Vec1 {
24     type Output = Vec1;
25
26     fn mul(self, s: &f64) -> Vec1 {
27     //~^ ERROR method `mul` has an incompatible type for trait
28         Vec1 {
29             x: self.x * *s
30         }
31     }
32 }
33
34 struct Vec2 {
35     x: f64,
36     y: f64
37 }
38
39 // Wrong type parameter ordering
40 impl Mul<Vec2> for Vec2 {
41     type Output = f64;
42
43     fn mul(self, s: f64) -> Vec2 {
44     //~^ ERROR method `mul` has an incompatible type for trait
45         Vec2 {
46             x: self.x * s,
47             y: self.y * s
48         }
49     }
50 }
51
52 struct Vec3 {
53     x: f64,
54     y: f64,
55     z: f64
56 }
57
58 // Unexpected return type
59 impl Mul<f64> for Vec3 {
60     type Output = i32;
61
62     fn mul(self, s: f64) -> f64 {
63     //~^ ERROR method `mul` has an incompatible type for trait
64         s
65     }
66 }
67
68 pub fn main() {
69     // Check that the usage goes from the trait declaration:
70
71     let x: Vec1 = Vec1 { x: 1.0 } * 2.0; // this is OK
72
73     let x: Vec2 = Vec2 { x: 1.0, y: 2.0 } * 2.0; // trait had reversed order
74     // (we no longer signal a compile error here, since the
75     //  error in the trait signature will cause compilation to
76     //  abort before we bother looking at function bodies.)
77
78     let x: i32 = Vec3 { x: 1.0, y: 2.0, z: 3.0 } * 2.0;
79 }