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