Skip to content

Commit 716effa

Browse files
committed
support deriving Hash for nullary structs
fixes #16530
1 parent 1c78ad9 commit 716effa

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/libsyntax/ext/deriving/hash.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn expand_deriving_hash<F>(cx: &mut ExtCtxt,
6363
fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
6464
let state_expr = match substr.nonself_args {
6565
[ref state_expr] => state_expr,
66-
_ => cx.span_bug(trait_span, "incorrect number of arguments in `deriving(Hash)`")
66+
_ => cx.span_bug(trait_span, "incorrect number of arguments in `derive(Hash)`")
6767
};
6868
let hash_ident = substr.method_ident;
6969
let call_hash = |&: span, thing_expr| {
@@ -86,16 +86,12 @@ fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure)
8686

8787
fs
8888
}
89-
_ => cx.span_bug(trait_span, "impossible substructure in `deriving(Hash)`")
89+
_ => cx.span_bug(trait_span, "impossible substructure in `derive(Hash)`")
9090
};
9191

9292
for &FieldInfo { ref self_, span, .. } in fields.iter() {
9393
stmts.push(call_hash(span, self_.clone()));
9494
}
9595

96-
if stmts.len() == 0 {
97-
cx.span_bug(trait_span, "#[derive(Hash)] needs at least one field");
98-
}
99-
10096
cx.expr_block(cx.block(trait_span, stmts, None))
10197
}

src/test/run-pass/issue-16530.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::hash::{SipHasher, hash};
12+
13+
#[derive(Hash)]
14+
struct Empty;
15+
16+
pub fn main() {
17+
assert!(hash::<_, SipHasher>(&Empty) == hash::<_, SipHasher>(&Empty));
18+
}

0 commit comments

Comments
 (0)