Skip to content

Unexpected lifetime issue when comparing Option<&i32> #42966

Closed
@CasualX

Description

@CasualX

I tried this code:

fn check(a: Option<&i32>, b: &i32) -> bool {
    a == Some(b)
}

I expected to see this happen: Code compiles and works as expected.

Switching the equality around like this compiles successfully and works as expected:

fn check(a: Option<&i32>, b: &i32) -> bool {
    Some(b) == a
}

Alternatively explicitly forcing both references to a shared lifetime works too:

fn check<'c>(a: Option<&'c i32>, b: &'c i32) -> bool {
    a == Some(b)
}

Instead, this happened: error[E0495]: cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements

Meta

https://play.rust-lang.org/?gist=9bd7c205c280df1ea37bb49cd3ecca25&version=stable&backtrace=0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions