@@ -1946,24 +1946,37 @@ impl<'tcx> TyCtxt<'tcx> {
1946
1946
}
1947
1947
}
1948
1948
1949
- /// An entry in an interner.
1949
+ // This type holds a `T` in the interner. The `T` is stored in the arena and
1950
+ // this type just holds a pointer to it, but it still effectively owns it. It
1951
+ // impls `Borrow` so that it can be looked up using the original
1952
+ // (non-arena-memory-owning) types.
1950
1953
struct Interned < ' tcx , T : ?Sized > ( & ' tcx T ) ;
1951
1954
1952
1955
impl < ' tcx , T : ' tcx + ?Sized > Clone for Interned < ' tcx , T > {
1953
1956
fn clone ( & self ) -> Self {
1954
1957
Interned ( self . 0 )
1955
1958
}
1956
1959
}
1960
+
1957
1961
impl < ' tcx , T : ' tcx + ?Sized > Copy for Interned < ' tcx , T > { }
1958
1962
1959
1963
impl < ' tcx , T : ' tcx + ?Sized > IntoPointer for Interned < ' tcx , T > {
1960
1964
fn into_pointer ( & self ) -> * const ( ) {
1961
1965
self . 0 as * const _ as * const ( )
1962
1966
}
1963
1967
}
1964
- // N.B., an `Interned<Ty>` compares and hashes as a `TyKind`.
1968
+
1969
+ #[ allow( rustc:: usage_of_ty_tykind) ]
1970
+ impl < ' tcx > Borrow < TyKind < ' tcx > > for Interned < ' tcx , TyS < ' tcx > > {
1971
+ fn borrow < ' a > ( & ' a self ) -> & ' a TyKind < ' tcx > {
1972
+ & self . 0 . kind ( )
1973
+ }
1974
+ }
1975
+
1965
1976
impl < ' tcx > PartialEq for Interned < ' tcx , TyS < ' tcx > > {
1966
1977
fn eq ( & self , other : & Interned < ' tcx , TyS < ' tcx > > ) -> bool {
1978
+ // The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
1979
+ // `x == y`.
1967
1980
self . 0 . kind ( ) == other. 0 . kind ( )
1968
1981
}
1969
1982
}
@@ -1972,19 +1985,21 @@ impl<'tcx> Eq for Interned<'tcx, TyS<'tcx>> {}
1972
1985
1973
1986
impl < ' tcx > Hash for Interned < ' tcx , TyS < ' tcx > > {
1974
1987
fn hash < H : Hasher > ( & self , s : & mut H ) {
1988
+ // The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
1975
1989
self . 0 . kind ( ) . hash ( s)
1976
1990
}
1977
1991
}
1978
1992
1979
- #[ allow( rustc:: usage_of_ty_tykind) ]
1980
- impl < ' tcx > Borrow < TyKind < ' tcx > > for Interned < ' tcx , TyS < ' tcx > > {
1981
- fn borrow < ' a > ( & ' a self ) -> & ' a TyKind < ' tcx > {
1982
- & self . 0 . kind ( )
1993
+ impl < ' tcx > Borrow < Binder < ' tcx , PredicateKind < ' tcx > > > for Interned < ' tcx , PredicateInner < ' tcx > > {
1994
+ fn borrow < ' a > ( & ' a self ) -> & ' a Binder < ' tcx , PredicateKind < ' tcx > > {
1995
+ & self . 0 . kind
1983
1996
}
1984
1997
}
1985
- // N.B., an `Interned<PredicateInner>` compares and hashes as a `PredicateKind`.
1998
+
1986
1999
impl < ' tcx > PartialEq for Interned < ' tcx , PredicateInner < ' tcx > > {
1987
2000
fn eq ( & self , other : & Interned < ' tcx , PredicateInner < ' tcx > > ) -> bool {
2001
+ // The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
2002
+ // `x == y`.
1988
2003
self . 0 . kind == other. 0 . kind
1989
2004
}
1990
2005
}
@@ -1993,19 +2008,21 @@ impl<'tcx> Eq for Interned<'tcx, PredicateInner<'tcx>> {}
1993
2008
1994
2009
impl < ' tcx > Hash for Interned < ' tcx , PredicateInner < ' tcx > > {
1995
2010
fn hash < H : Hasher > ( & self , s : & mut H ) {
2011
+ // The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
1996
2012
self . 0 . kind . hash ( s)
1997
2013
}
1998
2014
}
1999
2015
2000
- impl < ' tcx > Borrow < Binder < ' tcx , PredicateKind < ' tcx > > > for Interned < ' tcx , PredicateInner < ' tcx > > {
2001
- fn borrow < ' a > ( & ' a self ) -> & ' a Binder < ' tcx , PredicateKind < ' tcx > > {
2002
- & self . 0 . kind
2016
+ impl < ' tcx , T > Borrow < [ T ] > for Interned < ' tcx , List < T > > {
2017
+ fn borrow < ' a > ( & ' a self ) -> & ' a [ T ] {
2018
+ & self . 0 [ .. ]
2003
2019
}
2004
2020
}
2005
2021
2006
- // N.B., an `Interned<List<T>>` compares and hashes as its elements.
2007
2022
impl < ' tcx , T : PartialEq > PartialEq for Interned < ' tcx , List < T > > {
2008
2023
fn eq ( & self , other : & Interned < ' tcx , List < T > > ) -> bool {
2024
+ // The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
2025
+ // `x == y`.
2009
2026
self . 0 [ ..] == other. 0 [ ..]
2010
2027
}
2011
2028
}
@@ -2014,20 +2031,23 @@ impl<'tcx, T: Eq> Eq for Interned<'tcx, List<T>> {}
2014
2031
2015
2032
impl < ' tcx , T : Hash > Hash for Interned < ' tcx , List < T > > {
2016
2033
fn hash < H : Hasher > ( & self , s : & mut H ) {
2034
+ // The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
2017
2035
self . 0 [ ..] . hash ( s)
2018
2036
}
2019
2037
}
2020
2038
2021
- impl < ' tcx , T > Borrow < [ T ] > for Interned < ' tcx , List < T > > {
2022
- fn borrow < ' a > ( & ' a self ) -> & ' a [ T ] {
2023
- & self . 0 [ ..]
2024
- }
2025
- }
2026
-
2027
2039
macro_rules! direct_interners {
2028
2040
( $( $name: ident: $method: ident( $ty: ty) , ) +) => {
2029
- $( impl <' tcx> PartialEq for Interned <' tcx, $ty> {
2041
+ $( impl <' tcx> Borrow <$ty> for Interned <' tcx, $ty> {
2042
+ fn borrow<' a>( & ' a self ) -> & ' a $ty {
2043
+ & self . 0
2044
+ }
2045
+ }
2046
+
2047
+ impl <' tcx> PartialEq for Interned <' tcx, $ty> {
2030
2048
fn eq( & self , other: & Self ) -> bool {
2049
+ // The `Borrow` trait requires that `x.borrow() == y.borrow()`
2050
+ // equals `x == y`.
2031
2051
self . 0 == other. 0
2032
2052
}
2033
2053
}
@@ -2036,16 +2056,12 @@ macro_rules! direct_interners {
2036
2056
2037
2057
impl <' tcx> Hash for Interned <' tcx, $ty> {
2038
2058
fn hash<H : Hasher >( & self , s: & mut H ) {
2059
+ // The `Borrow` trait requires that `x.borrow().hash(s) ==
2060
+ // x.hash(s)`.
2039
2061
self . 0 . hash( s)
2040
2062
}
2041
2063
}
2042
2064
2043
- impl <' tcx> Borrow <$ty> for Interned <' tcx, $ty> {
2044
- fn borrow<' a>( & ' a self ) -> & ' a $ty {
2045
- & self . 0
2046
- }
2047
- }
2048
-
2049
2065
impl <' tcx> TyCtxt <' tcx> {
2050
2066
pub fn $method( self , v: $ty) -> & ' tcx $ty {
2051
2067
self . interners. $name. intern( v, |v| {
0 commit comments