]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/lifetime-elision-return-type-requires-explicit-lifetime.rs
rustc_typeck: do not overlap a borrow of TypeckTables with method lookup.
[rust.git] / src / test / compile-fail / lifetime-elision-return-type-requires-explicit-lifetime.rs
1 // Copyright 2014 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 // Lifetime annotation needed because we have no arguments.
12 fn f() -> &isize {    //~ ERROR missing lifetime specifier
13 //~^ HELP there is no value for it to be borrowed from
14 //~| HELP consider giving it a 'static lifetime
15     panic!()
16 }
17
18 // Lifetime annotation needed because we have two by-reference parameters.
19 fn g(_x: &isize, _y: &isize) -> &isize {    //~ ERROR missing lifetime specifier
20 //~^ HELP the signature does not say whether it is borrowed from `_x` or `_y`
21     panic!()
22 }
23
24 struct Foo<'a> {
25     x: &'a isize,
26 }
27
28 // Lifetime annotation needed because we have two lifetimes: one as a parameter
29 // and one on the reference.
30 fn h(_x: &Foo) -> &isize { //~ ERROR missing lifetime specifier
31 //~^ HELP the signature does not say which one of `_x`'s 2 elided lifetimes it is borrowed from
32     panic!()
33 }
34
35 fn i(_x: isize) -> &isize { //~ ERROR missing lifetime specifier
36 //~^ HELP this function's return type contains a borrowed value
37 //~| HELP consider giving it an explicit bounded or 'static lifetime
38     panic!()
39 }
40
41 // Cases which used to work but now don't.
42
43 type StaticStr = &'static str; // hides 'static
44 trait WithLifetime<'a> {
45     type Output; // can hide 'a
46 }
47
48 // This worked because the type of the first argument contains
49 // 'static, although StaticStr doesn't even have parameters.
50 fn j(_x: StaticStr) -> &isize { //~ ERROR missing lifetime specifier
51 //~^ HELP this function's return type contains a borrowed value
52 //~| HELP consider giving it an explicit bounded or 'static lifetime
53     panic!()
54 }
55
56 // This worked because the compiler resolved the argument type
57 // to <T as WithLifetime<'a>>::Output which has the hidden 'a.
58 fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> &isize {
59 //~^ ERROR missing lifetime specifier
60 //~| HELP this function's return type contains a borrowed value
61 //~| HELP consider giving it an explicit bounded or 'static lifetime
62     panic!()
63 }
64
65 fn main() {}