Skip to content

Commit deee0f7

Browse files
committed
Auto merge of #32252 - durka:derive-21714, r=alexcrichton
derive: use discriminant_value in #[derive(Hash)] derive: use discriminant_value in #[derive(Hash)] Fixes #21714. Spawned from #32139. r? @alexcrichton
2 parents abb3a10 + 1e67d8a commit deee0f7

File tree

6 files changed

+62
-37
lines changed

6 files changed

+62
-37
lines changed

mk/crates.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ DEPS_rustdoc := rustc rustc_driver native:hoedown serialize getopts \
123123
test rustc_lint rustc_front
124124

125125

126-
TOOL_DEPS_compiletest := test getopts
126+
TOOL_DEPS_compiletest := test getopts log
127127
TOOL_DEPS_rustdoc := rustdoc
128128
TOOL_DEPS_rustc := rustc_driver
129129
TOOL_DEPS_rustbook := std rustdoc

mk/main.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ endif
493493
LD_LIBRARY_PATH_ENV_HOSTDIR$(1)_T_$(2)_H_$(3) := \
494494
$$(CURDIR)/$$(HLIB$(1)_H_$(3)):$$(CFG_LLVM_INST_DIR_$(3))/lib
495495
LD_LIBRARY_PATH_ENV_TARGETDIR$(1)_T_$(2)_H_$(3) := \
496-
$$(CURDIR)/$$(TLIB1_T_$(2)_H_$(CFG_BUILD))
496+
$$(CURDIR)/$$(TLIB$(1)_T_$(2)_H_$(CFG_BUILD))
497497

498498
HOST_RPATH_VAR$(1)_T_$(2)_H_$(3) := \
499499
$$(LD_LIBRARY_PATH_ENV_NAME$(1)_T_$(2)_H_$(3))=$$(LD_LIBRARY_PATH_ENV_HOSTDIR$(1)_T_$(2)_H_$(3)):$$$$$$(LD_LIBRARY_PATH_ENV_NAME$(1)_T_$(2)_H_$(3))

src/libsyntax_ext/deriving/generic/mod.rs

+11-27
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ use syntax::ptr::P;
209209

210210
use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self_, Ty};
211211

212+
use deriving;
213+
212214
pub mod ty;
213215

214216
pub struct TraitDef<'a> {
@@ -381,22 +383,6 @@ fn find_type_parameters(ty: &ast::Ty, ty_param_names: &[ast::Name]) -> Vec<P<ast
381383
visitor.types
382384
}
383385

384-
/// Replacement for expr_unreachable which generates intrinsics::unreachable()
385-
/// instead of unreachable!()
386-
fn expr_unreachable_intrinsic(cx: &ExtCtxt, sp: Span) -> P<Expr> {
387-
let path = cx.std_path(&["intrinsics", "unreachable"]);
388-
let call = cx.expr_call_global(
389-
sp, path, vec![]);
390-
let unreachable = cx.expr_block(P(ast::Block {
391-
stmts: vec![],
392-
expr: Some(call),
393-
id: ast::DUMMY_NODE_ID,
394-
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
395-
span: sp }));
396-
397-
unreachable
398-
}
399-
400386
impl<'a> TraitDef<'a> {
401387
pub fn expand(&self,
402388
cx: &mut ExtCtxt,
@@ -1279,15 +1265,11 @@ impl<'a> MethodDef<'a> {
12791265

12801266
let mut first_ident = None;
12811267
for (&ident, self_arg) in vi_idents.iter().zip(&self_args) {
1282-
let path = cx.std_path(&["intrinsics", "discriminant_value"]);
1283-
let call = cx.expr_call_global(
1284-
sp, path, vec![cx.expr_addr_of(sp, self_arg.clone())]);
1285-
let variant_value = cx.expr_block(P(ast::Block {
1286-
stmts: vec![],
1287-
expr: Some(call),
1288-
id: ast::DUMMY_NODE_ID,
1289-
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
1290-
span: sp }));
1268+
let self_addr = cx.expr_addr_of(sp, self_arg.clone());
1269+
let variant_value = deriving::call_intrinsic(cx,
1270+
sp,
1271+
"discriminant_value",
1272+
vec![self_addr]);
12911273

12921274
let target_ty = cx.ty_ident(sp, cx.ident_of(target_type_name));
12931275
let variant_disr = cx.expr_cast(sp, variant_value, target_ty);
@@ -1315,7 +1297,9 @@ impl<'a> MethodDef<'a> {
13151297
//Since we know that all the arguments will match if we reach the match expression we
13161298
//add the unreachable intrinsics as the result of the catch all which should help llvm
13171299
//in optimizing it
1318-
match_arms.push(cx.arm(sp, vec![cx.pat_wild(sp)], expr_unreachable_intrinsic(cx, sp)));
1300+
match_arms.push(cx.arm(sp,
1301+
vec![cx.pat_wild(sp)],
1302+
deriving::call_intrinsic(cx, sp, "unreachable", vec![])));
13191303

13201304
// Final wrinkle: the self_args are expressions that deref
13211305
// down to desired l-values, but we cannot actually deref
@@ -1391,7 +1375,7 @@ impl<'a> MethodDef<'a> {
13911375
// derive Debug on such a type could here generate code
13921376
// that needs the feature gate enabled.)
13931377

1394-
expr_unreachable_intrinsic(cx, sp)
1378+
deriving::call_intrinsic(cx, sp, "unreachable", vec![])
13951379
}
13961380
else {
13971381

src/libsyntax_ext/deriving/hash.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,13 @@ 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 variant_value = deriving::call_intrinsic(cx,
86+
trait_span,
87+
"discriminant_value",
88+
vec![cx.expr_self(trait_span)]);
9189

92-
stmts.push(call_hash(trait_span, discriminant));
90+
stmts.push(call_hash(trait_span, variant_value));
9391

9492
fs
9593
}

src/libsyntax_ext/deriving/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use syntax::ext::build::AstBuilder;
1818
use syntax::feature_gate;
1919
use syntax::codemap::Span;
2020
use syntax::parse::token::{intern, intern_and_get_ident};
21+
use syntax::ptr::P;
2122

2223
macro_rules! pathvec {
2324
($($x:ident)::+) => (
@@ -271,3 +272,19 @@ fn hygienic_type_parameter(item: &Annotatable, base: &str) -> String {
271272
typaram
272273
}
273274

275+
/// Constructs an expression that calls an intrinsic
276+
fn call_intrinsic(cx: &ExtCtxt,
277+
span: Span,
278+
intrinsic: &str,
279+
args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
280+
let path = cx.std_path(&["intrinsics", intrinsic]);
281+
let call = cx.expr_call_global(span, path, args);
282+
283+
cx.expr_block(P(ast::Block {
284+
stmts: vec![],
285+
expr: Some(call),
286+
id: ast::DUMMY_NODE_ID,
287+
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
288+
span: span }))
289+
}
290+

src/test/run-pass/deriving-hash.rs

+26
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![feature(hash_default)]
1313

1414
use std::hash::{Hash, SipHasher, Hasher};
15+
use std::mem::size_of;
1516

1617
#[derive(Hash)]
1718
struct Person {
@@ -24,12 +25,30 @@ struct Person {
2425
#[derive(Hash)] struct __H__H;
2526
#[derive(Hash)] enum Collision<__H> { __H { __H__H: __H } }
2627

28+
#[derive(Hash)]
29+
enum E { A=1, B }
30+
2731
fn hash<T: Hash>(t: &T) -> u64 {
2832
let mut s = SipHasher::new_with_keys(0, 0);
2933
t.hash(&mut s);
3034
s.finish()
3135
}
3236

37+
struct FakeHasher<'a>(&'a mut Vec<u8>);
38+
impl<'a> Hasher for FakeHasher<'a> {
39+
fn finish(&self) -> u64 {
40+
unimplemented!()
41+
}
42+
43+
fn write(&mut self, bytes: &[u8]) {
44+
self.0.extend(bytes);
45+
}
46+
}
47+
48+
fn fake_hash(v: &mut Vec<u8>, e: E) {
49+
e.hash(&mut FakeHasher(v));
50+
}
51+
3352
fn main() {
3453
let person1 = Person {
3554
id: 5,
@@ -43,4 +62,11 @@ fn main() {
4362
};
4463
assert_eq!(hash(&person1), hash(&person1));
4564
assert!(hash(&person1) != hash(&person2));
65+
66+
// test #21714
67+
let mut va = vec![];
68+
let mut vb = vec![];
69+
fake_hash(&mut va, E::A);
70+
fake_hash(&mut vb, E::B);
71+
assert!(va != vb);
4672
}

0 commit comments

Comments
 (0)