Skip to content

Commit bcbb410

Browse files
committed
rustc_metadata: side-step ty{en,de}code for everything but Ty.
1 parent 0863012 commit bcbb410

File tree

7 files changed

+99
-380
lines changed

7 files changed

+99
-380
lines changed

src/librustc/ty/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ pub enum IntVarValue {
668668
/// from `T:'a` annotations appearing in the type definition. If
669669
/// this is `None`, then the default is inherited from the
670670
/// surrounding context. See RFC #599 for details.
671-
#[derive(Copy, Clone)]
671+
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
672672
pub enum ObjectLifetimeDefault<'tcx> {
673673
/// Require an explicit annotation. Occurs when multiple
674674
/// `T:'a` constraints are found.
@@ -681,7 +681,7 @@ pub enum ObjectLifetimeDefault<'tcx> {
681681
Specific(&'tcx Region),
682682
}
683683

684-
#[derive(Clone)]
684+
#[derive(Clone, RustcEncodable, RustcDecodable)]
685685
pub struct TypeParameterDef<'tcx> {
686686
pub name: Name,
687687
pub def_id: DefId,
@@ -691,7 +691,7 @@ pub struct TypeParameterDef<'tcx> {
691691
pub object_lifetime_default: ObjectLifetimeDefault<'tcx>,
692692
}
693693

694-
#[derive(Clone)]
694+
#[derive(Clone, RustcEncodable, RustcDecodable)]
695695
pub struct RegionParameterDef<'tcx> {
696696
pub name: Name,
697697
pub def_id: DefId,
@@ -719,7 +719,7 @@ impl<'tcx> RegionParameterDef<'tcx> {
719719

720720
/// Information about the formal type/lifetime parameters associated
721721
/// with an item or method. Analogous to hir::Generics.
722-
#[derive(Clone, Debug)]
722+
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
723723
pub struct Generics<'tcx> {
724724
pub parent: Option<DefId>,
725725
pub parent_regions: u32,
@@ -786,7 +786,7 @@ impl<'a, 'gcx, 'tcx> GenericPredicates<'tcx> {
786786
}
787787
}
788788

789-
#[derive(Clone, PartialEq, Eq, Hash)]
789+
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
790790
pub enum Predicate<'tcx> {
791791
/// Corresponds to `where Foo : Bar<A,B,C>`. `Foo` here would be
792792
/// the `Self` type of the trait reference and `A`, `B`, and `C`
@@ -910,7 +910,7 @@ impl<'a, 'gcx, 'tcx> Predicate<'tcx> {
910910
}
911911
}
912912

913-
#[derive(Clone, PartialEq, Eq, Hash)]
913+
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
914914
pub struct TraitPredicate<'tcx> {
915915
pub trait_ref: TraitRef<'tcx>
916916
}
@@ -967,11 +967,11 @@ impl<'tcx> PolyTraitPredicate<'tcx> {
967967
}
968968
}
969969

970-
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
970+
#[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
971971
pub struct EquatePredicate<'tcx>(pub Ty<'tcx>, pub Ty<'tcx>); // `0 == 1`
972972
pub type PolyEquatePredicate<'tcx> = ty::Binder<EquatePredicate<'tcx>>;
973973

974-
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
974+
#[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
975975
pub struct OutlivesPredicate<A,B>(pub A, pub B); // `A : B`
976976
pub type PolyOutlivesPredicate<A,B> = ty::Binder<OutlivesPredicate<A,B>>;
977977
pub type PolyRegionOutlivesPredicate<'tcx> = PolyOutlivesPredicate<&'tcx ty::Region,
@@ -990,7 +990,7 @@ pub type PolyTypeOutlivesPredicate<'tcx> = PolyOutlivesPredicate<Ty<'tcx>, &'tcx
990990
/// equality between arbitrary types. Processing an instance of Form
991991
/// #2 eventually yields one of these `ProjectionPredicate`
992992
/// instances to normalize the LHS.
993-
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
993+
#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
994994
pub struct ProjectionPredicate<'tcx> {
995995
pub projection_ty: ProjectionTy<'tcx>,
996996
pub ty: Ty<'tcx>,

src/librustc/ty/sty.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ pub struct TraitObject<'tcx> {
290290
/// Note that a `TraitRef` introduces a level of region binding, to
291291
/// account for higher-ranked trait bounds like `T : for<'a> Foo<&'a
292292
/// U>` or higher-ranked object types.
293-
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
293+
#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
294294
pub struct TraitRef<'tcx> {
295295
pub def_id: DefId,
296296
pub substs: &'tcx Substs<'tcx>,
@@ -366,7 +366,7 @@ impl<'tcx> PolyExistentialTraitRef<'tcx> {
366366
/// erase, or otherwise "discharge" these bound regions, we change the
367367
/// type from `Binder<T>` to just `T` (see
368368
/// e.g. `liberate_late_bound_regions`).
369-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
369+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
370370
pub struct Binder<T>(pub T);
371371

372372
impl<T> Binder<T> {
@@ -414,7 +414,7 @@ impl fmt::Debug for TypeFlags {
414414

415415
/// Represents the projection of an associated type. In explicit UFCS
416416
/// form this would be written `<T as Trait<..>>::N`.
417-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
417+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
418418
pub struct ProjectionTy<'tcx> {
419419
/// The trait reference `T as Trait<..>`.
420420
pub trait_ref: ty::TraitRef<'tcx>,
@@ -430,7 +430,7 @@ pub struct BareFnTy<'tcx> {
430430
pub sig: PolyFnSig<'tcx>,
431431
}
432432

433-
#[derive(Clone, PartialEq, Eq, Hash)]
433+
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
434434
pub struct ClosureTy<'tcx> {
435435
pub unsafety: hir::Unsafety,
436436
pub abi: abi::Abi,
@@ -443,7 +443,7 @@ pub struct ClosureTy<'tcx> {
443443
/// - `inputs` is the list of arguments and their modes.
444444
/// - `output` is the return type.
445445
/// - `variadic` indicates whether this is a variadic function. (only true for foreign fns)
446-
#[derive(Clone, PartialEq, Eq, Hash)]
446+
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
447447
pub struct FnSig<'tcx> {
448448
pub inputs: Vec<Ty<'tcx>>,
449449
pub output: Ty<'tcx>,

src/librustc/ty/subst.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use hir::def_id::DefId;
1414
use ty::{self, Ty, TyCtxt};
1515
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
1616

17-
use serialize;
17+
use serialize::{self, Encodable, Encoder, Decodable, Decoder};
1818
use syntax_pos::{Span, DUMMY_SP};
1919

2020
use core::nonzero::NonZero;
@@ -128,8 +128,40 @@ impl<'tcx> TypeFoldable<'tcx> for Kind<'tcx> {
128128
}
129129
}
130130

131+
impl<'tcx> Encodable for Kind<'tcx> {
132+
fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
133+
e.emit_enum("Kind", |e| {
134+
if let Some(ty) = self.as_type() {
135+
e.emit_enum_variant("Ty", TYPE_TAG, 1, |e| {
136+
e.emit_enum_variant_arg(0, |e| ty.encode(e))
137+
})
138+
} else if let Some(r) = self.as_region() {
139+
e.emit_enum_variant("Region", REGION_TAG, 1, |e| {
140+
e.emit_enum_variant_arg(0, |e| r.encode(e))
141+
})
142+
} else {
143+
bug!()
144+
}
145+
})
146+
}
147+
}
148+
149+
impl<'tcx> Decodable for Kind<'tcx> {
150+
fn decode<D: Decoder>(d: &mut D) -> Result<Kind<'tcx>, D::Error> {
151+
d.read_enum("Kind", |d| {
152+
d.read_enum_variant(&["Ty", "Region"], |d, tag| {
153+
match tag {
154+
TYPE_TAG => Ty::decode(d).map(Kind::from),
155+
REGION_TAG => <&ty::Region>::decode(d).map(Kind::from),
156+
_ => Err(d.error("invalid Kind tag"))
157+
}
158+
})
159+
})
160+
}
161+
}
162+
131163
/// A substitution mapping type/region parameters to new values.
132-
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
164+
#[derive(Clone, PartialEq, Eq, Debug, Hash, RustcEncodable, RustcDecodable)]
133165
pub struct Substs<'tcx> {
134166
params: Vec<Kind<'tcx>>
135167
}
@@ -297,7 +329,6 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx Substs<'tcx> {
297329
}
298330
}
299331

300-
impl<'tcx> serialize::UseSpecializedEncodable for &'tcx Substs<'tcx> {}
301332
impl<'tcx> serialize::UseSpecializedDecodable for &'tcx Substs<'tcx> {}
302333

303334
///////////////////////////////////////////////////////////////////////////

src/librustc_metadata/decoder.rs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,6 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
9494
pub fn cdata(&self) -> &'a cstore::CrateMetadata {
9595
self.cdata.expect("missing CrateMetadata in DecodeContext")
9696
}
97-
98-
fn read_ty_encoded<F, R>(&mut self, op: F) -> R
99-
where F: for<'x> FnOnce(&mut TyDecoder<'x,'tcx>) -> R
100-
{
101-
let pos = self.opaque.position();
102-
let doc = rbml::Doc::at(self.opaque.data, pos);
103-
self.opaque.advance(doc.end - pos);
104-
op(&mut TyDecoder::with_doc(self.tcx(), self.cdata().cnum, doc,
105-
&mut |d| translate_def_id(self.cdata(), d)))
106-
}
10797
}
10898

10999
macro_rules! decoder_methods {
@@ -243,13 +233,19 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
243233

244234
impl<'a, 'tcx> SpecializedDecoder<Ty<'tcx>> for DecodeContext<'a, 'tcx> {
245235
fn specialized_decode(&mut self) -> Result<Ty<'tcx>, Self::Error> {
246-
Ok(self.read_ty_encoded(|d| d.parse_ty()))
236+
let pos = self.opaque.position();
237+
let doc = rbml::Doc::at(self.opaque.data, pos);
238+
self.opaque.advance(doc.end - pos);
239+
Ok(TyDecoder::with_doc(self.tcx(), self.cdata().cnum, doc,
240+
&mut |d| translate_def_id(self.cdata(), d))
241+
.parse_ty())
247242
}
248243
}
249244

250245
impl<'a, 'tcx> SpecializedDecoder<&'tcx Substs<'tcx>> for DecodeContext<'a, 'tcx> {
251246
fn specialized_decode(&mut self) -> Result<&'tcx Substs<'tcx>, Self::Error> {
252-
Ok(self.read_ty_encoded(|d| d.parse_substs()))
247+
let substs = Substs::decode(self)?;
248+
Ok(self.tcx().mk_substs(substs))
253249
}
254250
}
255251

@@ -469,26 +465,25 @@ fn variant_disr_val(d: rbml::Doc) -> u64 {
469465
}
470466

471467
fn doc_type<'a, 'tcx>(doc: rbml::Doc, tcx: TyCtxt<'a, 'tcx, 'tcx>, cdata: Cmd) -> Ty<'tcx> {
472-
let tp = reader::get_doc(doc, tag_items_data_item_type);
473-
TyDecoder::with_doc(tcx, cdata.cnum, tp,
474-
&mut |did| translate_def_id(cdata, did))
475-
.parse_ty()
468+
maybe_doc_type(doc, tcx, cdata).expect("missing tag_items_data_item_type")
476469
}
477470

478471
fn maybe_doc_type<'a, 'tcx>(doc: rbml::Doc, tcx: TyCtxt<'a, 'tcx, 'tcx>, cdata: Cmd)
479472
-> Option<Ty<'tcx>> {
480473
reader::maybe_get_doc(doc, tag_items_data_item_type).map(|tp| {
481-
TyDecoder::with_doc(tcx, cdata.cnum, tp,
482-
&mut |did| translate_def_id(cdata, did))
483-
.parse_ty()
474+
let mut dcx = tp.decoder();
475+
dcx.tcx = Some(tcx);
476+
dcx.cdata = Some(cdata);
477+
Decodable::decode(&mut dcx).unwrap()
484478
})
485479
}
486480

487481
fn doc_trait_ref<'a, 'tcx>(doc: rbml::Doc, tcx: TyCtxt<'a, 'tcx, 'tcx>, cdata: Cmd)
488482
-> ty::TraitRef<'tcx> {
489-
TyDecoder::with_doc(tcx, cdata.cnum, doc,
490-
&mut |did| translate_def_id(cdata, did))
491-
.parse_trait_ref()
483+
let mut dcx = doc.decoder();
484+
dcx.tcx = Some(tcx);
485+
dcx.cdata = Some(cdata);
486+
Decodable::decode(&mut dcx).unwrap()
492487
}
493488

494489
fn item_trait_ref<'a, 'tcx>(doc: rbml::Doc, tcx: TyCtxt<'a, 'tcx, 'tcx>, cdata: Cmd)
@@ -1628,10 +1623,10 @@ fn doc_generics<'a, 'tcx>(base_doc: rbml::Doc,
16281623
cdata: Cmd)
16291624
-> &'tcx ty::Generics<'tcx>
16301625
{
1631-
let doc = reader::get_doc(base_doc, tag_item_generics);
1632-
TyDecoder::with_doc(tcx, cdata.cnum, doc,
1633-
&mut |did| translate_def_id(cdata, did))
1634-
.parse_generics()
1626+
let mut dcx = reader::get_doc(base_doc, tag_item_generics).decoder();
1627+
dcx.tcx = Some(tcx);
1628+
dcx.cdata = Some(cdata);
1629+
tcx.alloc_generics(Decodable::decode(&mut dcx).unwrap())
16351630
}
16361631

16371632
fn doc_predicate<'a, 'tcx>(cdata: Cmd,
@@ -1641,10 +1636,14 @@ fn doc_predicate<'a, 'tcx>(cdata: Cmd,
16411636
{
16421637
let predicate_pos = cdata.xref_index.lookup(
16431638
cdata.data(), reader::doc_as_u32(doc)).unwrap() as usize;
1644-
TyDecoder::new(
1645-
cdata.data(), cdata.cnum, predicate_pos, tcx,
1646-
&mut |did| translate_def_id(cdata, did)
1647-
).parse_predicate()
1639+
let mut dcx = rbml::Doc {
1640+
data: cdata.data(),
1641+
start: predicate_pos,
1642+
end: cdata.data().len(),
1643+
}.decoder();
1644+
dcx.tcx = Some(tcx);
1645+
dcx.cdata = Some(cdata);
1646+
Decodable::decode(&mut dcx).unwrap()
16481647
}
16491648

16501649
fn doc_predicates<'a, 'tcx>(base_doc: rbml::Doc,
@@ -1694,8 +1693,10 @@ pub fn closure_ty<'a, 'tcx>(cdata: Cmd, closure_id: DefIndex, tcx: TyCtxt<'a, 't
16941693
-> ty::ClosureTy<'tcx> {
16951694
let closure_doc = cdata.lookup_item(closure_id);
16961695
let closure_ty_doc = reader::get_doc(closure_doc, tag_items_closure_ty);
1697-
TyDecoder::with_doc(tcx, cdata.cnum, closure_ty_doc, &mut |did| translate_def_id(cdata, did))
1698-
.parse_closure_ty()
1696+
let mut dcx = closure_ty_doc.decoder();
1697+
dcx.tcx = Some(tcx);
1698+
dcx.cdata = Some(cdata);
1699+
Decodable::decode(&mut dcx).unwrap()
16991700
}
17001701

17011702
pub fn def_key(cdata: Cmd, id: DefIndex) -> hir_map::DefKey {

0 commit comments

Comments
 (0)