]> git.proxmox.com Git - rustc.git/blame - src/test/ui/object-lifetime/object-lifetime-default-elision.rs
New upstream version 1.37.0+dfsg1
[rustc.git] / src / test / ui / object-lifetime / object-lifetime-default-elision.rs
CommitLineData
85aaf69f
SL
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
6trait SomeTrait {
7 fn dummy(&self) { }
8}
9
10struct SomeStruct<'a> {
dc9dc135 11 r: Box<dyn SomeTrait+'a>
85aaf69f
SL
12}
13
14fn 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
dc9dc135 20fn load0<'a>(ss: &'a Box<dyn SomeTrait>) -> Box<dyn SomeTrait> {
85aaf69f
SL
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 //
c1a9b12d 27 // for<'a> fn(&'a Box<SomeTrait+'static>) -> Box<SomeTrait+'static>
85aaf69f 28 //
c1a9b12d 29 // Therefore, no type error.
85aaf69f
SL
30
31 deref(ss)
85aaf69f
SL
32}
33
dc9dc135 34fn load1(ss: &dyn SomeTrait) -> &dyn SomeTrait {
85aaf69f
SL
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
dc9dc135 48fn load2<'a>(ss: &'a dyn SomeTrait) -> &dyn SomeTrait {
85aaf69f
SL
49 // Same as `load1` but with an explicit name thrown in for fun.
50
51 ss
52}
53
dc9dc135 54fn load3<'a,'b>(ss: &'a dyn SomeTrait) -> &'b dyn SomeTrait {
85aaf69f
SL
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
8bb4bdeb 72 //~^ ERROR cannot infer
c34b1796 73 //~| ERROR cannot infer
85aaf69f
SL
74}
75
76fn main() {
77}