Skip to content

Commit 7c33793

Browse files
committed
fix rust-lang#21714 by using discriminant_value in #[derive(Hash)]
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.
1 parent a5090f7 commit 7c33793

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/libsyntax_ext/deriving/hash.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use deriving::generic::*;
1212
use deriving::generic::ty::*;
1313

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

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

88-
stmts.push(call_hash(trait_span, discriminant));
91+
stmts.push(call_hash(trait_span, variant_value));
8992

9093
fs
9194
}

0 commit comments

Comments
 (0)