@@ -2,20 +2,19 @@ use crate::consts::{constant_context, constant_simple};
2
2
use crate :: differing_macro_contexts;
3
3
use crate :: source:: snippet_opt;
4
4
use rustc_ast:: ast:: InlineAsmTemplatePiece ;
5
- use rustc_data_structures:: stable_hasher :: { HashStable , StableHasher } ;
5
+ use rustc_data_structures:: fx :: FxHasher ;
6
6
use rustc_hir:: def:: Res ;
7
7
use rustc_hir:: HirIdMap ;
8
8
use rustc_hir:: {
9
- BinOpKind , Block , BlockCheckMode , BodyId , BorrowKind , CaptureBy , Expr , ExprField , ExprKind , FnRetTy , GenericArg ,
10
- GenericArgs , Guard , HirId , InlineAsmOperand , Lifetime , LifetimeName , ParamName , Pat , PatField , PatKind , Path ,
11
- PathSegment , QPath , Stmt , StmtKind , Ty , TyKind , TypeBinding ,
9
+ BinOpKind , Block , BodyId , Expr , ExprField , ExprKind , FnRetTy , GenericArg , GenericArgs , Guard , HirId ,
10
+ InlineAsmOperand , Lifetime , LifetimeName , ParamName , Pat , PatField , PatKind , Path , PathSegment , QPath , Stmt ,
11
+ StmtKind , Ty , TyKind , TypeBinding ,
12
12
} ;
13
13
use rustc_lexer:: { tokenize, TokenKind } ;
14
14
use rustc_lint:: LateContext ;
15
- use rustc_middle:: ich:: StableHashingContextProvider ;
16
15
use rustc_middle:: ty:: TypeckResults ;
17
16
use rustc_span:: Symbol ;
18
- use std:: hash:: Hash ;
17
+ use std:: hash:: { Hash , Hasher } ;
19
18
20
19
/// Type used to check whether two ast are the same. This is different from the
21
20
/// operator
@@ -169,6 +168,12 @@ impl HirEqInterExpr<'_, '_, '_> {
169
168
}
170
169
}
171
170
171
+ pub fn eq_body ( & mut self , left : BodyId , right : BodyId ) -> bool {
172
+ let cx = self . inner . cx ;
173
+ let eval_const = |body| constant_context ( cx, cx. tcx . typeck_body ( body) ) . expr ( & cx. tcx . hir ( ) . body ( body) . value ) ;
174
+ eval_const ( left) == eval_const ( right)
175
+ }
176
+
172
177
#[ allow( clippy:: similar_names) ]
173
178
pub fn eq_expr ( & mut self , left : & Expr < ' _ > , right : & Expr < ' _ > ) -> bool {
174
179
if !self . inner . allow_side_effects && differing_macro_contexts ( left. span , right. span ) {
@@ -244,12 +249,7 @@ impl HirEqInterExpr<'_, '_, '_> {
244
249
self . inner . allow_side_effects && self . eq_path_segment ( l_path, r_path) && self . eq_exprs ( l_args, r_args)
245
250
} ,
246
251
( & ExprKind :: Repeat ( le, ref ll_id) , & ExprKind :: Repeat ( re, ref rl_id) ) => {
247
- let mut celcx = constant_context ( self . inner . cx , self . inner . cx . tcx . typeck_body ( ll_id. body ) ) ;
248
- let ll = celcx. expr ( & self . inner . cx . tcx . hir ( ) . body ( ll_id. body ) . value ) ;
249
- let mut celcx = constant_context ( self . inner . cx , self . inner . cx . tcx . typeck_body ( rl_id. body ) ) ;
250
- let rl = celcx. expr ( & self . inner . cx . tcx . hir ( ) . body ( rl_id. body ) . value ) ;
251
-
252
- self . eq_expr ( le, re) && ll == rl
252
+ self . eq_expr ( le, re) && self . eq_body ( ll_id. body , rl_id. body )
253
253
} ,
254
254
( & ExprKind :: Ret ( ref l) , & ExprKind :: Ret ( ref r) ) => both ( l, r, |l, r| self . eq_expr ( l, r) ) ,
255
255
( & ExprKind :: Path ( ref l) , & ExprKind :: Path ( ref r) ) => self . eq_qpath ( l, r) ,
@@ -285,6 +285,7 @@ impl HirEqInterExpr<'_, '_, '_> {
285
285
286
286
fn eq_generic_arg ( & mut self , left : & GenericArg < ' _ > , right : & GenericArg < ' _ > ) -> bool {
287
287
match ( left, right) {
288
+ ( GenericArg :: Const ( l) , GenericArg :: Const ( r) ) => self . eq_body ( l. value . body , r. value . body ) ,
288
289
( GenericArg :: Lifetime ( l_lt) , GenericArg :: Lifetime ( r_lt) ) => Self :: eq_lifetime ( l_lt, r_lt) ,
289
290
( GenericArg :: Type ( l_ty) , GenericArg :: Type ( r_ty) ) => self . eq_ty ( l_ty, r_ty) ,
290
291
_ => false ,
@@ -385,10 +386,7 @@ impl HirEqInterExpr<'_, '_, '_> {
385
386
match ( & left. kind , & right. kind ) {
386
387
( & TyKind :: Slice ( l_vec) , & TyKind :: Slice ( r_vec) ) => self . eq_ty ( l_vec, r_vec) ,
387
388
( & TyKind :: Array ( lt, ref ll_id) , & TyKind :: Array ( rt, ref rl_id) ) => {
388
- let cx = self . inner . cx ;
389
- let eval_const =
390
- |body| constant_context ( cx, cx. tcx . typeck_body ( body) ) . expr ( & cx. tcx . hir ( ) . body ( body) . value ) ;
391
- self . eq_ty ( lt, rt) && eval_const ( ll_id. body ) == eval_const ( rl_id. body )
389
+ self . eq_ty ( lt, rt) && self . eq_body ( ll_id. body , rl_id. body )
392
390
} ,
393
391
( & TyKind :: Ptr ( ref l_mut) , & TyKind :: Ptr ( ref r_mut) ) => {
394
392
l_mut. mutbl == r_mut. mutbl && self . eq_ty ( & * l_mut. ty , & * r_mut. ty )
@@ -512,15 +510,15 @@ pub struct SpanlessHash<'a, 'tcx> {
512
510
/// Context used to evaluate constant expressions.
513
511
cx : & ' a LateContext < ' tcx > ,
514
512
maybe_typeck_results : Option < & ' tcx TypeckResults < ' tcx > > ,
515
- s : StableHasher ,
513
+ s : FxHasher ,
516
514
}
517
515
518
516
impl < ' a , ' tcx > SpanlessHash < ' a , ' tcx > {
519
517
pub fn new ( cx : & ' a LateContext < ' tcx > ) -> Self {
520
518
Self {
521
519
cx,
522
520
maybe_typeck_results : cx. maybe_typeck_results ( ) ,
523
- s : StableHasher :: new ( ) ,
521
+ s : FxHasher :: default ( ) ,
524
522
}
525
523
}
526
524
@@ -537,13 +535,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
537
535
self . hash_expr ( e) ;
538
536
}
539
537
540
- match b. rules {
541
- BlockCheckMode :: DefaultBlock => 0 ,
542
- BlockCheckMode :: UnsafeBlock ( _) => 1 ,
543
- BlockCheckMode :: PushUnsafeBlock ( _) => 2 ,
544
- BlockCheckMode :: PopUnsafeBlock ( _) => 3 ,
545
- }
546
- . hash ( & mut self . s ) ;
538
+ std:: mem:: discriminant ( & b. rules ) . hash ( & mut self . s ) ;
547
539
}
548
540
549
541
#[ allow( clippy:: many_single_char_names, clippy:: too_many_lines) ]
@@ -554,21 +546,16 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
554
546
555
547
// const hashing may result in the same hash as some unrelated node, so add a sort of
556
548
// discriminant depending on which path we're choosing next
557
- simple_const. is_some ( ) . hash ( & mut self . s ) ;
558
-
559
- if let Some ( e) = simple_const {
560
- return e. hash ( & mut self . s ) ;
549
+ simple_const. hash ( & mut self . s ) ;
550
+ if simple_const. is_some ( ) {
551
+ return ;
561
552
}
562
553
563
554
std:: mem:: discriminant ( & e. kind ) . hash ( & mut self . s ) ;
564
555
565
556
match e. kind {
566
557
ExprKind :: AddrOf ( kind, m, e) => {
567
- match kind {
568
- BorrowKind :: Ref => 0 ,
569
- BorrowKind :: Raw => 1 ,
570
- }
571
- . hash ( & mut self . s ) ;
558
+ std:: mem:: discriminant ( & kind) . hash ( & mut self . s ) ;
572
559
m. hash ( & mut self . s ) ;
573
560
self . hash_expr ( e) ;
574
561
} ,
@@ -582,17 +569,15 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
582
569
self . hash_expr ( r) ;
583
570
} ,
584
571
ExprKind :: AssignOp ( ref o, l, r) => {
585
- o. node
586
- . hash_stable ( & mut self . cx . tcx . get_stable_hashing_context ( ) , & mut self . s ) ;
572
+ std:: mem:: discriminant ( & o. node ) . hash ( & mut self . s ) ;
587
573
self . hash_expr ( l) ;
588
574
self . hash_expr ( r) ;
589
575
} ,
590
576
ExprKind :: Block ( b, _) => {
591
577
self . hash_block ( b) ;
592
578
} ,
593
579
ExprKind :: Binary ( op, l, r) => {
594
- op. node
595
- . hash_stable ( & mut self . cx . tcx . get_stable_hashing_context ( ) , & mut self . s ) ;
580
+ std:: mem:: discriminant ( & op. node ) . hash ( & mut self . s ) ;
596
581
self . hash_expr ( l) ;
597
582
self . hash_expr ( r) ;
598
583
} ,
@@ -616,11 +601,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
616
601
self . hash_ty ( ty) ;
617
602
} ,
618
603
ExprKind :: Closure ( cap, _, eid, _, _) => {
619
- match cap {
620
- CaptureBy :: Value => 0 ,
621
- CaptureBy :: Ref => 1 ,
622
- }
623
- . hash ( & mut self . s ) ;
604
+ std:: mem:: discriminant ( & cap) . hash ( & mut self . s ) ;
624
605
// closures inherit TypeckResults
625
606
self . hash_expr ( & self . cx . tcx . hir ( ) . body ( eid) . value ) ;
626
607
} ,
@@ -694,8 +675,6 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
694
675
}
695
676
} ,
696
677
ExprKind :: If ( cond, then, ref else_opt) => {
697
- let c: fn ( _, _, _) -> _ = ExprKind :: If ;
698
- c. hash ( & mut self . s ) ;
699
678
self . hash_expr ( cond) ;
700
679
self . hash_expr ( then) ;
701
680
if let Some ( e) = * else_opt {
@@ -753,7 +732,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
753
732
self . hash_exprs ( v) ;
754
733
} ,
755
734
ExprKind :: Unary ( lop, le) => {
756
- lop . hash_stable ( & mut self . cx . tcx . get_stable_hashing_context ( ) , & mut self . s ) ;
735
+ std :: mem :: discriminant ( & lop ) . hash ( & mut self . s ) ;
757
736
self . hash_expr ( le) ;
758
737
} ,
759
738
}
@@ -766,7 +745,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
766
745
}
767
746
768
747
pub fn hash_name ( & mut self , n : Symbol ) {
769
- n. as_str ( ) . hash ( & mut self . s ) ;
748
+ n. hash ( & mut self . s ) ;
770
749
}
771
750
772
751
pub fn hash_qpath ( & mut self , p : & QPath < ' _ > ) {
@@ -778,7 +757,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
778
757
self . hash_name ( path. ident . name ) ;
779
758
} ,
780
759
QPath :: LangItem ( lang_item, ..) => {
781
- lang_item . hash_stable ( & mut self . cx . tcx . get_stable_hashing_context ( ) , & mut self . s ) ;
760
+ std :: mem :: discriminant ( & lang_item ) . hash ( & mut self . s ) ;
782
761
} ,
783
762
}
784
763
// self.maybe_typeck_results.unwrap().qpath_res(p, id).hash(&mut self.s);
@@ -788,7 +767,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
788
767
std:: mem:: discriminant ( & pat. kind ) . hash ( & mut self . s ) ;
789
768
match pat. kind {
790
769
PatKind :: Binding ( ann, _, _, pat) => {
791
- ann . hash_stable ( & mut self . cx . tcx . get_stable_hashing_context ( ) , & mut self . s ) ;
770
+ std :: mem :: discriminant ( & ann ) . hash ( & mut self . s ) ;
792
771
if let Some ( pat) = pat {
793
772
self . hash_pat ( pat) ;
794
773
}
@@ -808,11 +787,11 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
808
787
if let Some ( e) = e {
809
788
self . hash_expr ( e) ;
810
789
}
811
- i . hash_stable ( & mut self . cx . tcx . get_stable_hashing_context ( ) , & mut self . s ) ;
790
+ std :: mem :: discriminant ( & i ) . hash ( & mut self . s ) ;
812
791
} ,
813
- PatKind :: Ref ( pat, m ) => {
792
+ PatKind :: Ref ( pat, mu ) => {
814
793
self . hash_pat ( pat) ;
815
- m . hash ( & mut self . s ) ;
794
+ std :: mem :: discriminant ( & mu ) . hash ( & mut self . s ) ;
816
795
} ,
817
796
PatKind :: Slice ( l, m, r) => {
818
797
for pat in l {
@@ -857,6 +836,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
857
836
_ => {
858
837
for seg in path. segments {
859
838
self . hash_name ( seg. ident . name ) ;
839
+ self . hash_generic_args ( seg. args ( ) . args ) ;
860
840
}
861
841
} ,
862
842
}
@@ -928,10 +908,9 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
928
908
for arg in bfn. decl . inputs {
929
909
self . hash_ty ( arg) ;
930
910
}
911
+ std:: mem:: discriminant ( & bfn. decl . output ) . hash ( & mut self . s ) ;
931
912
match bfn. decl . output {
932
- FnRetTy :: DefaultReturn ( _) => {
933
- ( ) . hash ( & mut self . s ) ;
934
- } ,
913
+ FnRetTy :: DefaultReturn ( _) => { } ,
935
914
FnRetTy :: Return ( ty) => {
936
915
self . hash_ty ( ty) ;
937
916
} ,
@@ -943,24 +922,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
943
922
self . hash_ty ( ty) ;
944
923
}
945
924
} ,
946
- TyKind :: Path ( ref qpath) => match qpath {
947
- QPath :: Resolved ( ref maybe_ty, path) => {
948
- if let Some ( ty) = maybe_ty {
949
- self . hash_ty ( ty) ;
950
- }
951
- for segment in path. segments {
952
- segment. ident . name . hash ( & mut self . s ) ;
953
- self . hash_generic_args ( segment. args ( ) . args ) ;
954
- }
955
- } ,
956
- QPath :: TypeRelative ( ty, segment) => {
957
- self . hash_ty ( ty) ;
958
- segment. ident . name . hash ( & mut self . s ) ;
959
- } ,
960
- QPath :: LangItem ( lang_item, ..) => {
961
- lang_item. hash ( & mut self . s ) ;
962
- } ,
963
- } ,
925
+ TyKind :: Path ( ref qpath) => self . hash_qpath ( qpath) ,
964
926
TyKind :: OpaqueDef ( _, arg_list) => {
965
927
self . hash_generic_args ( arg_list) ;
966
928
} ,
0 commit comments