Skip to content

Commit 210f0dc

Browse files
committed
auto merge of #21162 : apasel422/rust/issue-16530, r=huonw
This fixes #16530 by hashing nullary structs [the same way as the empty tuple] (https://github.com/rust-lang/rust/blob/master/src/libcore/hash/mod.rs#L185). Other approaches are possible, but this was the simplest.
2 parents ee2bfae + 716effa commit 210f0dc

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 call_hash = |&: span, thing_expr| {
6969
let hash_path = {
@@ -96,16 +96,12 @@ fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure)
9696

9797
fs
9898
}
99-
_ => cx.span_bug(trait_span, "impossible substructure in `deriving(Hash)`")
99+
_ => cx.span_bug(trait_span, "impossible substructure in `derive(Hash)`")
100100
};
101101

102102
for &FieldInfo { ref self_, span, .. } in fields.iter() {
103103
stmts.push(call_hash(span, self_.clone()));
104104
}
105105

106-
if stmts.len() == 0 {
107-
cx.span_bug(trait_span, "#[derive(Hash)] needs at least one field");
108-
}
109-
110106
cx.expr_block(cx.block(trait_span, stmts, None))
111107
}

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)