]> git.lizzy.rs Git - rust.git/blob - src/test/ui/variance/variance-trait-matching.rs
Auto merge of #94216 - psumbera:sparc64-abi-fix2, r=nagisa
[rust.git] / src / test / ui / variance / variance-trait-matching.rs
1 #![allow(dead_code)]
2
3 // Get<T> is covariant in T
4 trait Get<T> {
5     fn get(&self) -> T;
6 }
7
8 struct Cloner<T:Clone> {
9     t: T
10 }
11
12 impl<T:Clone> Get<T> for Cloner<T> {
13     fn get(&self) -> T {
14         self.t.clone()
15     }
16 }
17
18 fn get<'a, G>(get: &G) -> i32
19     where G : Get<&'a i32>
20 {
21     // This fails to type-check because, without variance, we can't
22     // use `G : Get<&'a i32>` as evidence that `G : Get<&'b i32>`,
23     // even if `'a : 'b`.
24     pick(get, &22) //~ ERROR explicit lifetime required in the type of `get` [E0621]
25 }
26
27 fn pick<'b, G>(get: &'b G, if_odd: &'b i32) -> i32
28     where G : Get<&'b i32>
29 {
30     let v = *get.get();
31     if v % 2 != 0 { v } else { *if_odd }
32 }
33
34 fn main() {
35     let x = Cloner { t: &23 };
36     let y = get(&x);
37     assert_eq!(y, 23);
38 }