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

Lifetime error when using Self instead of <StructName> #66176

Closed
Aaron1011 opened this issue Nov 7, 2019 · 5 comments
Closed

Lifetime error when using Self instead of <StructName> #66176

Aaron1011 opened this issue Nov 7, 2019 · 5 comments
Labels
A-lifetimes Area: lifetime related A-NLL Area: Non Lexical Lifetimes (NLL) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Aaron1011
Copy link
Member

The following code compiles:

struct MyStruct<'a> {
    val: &'a bool
}

impl<'a> MyStruct<'a> {
    fn make_new() {
        let new_val = true;
        let my_val = MyStruct { val: &new_val };
    }
}

However, changing MyStruct { val: &new_val } to Self { val: &new_val }:

struct MyStruct<'a> {
    val: &'a bool
}

impl<'a> MyStruct<'a> {
    fn make_new() {
        let new_val = true;
        let my_val = Self { val: &new_val };
    }
}

gives the following error:

error[E0597]: `new_val` does not live long enough
 --> src/lib.rs:8:34
  |
5 | impl<'a> MyStruct<'a> {
  |      -- lifetime `'a` defined here
...
8 |         let my_val = Self { val: &new_val };
  |                                  ^^^^^^^^
  |                                  |
  |                                  borrowed value does not live long enough
  |                                  requires that `new_val` is borrowed for `'a`
9 |     }
  |     - `new_val` dropped here while still borrowed

Minimized from #65897 (comment) (specifically, bcder v0.1.0 (github))

@estebank estebank added A-lifetimes Area: lifetime related A-NLL Area: Non Lexical Lifetimes (NLL) regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 7, 2019
@Centril
Copy link
Contributor

Centril commented Nov 7, 2019

Seems like the error is correctly emitted in this case.

When we write Self, we really mean MyStruct<'a> with that lifetime argument applied to the parameter. In particular, we expect &'a bool. We have &new_val which is of type &'0 new_val but we cannot prove that '0 <: 'a and so therefore Self { val: &new_val } is ill-typed.

The same error message can be seen if we use MyStruct::<'a> directly:

struct MyStruct<'a> {
    val: &'a bool
}

impl<'a> MyStruct<'a> {
    fn make_new() {
        let new_val = true;
        let my_val = MyStruct::<'a> { val: &new_val };
        //~^ ERROR `new_val` does not live long enough
    }
}

@Aaron1011
Copy link
Member Author

That seems extremely counterintuive to me - I would expect Self to just refer to the type name.

At a minimum, I think we should add something to the error message to indicate that this is going on (I assume changing the expansion of Self would be a breaking change)

@eddyb
Copy link
Member

eddyb commented Nov 7, 2019

I wrote a longer message but then I noticed this:
The example provided does not compile on stable, and the linked crater failure involves Self::new(...) calls, not Self {...} struct creation syntax.

I would expect Self to just refer to the type name

It never did, in any position. It's always a fully applied type and Self<...> never works.
It would be quite less useful in several situations if it were just the name.
For example, the common case of fn new() -> Self.

@pnkfelix
Copy link
Member

pnkfelix commented Nov 7, 2019

triage: closing as not a bug.

@pnkfelix pnkfelix closed this as completed Nov 7, 2019
@Centril
Copy link
Contributor

Centril commented Nov 7, 2019

At a minimum, I think we should add something to the error message to indicate that this is going on (I assume changing the expansion of Self would be a breaking change)

Might be difficult, but worth it if not too intrusive? If so, a new issue should probably be opened for that aspect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: lifetime related A-NLL Area: Non Lexical Lifetimes (NLL) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants