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]
46 lines
1.4 KiB
Rust
46 lines
1.4 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.
|
|
|
|
// Test that the borrow checker prevents pointers to temporaries
|
|
// with statement lifetimes from escaping.
|
|
|
|
#![feature(macro_rules)]
|
|
|
|
use std::ops::Drop;
|
|
|
|
static mut FLAGS: u64 = 0;
|
|
|
|
struct Box<T> { f: T }
|
|
struct AddFlags { bits: u64 }
|
|
|
|
fn AddFlags(bits: u64) -> AddFlags {
|
|
AddFlags { bits: bits }
|
|
}
|
|
|
|
fn arg(x: &AddFlags) -> &AddFlags {
|
|
x
|
|
}
|
|
|
|
impl AddFlags {
|
|
fn get(&self) -> &AddFlags {
|
|
self
|
|
}
|
|
}
|
|
|
|
pub fn main() {
|
|
let _x = arg(&AddFlags(1)); //~ ERROR value does not live long enough
|
|
let _x = AddFlags(1).get(); //~ ERROR value does not live long enough
|
|
let _x = &*arg(&AddFlags(1)); //~ ERROR value does not live long enough
|
|
let ref _x = *arg(&AddFlags(1)); //~ ERROR value does not live long enough
|
|
let &ref _x = arg(&AddFlags(1)); //~ ERROR value does not live long enough
|
|
let _x = AddFlags(1).get(); //~ ERROR value does not live long enough
|
|
let Box { f: _x } = Box { f: AddFlags(1).get() }; //~ ERROR value does not live long enough
|
|
}
|