rust/src/test/run-pass/regions-dependent-addr-of.rs
Patrick Walton 6f99a27886 librustc: Implement lifetime elision.
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]
2014-07-19 13:10:58 -07:00

120 lines
2.5 KiB
Rust

// Copyright 2012 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 lifetimes are linked properly when we create dependent region pointers.
// Issue #3148.
struct A {
value: B
}
struct B {
v1: int,
v2: [int, ..3],
v3: Vec<int> ,
v4: C,
v5: Box<C>,
v6: Option<C>
}
struct C {
f: int
}
fn get_v1(a: &A) -> &int {
// Region inferencer must deduce that &v < L2 < L1
let foo = &a.value; // L1
&foo.v1 // L2
}
fn get_v2(a: &A, i: uint) -> &int {
let foo = &a.value;
&foo.v2[i]
}
fn get_v3(a: &A, i: uint) -> &int {
let foo = &a.value;
foo.v3.get(i)
}
fn get_v4(a: &A, _i: uint) -> &int {
let foo = &a.value;
&foo.v4.f
}
fn get_v5(a: &A, _i: uint) -> &int {
let foo = &a.value;
&foo.v5.f
}
fn get_v6_a(a: &A, _i: uint) -> &int {
match a.value.v6 {
Some(ref v) => &v.f,
None => fail!()
}
}
fn get_v6_b(a: &A, _i: uint) -> &int {
match *a {
A { value: B { v6: Some(ref v), .. } } => &v.f,
_ => fail!()
}
}
fn get_v6_c(a: &A, _i: uint) -> &int {
match a {
&A { value: B { v6: Some(ref v), .. } } => &v.f,
_ => fail!()
}
}
fn get_v5_ref(a: &A, _i: uint) -> &int {
match &a.value {
&B {v5: box C {f: ref v}, ..} => v
}
}
pub fn main() {
let a = A {value: B {v1: 22,
v2: [23, 24, 25],
v3: vec!(26, 27, 28),
v4: C { f: 29 },
v5: box C { f: 30 },
v6: Some(C { f: 31 })}};
let p = get_v1(&a);
assert_eq!(*p, a.value.v1);
let p = get_v2(&a, 1);
assert_eq!(*p, a.value.v2[1]);
let p = get_v3(&a, 1);
assert_eq!(*p, *a.value.v3.get(1));
let p = get_v4(&a, 1);
assert_eq!(*p, a.value.v4.f);
let p = get_v5(&a, 1);
assert_eq!(*p, a.value.v5.f);
let p = get_v6_a(&a, 1);
assert_eq!(*p, a.value.v6.unwrap().f);
let p = get_v6_b(&a, 1);
assert_eq!(*p, a.value.v6.unwrap().f);
let p = get_v6_c(&a, 1);
assert_eq!(*p, a.value.v6.unwrap().f);
let p = get_v5_ref(&a, 1);
assert_eq!(*p, a.value.v5.f);
}