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 on binary operators #18892

Closed
klutzy opened this issue Nov 12, 2014 · 3 comments
Closed

Lifetime error on binary operators #18892

klutzy opened this issue Nov 12, 2014 · 3 comments
Labels
A-lifetimes Area: Lifetimes / regions

Comments

@klutzy
Copy link
Contributor

klutzy commented Nov 12, 2014

pub struct S<'a>(&'a int);

impl<'a> PartialEq for S<'a> {
    fn eq(&self, b: &S<'a>) -> bool {
        true
    }
}

fn main() {
    let i = 1;
    let a = S(&i);

    {
        let j = 0;
        let b = S(&j);

        (a == b);
    }
}
a.rs:15:20: 15:21 error: `j` does not live long enough
a.rs:15         let b = S(&j);
                           ^
a.rs:9:11: 19:2 note: reference must be valid for the block at 9:10...
a.rs:9 fn main() {
a.rs:10     let i = 1;
a.rs:11     let a = S(&i);
a.rs:12
a.rs:13     {
a.rs:14         let j = 0;
        ...
a.rs:13:5: 18:6 note: ...but borrowed value is only valid for the block at 13:4
a.rs:13     {
a.rs:14         let j = 0;
a.rs:15         let b = S(&j);
a.rs:16
a.rs:17         (a == b);
a.rs:18     }

a == b causes error but b == a or a.eq(&b) don't. Same for a < b.

@japaric
Copy link
Member

japaric commented Nov 12, 2014

cc @nikomatsakis

@nikomatsakis
Copy link
Contributor

As discussed on IRC, I think this is basically a bug in the operator dispatch code, but one whose symptoms could be alleviated in a number of ways:

  1. The proposed reform the == operator so that it takes two type parameters would help.
  2. Variance (infer or permit declarations of variance for type parameters #3598) would help.
  3. Introducing another type variable to allow for the lifetimes to be inexact would help, and would presumably also eliminate the need for the "reborrow" that we do.

I'm not sure which of these three is best.

@klutzy
Copy link
Contributor Author

klutzy commented Apr 17, 2015

Solution 1 has been implemented:

impl<'a, 'b> PartialEq<S<'b>> for S<'a> {
    fn eq(&self, b: &S<'b>) -> bool {
        true
    }
}

Fixed!

@klutzy klutzy closed this as completed Apr 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions
Projects
None yet
Development

No branches or pull requests

4 participants