Skip to content

Check for "constants we match on must be PartialEq" misses lifetime-dependent impls #121007

Description

@RalfJung

The following should trigger a warning that we can only match on PartialEq consts, but it doesn't: (example by @lcnr, then slightly tweaked)

use std::marker::PhantomData;

struct Inv<'a>(PhantomData<*mut &'a ()>);

// This type is only sometimes `PartialEq`.
impl PartialEq for Inv<'static> {
    fn eq(&self, _: &Inv<'static>) -> bool {
        true
    }
}

impl<'a> Inv<'a> {
    // The value `None` makes this have structural equality for any type `Self`.
    const NOT_STATIC: Option<Self> = None;
}

fn foo<'a>(x: Option<Inv<'a>>) {
    match x {
        Inv::<'a>::NOT_STATIC => (),
        Some(_) => panic!()
    }
    
    // Enabling the next line confirms that the type does
    // indeed not implement `PartialEq`.
    //x == Inv::<'a>::NOT_STATIC;
}

fn main() {
    foo(None)
}

Here is another example of the same issue.

The problem is that we call predicate_must_hold_modulo_regions here, but only borrowck knows the actual lifetimes and borrowck has no idea that this trait obligation even exists.

We should leave some sort of trace in MIR that there is a PartialEq obligations to ensure borrowck can check this with the right lifetimes.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-patternsRelating to patterns and pattern matchingA-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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