]> git.lizzy.rs Git - rust.git/blob - src/test/ui/issue-54302-cases.rs
6d1c61c80f06e983d4224e20fb375f4bce55c4eb
[rust.git] / src / test / ui / issue-54302-cases.rs
1 // Copyright 2018 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 trait Mirror {
12     type Image;
13     fn coerce(self) -> Self::Image;
14 }
15
16 impl<T> Mirror for T {
17     type Image = T;
18     fn coerce(self) -> Self { self }
19 }
20
21 trait Foo<'x, T> {
22     fn foo(self) -> &'x T;
23 }
24
25 impl<'s, 'x, T: 'x> Foo<'x, T> for &'s T where &'s T: Foo2<'x, T> {
26     fn foo(self) -> &'x T { self.foo2() }
27 }
28
29 trait Foo2<'x, T> {
30     fn foo2(self) -> &'x T;
31 }
32
33 // example 1 - fails leak check
34 impl<'x> Foo2<'x, u32> for &'x u32
35 {
36     fn foo2(self) -> &'x u32 { self }
37 }
38
39 // example 2 - OK with this issue
40 impl<'x, 'a: 'x> Foo2<'x, i32> for &'a i32
41 {
42     fn foo2(self) -> &'x i32 { self }
43 }
44
45 // example 3 - fails due to issue #XYZ + Leak-check
46 impl<'x, T> Foo2<'x, u64> for T
47     where T: Mirror<Image=&'x u64>
48 {
49     fn foo2(self) -> &'x u64 { self.coerce() }
50 }
51
52 // example 4 - fails due to issue #XYZ
53 impl<'x, 'a: 'x, T> Foo2<'x, i64> for T
54     where T: Mirror<Image=&'a i64>
55 {
56     fn foo2(self) -> &'x i64 { self.coerce() }
57 }
58
59
60 trait RefFoo<T> {
61     fn ref_foo(&self) -> &'static T;
62 }
63
64 impl<T> RefFoo<T> for T where for<'a> &'a T: Foo<'static, T> {
65     fn ref_foo(&self) -> &'static T {
66         self.foo()
67     }
68 }
69
70
71 fn coerce_lifetime1(a: &u32) -> &'static u32
72 {
73     <u32 as RefFoo<u32>>::ref_foo(a)
74     //~^ ERROR the trait bound `for<'a> &'a u32: Foo2<'_, u32>` is not satisfied
75 }
76
77 fn coerce_lifetime2(a: &i32) -> &'static i32
78 {
79     <i32 as RefFoo<i32>>::ref_foo(a)
80     //~^ ERROR the requirement `for<'a> 'a : ` is not satisfied
81 }
82
83 fn coerce_lifetime3(a: &u64) -> &'static u64
84 {
85     <u64 as RefFoo<u64>>::ref_foo(a)
86     //~^ ERROR type mismatch resolving `for<'a> <&'a u64 as Mirror>::Image == &u64`
87 }
88
89 fn coerce_lifetime4(a: &i64) -> &'static i64
90 {
91     <i64 as RefFoo<i64>>::ref_foo(a)
92     //~^ ERROR type mismatch resolving `for<'a> <&'a i64 as Mirror>::Image == &i64`
93 }
94
95 fn main() {}