Skip to content

Commit c480b6a

Browse files
committed
fix #21714 by using discriminant_value in #[derive(Hash)]
This is the same approach taken in #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 01c0723 commit c480b6a

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
@@ -12,7 +12,7 @@ use deriving;
1212
use deriving::generic::*;
1313
use deriving::generic::ty::*;
1414

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

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

92-
stmts.push(call_hash(trait_span, discriminant));
95+
stmts.push(call_hash(trait_span, variant_value));
9396

9497
fs
9598
}

0 commit comments

Comments
 (0)