From 716effa34900781ddb201fbd50ee679a4964cb25 Mon Sep 17 00:00:00 2001 From: Andrew Paseltiner Date: Wed, 14 Jan 2015 18:56:17 -0500 Subject: [PATCH] support deriving `Hash` for nullary structs fixes #16530 --- src/libsyntax/ext/deriving/hash.rs | 8 ++------ src/test/run-pass/issue-16530.rs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 src/test/run-pass/issue-16530.rs diff --git a/src/libsyntax/ext/deriving/hash.rs b/src/libsyntax/ext/deriving/hash.rs index db99c14244324..0c3ceb2b90bf3 100644 --- a/src/libsyntax/ext/deriving/hash.rs +++ b/src/libsyntax/ext/deriving/hash.rs @@ -63,7 +63,7 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt, fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P { let state_expr = match substr.nonself_args { [ref state_expr] => state_expr, - _ => cx.span_bug(trait_span, "incorrect number of arguments in `deriving(Hash)`") + _ => cx.span_bug(trait_span, "incorrect number of arguments in `derive(Hash)`") }; let hash_ident = substr.method_ident; let call_hash = |&: span, thing_expr| { @@ -86,16 +86,12 @@ fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) fs } - _ => cx.span_bug(trait_span, "impossible substructure in `deriving(Hash)`") + _ => cx.span_bug(trait_span, "impossible substructure in `derive(Hash)`") }; for &FieldInfo { ref self_, span, .. } in fields.iter() { stmts.push(call_hash(span, self_.clone())); } - if stmts.len() == 0 { - cx.span_bug(trait_span, "#[derive(Hash)] needs at least one field"); - } - cx.expr_block(cx.block(trait_span, stmts, None)) } diff --git a/src/test/run-pass/issue-16530.rs b/src/test/run-pass/issue-16530.rs new file mode 100644 index 0000000000000..7e3b796235d26 --- /dev/null +++ b/src/test/run-pass/issue-16530.rs @@ -0,0 +1,18 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::hash::{SipHasher, hash}; + +#[derive(Hash)] +struct Empty; + +pub fn main() { + assert!(hash::<_, SipHasher>(&Empty) == hash::<_, SipHasher>(&Empty)); +}