]> git.lizzy.rs Git - rust.git/blob - tests/ui/typeck/issue-52082-type-param-shadows-existing-type.rs
Rollup merge of #106648 - Nilstrieb:poly-cleanup, r=compiler-errors
[rust.git] / tests / ui / typeck / issue-52082-type-param-shadows-existing-type.rs
1 // Fix issue 52082: Confusing error if accidentally defining a type parameter with the same name as
2 // an existing type
3 //
4 // To this end, make sure that when trying to retrieve a field of a (reference to) type parameter,
5 // rustc points to the point where the parameter was defined.
6 #[derive(Debug)]
7 struct Point
8 {
9     x: i32,
10     y: i32
11 }
12
13 impl Point
14 {
15     fn add(a: &Point, b: &Point) -> Point
16     {
17         Point {x: a.x + b.x, y: a.y + b.y}
18     }
19 }
20
21 trait Eq
22 {
23     fn equals_ref<T>(a: &T, b: &T) -> bool;
24     fn equals_val<T>(a: T, b: T) -> bool;
25 }
26
27 impl Eq for Point
28 {
29     fn equals_ref<Point>(a: &Point, b: &Point) -> bool
30     {
31         a.x == b.x && a.y == b.y //~ ERROR no field `x` on type `&Point` [E0609]
32                                  //~|ERROR no field `x` on type `&Point` [E0609]
33                                  //~|ERROR no field `y` on type `&Point` [E0609]
34                                  //~|ERROR no field `y` on type `&Point` [E0609]
35     }
36
37     fn equals_val<Point>(a: Point, b: Point) -> bool
38     {
39         a.x == b.x && a.y == b.y //~ ERROR no field `x` on type `Point` [E0609]
40                                  //~|ERROR no field `x` on type `Point` [E0609]
41                                  //~|ERROR no field `y` on type `Point` [E0609]
42                                  //~|ERROR no field `y` on type `Point` [E0609]
43     }
44 }
45
46 fn main()
47 {
48     let p1 = Point {x:  0, y: 10};
49     let p2 = Point {x: 20, y: 42};
50     println!("{:?}", Point::add(&p1, &p2));
51     println!("p1: {:?}, p2: {:?}", p1, p2);
52     println!("&p1 == &p2: {:?}", Point::equals_ref(&p1, &p2));
53     println!("p1 == p2: {:?}", Point::equals_val(p1, p2));
54 }