@@ -35,7 +35,7 @@ use ty::layout::{Layout, TargetDataLayout};
35
35
use ty:: maps;
36
36
use util:: common:: MemoizationMap ;
37
37
use util:: nodemap:: { NodeMap , NodeSet , DefIdMap , DefIdSet } ;
38
- use util:: nodemap:: FnvHashMap ;
38
+ use util:: nodemap:: { FnvHashMap , FnvHashSet } ;
39
39
40
40
use arena:: TypedArena ;
41
41
use std:: borrow:: Borrow ;
@@ -191,7 +191,7 @@ impl<'a, 'tcx> Tables<'tcx> {
191
191
192
192
impl < ' tcx > CommonTypes < ' tcx > {
193
193
fn new ( arena : & ' tcx TypedArena < TyS < ' tcx > > ,
194
- interner : & RefCell < FnvHashMap < InternedTy < ' tcx > , Ty < ' tcx > > > )
194
+ interner : & RefCell < FnvHashSet < InternedTy < ' tcx > > > )
195
195
-> CommonTypes < ' tcx >
196
196
{
197
197
let mk = |sty| TyCtxt :: intern_ty ( arena, interner, sty) ;
@@ -220,7 +220,8 @@ impl<'tcx> CommonTypes<'tcx> {
220
220
/// later on.
221
221
#[ derive( Copy , Clone ) ]
222
222
pub struct TyCtxt < ' a , ' tcx : ' a > {
223
- gcx : & ' a GlobalCtxt < ' tcx >
223
+ gcx : & ' a GlobalCtxt < ' tcx > ,
224
+
224
225
}
225
226
226
227
impl < ' a , ' tcx > Deref for TyCtxt < ' a , ' tcx > {
@@ -236,16 +237,12 @@ pub struct GlobalCtxt<'tcx> {
236
237
237
238
/// Specifically use a speedy hash algorithm for this hash map, it's used
238
239
/// quite often.
239
- // FIXME(eddyb) use a FnvHashSet<InternedTy<'tcx>> when equivalent keys can
240
- // queried from a HashSet.
241
- interner : RefCell < FnvHashMap < InternedTy < ' tcx > , Ty < ' tcx > > > ,
242
-
243
- // FIXME as above, use a hashset if equivalent elements can be queried.
244
- substs_interner : RefCell < FnvHashMap < & ' tcx Substs < ' tcx > , & ' tcx Substs < ' tcx > > > ,
245
- bare_fn_interner : RefCell < FnvHashMap < & ' tcx BareFnTy < ' tcx > , & ' tcx BareFnTy < ' tcx > > > ,
246
- region_interner : RefCell < FnvHashMap < & ' tcx Region , & ' tcx Region > > ,
247
- stability_interner : RefCell < FnvHashMap < & ' tcx attr:: Stability , & ' tcx attr:: Stability > > ,
248
- layout_interner : RefCell < FnvHashMap < & ' tcx Layout , & ' tcx Layout > > ,
240
+ interner : RefCell < FnvHashSet < InternedTy < ' tcx > > > ,
241
+ substs_interner : RefCell < FnvHashSet < InternedSubsts < ' tcx > > > ,
242
+ bare_fn_interner : RefCell < FnvHashSet < & ' tcx BareFnTy < ' tcx > > > ,
243
+ region_interner : RefCell < FnvHashSet < & ' tcx Region > > ,
244
+ stability_interner : RefCell < FnvHashSet < & ' tcx attr:: Stability > > ,
245
+ layout_interner : RefCell < FnvHashSet < & ' tcx Layout > > ,
249
246
250
247
pub dep_graph : DepGraph ,
251
248
@@ -518,7 +515,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
518
515
let interned = self . arenas . stability . alloc ( stab) ;
519
516
if let Some ( prev) = self . stability_interner
520
517
. borrow_mut ( )
521
- . insert ( interned , interned) {
518
+ . replace ( interned) {
522
519
bug ! ( "Tried to overwrite interned Stability: {:?}" , prev)
523
520
}
524
521
interned
@@ -532,7 +529,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
532
529
let interned = self . arenas . layout . alloc ( layout) ;
533
530
if let Some ( prev) = self . layout_interner
534
531
. borrow_mut ( )
535
- . insert ( interned , interned) {
532
+ . replace ( interned) {
536
533
bug ! ( "Tried to overwrite interned Layout: {:?}" , prev)
537
534
}
538
535
interned
@@ -571,18 +568,18 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
571
568
where F : for < ' b > FnOnce ( TyCtxt < ' b , ' tcx > ) -> R
572
569
{
573
570
let data_layout = TargetDataLayout :: parse ( s) ;
574
- let interner = RefCell :: new ( FnvHashMap ( ) ) ;
571
+ let interner = RefCell :: new ( FnvHashSet ( ) ) ;
575
572
let common_types = CommonTypes :: new ( & arenas. type_ , & interner) ;
576
573
let dep_graph = map. dep_graph . clone ( ) ;
577
574
let fulfilled_predicates = traits:: GlobalFulfilledPredicates :: new ( dep_graph. clone ( ) ) ;
578
575
tls:: enter ( GlobalCtxt {
579
576
arenas : arenas,
580
577
interner : interner,
581
- substs_interner : RefCell :: new ( FnvHashMap ( ) ) ,
582
- bare_fn_interner : RefCell :: new ( FnvHashMap ( ) ) ,
583
- region_interner : RefCell :: new ( FnvHashMap ( ) ) ,
584
- stability_interner : RefCell :: new ( FnvHashMap ( ) ) ,
585
- layout_interner : RefCell :: new ( FnvHashMap ( ) ) ,
578
+ substs_interner : RefCell :: new ( FnvHashSet ( ) ) ,
579
+ bare_fn_interner : RefCell :: new ( FnvHashSet ( ) ) ,
580
+ region_interner : RefCell :: new ( FnvHashSet ( ) ) ,
581
+ stability_interner : RefCell :: new ( FnvHashSet ( ) ) ,
582
+ layout_interner : RefCell :: new ( FnvHashSet ( ) ) ,
586
583
dep_graph : dep_graph. clone ( ) ,
587
584
types : common_types,
588
585
named_region_map : named_region_map,
@@ -656,7 +653,7 @@ pub trait Lift<'tcx> {
656
653
impl < ' a , ' tcx > Lift < ' tcx > for Ty < ' a > {
657
654
type Lifted = Ty < ' tcx > ;
658
655
fn lift_to_tcx < ' b > ( & self , tcx : TyCtxt < ' b , ' tcx > ) -> Option < Ty < ' tcx > > {
659
- if let Some ( & ty ) = tcx. interner . borrow ( ) . get ( & self . sty ) {
656
+ if let Some ( & InternedTy { ty } ) = tcx. interner . borrow ( ) . get ( & self . sty ) {
660
657
if * self as * const _ == ty as * const _ {
661
658
return Some ( ty) ;
662
659
}
@@ -668,7 +665,7 @@ impl<'a, 'tcx> Lift<'tcx> for Ty<'a> {
668
665
impl < ' a , ' tcx > Lift < ' tcx > for & ' a Substs < ' a > {
669
666
type Lifted = & ' tcx Substs < ' tcx > ;
670
667
fn lift_to_tcx < ' b > ( & self , tcx : TyCtxt < ' b , ' tcx > ) -> Option < & ' tcx Substs < ' tcx > > {
671
- if let Some ( & substs) = tcx. substs_interner . borrow ( ) . get ( * self ) {
668
+ if let Some ( & InternedSubsts { substs } ) = tcx. substs_interner . borrow ( ) . get ( * self ) {
672
669
if * self as * const _ == substs as * const _ {
673
670
return Some ( substs) ;
674
671
}
@@ -745,6 +742,8 @@ macro_rules! sty_debug_print {
745
742
#[ allow( non_snake_case) ]
746
743
mod inner {
747
744
use ty:: { self , TyCtxt } ;
745
+ use ty:: context:: InternedTy ;
746
+
748
747
#[ derive( Copy , Clone ) ]
749
748
struct DebugStat {
750
749
total: usize ,
@@ -761,7 +760,7 @@ macro_rules! sty_debug_print {
761
760
$( let mut $variant = total; ) *
762
761
763
762
764
- for ( _ , t ) in tcx. interner. borrow( ) . iter( ) {
763
+ for & InternedTy { ty : t } in tcx. interner. borrow( ) . iter( ) {
765
764
let variant = match t. sty {
766
765
ty:: TyBool | ty:: TyChar | ty:: TyInt ( ..) | ty:: TyUint ( ..) |
767
766
ty:: TyFloat ( ..) | ty:: TyStr => continue ,
@@ -817,7 +816,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
817
816
818
817
819
818
/// An entry in the type interner.
820
- pub struct InternedTy < ' tcx > {
819
+ struct InternedTy < ' tcx > {
821
820
ty : Ty < ' tcx >
822
821
}
823
822
@@ -836,12 +835,24 @@ impl<'tcx> Hash for InternedTy<'tcx> {
836
835
}
837
836
}
838
837
839
- impl < ' tcx > Borrow < TypeVariants < ' tcx > > for InternedTy < ' tcx > {
840
- fn borrow < ' a > ( & ' a self ) -> & ' a TypeVariants < ' tcx > {
838
+ impl < ' tcx : ' lcx , ' lcx > Borrow < TypeVariants < ' lcx > > for InternedTy < ' tcx > {
839
+ fn borrow < ' a > ( & ' a self ) -> & ' a TypeVariants < ' lcx > {
841
840
& self . ty . sty
842
841
}
843
842
}
844
843
844
+ /// An entry in the substs interner.
845
+ #[ derive( PartialEq , Eq , Hash ) ]
846
+ struct InternedSubsts < ' tcx > {
847
+ substs : & ' tcx Substs < ' tcx >
848
+ }
849
+
850
+ impl < ' tcx : ' lcx , ' lcx > Borrow < Substs < ' lcx > > for InternedSubsts < ' tcx > {
851
+ fn borrow < ' a > ( & ' a self ) -> & ' a Substs < ' lcx > {
852
+ self . substs
853
+ }
854
+ }
855
+
845
856
fn bound_list_is_sorted ( bounds : & [ ty:: PolyProjectionPredicate ] ) -> bool {
846
857
bounds. is_empty ( ) ||
847
858
bounds[ 1 ..] . iter ( ) . enumerate ( ) . all (
@@ -851,12 +862,14 @@ fn bound_list_is_sorted(bounds: &[ty::PolyProjectionPredicate]) -> bool {
851
862
impl < ' a , ' tcx > TyCtxt < ' a , ' tcx > {
852
863
// Type constructors
853
864
pub fn mk_substs ( self , substs : Substs < ' tcx > ) -> & ' tcx Substs < ' tcx > {
854
- if let Some ( substs ) = self . substs_interner . borrow ( ) . get ( & substs) {
855
- return * substs;
865
+ if let Some ( interned ) = self . substs_interner . borrow ( ) . get ( & substs) {
866
+ return interned . substs ;
856
867
}
857
868
858
869
let substs = self . arenas . substs . alloc ( substs) ;
859
- self . substs_interner . borrow_mut ( ) . insert ( substs, substs) ;
870
+ self . substs_interner . borrow_mut ( ) . insert ( InternedSubsts {
871
+ substs : substs
872
+ } ) ;
860
873
substs
861
874
}
862
875
@@ -876,7 +889,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
876
889
}
877
890
878
891
let bare_fn = self . arenas . bare_fn . alloc ( bare_fn) ;
879
- self . bare_fn_interner . borrow_mut ( ) . insert ( bare_fn, bare_fn ) ;
892
+ self . bare_fn_interner . borrow_mut ( ) . insert ( bare_fn) ;
880
893
bare_fn
881
894
}
882
895
@@ -886,19 +899,19 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
886
899
}
887
900
888
901
let region = self . arenas . region . alloc ( region) ;
889
- self . region_interner . borrow_mut ( ) . insert ( region, region ) ;
902
+ self . region_interner . borrow_mut ( ) . insert ( region) ;
890
903
region
891
904
}
892
905
893
906
fn intern_ty ( type_arena : & ' tcx TypedArena < TyS < ' tcx > > ,
894
- interner : & RefCell < FnvHashMap < InternedTy < ' tcx > , Ty < ' tcx > > > ,
907
+ interner : & RefCell < FnvHashSet < InternedTy < ' tcx > > > ,
895
908
st : TypeVariants < ' tcx > )
896
909
-> Ty < ' tcx > {
897
910
let ty: Ty /* don't be &mut TyS */ = {
898
911
let mut interner = interner. borrow_mut ( ) ;
899
912
match interner. get ( & st) {
900
- Some ( ty ) => return * ty,
901
- _ => ( )
913
+ Some ( & InternedTy { ty } ) => return ty,
914
+ None => ( )
902
915
}
903
916
904
917
let flags = super :: flags:: FlagComputation :: for_sty ( & st) ;
@@ -909,7 +922,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
909
922
region_depth : flags. depth , } ) ,
910
923
} ;
911
924
912
- interner. insert ( InternedTy { ty : ty } , ty ) ;
925
+ interner. insert ( InternedTy { ty : ty } ) ;
913
926
ty
914
927
} ;
915
928
0 commit comments