]> git.lizzy.rs Git - rust.git/blob - tests/ui/regions/regions-name-undeclared.rs
internally change regions to be covariant
[rust.git] / tests / ui / regions / regions-name-undeclared.rs
1 // edition:2018
2 // Check that lifetime resolver enforces the lifetime name scoping
3 // rules correctly in various scenarios.
4
5 struct Foo<'a> {
6     x: &'a isize
7 }
8
9 impl<'a> Foo<'a> {
10     // &'a is inherited:
11     fn m1(&self, arg: &'a isize) { }
12     fn m2(&'a self) { }
13     fn m3(&self, arg: Foo<'a>) { }
14
15     // &'b is not:
16     fn m4(&self, arg: &'b isize) { } //~ ERROR undeclared lifetime
17     fn m5(&'b self) { } //~ ERROR undeclared lifetime
18     fn m6(&self, arg: Foo<'b>) { } //~ ERROR undeclared lifetime
19 }
20
21 fn bar<'a>(x: &'a isize) {
22     // &'a is visible to code:
23     let y: &'a isize = x;
24
25     // &'a is not visible to *items*:
26     type X = Option<&'a isize>; //~ ERROR can't use generic parameters from outer item
27     enum E {
28         E1(&'a isize) //~ ERROR can't use generic parameters from outer item
29     }
30     struct S {
31         f: &'a isize //~ ERROR can't use generic parameters from outer item
32     }
33     fn f(a: &'a isize) { } //~ ERROR can't use generic parameters from outer item
34
35     // &'a CAN be declared on functions and used then:
36     fn g<'a>(a: &'a isize) { } // OK
37     fn h(a: Box<dyn for<'a> FnOnce(&'a isize)>) { } // OK
38 }
39
40 // Test nesting of lifetimes in fn type declarations
41 fn fn_types(a: &'a isize, //~ ERROR undeclared lifetime
42             b: Box<dyn for<'a> FnOnce(&'a isize,
43                                   &'b isize, //~ ERROR undeclared lifetime
44                                   Box<dyn for<'b> FnOnce(&'a isize,
45                                                      &'b isize)>,
46                                   &'b isize)>, //~ ERROR undeclared lifetime
47             c: &'a isize) //~ ERROR undeclared lifetime
48 {
49 }
50
51 struct Bug {}
52 impl Bug {
53     async fn buggy(&self) -> &'a str { //~ ERROR use of undeclared lifetime name `'a`
54         todo!()
55     }
56 }
57
58 pub fn main() {}