2014-02-15 00:26:51 -06:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// ignore-tidy-linelength
|
|
|
|
|
2015-02-12 09:29:52 -06:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
struct Foo<'x> { bar: isize, marker: PhantomData<&'x ()> }
|
2015-01-08 04:54:35 -06:00
|
|
|
fn foo1<'a>(x: &Foo) -> &'a isize {
|
|
|
|
//~^ HELP: consider using an explicit lifetime parameter as shown: fn foo1<'a>(x: &'a Foo) -> &'a isize
|
2014-02-15 00:26:51 -06:00
|
|
|
&x.bar //~ ERROR: cannot infer
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn foo2<'a, 'b>(x: &'a Foo) -> &'b isize {
|
|
|
|
//~^ HELP: consider using an explicit lifetime parameter as shown: fn foo2<'a>(x: &'a Foo) -> &'a isize
|
2014-02-15 00:26:51 -06:00
|
|
|
&x.bar //~ ERROR: cannot infer
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn foo3<'a>(x: &Foo) -> (&'a isize, &'a isize) {
|
|
|
|
//~^ HELP: consider using an explicit lifetime parameter as shown: fn foo3<'a>(x: &'a Foo) -> (&'a isize, &'a isize)
|
2014-02-15 00:26:51 -06:00
|
|
|
(&x.bar, &x.bar) //~ ERROR: cannot infer
|
|
|
|
//~^ ERROR: cannot infer
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn foo4<'a, 'b>(x: &'a Foo) -> (&'b isize, &'a isize, &'b isize) {
|
|
|
|
//~^ HELP: consider using an explicit lifetime parameter as shown: fn foo4<'a>(x: &'a Foo) -> (&'a isize, &'a isize, &'a isize)
|
2014-02-15 00:26:51 -06:00
|
|
|
(&x.bar, &x.bar, &x.bar) //~ ERROR: cannot infer
|
|
|
|
//~^ ERROR: cannot infer
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
struct Cat<'x, T> { cat: &'x isize, t: T }
|
|
|
|
struct Dog<'y> { dog: &'y isize }
|
2014-03-24 18:11:44 -05:00
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn cat2<'x, 'y>(x: Cat<'x, Dog<'y>>) -> &'x isize {
|
2016-03-18 13:47:40 -05:00
|
|
|
//~^ HELP consider using an explicit lifetime parameter as shown: fn cat2<'x>(x: Cat<'x, Dog<'x>>) -> &'x isize
|
2016-03-17 03:15:06 -05:00
|
|
|
x.t.dog //~ ERROR E0312
|
2014-03-24 18:11:44 -05:00
|
|
|
}
|
2014-02-15 00:26:51 -06:00
|
|
|
|
Support lifetime suggestion for method
This includes a change to the way lifetime names are generated. Say we
figure that `[#0, 'a, 'b]` have to be the same lifetimes, then instead
of just generating a new lifetime `'c` like before to replace them, we
would reuse `'a`. This is done so that when the lifetime name comes
from an impl, we don't give something that's completely off, and we
don't have to do much work to figure out where the name came from. For
example, for the following code snippet:
```rust
struct Baz<'x> {
bar: &'x int
}
impl<'x> Baz<'x> {
fn baz1(&self) -> &int {
self.bar
}
}
```
`[#1, 'x]` (where `#1` is BrAnon(1) and refers to lifetime of `&int`)
have to be marked the same lifetime. With the old method, we would
generate a new lifetime `'a` and suggest `fn baz1(&self) -> &'a int`
or `fn baz1<'a>(&self) -> &'a int`, both of which are wrong.
2014-03-26 18:12:50 -05:00
|
|
|
struct Baz<'x> {
|
2015-01-08 04:54:35 -06:00
|
|
|
bar: &'x isize
|
Support lifetime suggestion for method
This includes a change to the way lifetime names are generated. Say we
figure that `[#0, 'a, 'b]` have to be the same lifetimes, then instead
of just generating a new lifetime `'c` like before to replace them, we
would reuse `'a`. This is done so that when the lifetime name comes
from an impl, we don't give something that's completely off, and we
don't have to do much work to figure out where the name came from. For
example, for the following code snippet:
```rust
struct Baz<'x> {
bar: &'x int
}
impl<'x> Baz<'x> {
fn baz1(&self) -> &int {
self.bar
}
}
```
`[#1, 'x]` (where `#1` is BrAnon(1) and refers to lifetime of `&int`)
have to be marked the same lifetime. With the old method, we would
generate a new lifetime `'a` and suggest `fn baz1(&self) -> &'a int`
or `fn baz1<'a>(&self) -> &'a int`, both of which are wrong.
2014-03-26 18:12:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Baz<'a> {
|
2015-01-08 04:54:35 -06:00
|
|
|
fn baz2<'b>(&self, x: &isize) -> (&'b isize, &'b isize) {
|
2016-07-22 11:44:09 -05:00
|
|
|
//~^ HELP consider using an explicit lifetime parameter as shown: fn baz2<'b>(&self, x: &'
|
|
|
|
// FIXME #35038: The above suggestion is different on Linux and Mac.
|
2016-03-17 03:15:06 -05:00
|
|
|
(self.bar, x) //~ ERROR E0312
|
|
|
|
//~^ ERROR E0312
|
Support lifetime suggestion for method
This includes a change to the way lifetime names are generated. Say we
figure that `[#0, 'a, 'b]` have to be the same lifetimes, then instead
of just generating a new lifetime `'c` like before to replace them, we
would reuse `'a`. This is done so that when the lifetime name comes
from an impl, we don't give something that's completely off, and we
don't have to do much work to figure out where the name came from. For
example, for the following code snippet:
```rust
struct Baz<'x> {
bar: &'x int
}
impl<'x> Baz<'x> {
fn baz1(&self) -> &int {
self.bar
}
}
```
`[#1, 'x]` (where `#1` is BrAnon(1) and refers to lifetime of `&int`)
have to be marked the same lifetime. With the old method, we would
generate a new lifetime `'a` and suggest `fn baz1(&self) -> &'a int`
or `fn baz1<'a>(&self) -> &'a int`, both of which are wrong.
2014-03-26 18:12:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 00:26:51 -06:00
|
|
|
fn main() {}
|