@@ -1946,24 +1946,37 @@ impl<'tcx> TyCtxt<'tcx> {
19461946 }
19471947}
19481948
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.
19501953struct Interned < ' tcx , T : ?Sized > ( & ' tcx T ) ;
19511954
19521955impl < ' tcx , T : ' tcx + ?Sized > Clone for Interned < ' tcx , T > {
19531956 fn clone ( & self ) -> Self {
19541957 Interned ( self . 0 )
19551958 }
19561959}
1960+
19571961impl < ' tcx , T : ' tcx + ?Sized > Copy for Interned < ' tcx , T > { }
19581962
19591963impl < ' tcx , T : ' tcx + ?Sized > IntoPointer for Interned < ' tcx , T > {
19601964 fn into_pointer ( & self ) -> * const ( ) {
19611965 self . 0 as * const _ as * const ( )
19621966 }
19631967}
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+
19651976impl < ' tcx > PartialEq for Interned < ' tcx , TyS < ' tcx > > {
19661977 fn eq ( & self , other : & Interned < ' tcx , TyS < ' tcx > > ) -> bool {
1978+ // The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
1979+ // `x == y`.
19671980 self . 0 . kind ( ) == other. 0 . kind ( )
19681981 }
19691982}
@@ -1972,19 +1985,21 @@ impl<'tcx> Eq for Interned<'tcx, TyS<'tcx>> {}
19721985
19731986impl < ' tcx > Hash for Interned < ' tcx , TyS < ' tcx > > {
19741987 fn hash < H : Hasher > ( & self , s : & mut H ) {
1988+ // The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
19751989 self . 0 . kind ( ) . hash ( s)
19761990 }
19771991}
19781992
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
19831996 }
19841997}
1985- // N.B., an `Interned<PredicateInner>` compares and hashes as a `PredicateKind`.
1998+
19861999impl < ' tcx > PartialEq for Interned < ' tcx , PredicateInner < ' tcx > > {
19872000 fn eq ( & self , other : & Interned < ' tcx , PredicateInner < ' tcx > > ) -> bool {
2001+ // The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
2002+ // `x == y`.
19882003 self . 0 . kind == other. 0 . kind
19892004 }
19902005}
@@ -1993,19 +2008,21 @@ impl<'tcx> Eq for Interned<'tcx, PredicateInner<'tcx>> {}
19932008
19942009impl < ' tcx > Hash for Interned < ' tcx , PredicateInner < ' tcx > > {
19952010 fn hash < H : Hasher > ( & self , s : & mut H ) {
2011+ // The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
19962012 self . 0 . kind . hash ( s)
19972013 }
19982014}
19992015
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 [ .. ]
20032019 }
20042020}
20052021
2006- // N.B., an `Interned<List<T>>` compares and hashes as its elements.
20072022impl < ' tcx , T : PartialEq > PartialEq for Interned < ' tcx , List < T > > {
20082023 fn eq ( & self , other : & Interned < ' tcx , List < T > > ) -> bool {
2024+ // The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
2025+ // `x == y`.
20092026 self . 0 [ ..] == other. 0 [ ..]
20102027 }
20112028}
@@ -2014,20 +2031,23 @@ impl<'tcx, T: Eq> Eq for Interned<'tcx, List<T>> {}
20142031
20152032impl < ' tcx , T : Hash > Hash for Interned < ' tcx , List < T > > {
20162033 fn hash < H : Hasher > ( & self , s : & mut H ) {
2034+ // The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
20172035 self . 0 [ ..] . hash ( s)
20182036 }
20192037}
20202038
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-
20272039macro_rules! direct_interners {
20282040 ( $( $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> {
20302048 fn eq( & self , other: & Self ) -> bool {
2049+ // The `Borrow` trait requires that `x.borrow() == y.borrow()`
2050+ // equals `x == y`.
20312051 self . 0 == other. 0
20322052 }
20332053 }
@@ -2036,16 +2056,12 @@ macro_rules! direct_interners {
20362056
20372057 impl <' tcx> Hash for Interned <' tcx, $ty> {
20382058 fn hash<H : Hasher >( & self , s: & mut H ) {
2059+ // The `Borrow` trait requires that `x.borrow().hash(s) ==
2060+ // x.hash(s)`.
20392061 self . 0 . hash( s)
20402062 }
20412063 }
20422064
2043- impl <' tcx> Borrow <$ty> for Interned <' tcx, $ty> {
2044- fn borrow<' a>( & ' a self ) -> & ' a $ty {
2045- & self . 0
2046- }
2047- }
2048-
20492065 impl <' tcx> TyCtxt <' tcx> {
20502066 pub fn $method( self , v: $ty) -> & ' tcx $ty {
20512067 self . interners. $name. intern( v, |v| {
0 commit comments