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