Skip to content

Commit

Permalink
fix rust-lang#21714 by using discriminant_value in #[derive(Hash)]
Browse files Browse the repository at this point in the history
This is the same approach taken in rust-lang#24270, except that this
should not be a breaking change because it only changes the output
of hash functions, which nobody should be relying on.
  • Loading branch information
durka committed Mar 10, 2016
1 parent a5090f7 commit 7c33793
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/libsyntax_ext/deriving/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use deriving::generic::*;
use deriving::generic::ty::*;

use syntax::ast::{MetaItem, Expr, Mutability};
use syntax::ast::{self, MetaItem, Expr, Mutability};
use syntax::codemap::Span;
use syntax::ext::base::{ExtCtxt, Annotatable};
use syntax::ext::build::AstBuilder;
Expand Down Expand Up @@ -77,15 +77,18 @@ fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure)

let fields = match *substr.fields {
Struct(_, ref fs) => fs,
EnumMatching(index, variant, ref fs) => {
// Determine the discriminant. We will feed this value to the byte
// iteration function.
let discriminant = match variant.node.disr_expr {
Some(ref d) => d.clone(),
None => cx.expr_usize(trait_span, index)
};
EnumMatching(_, _, ref fs) => {
let path = cx.std_path(&["intrinsics", "discriminant_value"]);
let call = cx.expr_call_global(
trait_span, path, vec![cx.expr_self(trait_span)]);
let variant_value = cx.expr_block(P(ast::Block {
stmts: vec![],
expr: Some(call),
id: ast::DUMMY_NODE_ID,
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
span: trait_span }));

stmts.push(call_hash(trait_span, discriminant));
stmts.push(call_hash(trait_span, variant_value));

fs
}
Expand Down

0 comments on commit 7c33793

Please sign in to comment.