Skip to content

Lifetime error on binary operators #18892

Closed
@klutzy

Description

@klutzy
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions