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

structural_match bug: deep refs like & &B leak use of PartialEq #62307

Closed
pnkfelix opened this issue Jul 2, 2019 · 3 comments · Fixed by #62339
Closed

structural_match bug: deep refs like & &B leak use of PartialEq #62307

pnkfelix opened this issue Jul 2, 2019 · 3 comments · Fixed by #62339
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Jul 2, 2019

(Spawned off of investigation of #61188)

rust-lang/rfcs#1445 says that we're supposed to reject uses of consts with ADT's that don't implement #[structural_match] (i.e. ADT's that do not do #[derive(PartialEq, Eq)].

Unfortunately, we did not quite finish the job. Consider the following code (play):

#[derive(Debug)]
struct B(i32);

// Overriding PartialEq to use this strange notion of equality exposes
// whether `match` is using structural-equality or method-dispatch
// under the hood, which is the antithesis of rust-lang/rfcs#1445
impl PartialEq for B {
    fn eq(&self, other: &B) -> bool { std::cmp::min(self.0, other.0) == 0 }
}

const ZER_B: & & B = & & B(0);
const ONE_B: & & B = & & B(1);

fn main() {
    if let ONE_B = ZER_B {
        println!("CLAIM Z: {:?} matches {:?}", ONE_B, ZER_B);
    }

    if let ONE_B = ONE_B {
        println!("CLAIM O: {:?} matches {:?}", ONE_B, ONE_B);
    }
}

This prints:

CLAIM Z: B(1) matches B(0)

but I would expect to see something like the error you get if you use constants of type &B instead of & &B:

error: to use a constant of type `B` in a pattern, `B` must be annotated with `#[derive(PartialEq, Eq)]`
  --> src/main.rs:15:12
   |
15 |     if let ONE_B = ZER_B {
   |            ^^^^^
@Centril Centril added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 2, 2019
@Centril
Copy link
Contributor

Centril commented Jul 2, 2019

@pnkfelix
Copy link
Member Author

pnkfelix commented Jul 3, 2019

I have a patch for this in progress.

@pnkfelix
Copy link
Member Author

pnkfelix commented Jul 3, 2019

(the change suggested in PR #62339 fixes this and has a regression test for it.)

bors added a commit that referenced this issue Jul 10, 2019
…l-match-check, r=nikomatsakis

use visitor for #[structural_match] check

This changes the code so that we recur down the structure of a type of a const (rather than just inspecting at a shallow one or two levels) when we are looking to see if it has an ADT that did not derive `PartialEq` and `Eq`.

Fix #61188

Fix #62307

Cc #62336
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants