Skip to content

Lifetime duration errors before lifetimes are bound #16921

New issue

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

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

Already on GitHub? # to your account

Closed
mrmonday opened this issue Sep 1, 2014 · 3 comments
Closed

Lifetime duration errors before lifetimes are bound #16921

mrmonday opened this issue Sep 1, 2014 · 3 comments

Comments

@mrmonday
Copy link
Contributor

mrmonday commented Sep 1, 2014

The following code:

struct Bar<'b> {
    _field : &'b [u8]
}
struct Foo<'a, 'b> {
    _field: &'a Bar<'b>
}
fn main() {
}

Playpen: http://is.gd/oPQAxE
Gives the errors:

<anon>:4:1: 6:2 error: in type `&'a Bar<'b>`, reference has a longer lifetime than the data it references
<anon>:4 struct Foo<'a, 'b> {
<anon>:5     _field: &'a Bar<'b>
<anon>:6 }
<anon>:4:1: 6:2 note: the pointer is valid for the lifetime 'a as defined on the struct at 4:0
<anon>:4 struct Foo<'a, 'b> {
<anon>:5     _field: &'a Bar<'b>
<anon>:6 }
<anon>:4:1: 6:2 note: but the referenced data is only valid for the lifetime 'b as defined on the struct at 4:0
<anon>:4 struct Foo<'a, 'b> {
<anon>:5     _field: &'a Bar<'b>
<anon>:6 }
error: aborting due to previous error
playpen: application terminated with error code 101
Program ended.

Despite the lifetimes of 'a and 'b not having been bound yet.
This is happens using rustc 0.12.0-pre (23c1f9b3c 2014-08-30 21:51:25 +0000).

@huonw
Copy link
Member

huonw commented Sep 1, 2014

I don't think this is a bug. The only way for the &'a Bar<'b> pointer to be valid is if the 'b lifetime is at least as long as the 'a one (if it wasn't, you would have a pointer apparently valid for 'a pointing to data only valid for shorter lifetime). The correct fix is to tell the compiler that 'b is at least as long as 'a:

struct Bar<'b> {
    _field : &'b [u8]
}
struct Foo<'a, 'b: 'a> {
    _field: &'a Bar<'b>
}
fn main() {
}

Closing as not-a-bug (although maybe @nikomatsakis could just confirm).

@huonw huonw closed this as completed Sep 1, 2014
@eddyb
Copy link
Member

eddyb commented Sep 1, 2014

For the reference, this is a [breaking-change] introduced by #16453.

@nikomatsakis
Copy link
Contributor

Confirm.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants