-
Notifications
You must be signed in to change notification settings - Fork 32
/
lifetime-elision-return-type-requires-explicit-lifetime.rs
60 lines (48 loc) · 1.59 KB
/
lifetime-elision-return-type-requires-explicit-lifetime.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Lifetime annotation needed because we have no arguments.
fn f() -> &isize { //~ ERROR missing lifetime specifier
panic!()
}
// Lifetime annotation needed because we have two by-reference parameters.
fn g(_x: &isize, _y: &isize) -> &isize { //~ ERROR missing lifetime specifier
panic!()
}
struct Foo<'a> {
x: &'a isize,
}
// Lifetime annotation needed because we have two lifetimes: one as a parameter
// and one on the reference.
fn h(_x: &Foo) -> &isize { //~ ERROR missing lifetime specifier
panic!()
}
fn i(_x: isize) -> &isize { //~ ERROR missing lifetime specifier
panic!()
}
// Cases which used to work but now don't.
type StaticStr = &'static str; // hides 'static
trait WithLifetime<'a> {
type Output; // can hide 'a
}
// This worked because the type of the first argument contains
// 'static, although StaticStr doesn't even have parameters.
fn j(_x: StaticStr) -> &isize { //~ ERROR missing lifetime specifier
panic!()
}
// This worked because the compiler resolved the argument type
// to <T as WithLifetime<'a>>::Output which has the hidden 'a.
fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> &isize {
//~^ ERROR missing lifetime specifier
panic!()
}
fn l<'a>(_: &'a str, _: &'a str) -> &str { "" }
//~^ ERROR missing lifetime specifier
// This is ok because both `'a` are for the same parameter.
fn m<'a>(_: &'a Foo<'a>) -> &str { "" }
fn main() {}
// ferrocene-annotations: fls_l9ebxrlxyawd
// Lifetime Elision
//
// ferrocene-annotations: fls_jeoas4n6su4
// Trait and Lifetime Bounds
//
// ferrocene-annotations: fls_hethxxbcg7ja
// Function Lifetime Elision