2020-08-24 04:51:32 +00:00
|
|
|
// edition:2018
|
2013-10-28 21:37:10 +00:00
|
|
|
// Check that lifetime resolver enforces the lifetime name scoping
|
|
|
|
// rules correctly in various scenarios.
|
|
|
|
|
|
|
|
struct Foo<'a> {
|
2015-01-08 10:54:35 +00:00
|
|
|
x: &'a isize
|
2013-10-28 21:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Foo<'a> {
|
|
|
|
// &'a is inherited:
|
2015-01-08 10:54:35 +00:00
|
|
|
fn m1(&self, arg: &'a isize) { }
|
2013-10-28 21:37:10 +00:00
|
|
|
fn m2(&'a self) { }
|
|
|
|
fn m3(&self, arg: Foo<'a>) { }
|
|
|
|
|
|
|
|
// &'b is not:
|
2015-01-08 10:54:35 +00:00
|
|
|
fn m4(&self, arg: &'b isize) { } //~ ERROR undeclared lifetime
|
2013-10-28 21:37:10 +00:00
|
|
|
fn m5(&'b self) { } //~ ERROR undeclared lifetime
|
|
|
|
fn m6(&self, arg: Foo<'b>) { } //~ ERROR undeclared lifetime
|
|
|
|
}
|
|
|
|
|
2015-01-08 10:54:35 +00:00
|
|
|
fn bar<'a>(x: &'a isize) {
|
2013-10-28 21:37:10 +00:00
|
|
|
// &'a is visible to code:
|
2015-01-08 10:54:35 +00:00
|
|
|
let y: &'a isize = x;
|
2013-10-28 21:37:10 +00:00
|
|
|
|
|
|
|
// &'a is not visible to *items*:
|
2022-03-06 14:44:48 +00:00
|
|
|
type X = Option<&'a isize>; //~ ERROR can't use generic parameters from outer item
|
2013-10-28 21:37:10 +00:00
|
|
|
enum E {
|
2022-03-06 14:44:48 +00:00
|
|
|
E1(&'a isize) //~ ERROR can't use generic parameters from outer item
|
2013-10-28 21:37:10 +00:00
|
|
|
}
|
|
|
|
struct S {
|
2022-03-06 14:44:48 +00:00
|
|
|
f: &'a isize //~ ERROR can't use generic parameters from outer item
|
2013-10-28 21:37:10 +00:00
|
|
|
}
|
2022-03-06 14:44:48 +00:00
|
|
|
fn f(a: &'a isize) { } //~ ERROR can't use generic parameters from outer item
|
2013-10-28 21:37:10 +00:00
|
|
|
|
|
|
|
// &'a CAN be declared on functions and used then:
|
2015-01-08 10:54:35 +00:00
|
|
|
fn g<'a>(a: &'a isize) { } // OK
|
2019-05-28 18:46:13 +00:00
|
|
|
fn h(a: Box<dyn for<'a> FnOnce(&'a isize)>) { } // OK
|
2013-10-28 21:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test nesting of lifetimes in fn type declarations
|
2015-01-08 10:54:35 +00:00
|
|
|
fn fn_types(a: &'a isize, //~ ERROR undeclared lifetime
|
2019-05-28 18:46:13 +00:00
|
|
|
b: Box<dyn for<'a> FnOnce(&'a isize,
|
2015-01-08 10:54:35 +00:00
|
|
|
&'b isize, //~ ERROR undeclared lifetime
|
2019-05-28 18:46:13 +00:00
|
|
|
Box<dyn for<'b> FnOnce(&'a isize,
|
2015-01-08 10:54:35 +00:00
|
|
|
&'b isize)>,
|
|
|
|
&'b isize)>, //~ ERROR undeclared lifetime
|
|
|
|
c: &'a isize) //~ ERROR undeclared lifetime
|
2013-10-28 21:37:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-24 04:51:32 +00:00
|
|
|
struct Bug {}
|
|
|
|
impl Bug {
|
|
|
|
async fn buggy(&self) -> &'a str { //~ ERROR use of undeclared lifetime name `'a`
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-28 21:37:10 +00:00
|
|
|
pub fn main() {}
|