]> git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/object-lifetime-default-elision.rs
Imported Upstream version 1.1.0+dfsg1
[rustc.git] / src / test / compile-fail / 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+'a>) -> Box<SomeTrait+'static>
38 //
39 // Therefore, we get a type error attempting to return `deref(ss)`
40 // since `SomeTrait+'a <: SomeTrait+'static` does not hold.
41
42 deref(ss)
43 //~^ ERROR cannot infer
44 }
45
46 fn load1(ss: &SomeTrait) -> &SomeTrait {
47 // Under old rules, the fully elaborated types of input/output were:
48 //
49 // for<'a,'b> fn(&'a (SomeTrait+'b)) -> &'a (SomeTrait+'a)
50 //
51 // Under new rules the result is:
52 //
53 // for<'a> fn(&'a (SomeTrait+'a)) -> &'a (SomeTrait+'a)
54 //
55 // In both cases, returning `ss` is legal.
56
57 ss
58 }
59
60 fn load2<'a>(ss: &'a SomeTrait) -> &SomeTrait {
61 // Same as `load1` but with an explicit name thrown in for fun.
62
63 ss
64 }
65
66 fn load3<'a,'b>(ss: &'a SomeTrait) -> &'b SomeTrait {
67 // Under old rules, the fully elaborated types of input/output were:
68 //
69 // for<'a,'b,'c>fn(&'a (SomeTrait+'c)) -> &'b (SomeTrait+'a)
70 //
71 // Based on the input/output types, the compiler could infer that
72 // 'c : 'a
73 // 'b : 'a
74 // must hold, and therefore it permitted `&'a (Sometrait+'c)` to be
75 // coerced to `&'b (SomeTrait+'a)`.
76 //
77 // Under the newer defaults, though, we get:
78 //
79 // for<'a,'b> fn(&'a (SomeTrait+'a)) -> &'b (SomeTrait+'b)
80 //
81 // which fails to type check.
82
83 ss
84 //~^ ERROR lifetime bound not satisfied
85 //~| ERROR cannot infer
86 }
87
88 fn main() {
89 }