]> git.proxmox.com Git - rustc.git/blob - src/test/ui/nll/outlives-suggestion-simple.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / src / test / ui / nll / outlives-suggestion-simple.rs
1 // Test the simplest of outlives suggestions.
2
3 #![feature(nll)]
4
5 fn foo1<'a, 'b>(x: &'a usize) -> &'b usize {
6 x //~ERROR lifetime may not live long enough
7 }
8
9 fn foo2<'a>(x: &'a usize) -> &'static usize {
10 x //~ERROR lifetime may not live long enough
11 }
12
13 fn foo3<'a, 'b>(x: &'a usize, y: &'b usize) -> (&'b usize, &'a usize) {
14 (x, y) //~ERROR lifetime may not live long enough
15 //~^ERROR lifetime may not live long enough
16 }
17
18 fn foo4<'a, 'b, 'c>(x: &'a usize) -> (&'b usize, &'c usize) {
19 // FIXME: ideally, we suggest 'a: 'b + 'c, but as of today (may 04, 2019), the null error
20 // reporting stops after the first error in a MIR def so as not to produce too many errors, so
21 // currently we only report 'a: 'b. The user would then re-run and get another error.
22 (x, x) //~ERROR lifetime may not live long enough
23 }
24
25 struct Foo<'a> {
26 x: &'a usize,
27 }
28
29 impl Foo<'static> {
30 pub fn foo<'a>(x: &'a usize) -> Self {
31 Foo { x } //~ERROR lifetime may not live long enough
32 }
33 }
34
35 struct Bar<'a> {
36 x: &'a usize,
37 }
38
39 impl<'a> Bar<'a> {
40 pub fn get<'b>(&self) -> &'b usize {
41 self.x //~ERROR lifetime may not live long enough
42 }
43 }
44
45 // source: https://stackoverflow.com/questions/41417057/why-do-i-get-a-lifetime-error-when-i-use-a-mutable-reference-in-a-struct-instead
46 struct Baz<'a> {
47 x: &'a mut i32,
48 }
49
50 impl<'a> Baz<'a> {
51 fn get<'b>(&'b self) -> &'a i32 {
52 self.x //~ERROR lifetime may not live long enough
53 }
54 }
55
56 // source: https://stackoverflow.com/questions/41204134/rust-lifetime-error
57 struct Bar2<'a> {
58 bar: &'a str,
59 }
60 impl<'a> Bar2<'a> {
61 fn new(foo: &'a Foo2<'a>) -> Bar2<'a> {
62 Bar2 { bar: foo.raw }
63 }
64 }
65
66 pub struct Foo2<'a> {
67 raw: &'a str,
68 cell: std::cell::Cell<&'a str>,
69 }
70 impl<'a> Foo2<'a> {
71 // should not produce outlives suggestions to name 'self
72 fn get_bar(&self) -> Bar2 {
73 Bar2::new(&self) //~ERROR borrowed data escapes outside of associated function
74 }
75 }
76
77 fn main() {}