]> git.lizzy.rs Git - rust.git/blob - src/test/ui/issues/issue-3743.rs
Auto merge of #78066 - bugadani:wat, r=jonas-schievink
[rust.git] / src / test / ui / issues / issue-3743.rs
1 // run-pass
2 // If `Mul` used an associated type for its output, this test would
3 // work more smoothly.
4
5 use std::ops::Mul;
6
7 #[derive(Copy, Clone)]
8 struct Vec2 {
9     x: f64,
10     y: f64
11 }
12
13 // methods we want to export as methods as well as operators
14 impl Vec2 {
15 #[inline(always)]
16     fn vmul(self, other: f64) -> Vec2 {
17         Vec2 { x: self.x * other, y: self.y * other }
18     }
19 }
20
21 // Right-hand-side operator visitor pattern
22 trait RhsOfVec2Mul {
23     type Result;
24
25     fn mul_vec2_by(&self, lhs: &Vec2) -> Self::Result;
26 }
27
28 // Vec2's implementation of Mul "from the other side" using the above trait
29 impl<Res, Rhs: RhsOfVec2Mul<Result=Res>> Mul<Rhs> for Vec2 {
30     type Output = Res;
31
32     fn mul(self, rhs: Rhs) -> Res { rhs.mul_vec2_by(&self) }
33 }
34
35 // Implementation of 'f64 as right-hand-side of Vec2::Mul'
36 impl RhsOfVec2Mul for f64 {
37     type Result = Vec2;
38
39     fn mul_vec2_by(&self, lhs: &Vec2) -> Vec2 { lhs.vmul(*self) }
40 }
41
42 // Usage with failing inference
43 pub fn main() {
44     let a = Vec2 { x: 3.0f64, y: 4.0f64 };
45
46     // the following compiles and works properly
47     let v1: Vec2 = a * 3.0f64;
48     println!("{} {}", v1.x, v1.y);
49
50     // the following compiles but v2 will not be Vec2 yet and
51     // using it later will cause an error that the type of v2
52     // must be known
53     let v2 = a * 3.0f64;
54     println!("{} {}", v2.x, v2.y); // error regarding v2's type
55 }