diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 0aaba81819b..a7b0ff45b97 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -120,9 +120,9 @@ impl<'tcx, E: TyEncoder<'tcx>> Encodable for Ty<'tcx> { } } -impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::PredicateKind<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Binder> { fn encode(&self, e: &mut E) -> Result<(), E::Error> { - encode_with_shorthand(e, self, TyEncoder::predicate_shorthands) + encode_with_shorthand(e, &self.skip_binder(), TyEncoder::predicate_shorthands) } } @@ -226,18 +226,18 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for Ty<'tcx> { } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::PredicateKind<'tcx> { - fn decode(decoder: &mut D) -> Result, D::Error> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Binder> { + fn decode(decoder: &mut D) -> Result>, D::Error> { // Handle shorthands first, if we have an usize > 0x80. - if decoder.positioned_at_shorthand() { + Ok(ty::Binder::bind(if decoder.positioned_at_shorthand() { let pos = decoder.read_usize()?; assert!(pos >= SHORTHAND_OFFSET); let shorthand = pos - SHORTHAND_OFFSET; - decoder.with_position(shorthand, ty::PredicateKind::decode) + decoder.with_position(shorthand, ty::PredicateKind::decode)? } else { - Ok(ty::PredicateKind::decode(decoder)?) - } + ty::PredicateKind::decode(decoder)? + })) } } @@ -471,3 +471,28 @@ macro_rules! implement_ty_decoder { } } } + +macro_rules! impl_binder_encode_decode { + ($($t:ty),+ $(,)?) => { + $( + impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Binder<$t> { + fn encode(&self, e: &mut E) -> Result<(), E::Error> { + self.as_ref().skip_binder().encode(e) + } + } + impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Binder<$t> { + fn decode(decoder: &mut D) -> Result { + Ok(ty::Binder::bind(Decodable::decode(decoder)?)) + } + } + )* + } +} + +impl_binder_encode_decode! { + &'tcx ty::List>, + ty::FnSig<'tcx>, + ty::ExistentialPredicate<'tcx>, + ty::TraitRef<'tcx>, + Vec>, +} diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 88a2aac010c..099c5aa84e5 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1081,7 +1081,7 @@ impl<'a, 'tcx> HashStable> for Predicate<'tcx> { } } -#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable)] #[derive(HashStable, TypeFoldable)] pub enum PredicateKind<'tcx> { /// Corresponds to `where Foo: Bar`. `Foo` here would be diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index d43c5135d90..cdf443975f4 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -955,7 +955,7 @@ impl<'tcx> PolyExistentialTraitRef<'tcx> { /// erase, or otherwise "discharge" these bound vars, we change the /// type from `Binder` to just `T` (see /// e.g., `liberate_late_bound_regions`). -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)] +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub struct Binder(T); impl Binder {