-
Notifications
You must be signed in to change notification settings - Fork 13k
/
Copy pathsvh_visitor.rs
1111 lines (996 loc) · 37.2 KB
/
svh_visitor.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2012-2014 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use self::SawExprComponent::*;
use self::SawAbiComponent::*;
use self::SawItemComponent::*;
use self::SawPatComponent::*;
use self::SawTyComponent::*;
use self::SawTraitOrImplItemComponent::*;
use syntax::abi::Abi;
use syntax::ast::{self, Name, NodeId};
use syntax::attr;
use syntax::ext::hygiene::SyntaxContext;
use syntax::parse::token;
use syntax::symbol::InternedString;
use syntax_pos::{Span, BytePos};
use syntax::tokenstream;
use rustc::hir;
use rustc::hir::*;
use rustc::hir::def::Def;
use rustc::hir::def_id::DefId;
use rustc::hir::intravisit::{self as visit, Visitor};
use rustc::ich::{DefPathHashes, CachingCodemapView, IGNORED_ATTRIBUTES};
use rustc::ty::TyCtxt;
use std::hash::{Hash, Hasher};
use super::IchHasher;
pub struct StrictVersionHashVisitor<'a, 'hash: 'a, 'tcx: 'hash> {
pub tcx: TyCtxt<'hash, 'tcx, 'tcx>,
pub st: &'a mut IchHasher,
// collect a deterministic hash of def-ids that we have seen
def_path_hashes: &'a mut DefPathHashes<'hash, 'tcx>,
hash_spans: bool,
codemap: &'a mut CachingCodemapView<'tcx>,
overflow_checks_enabled: bool,
hash_bodies: bool,
}
impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
pub fn new(st: &'a mut IchHasher,
tcx: TyCtxt<'hash, 'tcx, 'tcx>,
def_path_hashes: &'a mut DefPathHashes<'hash, 'tcx>,
codemap: &'a mut CachingCodemapView<'tcx>,
hash_spans: bool,
hash_bodies: bool)
-> Self {
let check_overflow = tcx.sess.overflow_checks();
StrictVersionHashVisitor {
st: st,
tcx: tcx,
def_path_hashes: def_path_hashes,
hash_spans: hash_spans,
codemap: codemap,
overflow_checks_enabled: check_overflow,
hash_bodies: hash_bodies,
}
}
fn compute_def_id_hash(&mut self, def_id: DefId) -> u64 {
self.def_path_hashes.hash(def_id)
}
// Hash a span in a stable way. We can't directly hash the span's BytePos
// fields (that would be similar to hashing pointers, since those are just
// offsets into the CodeMap). Instead, we hash the (file name, line, column)
// triple, which stays the same even if the containing FileMap has moved
// within the CodeMap.
// Also note that we are hashing byte offsets for the column, not unicode
// codepoint offsets. For the purpose of the hash that's sufficient.
// Also, hashing filenames is expensive so we avoid doing it twice when the
// span starts and ends in the same file, which is almost always the case.
fn hash_span(&mut self, span: Span) {
debug!("hash_span: st={:?}", self.st);
// If this is not an empty or invalid span, we want to hash the last
// position that belongs to it, as opposed to hashing the first
// position past it.
let span_hi = if span.hi > span.lo {
// We might end up in the middle of a multibyte character here,
// but that's OK, since we are not trying to decode anything at
// this position.
span.hi - BytePos(1)
} else {
span.hi
};
let expn_kind = if span.ctxt == SyntaxContext::empty() {
SawSpanExpnKind::NoExpansion
} else {
SawSpanExpnKind::SomeExpansion
};
let loc1 = self.codemap.byte_pos_to_line_and_col(span.lo);
let loc1 = loc1.as_ref()
.map(|&(ref fm, line, col)| (&fm.name[..], line, col))
.unwrap_or(("???", 0, BytePos(0)));
let loc2 = self.codemap.byte_pos_to_line_and_col(span_hi);
let loc2 = loc2.as_ref()
.map(|&(ref fm, line, col)| (&fm.name[..], line, col))
.unwrap_or(("???", 0, BytePos(0)));
let saw = if loc1.0 == loc2.0 {
SawSpan(loc1.0,
loc1.1, loc1.2,
loc2.1, loc2.2,
expn_kind)
} else {
SawSpanTwoFiles(loc1.0, loc1.1, loc1.2,
loc2.0, loc2.1, loc2.2,
expn_kind)
};
saw.hash(self.st);
if expn_kind == SawSpanExpnKind::SomeExpansion {
self.hash_span(span.source_callsite());
}
}
fn hash_discriminant<T>(&mut self, v: &T) {
unsafe {
let disr = ::std::intrinsics::discriminant_value(v);
debug!("hash_discriminant: disr={}, st={:?}", disr, self.st);
disr.hash(self.st);
}
}
}
// To off-load the bulk of the hash-computation on #[derive(Hash)],
// we define a set of enums corresponding to the content that our
// crate visitor will encounter as it traverses the ast.
//
// The important invariant is that all of the Saw*Component enums
// do not carry any Spans, Names, or Idents.
//
// Not carrying any Names/Idents is the important fix for problem
// noted on PR #13948: using the ident.name as the basis for a
// hash leads to unstable SVH, because ident.name is just an index
// into intern table (i.e. essentially a random address), not
// computed from the name content.
//
// With the below enums, the SVH computation is not sensitive to
// artifacts of how rustc was invoked nor of how the source code
// was laid out. (Or at least it is *less* sensitive.)
// This enum represents the different potential bits of code the
// visitor could encounter that could affect the ABI for the crate,
// and assigns each a distinct tag to feed into the hash computation.
#[derive(Hash)]
enum SawAbiComponent<'a> {
// FIXME (#14132): should we include (some function of)
// ident.ctxt as well?
SawIdent(InternedString),
SawStructDef(InternedString),
SawLifetime,
SawLifetimeDef(usize),
SawMod,
SawForeignItem(SawForeignItemComponent),
SawItem(SawItemComponent),
SawTy(SawTyComponent),
SawFnDecl(bool),
SawGenerics,
SawTraitItem(SawTraitOrImplItemComponent),
SawImplItem(SawTraitOrImplItemComponent),
SawStructField,
SawVariant(bool),
SawQPath,
SawPathSegment,
SawPathParameters,
SawBlock,
SawPat(SawPatComponent),
SawLocal,
SawArm,
SawExpr(SawExprComponent<'a>),
SawStmt,
SawVis,
SawAssociatedItemKind(hir::AssociatedItemKind),
SawDefaultness(hir::Defaultness),
SawWherePredicate,
SawTyParamBound,
SawPolyTraitRef,
SawAssocTypeBinding,
SawAttribute(ast::AttrStyle),
SawMacroDef,
SawSpan(&'a str,
usize, BytePos,
usize, BytePos,
SawSpanExpnKind),
SawSpanTwoFiles(&'a str, usize, BytePos,
&'a str, usize, BytePos,
SawSpanExpnKind),
}
/// SawExprComponent carries all of the information that we want
/// to include in the hash that *won't* be covered by the
/// subsequent recursive traversal of the expression's
/// substructure by the visitor.
///
/// We know every Expr_ variant is covered by a variant because
/// `fn saw_expr` maps each to some case below. Ensuring that
/// each variant carries an appropriate payload has to be verified
/// by hand.
///
/// (However, getting that *exactly* right is not so important
/// because the SVH is just a developer convenience; there is no
/// guarantee of collision-freedom, hash collisions are just
/// (hopefully) unlikely.)
///
/// The xxxComponent enums and saw_xxx functions for Item, Pat,
/// Ty, TraitItem and ImplItem follow the same methodology.
#[derive(Hash)]
enum SawExprComponent<'a> {
SawExprLoop(Option<InternedString>),
SawExprField(InternedString),
SawExprTupField(usize),
SawExprBreak(Option<InternedString>),
SawExprAgain(Option<InternedString>),
SawExprBox,
SawExprArray,
SawExprCall,
SawExprMethodCall,
SawExprTup,
SawExprBinary(hir::BinOp_),
SawExprUnary(hir::UnOp),
SawExprLit(ast::LitKind),
SawExprLitStr(InternedString, ast::StrStyle),
SawExprLitFloat(InternedString, Option<ast::FloatTy>),
SawExprCast,
SawExprType,
SawExprIf,
SawExprWhile,
SawExprMatch,
SawExprClosure(CaptureClause),
SawExprBlock,
SawExprAssign,
SawExprAssignOp(hir::BinOp_),
SawExprIndex,
SawExprPath,
SawExprAddrOf(hir::Mutability),
SawExprRet,
SawExprInlineAsm(StableInlineAsm<'a>),
SawExprStruct,
SawExprRepeat,
}
// The boolean returned indicates whether the span of this expression is always
// significant, regardless of debuginfo.
fn saw_expr<'a>(node: &'a Expr_,
overflow_checks_enabled: bool)
-> (SawExprComponent<'a>, bool) {
let binop_can_panic_at_runtime = |binop| {
match binop {
BiAdd |
BiSub |
BiMul => overflow_checks_enabled,
BiDiv |
BiRem => true,
BiAnd |
BiOr |
BiBitXor |
BiBitAnd |
BiBitOr |
BiShl |
BiShr |
BiEq |
BiLt |
BiLe |
BiNe |
BiGe |
BiGt => false
}
};
let unop_can_panic_at_runtime = |unop| {
match unop {
UnDeref |
UnNot => false,
UnNeg => overflow_checks_enabled,
}
};
match *node {
ExprBox(..) => (SawExprBox, false),
ExprArray(..) => (SawExprArray, false),
ExprCall(..) => (SawExprCall, false),
ExprMethodCall(..) => (SawExprMethodCall, false),
ExprTup(..) => (SawExprTup, false),
ExprBinary(op, ..) => {
(SawExprBinary(op.node), binop_can_panic_at_runtime(op.node))
}
ExprUnary(op, _) => {
(SawExprUnary(op), unop_can_panic_at_runtime(op))
}
ExprLit(ref lit) => (saw_lit(lit), false),
ExprCast(..) => (SawExprCast, false),
ExprType(..) => (SawExprType, false),
ExprIf(..) => (SawExprIf, false),
ExprWhile(..) => (SawExprWhile, false),
ExprLoop(_, id, _) => (SawExprLoop(id.map(|id| id.node.as_str())), false),
ExprMatch(..) => (SawExprMatch, false),
ExprClosure(cc, _, _, _) => (SawExprClosure(cc), false),
ExprBlock(..) => (SawExprBlock, false),
ExprAssign(..) => (SawExprAssign, false),
ExprAssignOp(op, ..) => {
(SawExprAssignOp(op.node), binop_can_panic_at_runtime(op.node))
}
ExprField(_, name) => (SawExprField(name.node.as_str()), false),
ExprTupField(_, id) => (SawExprTupField(id.node), false),
ExprIndex(..) => (SawExprIndex, true),
ExprPath(_) => (SawExprPath, false),
ExprAddrOf(m, _) => (SawExprAddrOf(m), false),
ExprBreak(label, _) => (SawExprBreak(label.ident.map(|i|
i.node.name.as_str())), false),
ExprAgain(label) => (SawExprAgain(label.ident.map(|i|
i.node.name.as_str())), false),
ExprRet(..) => (SawExprRet, false),
ExprInlineAsm(ref a,..) => (SawExprInlineAsm(StableInlineAsm(a)), false),
ExprStruct(..) => (SawExprStruct, false),
ExprRepeat(..) => (SawExprRepeat, false),
}
}
fn saw_lit(lit: &ast::Lit) -> SawExprComponent<'static> {
match lit.node {
ast::LitKind::Str(s, style) => SawExprLitStr(s.as_str(), style),
ast::LitKind::Float(s, ty) => SawExprLitFloat(s.as_str(), Some(ty)),
ast::LitKind::FloatUnsuffixed(s) => SawExprLitFloat(s.as_str(), None),
ref node @ _ => SawExprLit(node.clone()),
}
}
#[derive(Hash)]
enum SawItemComponent {
SawItemExternCrate,
SawItemUse(UseKind),
SawItemStatic(Mutability),
SawItemConst,
SawItemFn(Unsafety, Constness, Abi),
SawItemMod,
SawItemForeignMod(Abi),
SawItemTy,
SawItemEnum,
SawItemStruct,
SawItemUnion,
SawItemTrait(Unsafety),
SawItemDefaultImpl(Unsafety),
SawItemImpl(Unsafety, ImplPolarity)
}
fn saw_item(node: &Item_) -> SawItemComponent {
match *node {
ItemExternCrate(..) => SawItemExternCrate,
ItemUse(_, kind) => SawItemUse(kind),
ItemStatic(_, mutability, _) => SawItemStatic(mutability),
ItemConst(..) =>SawItemConst,
ItemFn(_, unsafety, constness, abi, _, _) => SawItemFn(unsafety, constness, abi),
ItemMod(..) => SawItemMod,
ItemForeignMod(ref fm) => SawItemForeignMod(fm.abi),
ItemTy(..) => SawItemTy,
ItemEnum(..) => SawItemEnum,
ItemStruct(..) => SawItemStruct,
ItemUnion(..) => SawItemUnion,
ItemTrait(unsafety, ..) => SawItemTrait(unsafety),
ItemDefaultImpl(unsafety, _) => SawItemDefaultImpl(unsafety),
ItemImpl(unsafety, implpolarity, ..) => SawItemImpl(unsafety, implpolarity)
}
}
#[derive(Hash)]
enum SawForeignItemComponent {
Static { mutable: bool },
Fn,
}
#[derive(Hash)]
enum SawPatComponent {
SawPatWild,
SawPatBinding(BindingMode),
SawPatStruct,
SawPatTupleStruct,
SawPatPath,
SawPatTuple,
SawPatBox,
SawPatRef(Mutability),
SawPatLit,
SawPatRange,
SawPatSlice
}
fn saw_pat(node: &PatKind) -> SawPatComponent {
match *node {
PatKind::Wild => SawPatWild,
PatKind::Binding(bindingmode, ..) => SawPatBinding(bindingmode),
PatKind::Struct(..) => SawPatStruct,
PatKind::TupleStruct(..) => SawPatTupleStruct,
PatKind::Path(_) => SawPatPath,
PatKind::Tuple(..) => SawPatTuple,
PatKind::Box(..) => SawPatBox,
PatKind::Ref(_, mutability) => SawPatRef(mutability),
PatKind::Lit(..) => SawPatLit,
PatKind::Range(..) => SawPatRange,
PatKind::Slice(..) => SawPatSlice
}
}
#[derive(Hash)]
enum SawTyComponent {
SawTySlice,
SawTyArray,
SawTyPtr(Mutability),
SawTyRptr(Mutability),
SawTyBareFn(Unsafety, Abi),
SawTyNever,
SawTyTup,
SawTyPath,
SawTyObjectSum,
SawTyImplTrait,
SawTyTypeof,
SawTyInfer
}
fn saw_ty(node: &Ty_) -> SawTyComponent {
match *node {
TySlice(..) => SawTySlice,
TyArray(..) => SawTyArray,
TyPtr(ref mty) => SawTyPtr(mty.mutbl),
TyRptr(_, ref mty) => SawTyRptr(mty.mutbl),
TyBareFn(ref barefnty) => SawTyBareFn(barefnty.unsafety, barefnty.abi),
TyNever => SawTyNever,
TyTup(..) => SawTyTup,
TyPath(_) => SawTyPath,
TyTraitObject(..) => SawTyObjectSum,
TyImplTrait(..) => SawTyImplTrait,
TyTypeof(..) => SawTyTypeof,
TyInfer => SawTyInfer
}
}
#[derive(Hash)]
enum SawTraitOrImplItemComponent {
SawTraitOrImplItemConst,
// The boolean signifies whether a body is present
SawTraitOrImplItemMethod(Unsafety, Constness, Abi, bool),
SawTraitOrImplItemType
}
fn saw_trait_item(ti: &TraitItemKind) -> SawTraitOrImplItemComponent {
match *ti {
TraitItemKind::Const(..) => SawTraitOrImplItemConst,
TraitItemKind::Method(ref sig, TraitMethod::Required(_)) =>
SawTraitOrImplItemMethod(sig.unsafety, sig.constness, sig.abi, false),
TraitItemKind::Method(ref sig, TraitMethod::Provided(_)) =>
SawTraitOrImplItemMethod(sig.unsafety, sig.constness, sig.abi, true),
TraitItemKind::Type(..) => SawTraitOrImplItemType
}
}
fn saw_impl_item(ii: &ImplItemKind) -> SawTraitOrImplItemComponent {
match *ii {
ImplItemKind::Const(..) => SawTraitOrImplItemConst,
ImplItemKind::Method(ref sig, _) =>
SawTraitOrImplItemMethod(sig.unsafety, sig.constness, sig.abi, true),
ImplItemKind::Type(..) => SawTraitOrImplItemType
}
}
#[derive(Clone, Copy, Hash, Eq, PartialEq)]
enum SawSpanExpnKind {
NoExpansion,
SomeExpansion,
}
/// A wrapper that provides a stable Hash implementation.
struct StableInlineAsm<'a>(&'a InlineAsm);
impl<'a> Hash for StableInlineAsm<'a> {
fn hash<H: Hasher>(&self, state: &mut H) {
let InlineAsm {
asm,
asm_str_style,
ref outputs,
ref inputs,
ref clobbers,
volatile,
alignstack,
dialect,
ctxt: _, // This is used for error reporting
} = *self.0;
asm.as_str().hash(state);
asm_str_style.hash(state);
outputs.len().hash(state);
for output in outputs {
let InlineAsmOutput { constraint, is_rw, is_indirect } = *output;
constraint.as_str().hash(state);
is_rw.hash(state);
is_indirect.hash(state);
}
inputs.len().hash(state);
for input in inputs {
input.as_str().hash(state);
}
clobbers.len().hash(state);
for clobber in clobbers {
clobber.as_str().hash(state);
}
volatile.hash(state);
alignstack.hash(state);
dialect.hash(state);
}
}
macro_rules! hash_attrs {
($visitor:expr, $attrs:expr) => ({
let attrs = $attrs;
if attrs.len() > 0 {
$visitor.hash_attributes(attrs);
}
})
}
macro_rules! hash_span {
($visitor:expr, $span:expr) => ({
hash_span!($visitor, $span, false)
});
($visitor:expr, $span:expr, $force:expr) => ({
if $force || $visitor.hash_spans {
$visitor.hash_span($span);
}
});
}
impl<'a, 'hash, 'tcx> Visitor<'tcx> for StrictVersionHashVisitor<'a, 'hash, 'tcx> {
fn nested_visit_map<'this>(&'this mut self) -> visit::NestedVisitorMap<'this, 'tcx> {
if self.hash_bodies {
visit::NestedVisitorMap::OnlyBodies(&self.tcx.hir)
} else {
visit::NestedVisitorMap::None
}
}
fn visit_variant_data(&mut self,
s: &'tcx VariantData,
name: Name,
_: &'tcx Generics,
_: NodeId,
span: Span) {
debug!("visit_variant_data: st={:?}", self.st);
SawStructDef(name.as_str()).hash(self.st);
hash_span!(self, span);
visit::walk_struct_def(self, s);
}
fn visit_variant(&mut self,
v: &'tcx Variant,
g: &'tcx Generics,
item_id: NodeId) {
debug!("visit_variant: st={:?}", self.st);
SawVariant(v.node.disr_expr.is_some()).hash(self.st);
hash_attrs!(self, &v.node.attrs);
visit::walk_variant(self, v, g, item_id)
}
fn visit_name(&mut self, span: Span, name: Name) {
debug!("visit_name: st={:?}", self.st);
SawIdent(name.as_str()).hash(self.st);
hash_span!(self, span);
}
fn visit_lifetime(&mut self, l: &'tcx Lifetime) {
debug!("visit_lifetime: st={:?}", self.st);
SawLifetime.hash(self.st);
visit::walk_lifetime(self, l);
}
fn visit_lifetime_def(&mut self, l: &'tcx LifetimeDef) {
debug!("visit_lifetime_def: st={:?}", self.st);
SawLifetimeDef(l.bounds.len()).hash(self.st);
visit::walk_lifetime_def(self, l);
}
fn visit_expr(&mut self, ex: &'tcx Expr) {
debug!("visit_expr: st={:?}", self.st);
let (saw_expr, force_span) = saw_expr(&ex.node,
self.overflow_checks_enabled);
SawExpr(saw_expr).hash(self.st);
// No need to explicitly hash the discriminant here, since we are
// implicitly hashing the discriminant of SawExprComponent.
hash_span!(self, ex.span, force_span);
hash_attrs!(self, &ex.attrs);
// Always hash nested constant bodies (e.g. n in `[x; n]`).
let hash_bodies = self.hash_bodies;
self.hash_bodies = true;
visit::walk_expr(self, ex);
self.hash_bodies = hash_bodies;
}
fn visit_stmt(&mut self, s: &'tcx Stmt) {
debug!("visit_stmt: st={:?}", self.st);
// We don't want to modify the hash for decls, because
// they might be item decls (if they are local decls,
// we'll hash that fact in visit_local); but we do want to
// remember if this was a StmtExpr or StmtSemi (the later
// had an explicit semi-colon; this affects the typing
// rules).
match s.node {
StmtDecl(..) => (),
StmtExpr(..) => {
SawStmt.hash(self.st);
self.hash_discriminant(&s.node);
hash_span!(self, s.span);
}
StmtSemi(..) => {
SawStmt.hash(self.st);
self.hash_discriminant(&s.node);
hash_span!(self, s.span);
}
}
visit::walk_stmt(self, s)
}
fn visit_foreign_item(&mut self, i: &'tcx ForeignItem) {
debug!("visit_foreign_item: st={:?}", self.st);
match i.node {
ForeignItemFn(..) => {
SawForeignItem(SawForeignItemComponent::Fn)
}
ForeignItemStatic(_, mutable) => {
SawForeignItem(SawForeignItemComponent::Static {
mutable: mutable
})
}
}.hash(self.st);
hash_span!(self, i.span);
hash_attrs!(self, &i.attrs);
visit::walk_foreign_item(self, i)
}
fn visit_item(&mut self, i: &'tcx Item) {
debug!("visit_item: {:?} st={:?}", i, self.st);
self.maybe_enable_overflow_checks(&i.attrs);
SawItem(saw_item(&i.node)).hash(self.st);
hash_span!(self, i.span);
hash_attrs!(self, &i.attrs);
visit::walk_item(self, i)
}
fn visit_mod(&mut self, m: &'tcx Mod, span: Span, n: NodeId) {
debug!("visit_mod: st={:?}", self.st);
SawMod.hash(self.st);
hash_span!(self, span);
visit::walk_mod(self, m, n)
}
fn visit_ty(&mut self, t: &'tcx Ty) {
debug!("visit_ty: st={:?}", self.st);
SawTy(saw_ty(&t.node)).hash(self.st);
hash_span!(self, t.span);
// Always hash nested constant bodies (e.g. N in `[T; N]`).
let hash_bodies = self.hash_bodies;
self.hash_bodies = true;
visit::walk_ty(self, t);
self.hash_bodies = hash_bodies;
}
fn visit_generics(&mut self, g: &'tcx Generics) {
debug!("visit_generics: st={:?}", self.st);
SawGenerics.hash(self.st);
visit::walk_generics(self, g)
}
fn visit_fn_decl(&mut self, fd: &'tcx FnDecl) {
debug!("visit_fn_decl: st={:?}", self.st);
SawFnDecl(fd.variadic).hash(self.st);
visit::walk_fn_decl(self, fd)
}
fn visit_trait_item(&mut self, ti: &'tcx TraitItem) {
debug!("visit_trait_item: st={:?}", self.st);
self.maybe_enable_overflow_checks(&ti.attrs);
SawTraitItem(saw_trait_item(&ti.node)).hash(self.st);
hash_span!(self, ti.span);
hash_attrs!(self, &ti.attrs);
visit::walk_trait_item(self, ti)
}
fn visit_impl_item(&mut self, ii: &'tcx ImplItem) {
debug!("visit_impl_item: st={:?}", self.st);
self.maybe_enable_overflow_checks(&ii.attrs);
SawImplItem(saw_impl_item(&ii.node)).hash(self.st);
hash_span!(self, ii.span);
hash_attrs!(self, &ii.attrs);
visit::walk_impl_item(self, ii)
}
fn visit_struct_field(&mut self, s: &'tcx StructField) {
debug!("visit_struct_field: st={:?}", self.st);
SawStructField.hash(self.st);
hash_span!(self, s.span);
hash_attrs!(self, &s.attrs);
visit::walk_struct_field(self, s)
}
fn visit_qpath(&mut self, qpath: &'tcx QPath, id: NodeId, span: Span) {
debug!("visit_qpath: st={:?}", self.st);
SawQPath.hash(self.st);
self.hash_discriminant(qpath);
visit::walk_qpath(self, qpath, id, span)
}
fn visit_path(&mut self, path: &'tcx Path, _: ast::NodeId) {
debug!("visit_path: st={:?}", self.st);
hash_span!(self, path.span);
visit::walk_path(self, path)
}
fn visit_def_mention(&mut self, def: Def) {
self.hash_def(def);
}
fn visit_block(&mut self, b: &'tcx Block) {
debug!("visit_block: st={:?}", self.st);
SawBlock.hash(self.st);
hash_span!(self, b.span);
visit::walk_block(self, b)
}
fn visit_pat(&mut self, p: &'tcx Pat) {
debug!("visit_pat: st={:?}", self.st);
SawPat(saw_pat(&p.node)).hash(self.st);
hash_span!(self, p.span);
visit::walk_pat(self, p)
}
fn visit_local(&mut self, l: &'tcx Local) {
debug!("visit_local: st={:?}", self.st);
SawLocal.hash(self.st);
hash_attrs!(self, &l.attrs);
visit::walk_local(self, l)
// No need to hash span, we are hashing all component spans
}
fn visit_arm(&mut self, a: &'tcx Arm) {
debug!("visit_arm: st={:?}", self.st);
SawArm.hash(self.st);
hash_attrs!(self, &a.attrs);
visit::walk_arm(self, a)
}
fn visit_id(&mut self, id: NodeId) {
debug!("visit_id: id={} st={:?}", id, self.st);
self.hash_resolve(id)
}
fn visit_vis(&mut self, v: &'tcx Visibility) {
debug!("visit_vis: st={:?}", self.st);
SawVis.hash(self.st);
self.hash_discriminant(v);
visit::walk_vis(self, v)
}
fn visit_associated_item_kind(&mut self, kind: &'tcx AssociatedItemKind) {
debug!("visit_associated_item_kind: st={:?}", self.st);
SawAssociatedItemKind(*kind).hash(self.st);
visit::walk_associated_item_kind(self, kind);
}
fn visit_defaultness(&mut self, defaultness: &'tcx Defaultness) {
debug!("visit_associated_item_kind: st={:?}", self.st);
SawDefaultness(*defaultness).hash(self.st);
visit::walk_defaultness(self, defaultness);
}
fn visit_where_predicate(&mut self, predicate: &'tcx WherePredicate) {
debug!("visit_where_predicate: st={:?}", self.st);
SawWherePredicate.hash(self.st);
self.hash_discriminant(predicate);
// Ignoring span. Any important nested components should be visited.
visit::walk_where_predicate(self, predicate)
}
fn visit_ty_param_bound(&mut self, bounds: &'tcx TyParamBound) {
debug!("visit_ty_param_bound: st={:?}", self.st);
SawTyParamBound.hash(self.st);
self.hash_discriminant(bounds);
// The TraitBoundModifier in TraitTyParamBound will be hash in
// visit_poly_trait_ref()
visit::walk_ty_param_bound(self, bounds)
}
fn visit_poly_trait_ref(&mut self, t: &'tcx PolyTraitRef, m: TraitBoundModifier) {
debug!("visit_poly_trait_ref: st={:?}", self.st);
SawPolyTraitRef.hash(self.st);
m.hash(self.st);
visit::walk_poly_trait_ref(self, t, m)
}
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'tcx PathSegment) {
debug!("visit_path_segment: st={:?}", self.st);
SawPathSegment.hash(self.st);
visit::walk_path_segment(self, path_span, path_segment)
}
fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'tcx PathParameters) {
debug!("visit_path_parameters: st={:?}", self.st);
SawPathParameters.hash(self.st);
self.hash_discriminant(path_parameters);
visit::walk_path_parameters(self, path_span, path_parameters)
}
fn visit_assoc_type_binding(&mut self, type_binding: &'tcx TypeBinding) {
debug!("visit_assoc_type_binding: st={:?}", self.st);
SawAssocTypeBinding.hash(self.st);
hash_span!(self, type_binding.span);
visit::walk_assoc_type_binding(self, type_binding)
}
fn visit_attribute(&mut self, _: &ast::Attribute) {
// We explicitly do not use this method, since doing that would
// implicitly impose an order on the attributes being hashed, while we
// explicitly don't want their order to matter
}
fn visit_macro_def(&mut self, macro_def: &'tcx MacroDef) {
debug!("visit_macro_def: st={:?}", self.st);
SawMacroDef.hash(self.st);
hash_attrs!(self, ¯o_def.attrs);
for tt in macro_def.body.trees() {
self.hash_token_tree(&tt);
}
visit::walk_macro_def(self, macro_def)
}
}
#[derive(Hash)]
pub enum DefHash {
SawDefId,
SawLabel,
SawPrimTy,
SawSelfTy,
SawErr,
}
impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
fn hash_resolve(&mut self, id: ast::NodeId) {
// Because whether or not a given id has an entry is dependent
// solely on expr variant etc, we don't need to hash whether
// or not an entry was present (we are already hashing what
// variant it is above when we visit the HIR).
if let Some(traits) = self.tcx.trait_map.get(&id) {
debug!("hash_resolve: id={:?} traits={:?} st={:?}", id, traits, self.st);
traits.len().hash(self.st);
// The ordering of the candidates is not fixed. So we hash
// the def-ids and then sort them and hash the collection.
let mut candidates: Vec<_> =
traits.iter()
.map(|&TraitCandidate { def_id, import_id: _ }| {
self.compute_def_id_hash(def_id)
})
.collect();
candidates.sort();
candidates.hash(self.st);
}
}
fn hash_def_id(&mut self, def_id: DefId) {
self.compute_def_id_hash(def_id).hash(self.st);
}
fn hash_def(&mut self, def: Def) {
match def {
// Crucial point: for all of these variants, the variant +
// add'l data that is added is always the same if the
// def-id is the same, so it suffices to hash the def-id
Def::Fn(..) |
Def::Mod(..) |
Def::Static(..) |
Def::Variant(..) |
Def::VariantCtor(..) |
Def::Enum(..) |
Def::TyAlias(..) |
Def::AssociatedTy(..) |
Def::TyParam(..) |
Def::Struct(..) |
Def::StructCtor(..) |
Def::Union(..) |
Def::Trait(..) |
Def::Method(..) |
Def::Const(..) |
Def::AssociatedConst(..) |
Def::Local(..) |
Def::Upvar(..) |
Def::Macro(..) => {
DefHash::SawDefId.hash(self.st);
self.hash_def_id(def.def_id());
}
Def::Label(..) => {
DefHash::SawLabel.hash(self.st);
// we don't encode the `id` because it always refers to something
// within this item, so if it changed, there would have to be other
// changes too
}
Def::PrimTy(ref prim_ty) => {
DefHash::SawPrimTy.hash(self.st);
prim_ty.hash(self.st);
}
Def::SelfTy(..) => {
DefHash::SawSelfTy.hash(self.st);
// the meaning of Self is always the same within a
// given context, so we don't need to hash the other
// fields
}
Def::Err => {
DefHash::SawErr.hash(self.st);
}
}
}
pub fn hash_attributes(&mut self, attributes: &[ast::Attribute]) {
debug!("hash_attributes: st={:?}", self.st);
let indices = self.indices_sorted_by(attributes, |attr| attr.name());
for i in indices {
let attr = &attributes[i];
match attr.name() {
Some(name) if IGNORED_ATTRIBUTES.contains(&&*name.as_str()) => continue,
_ => {}
};
if !attr.is_sugared_doc {
SawAttribute(attr.style).hash(self.st);
for segment in &attr.path.segments {
SawIdent(segment.identifier.name.as_str()).hash(self.st);
}
for tt in attr.tokens.trees() {
self.hash_token_tree(&tt);
}
}
}
}
fn indices_sorted_by<T, K, F>(&mut self, items: &[T], get_key: F) -> Vec<usize>
where K: Ord,
F: Fn(&T) -> K
{
let mut indices = Vec::with_capacity(items.len());
indices.extend(0 .. items.len());
indices.sort_by_key(|index| get_key(&items[*index]));
indices
}
fn maybe_enable_overflow_checks(&mut self, item_attrs: &[ast::Attribute]) {
if attr::contains_name(item_attrs, "rustc_inherit_overflow_checks") {
self.overflow_checks_enabled = true;
}
}
fn hash_token_tree(&mut self, tt: &tokenstream::TokenTree) {
self.hash_discriminant(tt);
match *tt {
tokenstream::TokenTree::Token(span, ref token) => {
hash_span!(self, span);
self.hash_token(token, span);
}
tokenstream::TokenTree::Delimited(span, ref delimited) => {
hash_span!(self, span);
delimited.delim.hash(self.st);
for sub_tt in delimited.stream().trees() {
self.hash_token_tree(&sub_tt);
}