@@ -125,7 +125,7 @@ impl<'tcx> GlobalArenas<'tcx> {
125
125
}
126
126
}
127
127
128
- type InternedSet < ' tcx , T > = Lock < FxHashSet < Interned < ' tcx , T > > > ;
128
+ type InternedSet < ' tcx , T > = Lock < FxInterner < Interned < ' tcx , T > > > ;
129
129
130
130
pub struct CtxtInterners < ' tcx > {
131
131
/// The arena that types, regions, etc are allocated from
@@ -789,12 +789,9 @@ impl<'tcx> CommonTypes<'tcx> {
789
789
790
790
let mk = |sty| CtxtInterners :: intern_ty ( interners, interners, sty) ;
791
791
let mk_region = |r| {
792
- if let Some ( r) = interners. region . borrow ( ) . get ( & r) {
793
- return r. 0 ;
794
- }
795
- let r = interners. arena . alloc ( r) ;
796
- interners. region . borrow_mut ( ) . insert ( Interned ( r) ) ;
797
- & * r
792
+ interners. region . borrow_mut ( ) . intern ( r, |r| {
793
+ Interned ( interners. arena . alloc ( r) )
794
+ } ) . 0
798
795
} ;
799
796
CommonTypes {
800
797
bool : mk ( TyBool ) ,
@@ -905,14 +902,14 @@ pub struct GlobalCtxt<'tcx> {
905
902
/// Data layout specification for the current target.
906
903
pub data_layout : TargetDataLayout ,
907
904
908
- stability_interner : Lock < FxHashSet < & ' tcx attr:: Stability > > ,
905
+ stability_interner : Lock < FxInterner < & ' tcx attr:: Stability > > ,
909
906
910
907
/// Stores the value of constants (and deduplicates the actual memory)
911
908
allocation_interner : Lock < FxHashSet < & ' tcx Allocation > > ,
912
909
913
910
pub alloc_map : Lock < interpret:: AllocMap < ' tcx , & ' tcx Allocation > > ,
914
911
915
- layout_interner : Lock < FxHashSet < & ' tcx LayoutDetails > > ,
912
+ layout_interner : Lock < FxInterner < & ' tcx LayoutDetails > > ,
916
913
917
914
/// A general purpose channel to throw data out the back towards LLVM worker
918
915
/// threads.
@@ -995,16 +992,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
995
992
self ,
996
993
alloc : Allocation ,
997
994
) -> & ' gcx Allocation {
998
- let allocs = & mut self . allocation_interner . borrow_mut ( ) ;
999
- if let Some ( alloc) = allocs. get ( & alloc) {
1000
- return alloc;
1001
- }
1002
-
1003
- let interned = self . global_arenas . const_allocs . alloc ( alloc) ;
1004
- if let Some ( prev) = allocs. replace ( interned) {
1005
- bug ! ( "Tried to overwrite interned Allocation: {:#?}" , prev)
1006
- }
1007
- interned
995
+ self . allocation_interner . borrow_mut ( ) . intern ( alloc, |alloc| {
996
+ self . global_arenas . const_allocs . alloc ( alloc)
997
+ } )
1008
998
}
1009
999
1010
1000
/// Allocates a byte or string literal for `mir::interpret`
@@ -1016,29 +1006,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
1016
1006
}
1017
1007
1018
1008
pub fn intern_stability ( self , stab : attr:: Stability ) -> & ' gcx attr:: Stability {
1019
- let mut stability_interner = self . stability_interner . borrow_mut ( ) ;
1020
- if let Some ( st) = stability_interner. get ( & stab) {
1021
- return st;
1022
- }
1023
-
1024
- let interned = self . global_interners . arena . alloc ( stab) ;
1025
- if let Some ( prev) = stability_interner. replace ( interned) {
1026
- bug ! ( "Tried to overwrite interned Stability: {:?}" , prev)
1027
- }
1028
- interned
1009
+ self . stability_interner . borrow_mut ( ) . intern ( stab, |stab| {
1010
+ self . global_interners . arena . alloc ( stab)
1011
+ } )
1029
1012
}
1030
1013
1031
1014
pub fn intern_layout ( self , layout : LayoutDetails ) -> & ' gcx LayoutDetails {
1032
- let mut layout_interner = self . layout_interner . borrow_mut ( ) ;
1033
- if let Some ( layout) = layout_interner. get ( & layout) {
1034
- return layout;
1035
- }
1036
-
1037
- let interned = self . global_arenas . layout . alloc ( layout) ;
1038
- if let Some ( prev) = layout_interner. replace ( interned) {
1039
- bug ! ( "Tried to overwrite interned Layout: {:?}" , prev)
1040
- }
1041
- interned
1015
+ self . layout_interner . borrow_mut ( ) . intern ( layout, |layout| {
1016
+ self . global_arenas . layout . alloc ( layout)
1017
+ } )
1042
1018
}
1043
1019
1044
1020
pub fn lift < T : ?Sized + Lift < ' tcx > > ( self , value : & T ) -> Option < T :: Lifted > {
@@ -1160,8 +1136,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
1160
1136
evaluation_cache : traits:: EvaluationCache :: new ( ) ,
1161
1137
crate_name : Symbol :: intern ( crate_name) ,
1162
1138
data_layout,
1163
- layout_interner : Lock :: new ( FxHashSet ( ) ) ,
1164
- stability_interner : Lock :: new ( FxHashSet ( ) ) ,
1139
+ layout_interner : Default :: default ( ) ,
1140
+ stability_interner : Default :: default ( ) ,
1165
1141
allocation_interner : Lock :: new ( FxHashSet ( ) ) ,
1166
1142
alloc_map : Lock :: new ( interpret:: AllocMap :: new ( ) ) ,
1167
1143
tx_to_llvm_workers : Lock :: new ( tx) ,
@@ -1898,7 +1874,7 @@ macro_rules! sty_debug_print {
1898
1874
( $ctxt: expr, $( $variant: ident) ,* ) => { {
1899
1875
// curious inner module to allow variant names to be used as
1900
1876
// variable names.
1901
- #[ allow( non_snake_case) ]
1877
+ #[ allow( non_snake_case, warnings ) ]
1902
1878
mod inner {
1903
1879
use ty:: { self , TyCtxt } ;
1904
1880
use ty:: context:: Interned ;
@@ -2085,37 +2061,28 @@ macro_rules! intern_method {
2085
2061
// determine that all contents are in the global tcx.
2086
2062
// See comments on Lift for why we can't use that.
2087
2063
if ( $keep_in_local_tcx) ( & v) {
2088
- let mut interner = self . interners. $name. borrow_mut( ) ;
2089
- if let Some ( & Interned ( v) ) = interner. get( key) {
2090
- return v;
2091
- }
2092
-
2093
- // Make sure we don't end up with inference
2094
- // types/regions in the global tcx.
2095
- if self . is_global( ) {
2096
- bug!( "Attempted to intern `{:?}` which contains \
2097
- inference types/regions in the global type context",
2098
- v) ;
2099
- }
2100
-
2101
- let i = $alloc_method( & self . interners. arena, v) ;
2102
- interner. insert( Interned ( i) ) ;
2103
- i
2064
+ self . interners. $name. borrow_mut( ) . intern_ref( key, || {
2065
+ // Make sure we don't end up with inference
2066
+ // types/regions in the global tcx.
2067
+ if self . is_global( ) {
2068
+ bug!( "Attempted to intern `{:?}` which contains \
2069
+ inference types/regions in the global type context",
2070
+ v) ;
2071
+ }
2072
+
2073
+ Interned ( $alloc_method( & self . interners. arena, v) )
2074
+ } ) . 0
2104
2075
} else {
2105
- let mut interner = self . global_interners. $name. borrow_mut( ) ;
2106
- if let Some ( & Interned ( v) ) = interner. get( key) {
2107
- return v;
2108
- }
2109
-
2110
- // This transmutes $alloc<'tcx> to $alloc<'gcx>
2111
- let v = unsafe {
2112
- mem:: transmute( v)
2113
- } ;
2114
- let i: & $lt_tcx $ty = $alloc_method( & self . global_interners. arena, v) ;
2115
- // Cast to 'gcx
2116
- let i = unsafe { mem:: transmute( i) } ;
2117
- interner. insert( Interned ( i) ) ;
2118
- i
2076
+ self . global_interners. $name. borrow_mut( ) . intern_ref( key, || {
2077
+ // This transmutes $alloc<'tcx> to $alloc<'gcx>
2078
+ let v = unsafe {
2079
+ mem:: transmute( v)
2080
+ } ;
2081
+ let i: & $lt_tcx $ty = $alloc_method( & self . global_interners. arena, v) ;
2082
+ // Cast to 'gcx
2083
+ let i = unsafe { mem:: transmute( i) } ;
2084
+ Interned ( i)
2085
+ } ) . 0
2119
2086
}
2120
2087
}
2121
2088
}
0 commit comments