Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

librustc: Implement lifetime elision. #15767

Closed
wants to merge 1 commit into from

Conversation

pcwalton
Copy link
Contributor

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]

r? @nick29581

@@ -21,34 +21,28 @@ fn foo2<'a, 'b>(x: &'a Foo) -> &'b int {
&x.bar //~ ERROR: cannot infer
}

fn foo3(x: &Foo) -> (&int, &int) {
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this note and the one above seem inappropriate now. They should probably be removed (from the test, but also from the compile, if possible)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain in more detail? It doesn't seem immediately inappropriate to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I missed that there was no lifetime on the arg. Ignore that comment

@nrc
Copy link
Member

nrc commented Jul 18, 2014

looks good r=me with the test changes discussed on irc.

@bluss
Copy link
Member

bluss commented Jul 18, 2014

Disallowing naked -> &Foo would close issue #2478

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 rust-lang#15552.

[breaking-change]
bors added a commit that referenced this pull request Jul 20, 2014
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]

r? @nick29581
@bors bors closed this Jul 20, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement lifetime elision
4 participants