]> git.proxmox.com Git - rustc.git/blame - src/test/ui/nll/outlives-suggestion-simple.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / test / ui / nll / outlives-suggestion-simple.rs
CommitLineData
60c5eb7d
XL
1// Test the simplest of outlives suggestions.
2
60c5eb7d
XL
3fn foo1<'a, 'b>(x: &'a usize) -> &'b usize {
4 x //~ERROR lifetime may not live long enough
5}
6
7fn foo2<'a>(x: &'a usize) -> &'static usize {
8 x //~ERROR lifetime may not live long enough
9}
10
11fn 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
16fn 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
23struct Foo<'a> {
24 x: &'a usize,
25}
26
27impl Foo<'static> {
28 pub fn foo<'a>(x: &'a usize) -> Self {
29 Foo { x } //~ERROR lifetime may not live long enough
30 }
31}
32
33struct Bar<'a> {
34 x: &'a usize,
35}
36
37impl<'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
44struct Baz<'a> {
45 x: &'a mut i32,
46}
47
48impl<'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
55struct Bar2<'a> {
56 bar: &'a str,
57}
58impl<'a> Bar2<'a> {
59 fn new(foo: &'a Foo2<'a>) -> Bar2<'a> {
60 Bar2 { bar: foo.raw }
61 }
62}
63
64pub struct Foo2<'a> {
65 raw: &'a str,
66 cell: std::cell::Cell<&'a str>,
67}
68impl<'a> Foo2<'a> {
69 // should not produce outlives suggestions to name 'self
70 fn get_bar(&self) -> Bar2 {
5099ac24 71 Bar2::new(&self) //~ERROR lifetime may not live long enough
60c5eb7d
XL
72 }
73}
74
75fn main() {}