6f99a27886
This implements RFC 39. Omitted lifetimes in return values will now be inferred to more useful defaults, and an error is reported if a lifetime in a return type is omitted and one of the two lifetime elision rules does not specify what it should be. This primarily breaks two uncommon code patterns. The first is this: unsafe fn get_foo_out_of_thin_air() -> &Foo { ... } This should be changed to: unsafe fn get_foo_out_of_thin_air() -> &'static Foo { ... } The second pattern that needs to be changed is this: enum MaybeBorrowed<'a> { Borrowed(&'a str), Owned(String), } fn foo() -> MaybeBorrowed { Owned(format!("hello world")) } Change code like this to: enum MaybeBorrowed<'a> { Borrowed(&'a str), Owned(String), } fn foo() -> MaybeBorrowed<'static> { Owned(format!("hello world")) } Closes #15552. [breaking-change]
75 lines
2.7 KiB
Rust
75 lines
2.7 KiB
Rust
// 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
|
|
|
|
struct Foo<'x> { bar: int }
|
|
fn foo1<'a>(x: &Foo) -> &'a int {
|
|
//~^ NOTE: consider using an explicit lifetime parameter as shown: fn foo1<'a>(x: &'a Foo) -> &'a int
|
|
&x.bar //~ ERROR: cannot infer
|
|
}
|
|
|
|
fn foo2<'a, 'b>(x: &'a Foo) -> &'b int {
|
|
//~^ NOTE: consider using an explicit lifetime parameter as shown: fn foo2<'a>(x: &'a Foo) -> &'a int
|
|
&x.bar //~ ERROR: cannot infer
|
|
}
|
|
|
|
fn foo3<'a>(x: &Foo) -> (&'a int, &'a int) {
|
|
//~^ NOTE: consider using an explicit lifetime parameter as shown: fn foo3<'a>(x: &'a Foo) -> (&'a int, &'a int)
|
|
(&x.bar, &x.bar) //~ ERROR: cannot infer
|
|
//~^ ERROR: cannot infer
|
|
}
|
|
|
|
fn foo4<'a, 'b>(x: &'a Foo) -> (&'b int, &'a int, &'b int) {
|
|
//~^ NOTE: consider using an explicit lifetime parameter as shown: fn foo4<'a>(x: &'a Foo) -> (&'a int, &'a int, &'a int)
|
|
(&x.bar, &x.bar, &x.bar) //~ ERROR: cannot infer
|
|
//~^ ERROR: cannot infer
|
|
}
|
|
|
|
struct Bar<'x, 'y, 'z> { bar: &'y int, baz: int }
|
|
fn bar1<'a>(x: &Bar) -> (&'a int, &'a int, &'a int) {
|
|
//~^ NOTE: consider using an explicit lifetime parameter as shown: fn bar1<'b, 'c, 'a>(x: &'a Bar<'b, 'a, 'c>) -> (&'a int, &'a int, &'a int)
|
|
(x.bar, &x.baz, &x.baz) //~ ERROR: mismatched types
|
|
//~^ ERROR: cannot infer
|
|
//~^^ ERROR: cannot infer
|
|
}
|
|
|
|
fn bar2<'a, 'b, 'c>(x: &Bar<'a, 'b, 'c>) -> (&'a int, &'a int, &'a int) {
|
|
//~^ NOTE: consider using an explicit lifetime parameter as shown: fn bar2<'a, 'c>(x: &'a Bar<'a, 'a, 'c>) -> (&'a int, &'a int, &'a int)
|
|
(x.bar, &x.baz, &x.baz) //~ ERROR: mismatched types
|
|
//~^ ERROR: cannot infer
|
|
//~^^ ERROR: cannot infer
|
|
}
|
|
|
|
struct Cat<'x, T> { cat: &'x int, t: T }
|
|
struct Dog<'y> { dog: &'y int }
|
|
|
|
fn cat2<'x, 'y>(x: Cat<'x, Dog<'y>>) -> &'x int {
|
|
//~^ NOTE: consider using an explicit lifetime parameter as shown: fn cat2<'x>(x: Cat<'x, Dog<'x>>) -> &'x int
|
|
x.t.dog //~ ERROR: mismatched types
|
|
}
|
|
|
|
struct Baz<'x> {
|
|
bar: &'x int
|
|
}
|
|
|
|
|
|
impl<'a> Baz<'a> {
|
|
fn baz2<'b>(&self, x: &int) -> (&'b int, &'b int) {
|
|
// The lifetime that gets assigned to `x` seems somewhat random.
|
|
// I have disabled this test for the time being. --pcwalton
|
|
(self.bar, x) //~ ERROR: cannot infer
|
|
//~^ ERROR: mismatched types
|
|
//~^^ ERROR: mismatched types
|
|
}
|
|
}
|
|
|
|
fn main() {}
|