Skip to content

False type-mismatch for derive(Hash) for structs with fields called "state" #14860

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

Closed
valyagolev opened this issue May 20, 2023 · 4 comments
Closed
Labels
A-macro macro expansion C-bug Category: bug

Comments

@valyagolev
Copy link

valyagolev commented May 20, 2023

As minimal reproduction as it gets: cargo new --lib repro, then:

#[derive(Hash)]
pub enum BgGameState {
    Over, // you can comment it out as well
}

#[derive(Hash)]
pub struct BgGame {
    pub state: BgGameState, // rename it, it will go away
}
repro

rust-analyzer version: 0.4.1521-standalone, also some earlier versions

rustc version: tried with rustc 1.69.0 (84c898d65 2023-04-16), rustc 1.71.0-nightly (521f4dae1 2023-05-19), also some earlier versions

relevant settings: It's VS Code remote on my box, via SSH, but I've been running it without issues for many months

@valyagolev valyagolev added the C-bug Category: bug label May 20, 2023
@valyagolev valyagolev changed the title False type-mismatch for derive(Hash) ... probably doing something wrong False type-mismatch for derive(Hash) for structs with fields called "state" May 20, 2023
@valyagolev
Copy link
Author

valyagolev commented May 20, 2023

Ohhh, I figured it out. If I rename "state", it goes away :-)

(I guess nobody ever tried to use hashing for anything with "state"... makes sense)

@HKalbasi
Copy link
Member

Yes this happens since state is used in the macro expansion code:

impl core::hash::Hash for BgGame {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        core::mem::discriminant(self).hash(state);
        match self {
            BgGame { state: state } => {
                               // ^^^ state shadows the state
                state.hash(state);
            }
        }
    }
}

@HKalbasi HKalbasi added the A-macro macro expansion label May 20, 2023
@lowr
Copy link
Contributor

lowr commented May 21, 2023

This should be resolved when we implement macro hygiene. Closing as a duplicate of #11681.

@lowr lowr closed this as not planned Won't fix, can't repro, duplicate, stale May 21, 2023
@flodiebold
Copy link
Member

Maybe we could use a name that's less likely to collide until then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macro macro expansion C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

4 participants