Skip to content

Lifetime inference misses what seems like a simple case #15735

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

Closed
zwarich opened this issue Jul 17, 2014 · 4 comments
Closed

Lifetime inference misses what seems like a simple case #15735

zwarich opened this issue Jul 17, 2014 · 4 comments
Labels
A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@zwarich
Copy link

zwarich commented Jul 17, 2014

I distilled this from an example in some actual code I have, although I'm not entirely sure that the distilled example reflects the problem in my original code. The program

struct A;

struct B<'a> {
    a: &'a A,
    b: &'a A,
}

impl <'a> B<'a> {
    fn foo<'b>(&'b self) {
        B {
            a: self.a,
            b: self.b,
        };
    }
}

fn main() { }

fails to compile with this error:

test.rs:10:9: 13:10 error: cannot infer an appropriate lifetime for lifetime parameter `'a due to conflicting requirements
test.rs:10         B {
test.rs:11             a: self.a,
test.rs:12             b: self.b,
test.rs:13         };
test.rs:12:16: 12:22 note: first, the lifetime must be contained by the expression at 12:15...
test.rs:12             b: self.b,
                          ^~~~~~
test.rs:12:16: 12:22 note: ...so that automatically reference is valid at the time of borrow
test.rs:12             b: self.b,
                          ^~~~~~
test.rs:11:16: 11:22 note: but, the lifetime must also be contained by the expression at 11:15...
test.rs:11             a: self.a,
                          ^~~~~~
test.rs:11:16: 11:22 note: ...so that automatically reference is valid at the time of borrow
test.rs:11             a: self.a,
                          ^~~~~~

If I change the program slightly so that foo returns a B<'b> then everything works fine:

struct A;

struct B<'a> {
    a: &'a A,
    b: &'a A,
}

impl <'a> B<'a> {
    fn foo<'b>(&'b self) -> B<'b> {
        B {
            a: self.a,
            b: self.b,
        }
    }
}

fn main() { }
@zwarich
Copy link
Author

zwarich commented Jul 17, 2014

@nikomatsakis @pnkfelix Any clue what's going on here?

@nikomatsakis
Copy link
Contributor

I'm not sure, I have to investigate.

@Ryman
Copy link
Contributor

Ryman commented Feb 27, 2015

This is now fixed: http://is.gd/DjKFae

@huonw huonw added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Jan 8, 2016
dlrobertson added a commit to dlrobertson/rust that referenced this issue Feb 10, 2016
bors added a commit that referenced this issue Feb 11, 2016
After some digging I couldn't find a test for #15735, so I added a simplified version to `run-pass` and tested it against 80a3f45 to ensure it fails.
@jooert
Copy link
Contributor

jooert commented Apr 24, 2016

Triage: A regression test has been added in #31527; can be closed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

No branches or pull requests

6 participants