Skip to content

deriving(Hash) on enum generates method call on generic integer (1.hash()) #18573

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
klutzy opened this issue Nov 3, 2014 · 3 comments
Closed

Comments

@klutzy
Copy link
Contributor

klutzy commented Nov 3, 2014

#[deriving(Hash)]
enum E {
    A = 1,
    B,
}
error: unable to infer enough type information to locate the impl of the trait `collections::hash::Hash` for the type `_`; type annotations required
#[deriving(Hash)]
           ^~~~

rustc --pretty=expanded output:

#![crate_type = "lib"]
#![feature(phase)]
#![no_std]
#![feature(globs)]
#[phase(plugin, link)]
extern crate "std" as std;
#[prelude_import]
use std::prelude::*;

enum E { A = 1, B, }
#[automatically_derived]
impl ::std::hash::Hash for E {
    #[inline]
    fn hash(&self, __arg_0: &mut ::std::hash::sip::SipState) {
        match (&*self,) {
            (&A,) => { 1.hash(__arg_0); }
            (&B,) => { 1u.hash(__arg_0); }
        }
    }
}

For A = 1, rustc generates 1.hash(__arg_0) but it doesn't compile because 1 is generic integer.

Moreoever, for B rustc generates 1u.hash(__arg_0) but it should be 2u.

@steveklabnik
Copy link
Member

This compiles fine today.

@klutzy
Copy link
Contributor Author

klutzy commented Jan 28, 2015

This compiles because 1 is now 1i32. It still hashes 1 for A and 1us for B, thus causes collision on 32-bit machine:

use std::hash::{hash, SipHasher};

#[derive(Hash)]
enum E {
    A = 1,
    B,
}

fn main() {
    let a = hash::<_,   SipHasher>(&E::A);
    let b = hash::<_,   SipHasher>(&E::B);

    println!("{}, {}, {}", a, b, a == b);
}
$ rustc a.rs --target=i686-unknown-linux-gnu && ./a
13715208377448023093, 13715208377448023093, true

@klutzy
Copy link
Contributor Author

klutzy commented Jan 28, 2015

(Opened #21714 for the collision issue)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants