@@ -113,6 +113,12 @@ pub struct CtxtInterners<'tcx> {
113
113
bound_variable_kinds : InternedSet < ' tcx , List < ty:: BoundVariableKind > > ,
114
114
layout : InternedSet < ' tcx , Layout > ,
115
115
adt_def : InternedSet < ' tcx , AdtDef > ,
116
+
117
+ /// `#[stable]` and `#[unstable]` attributes
118
+ stability : InternedSet < ' tcx , attr:: Stability > ,
119
+
120
+ /// `#[rustc_const_stable]` and `#[rustc_const_unstable]` attributes
121
+ const_stability : InternedSet < ' tcx , attr:: ConstStability > ,
116
122
}
117
123
118
124
impl < ' tcx > CtxtInterners < ' tcx > {
@@ -134,6 +140,8 @@ impl<'tcx> CtxtInterners<'tcx> {
134
140
bound_variable_kinds : Default :: default ( ) ,
135
141
layout : Default :: default ( ) ,
136
142
adt_def : Default :: default ( ) ,
143
+ stability : Default :: default ( ) ,
144
+ const_stability : Default :: default ( ) ,
137
145
}
138
146
}
139
147
@@ -1035,12 +1043,6 @@ pub struct GlobalCtxt<'tcx> {
1035
1043
/// Data layout specification for the current target.
1036
1044
pub data_layout : TargetDataLayout ,
1037
1045
1038
- /// `#[stable]` and `#[unstable]` attributes
1039
- stability_interner : ShardedHashMap < & ' tcx attr:: Stability , ( ) > ,
1040
-
1041
- /// `#[rustc_const_stable]` and `#[rustc_const_unstable]` attributes
1042
- const_stability_interner : ShardedHashMap < & ' tcx attr:: ConstStability , ( ) > ,
1043
-
1044
1046
/// Stores memory for globals (statics/consts).
1045
1047
pub ( crate ) alloc_map : Lock < interpret:: AllocMap < ' tcx > > ,
1046
1048
@@ -1092,16 +1094,6 @@ impl<'tcx> TyCtxt<'tcx> {
1092
1094
self . create_memory_alloc ( alloc)
1093
1095
}
1094
1096
1095
- // FIXME(eddyb) move to `direct_interners!`.
1096
- pub fn intern_stability ( self , stab : attr:: Stability ) -> & ' tcx attr:: Stability {
1097
- self . stability_interner . intern ( stab, |stab| self . arena . alloc ( stab) )
1098
- }
1099
-
1100
- // FIXME(eddyb) move to `direct_interners!`.
1101
- pub fn intern_const_stability ( self , stab : attr:: ConstStability ) -> & ' tcx attr:: ConstStability {
1102
- self . const_stability_interner . intern ( stab, |stab| self . arena . alloc ( stab) )
1103
- }
1104
-
1105
1097
/// Returns a range of the start/end indices specified with the
1106
1098
/// `rustc_layout_scalar_valid_range` attribute.
1107
1099
// FIXME(eddyb) this is an awkward spot for this method, maybe move it?
@@ -1185,8 +1177,6 @@ impl<'tcx> TyCtxt<'tcx> {
1185
1177
evaluation_cache : Default :: default ( ) ,
1186
1178
crate_name : Symbol :: intern ( crate_name) ,
1187
1179
data_layout,
1188
- stability_interner : Default :: default ( ) ,
1189
- const_stability_interner : Default :: default ( ) ,
1190
1180
alloc_map : Lock :: new ( interpret:: AllocMap :: new ( ) ) ,
1191
1181
output_filenames : Arc :: new ( output_filenames) ,
1192
1182
}
@@ -1952,11 +1942,11 @@ impl<'tcx> TyCtxt<'tcx> {
1952
1942
1953
1943
writeln ! ( fmt, "InternalSubsts interner: #{}" , self . 0 . interners. substs. len( ) ) ?;
1954
1944
writeln ! ( fmt, "Region interner: #{}" , self . 0 . interners. region. len( ) ) ?;
1955
- writeln ! ( fmt, "Stability interner: #{}" , self . 0 . stability_interner . len( ) ) ?;
1945
+ writeln ! ( fmt, "Stability interner: #{}" , self . 0 . interners . stability . len( ) ) ?;
1956
1946
writeln ! (
1957
1947
fmt,
1958
1948
"Const Stability interner: #{}" ,
1959
- self . 0 . const_stability_interner . len( )
1949
+ self . 0 . interners . const_stability . len( )
1960
1950
) ?;
1961
1951
writeln ! (
1962
1952
fmt,
@@ -1973,24 +1963,37 @@ impl<'tcx> TyCtxt<'tcx> {
1973
1963
}
1974
1964
}
1975
1965
1976
- /// An entry in an interner.
1966
+ // This type holds a `T` in the interner. The `T` is stored in the arena and
1967
+ // this type just holds a pointer to it, but it still effectively owns it. It
1968
+ // impls `Borrow` so that it can be looked up using the original
1969
+ // (non-arena-memory-owning) types.
1977
1970
struct Interned < ' tcx , T : ?Sized > ( & ' tcx T ) ;
1978
1971
1979
1972
impl < ' tcx , T : ' tcx + ?Sized > Clone for Interned < ' tcx , T > {
1980
1973
fn clone ( & self ) -> Self {
1981
1974
Interned ( self . 0 )
1982
1975
}
1983
1976
}
1977
+
1984
1978
impl < ' tcx , T : ' tcx + ?Sized > Copy for Interned < ' tcx , T > { }
1985
1979
1986
1980
impl < ' tcx , T : ' tcx + ?Sized > IntoPointer for Interned < ' tcx , T > {
1987
1981
fn into_pointer ( & self ) -> * const ( ) {
1988
1982
self . 0 as * const _ as * const ( )
1989
1983
}
1990
1984
}
1991
- // N.B., an `Interned<Ty>` compares and hashes as a `TyKind`.
1985
+
1986
+ #[ allow( rustc:: usage_of_ty_tykind) ]
1987
+ impl < ' tcx > Borrow < TyKind < ' tcx > > for Interned < ' tcx , TyS < ' tcx > > {
1988
+ fn borrow < ' a > ( & ' a self ) -> & ' a TyKind < ' tcx > {
1989
+ & self . 0 . kind ( )
1990
+ }
1991
+ }
1992
+
1992
1993
impl < ' tcx > PartialEq for Interned < ' tcx , TyS < ' tcx > > {
1993
1994
fn eq ( & self , other : & Interned < ' tcx , TyS < ' tcx > > ) -> bool {
1995
+ // The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
1996
+ // `x == y`.
1994
1997
self . 0 . kind ( ) == other. 0 . kind ( )
1995
1998
}
1996
1999
}
@@ -1999,19 +2002,21 @@ impl<'tcx> Eq for Interned<'tcx, TyS<'tcx>> {}
1999
2002
2000
2003
impl < ' tcx > Hash for Interned < ' tcx , TyS < ' tcx > > {
2001
2004
fn hash < H : Hasher > ( & self , s : & mut H ) {
2005
+ // The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
2002
2006
self . 0 . kind ( ) . hash ( s)
2003
2007
}
2004
2008
}
2005
2009
2006
- #[ allow( rustc:: usage_of_ty_tykind) ]
2007
- impl < ' tcx > Borrow < TyKind < ' tcx > > for Interned < ' tcx , TyS < ' tcx > > {
2008
- fn borrow < ' a > ( & ' a self ) -> & ' a TyKind < ' tcx > {
2009
- & self . 0 . kind ( )
2010
+ impl < ' tcx > Borrow < Binder < ' tcx , PredicateKind < ' tcx > > > for Interned < ' tcx , PredicateInner < ' tcx > > {
2011
+ fn borrow < ' a > ( & ' a self ) -> & ' a Binder < ' tcx , PredicateKind < ' tcx > > {
2012
+ & self . 0 . kind
2010
2013
}
2011
2014
}
2012
- // N.B., an `Interned<PredicateInner>` compares and hashes as a `PredicateKind`.
2015
+
2013
2016
impl < ' tcx > PartialEq for Interned < ' tcx , PredicateInner < ' tcx > > {
2014
2017
fn eq ( & self , other : & Interned < ' tcx , PredicateInner < ' tcx > > ) -> bool {
2018
+ // The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
2019
+ // `x == y`.
2015
2020
self . 0 . kind == other. 0 . kind
2016
2021
}
2017
2022
}
@@ -2020,19 +2025,21 @@ impl<'tcx> Eq for Interned<'tcx, PredicateInner<'tcx>> {}
2020
2025
2021
2026
impl < ' tcx > Hash for Interned < ' tcx , PredicateInner < ' tcx > > {
2022
2027
fn hash < H : Hasher > ( & self , s : & mut H ) {
2028
+ // The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
2023
2029
self . 0 . kind . hash ( s)
2024
2030
}
2025
2031
}
2026
2032
2027
- impl < ' tcx > Borrow < Binder < ' tcx , PredicateKind < ' tcx > > > for Interned < ' tcx , PredicateInner < ' tcx > > {
2028
- fn borrow < ' a > ( & ' a self ) -> & ' a Binder < ' tcx , PredicateKind < ' tcx > > {
2029
- & self . 0 . kind
2033
+ impl < ' tcx , T > Borrow < [ T ] > for Interned < ' tcx , List < T > > {
2034
+ fn borrow < ' a > ( & ' a self ) -> & ' a [ T ] {
2035
+ & self . 0 [ .. ]
2030
2036
}
2031
2037
}
2032
2038
2033
- // N.B., an `Interned<List<T>>` compares and hashes as its elements.
2034
2039
impl < ' tcx , T : PartialEq > PartialEq for Interned < ' tcx , List < T > > {
2035
2040
fn eq ( & self , other : & Interned < ' tcx , List < T > > ) -> bool {
2041
+ // The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
2042
+ // `x == y`.
2036
2043
self . 0 [ ..] == other. 0 [ ..]
2037
2044
}
2038
2045
}
@@ -2041,20 +2048,23 @@ impl<'tcx, T: Eq> Eq for Interned<'tcx, List<T>> {}
2041
2048
2042
2049
impl < ' tcx , T : Hash > Hash for Interned < ' tcx , List < T > > {
2043
2050
fn hash < H : Hasher > ( & self , s : & mut H ) {
2051
+ // The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
2044
2052
self . 0 [ ..] . hash ( s)
2045
2053
}
2046
2054
}
2047
2055
2048
- impl < ' tcx , T > Borrow < [ T ] > for Interned < ' tcx , List < T > > {
2049
- fn borrow < ' a > ( & ' a self ) -> & ' a [ T ] {
2050
- & self . 0 [ ..]
2051
- }
2052
- }
2053
-
2054
2056
macro_rules! direct_interners {
2055
2057
( $( $name: ident: $method: ident( $ty: ty) , ) +) => {
2056
- $( impl <' tcx> PartialEq for Interned <' tcx, $ty> {
2058
+ $( impl <' tcx> Borrow <$ty> for Interned <' tcx, $ty> {
2059
+ fn borrow<' a>( & ' a self ) -> & ' a $ty {
2060
+ & self . 0
2061
+ }
2062
+ }
2063
+
2064
+ impl <' tcx> PartialEq for Interned <' tcx, $ty> {
2057
2065
fn eq( & self , other: & Self ) -> bool {
2066
+ // The `Borrow` trait requires that `x.borrow() == y.borrow()`
2067
+ // equals `x == y`.
2058
2068
self . 0 == other. 0
2059
2069
}
2060
2070
}
@@ -2063,16 +2073,12 @@ macro_rules! direct_interners {
2063
2073
2064
2074
impl <' tcx> Hash for Interned <' tcx, $ty> {
2065
2075
fn hash<H : Hasher >( & self , s: & mut H ) {
2076
+ // The `Borrow` trait requires that `x.borrow().hash(s) ==
2077
+ // x.hash(s)`.
2066
2078
self . 0 . hash( s)
2067
2079
}
2068
2080
}
2069
2081
2070
- impl <' tcx> Borrow <$ty> for Interned <' tcx, $ty> {
2071
- fn borrow<' a>( & ' a self ) -> & ' a $ty {
2072
- & self . 0
2073
- }
2074
- }
2075
-
2076
2082
impl <' tcx> TyCtxt <' tcx> {
2077
2083
pub fn $method( self , v: $ty) -> & ' tcx $ty {
2078
2084
self . interners. $name. intern( v, |v| {
@@ -2089,6 +2095,8 @@ direct_interners! {
2089
2095
const_allocation: intern_const_alloc( Allocation ) ,
2090
2096
layout: intern_layout( Layout ) ,
2091
2097
adt_def: intern_adt_def( AdtDef ) ,
2098
+ stability: intern_stability( attr:: Stability ) ,
2099
+ const_stability: intern_const_stability( attr:: ConstStability ) ,
2092
2100
}
2093
2101
2094
2102
macro_rules! slice_interners {
0 commit comments