]> git.lizzy.rs Git - rust.git/blob - src/test/ui/associated-types/associated-types-eq-hr.rs
Rollup merge of #53317 - estebank:abolish-ice, r=oli-obk
[rust.git] / src / test / ui / associated-types / associated-types-eq-hr.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 // Check testing of equality constraints in a higher-ranked context.
12
13 pub trait TheTrait<T> {
14     type A;
15
16     fn get(&self, t: T) -> Self::A;
17 }
18
19 struct IntStruct {
20     x: isize
21 }
22
23 impl<'a> TheTrait<&'a isize> for IntStruct {
24     type A = &'a isize;
25
26     fn get(&self, t: &'a isize) -> &'a isize {
27         t
28     }
29 }
30
31 struct UintStruct {
32     x: isize
33 }
34
35 impl<'a> TheTrait<&'a isize> for UintStruct {
36     type A = &'a usize;
37
38     fn get(&self, t: &'a isize) -> &'a usize {
39         panic!()
40     }
41 }
42
43 struct Tuple {
44 }
45
46 impl<'a> TheTrait<(&'a isize, &'a isize)> for Tuple {
47     type A = &'a isize;
48
49     fn get(&self, t: (&'a isize, &'a isize)) -> &'a isize {
50         t.0
51     }
52 }
53
54 fn foo<T>()
55     where T : for<'x> TheTrait<&'x isize, A = &'x isize>
56 {
57     // ok for IntStruct, but not UintStruct
58 }
59
60 fn bar<T>()
61     where T : for<'x> TheTrait<&'x isize, A = &'x usize>
62 {
63     // ok for UintStruct, but not IntStruct
64 }
65
66 fn tuple_one<T>()
67     where T : for<'x,'y> TheTrait<(&'x isize, &'y isize), A = &'x isize>
68 {
69     // not ok for tuple, two lifetimes and we pick first
70 }
71
72 fn tuple_two<T>()
73     where T : for<'x,'y> TheTrait<(&'x isize, &'y isize), A = &'y isize>
74 {
75     // not ok for tuple, two lifetimes and we pick second
76 }
77
78 fn tuple_three<T>()
79     where T : for<'x> TheTrait<(&'x isize, &'x isize), A = &'x isize>
80 {
81     // ok for tuple
82 }
83
84 fn tuple_four<T>()
85     where T : for<'x,'y> TheTrait<(&'x isize, &'y isize)>
86 {
87     // not ok for tuple, two lifetimes, and lifetime matching is invariant
88 }
89
90 pub fn main() {
91     foo::<IntStruct>();
92     foo::<UintStruct>(); //~ ERROR type mismatch
93
94     bar::<IntStruct>(); //~ ERROR type mismatch
95     bar::<UintStruct>();
96
97     tuple_one::<Tuple>();
98     //~^ ERROR E0277
99     //~| ERROR type mismatch
100
101     tuple_two::<Tuple>();
102     //~^ ERROR E0277
103     //~| ERROR type mismatch
104
105     tuple_three::<Tuple>();
106
107     tuple_four::<Tuple>();
108     //~^ ERROR E0277
109 }