]> git.lizzy.rs Git - rust.git/blob - src/test/ui/object-lifetime/object-lifetime-default-elision.rs
Move parse-fail tests to UI
[rust.git] / src / test / ui / object-lifetime / object-lifetime-default-elision.rs
1 // Copyright 2015 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 // Test various cases where the old rules under lifetime elision
12 // yield slightly different results than the new rules.
13
14 #![allow(dead_code)]
15
16 trait SomeTrait {
17     fn dummy(&self) { }
18 }
19
20 struct SomeStruct<'a> {
21     r: Box<SomeTrait+'a>
22 }
23
24 fn deref<T>(ss: &T) -> T {
25     // produces the type of a deref without worrying about whether a
26     // move out would actually be legal
27     loop { }
28 }
29
30 fn load0<'a>(ss: &'a Box<SomeTrait>) -> Box<SomeTrait> {
31     // Under old rules, the fully elaborated types of input/output were:
32     //
33     // for<'a,'b> fn(&'a Box<SomeTrait+'b>) -> Box<SomeTrait+'a>
34     //
35     // Under new rules the result is:
36     //
37     // for<'a> fn(&'a Box<SomeTrait+'static>) -> Box<SomeTrait+'static>
38     //
39     // Therefore, no type error.
40
41     deref(ss)
42 }
43
44 fn load1(ss: &SomeTrait) -> &SomeTrait {
45     // Under old rules, the fully elaborated types of input/output were:
46     //
47     // for<'a,'b> fn(&'a (SomeTrait+'b)) -> &'a (SomeTrait+'a)
48     //
49     // Under new rules the result is:
50     //
51     // for<'a> fn(&'a (SomeTrait+'a)) -> &'a (SomeTrait+'a)
52     //
53     // In both cases, returning `ss` is legal.
54
55     ss
56 }
57
58 fn load2<'a>(ss: &'a SomeTrait) -> &SomeTrait {
59     // Same as `load1` but with an explicit name thrown in for fun.
60
61     ss
62 }
63
64 fn load3<'a,'b>(ss: &'a SomeTrait) -> &'b SomeTrait {
65     // Under old rules, the fully elaborated types of input/output were:
66     //
67     // for<'a,'b,'c>fn(&'a (SomeTrait+'c)) -> &'b (SomeTrait+'a)
68     //
69     // Based on the input/output types, the compiler could infer that
70     //     'c : 'a
71     //     'b : 'a
72     // must hold, and therefore it permitted `&'a (Sometrait+'c)` to be
73     // coerced to `&'b (SomeTrait+'a)`.
74     //
75     // Under the newer defaults, though, we get:
76     //
77     // for<'a,'b> fn(&'a (SomeTrait+'a)) -> &'b (SomeTrait+'b)
78     //
79     // which fails to type check.
80
81     ss
82         //~^ ERROR cannot infer
83         //~| ERROR cannot infer
84 }
85
86 fn main() {
87 }