diff --git a/src/librustc_metadata/astencode.rs b/src/librustc_metadata/astencode.rs index ad0e8e1c37623..1bdf34afe8277 100644 --- a/src/librustc_metadata/astencode.rs +++ b/src/librustc_metadata/astencode.rs @@ -53,11 +53,16 @@ use rustc_serialize as serialize; use rustc_serialize::{Decodable, Decoder, DecoderHelpers}; use rustc_serialize::{Encodable, EncoderHelpers}; -#[cfg(test)] use std::io::Cursor; -#[cfg(test)] use syntax::parse; -#[cfg(test)] use syntax::ast::NodeId; -#[cfg(test)] use rustc::hir::print as pprust; -#[cfg(test)] use rustc::hir::lowering::{LoweringContext, DummyResolver}; +#[cfg(test)] +use std::io::Cursor; +#[cfg(test)] +use syntax::parse; +#[cfg(test)] +use syntax::ast::NodeId; +#[cfg(test)] +use rustc::hir::print as pprust; +#[cfg(test)] +use rustc::hir::lowering::{DummyResolver, LoweringContext}; struct DecodeContext<'a, 'b, 'tcx: 'a> { tcx: TyCtxt<'a, 'tcx, 'tcx>, @@ -75,9 +80,7 @@ trait tr { // ______________________________________________________________________ // Top-level methods. -pub fn encode_inlined_item(ecx: &e::EncodeContext, - rbml_w: &mut Encoder, - ii: InlinedItemRef) { +pub fn encode_inlined_item(ecx: &e::EncodeContext, rbml_w: &mut Encoder, ii: InlinedItemRef) { let id = match ii { InlinedItemRef::Item(i) => i.id, InlinedItemRef::Foreign(i) => i.id, @@ -138,7 +141,7 @@ pub fn decode_inlined_item<'a, 'tcx>(cdata: &cstore::crate_metadata, tcx: tcx, from_id_range: from_id_range, to_id_range: to_id_range, - last_filemap_index: Cell::new(0) + last_filemap_index: Cell::new(0), }; let ii = ast_map::map_decoded_item(&dcx.tcx.map, parent_def_path, @@ -149,12 +152,12 @@ pub fn decode_inlined_item<'a, 'tcx>(cdata: &cstore::crate_metadata, InlinedItem::Item(ref i) => i.name, InlinedItem::Foreign(ref i) => i.name, InlinedItem::TraitItem(_, ref ti) => ti.name, - InlinedItem::ImplItem(_, ref ii) => ii.name + InlinedItem::ImplItem(_, ref ii) => ii.name, }; debug!("Fn named: {}", name); debug!("< Decoded inlined fn: {}::{}", - tcx.item_path_str(parent_did), - name); + tcx.item_path_str(parent_did), + name); region::resolve_inlined_item(&tcx.sess, &tcx.region_maps, ii); decode_side_tables(dcx, ast_doc); copy_item_types(dcx, ii, orig_did); @@ -169,14 +172,18 @@ pub fn decode_inlined_item<'a, 'tcx>(cdata: &cstore::crate_metadata, // ______________________________________________________________________ // Enumerating the IDs which appear in an AST -fn reserve_id_range(sess: &Session, - from_id_range: IdRange) -> IdRange { +fn reserve_id_range(sess: &Session, from_id_range: IdRange) -> IdRange { // Handle the case of an empty range: - if from_id_range.empty() { return from_id_range; } + if from_id_range.empty() { + return from_id_range; + } let cnt = from_id_range.max - from_id_range.min; let to_id_min = sess.reserve_node_ids(cnt); let to_id_max = to_id_min + cnt; - IdRange { min: to_id_min, max: to_id_max } + IdRange { + min: to_id_min, + max: to_id_max, + } } impl<'a, 'b, 'tcx> DecodeContext<'a, 'b, 'tcx> { @@ -236,7 +243,7 @@ trait def_id_encoder_helpers { fn emit_def_id(&mut self, did: DefId); } -impl def_id_encoder_helpers for S +impl def_id_encoder_helpers for S where ::Error: Debug { fn emit_def_id(&mut self, did: DefId) { @@ -246,11 +253,10 @@ impl def_id_encoder_helpers for S trait def_id_decoder_helpers { fn read_def_id(&mut self, dcx: &DecodeContext) -> DefId; - fn read_def_id_nodcx(&mut self, - cdata: &cstore::crate_metadata) -> DefId; + fn read_def_id_nodcx(&mut self, cdata: &cstore::crate_metadata) -> DefId; } -impl def_id_decoder_helpers for D +impl def_id_decoder_helpers for D where ::Error: Debug { fn read_def_id(&mut self, dcx: &DecodeContext) -> DefId { @@ -258,9 +264,7 @@ impl def_id_decoder_helpers for D did.tr(dcx) } - fn read_def_id_nodcx(&mut self, - cdata: &cstore::crate_metadata) - -> DefId { + fn read_def_id_nodcx(&mut self, cdata: &cstore::crate_metadata) -> DefId { let did: DefId = Decodable::decode(self).unwrap(); decoder::translate_def_id(cdata, did) } @@ -285,22 +289,24 @@ struct NestedItemsDropper; impl Folder for NestedItemsDropper { fn fold_block(&mut self, blk: P) -> P { blk.and_then(|hir::Block {id, stmts, expr, rules, span, ..}| { - let stmts_sans_items = stmts.into_iter().filter_map(|stmt| { - let use_stmt = match stmt.node { - hir::StmtExpr(_, _) | hir::StmtSemi(_, _) => true, - hir::StmtDecl(ref decl, _) => { - match decl.node { - hir::DeclLocal(_) => true, - hir::DeclItem(_) => false, - } - } - }; - if use_stmt { - Some(stmt) - } else { - None - } - }).collect(); + let stmts_sans_items = stmts.into_iter() + .filter_map(|stmt| { + let use_stmt = match stmt.node { + hir::StmtExpr(_, _) | hir::StmtSemi(_, _) => true, + hir::StmtDecl(ref decl, _) => { + match decl.node { + hir::DeclLocal(_) => true, + hir::DeclItem(_) => false, + } + } + }; + if use_stmt { + Some(stmt) + } else { + None + } + }) + .collect(); let blk_sans_items = P(hir::Block { stmts: stmts_sans_items, expr: expr, @@ -328,9 +334,7 @@ fn simplify_ast(ii: InlinedItemRef) -> InlinedItem { match ii { // HACK we're not dropping items. - InlinedItemRef::Item(i) => { - InlinedItem::Item(P(fold::noop_fold_item(i.clone(), &mut fld))) - } + InlinedItemRef::Item(i) => InlinedItem::Item(P(fold::noop_fold_item(i.clone(), &mut fld))), InlinedItemRef::TraitItem(d, ti) => { InlinedItem::TraitItem(d, P(fold::noop_fold_trait_item(ti.clone(), &mut fld))) } @@ -360,37 +364,38 @@ fn decode_def(dcx: &DecodeContext, dsr: &mut reader::Decoder) -> Def { impl tr for Def { fn tr(&self, dcx: &DecodeContext) -> Def { match *self { - Def::Fn(did) => Def::Fn(did.tr(dcx)), - Def::Method(did) => Def::Method(did.tr(dcx)), - Def::SelfTy(opt_did, impl_id) => { Def::SelfTy(opt_did.map(|did| did.tr(dcx)), - impl_id.map(|id| dcx.tr_id(id))) } - Def::Mod(did) => { Def::Mod(did.tr(dcx)) } - Def::ForeignMod(did) => { Def::ForeignMod(did.tr(dcx)) } - Def::Static(did, m) => { Def::Static(did.tr(dcx), m) } - Def::Const(did) => { Def::Const(did.tr(dcx)) } - Def::AssociatedConst(did) => Def::AssociatedConst(did.tr(dcx)), - Def::Local(_, nid) => { - let nid = dcx.tr_id(nid); - let did = dcx.tcx.map.local_def_id(nid); - Def::Local(did, nid) - } - Def::Variant(e_did, v_did) => Def::Variant(e_did.tr(dcx), v_did.tr(dcx)), - Def::Trait(did) => Def::Trait(did.tr(dcx)), - Def::Enum(did) => Def::Enum(did.tr(dcx)), - Def::TyAlias(did) => Def::TyAlias(did.tr(dcx)), - Def::AssociatedTy(trait_did, did) => - Def::AssociatedTy(trait_did.tr(dcx), did.tr(dcx)), - Def::PrimTy(p) => Def::PrimTy(p), - Def::TyParam(s, index, def_id, n) => Def::TyParam(s, index, def_id.tr(dcx), n), - Def::Upvar(_, nid1, index, nid2) => { - let nid1 = dcx.tr_id(nid1); - let nid2 = dcx.tr_id(nid2); - let did1 = dcx.tcx.map.local_def_id(nid1); - Def::Upvar(did1, nid1, index, nid2) - } - Def::Struct(did) => Def::Struct(did.tr(dcx)), - Def::Label(nid) => Def::Label(dcx.tr_id(nid)), - Def::Err => Def::Err, + Def::Fn(did) => Def::Fn(did.tr(dcx)), + Def::Method(did) => Def::Method(did.tr(dcx)), + Def::SelfTy(opt_did, impl_id) => { + Def::SelfTy(opt_did.map(|did| did.tr(dcx)), + impl_id.map(|id| dcx.tr_id(id))) + } + Def::Mod(did) => Def::Mod(did.tr(dcx)), + Def::ForeignMod(did) => Def::ForeignMod(did.tr(dcx)), + Def::Static(did, m) => Def::Static(did.tr(dcx), m), + Def::Const(did) => Def::Const(did.tr(dcx)), + Def::AssociatedConst(did) => Def::AssociatedConst(did.tr(dcx)), + Def::Local(_, nid) => { + let nid = dcx.tr_id(nid); + let did = dcx.tcx.map.local_def_id(nid); + Def::Local(did, nid) + } + Def::Variant(e_did, v_did) => Def::Variant(e_did.tr(dcx), v_did.tr(dcx)), + Def::Trait(did) => Def::Trait(did.tr(dcx)), + Def::Enum(did) => Def::Enum(did.tr(dcx)), + Def::TyAlias(did) => Def::TyAlias(did.tr(dcx)), + Def::AssociatedTy(trait_did, did) => Def::AssociatedTy(trait_did.tr(dcx), did.tr(dcx)), + Def::PrimTy(p) => Def::PrimTy(p), + Def::TyParam(s, index, def_id, n) => Def::TyParam(s, index, def_id.tr(dcx), n), + Def::Upvar(_, nid1, index, nid2) => { + let nid1 = dcx.tr_id(nid1); + let nid2 = dcx.tr_id(nid2); + let did1 = dcx.tcx.map.local_def_id(nid1); + Def::Upvar(did1, nid1, index, nid2) + } + Def::Struct(did) => Def::Struct(did.tr(dcx)), + Def::Label(nid) => Def::Label(dcx.tr_id(nid)), + Def::Err => Def::Err, } } } @@ -403,14 +408,12 @@ fn encode_freevar_entry(rbml_w: &mut Encoder, fv: &hir::Freevar) { } trait rbml_decoder_helper { - fn read_freevar_entry(&mut self, dcx: &DecodeContext) - -> hir::Freevar; + fn read_freevar_entry(&mut self, dcx: &DecodeContext) -> hir::Freevar; fn read_capture_mode(&mut self) -> hir::CaptureClause; } impl<'a> rbml_decoder_helper for reader::Decoder<'a> { - fn read_freevar_entry(&mut self, dcx: &DecodeContext) - -> hir::Freevar { + fn read_freevar_entry(&mut self, dcx: &DecodeContext) -> hir::Freevar { let fv: hir::Freevar = Decodable::decode(self).unwrap(); fv.tr(dcx) } @@ -434,7 +437,8 @@ impl tr for hir::Freevar { // Encoding and decoding of MethodCallee trait read_method_callee_helper<'tcx> { - fn read_method_callee<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) + fn read_method_callee<'a, 'b>(&mut self, + dcx: &DecodeContext<'a, 'b, 'tcx>) -> (u32, ty::MethodCallee<'tcx>); } @@ -445,40 +449,37 @@ fn encode_method_callee<'a, 'tcx>(ecx: &e::EncodeContext<'a, 'tcx>, use rustc_serialize::Encoder; rbml_w.emit_struct("MethodCallee", 4, |rbml_w| { - rbml_w.emit_struct_field("autoderef", 0, |rbml_w| { - autoderef.encode(rbml_w) - }); - rbml_w.emit_struct_field("def_id", 1, |rbml_w| { - Ok(rbml_w.emit_def_id(method.def_id)) - }); - rbml_w.emit_struct_field("ty", 2, |rbml_w| { - Ok(rbml_w.emit_ty(ecx, method.ty)) - }); - rbml_w.emit_struct_field("substs", 3, |rbml_w| { - Ok(rbml_w.emit_substs(ecx, &method.substs)) - }) - }).unwrap(); + rbml_w.emit_struct_field("autoderef", 0, |rbml_w| autoderef.encode(rbml_w)); + rbml_w.emit_struct_field("def_id", 1, |rbml_w| Ok(rbml_w.emit_def_id(method.def_id))); + rbml_w.emit_struct_field("ty", 2, |rbml_w| Ok(rbml_w.emit_ty(ecx, method.ty))); + rbml_w.emit_struct_field("substs", + 3, + |rbml_w| Ok(rbml_w.emit_substs(ecx, &method.substs))) + }) + .unwrap(); } impl<'a, 'tcx> read_method_callee_helper<'tcx> for reader::Decoder<'a> { - fn read_method_callee<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) + fn read_method_callee<'b, 'c>(&mut self, + dcx: &DecodeContext<'b, 'c, 'tcx>) -> (u32, ty::MethodCallee<'tcx>) { self.read_struct("MethodCallee", 4, |this| { - let autoderef = this.read_struct_field("autoderef", 0, - Decodable::decode).unwrap(); - Ok((autoderef, ty::MethodCallee { - def_id: this.read_struct_field("def_id", 1, |this| { - Ok(this.read_def_id(dcx)) - }).unwrap(), - ty: this.read_struct_field("ty", 2, |this| { - Ok(this.read_ty(dcx)) - }).unwrap(), - substs: this.read_struct_field("substs", 3, |this| { - Ok(dcx.tcx.mk_substs(this.read_substs(dcx))) - }).unwrap() - })) - }).unwrap() + let autoderef = this.read_struct_field("autoderef", 0, Decodable::decode) + .unwrap(); + Ok((autoderef, + ty::MethodCallee { + def_id: this.read_struct_field("def_id", 1, |this| Ok(this.read_def_id(dcx))) + .unwrap(), + ty: this.read_struct_field("ty", 2, |this| Ok(this.read_ty(dcx))) + .unwrap(), + substs: this.read_struct_field("substs", 3, |this| { + Ok(dcx.tcx.mk_substs(this.read_substs(dcx))) + }) + .unwrap(), + })) + }) + .unwrap() } } @@ -493,98 +494,97 @@ trait rbml_writer_helpers<'tcx> { fn emit_region(&mut self, ecx: &e::EncodeContext, r: ty::Region); fn emit_ty<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>, ty: Ty<'tcx>); fn emit_tys<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>, tys: &[Ty<'tcx>]); - fn emit_predicate<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>, + fn emit_predicate<'a>(&mut self, + ecx: &e::EncodeContext<'a, 'tcx>, predicate: &ty::Predicate<'tcx>); - fn emit_trait_ref<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>, - ty: &ty::TraitRef<'tcx>); - fn emit_substs<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>, - substs: &subst::Substs<'tcx>); - fn emit_existential_bounds<'b>(&mut self, ecx: &e::EncodeContext<'b,'tcx>, + fn emit_trait_ref<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>, ty: &ty::TraitRef<'tcx>); + fn emit_substs<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>, substs: &subst::Substs<'tcx>); + fn emit_existential_bounds<'b>(&mut self, + ecx: &e::EncodeContext<'b, 'tcx>, bounds: &ty::ExistentialBounds<'tcx>); fn emit_builtin_bounds(&mut self, ecx: &e::EncodeContext, bounds: &ty::BuiltinBounds); fn emit_upvar_capture(&mut self, ecx: &e::EncodeContext, capture: &ty::UpvarCapture); - fn emit_auto_adjustment<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>, + fn emit_auto_adjustment<'a>(&mut self, + ecx: &e::EncodeContext<'a, 'tcx>, adj: &adjustment::AutoAdjustment<'tcx>); - fn emit_autoref<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>, + fn emit_autoref<'a>(&mut self, + ecx: &e::EncodeContext<'a, 'tcx>, autoref: &adjustment::AutoRef<'tcx>); - fn emit_auto_deref_ref<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>, + fn emit_auto_deref_ref<'a>(&mut self, + ecx: &e::EncodeContext<'a, 'tcx>, auto_deref_ref: &adjustment::AutoDerefRef<'tcx>); } impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> { fn emit_region(&mut self, ecx: &e::EncodeContext, r: ty::Region) { - self.emit_opaque(|this| Ok(tyencode::enc_region(&mut this.cursor, - &ecx.ty_str_ctxt(), - r))); + self.emit_opaque(|this| Ok(tyencode::enc_region(&mut this.cursor, &ecx.ty_str_ctxt(), r))); } fn emit_ty<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>, ty: Ty<'tcx>) { - self.emit_opaque(|this| Ok(tyencode::enc_ty(&mut this.cursor, - &ecx.ty_str_ctxt(), - ty))); + self.emit_opaque(|this| Ok(tyencode::enc_ty(&mut this.cursor, &ecx.ty_str_ctxt(), ty))); } fn emit_tys<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>, tys: &[Ty<'tcx>]) { self.emit_from_vec(tys, |this, ty| Ok(this.emit_ty(ecx, *ty))); } - fn emit_trait_ref<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>, + fn emit_trait_ref<'b>(&mut self, + ecx: &e::EncodeContext<'b, 'tcx>, trait_ref: &ty::TraitRef<'tcx>) { - self.emit_opaque(|this| Ok(tyencode::enc_trait_ref(&mut this.cursor, - &ecx.ty_str_ctxt(), - *trait_ref))); + self.emit_opaque(|this| { + Ok(tyencode::enc_trait_ref(&mut this.cursor, &ecx.ty_str_ctxt(), *trait_ref)) + }); } - fn emit_predicate<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>, + fn emit_predicate<'b>(&mut self, + ecx: &e::EncodeContext<'b, 'tcx>, predicate: &ty::Predicate<'tcx>) { self.emit_opaque(|this| { - Ok(tyencode::enc_predicate(&mut this.cursor, - &ecx.ty_str_ctxt(), - predicate)) + Ok(tyencode::enc_predicate(&mut this.cursor, &ecx.ty_str_ctxt(), predicate)) }); } - fn emit_existential_bounds<'b>(&mut self, ecx: &e::EncodeContext<'b,'tcx>, + fn emit_existential_bounds<'b>(&mut self, + ecx: &e::EncodeContext<'b, 'tcx>, bounds: &ty::ExistentialBounds<'tcx>) { - self.emit_opaque(|this| Ok(tyencode::enc_existential_bounds(&mut this.cursor, - &ecx.ty_str_ctxt(), - bounds))); + self.emit_opaque(|this| { + Ok(tyencode::enc_existential_bounds(&mut this.cursor, &ecx.ty_str_ctxt(), bounds)) + }); } fn emit_builtin_bounds(&mut self, ecx: &e::EncodeContext, bounds: &ty::BuiltinBounds) { - self.emit_opaque(|this| Ok(tyencode::enc_builtin_bounds(&mut this.cursor, - &ecx.ty_str_ctxt(), - bounds))); + self.emit_opaque(|this| { + Ok(tyencode::enc_builtin_bounds(&mut this.cursor, &ecx.ty_str_ctxt(), bounds)) + }); } fn emit_upvar_capture(&mut self, ecx: &e::EncodeContext, capture: &ty::UpvarCapture) { use rustc_serialize::Encoder; self.emit_enum("UpvarCapture", |this| { - match *capture { - ty::UpvarCapture::ByValue => { - this.emit_enum_variant("ByValue", 1, 0, |_| Ok(())) - } - ty::UpvarCapture::ByRef(ty::UpvarBorrow { kind, region }) => { - this.emit_enum_variant("ByRef", 2, 0, |this| { - this.emit_enum_variant_arg(0, - |this| kind.encode(this)); - this.emit_enum_variant_arg(1, - |this| Ok(this.emit_region(ecx, region))) - }) + match *capture { + ty::UpvarCapture::ByValue => { + this.emit_enum_variant("ByValue", 1, 0, |_| Ok(())) + } + ty::UpvarCapture::ByRef(ty::UpvarBorrow { kind, region }) => { + this.emit_enum_variant("ByRef", 2, 0, |this| { + this.emit_enum_variant_arg(0, |this| kind.encode(this)); + this.emit_enum_variant_arg(1, |this| Ok(this.emit_region(ecx, region))) + }) + } } - } - }).unwrap() + }) + .unwrap() } - fn emit_substs<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>, - substs: &subst::Substs<'tcx>) { - self.emit_opaque(|this| Ok(tyencode::enc_substs(&mut this.cursor, - &ecx.ty_str_ctxt(), - substs))); + fn emit_substs<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>, substs: &subst::Substs<'tcx>) { + self.emit_opaque(|this| { + Ok(tyencode::enc_substs(&mut this.cursor, &ecx.ty_str_ctxt(), substs)) + }); } - fn emit_auto_adjustment<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>, + fn emit_auto_adjustment<'b>(&mut self, + ecx: &e::EncodeContext<'b, 'tcx>, adj: &adjustment::AutoAdjustment<'tcx>) { use rustc_serialize::Encoder; @@ -595,28 +595,26 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> { } adjustment::AdjustUnsafeFnPointer => { - this.emit_enum_variant("AdjustUnsafeFnPointer", 2, 0, |_| { - Ok(()) - }) + this.emit_enum_variant("AdjustUnsafeFnPointer", 2, 0, |_| Ok(())) } adjustment::AdjustMutToConstPointer => { - this.emit_enum_variant("AdjustMutToConstPointer", 3, 0, |_| { - Ok(()) - }) + this.emit_enum_variant("AdjustMutToConstPointer", 3, 0, |_| Ok(())) } adjustment::AdjustDerefRef(ref auto_deref_ref) => { this.emit_enum_variant("AdjustDerefRef", 4, 2, |this| { - this.emit_enum_variant_arg(0, - |this| Ok(this.emit_auto_deref_ref(ecx, auto_deref_ref))) + this.emit_enum_variant_arg(0, |this| { + Ok(this.emit_auto_deref_ref(ecx, auto_deref_ref)) + }) }) } } }); } - fn emit_autoref<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>, + fn emit_autoref<'b>(&mut self, + ecx: &e::EncodeContext<'b, 'tcx>, autoref: &adjustment::AutoRef<'tcx>) { use rustc_serialize::Encoder; @@ -624,8 +622,7 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> { match autoref { &adjustment::AutoPtr(r, m) => { this.emit_enum_variant("AutoPtr", 0, 2, |this| { - this.emit_enum_variant_arg(0, - |this| Ok(this.emit_region(ecx, *r))); + this.emit_enum_variant_arg(0, |this| Ok(this.emit_region(ecx, *r))); this.emit_enum_variant_arg(1, |this| m.encode(this)) }) } @@ -638,12 +635,15 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> { }); } - fn emit_auto_deref_ref<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>, + fn emit_auto_deref_ref<'b>(&mut self, + ecx: &e::EncodeContext<'b, 'tcx>, auto_deref_ref: &adjustment::AutoDerefRef<'tcx>) { use rustc_serialize::Encoder; self.emit_struct("AutoDerefRef", 2, |this| { - this.emit_struct_field("autoderefs", 0, |this| auto_deref_ref.autoderefs.encode(this)); + this.emit_struct_field("autoderefs", + 0, + |this| auto_deref_ref.autoderefs.encode(this)); this.emit_struct_field("autoref", 1, |this| { this.emit_option(|this| { @@ -658,9 +658,7 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> { this.emit_option(|this| { match auto_deref_ref.unsize { None => this.emit_option_none(), - Some(target) => this.emit_option_some(|this| { - Ok(this.emit_ty(ecx, target)) - }) + Some(target) => this.emit_option_some(|this| Ok(this.emit_ty(ecx, target))), } }) }) @@ -674,10 +672,8 @@ trait write_tag_and_id { } impl<'a> write_tag_and_id for Encoder<'a> { - fn tag(&mut self, - tag_id: c::astencode_tag, - f: F) where - F: FnOnce(&mut Encoder<'a>), + fn tag(&mut self, tag_id: c::astencode_tag, f: F) + where F: FnOnce(&mut Encoder<'a>) { self.start_tag(tag_id as usize); f(self); @@ -689,32 +685,27 @@ impl<'a> write_tag_and_id for Encoder<'a> { } } -struct SideTableEncodingIdVisitor<'a, 'b:'a, 'c:'a, 'tcx:'c> { +struct SideTableEncodingIdVisitor<'a, 'b: 'a, 'c: 'a, 'tcx: 'c> { ecx: &'a e::EncodeContext<'c, 'tcx>, rbml_w: &'a mut Encoder<'b>, } -impl<'a, 'b, 'c, 'tcx> IdVisitingOperation for - SideTableEncodingIdVisitor<'a, 'b, 'c, 'tcx> { +impl<'a, 'b, 'c, 'tcx> IdVisitingOperation for SideTableEncodingIdVisitor<'a, 'b, 'c, 'tcx> { fn visit_id(&mut self, id: ast::NodeId) { encode_side_tables_for_id(self.ecx, self.rbml_w, id) } } -fn encode_side_tables_for_ii(ecx: &e::EncodeContext, - rbml_w: &mut Encoder, - ii: &InlinedItem) { +fn encode_side_tables_for_ii(ecx: &e::EncodeContext, rbml_w: &mut Encoder, ii: &InlinedItem) { rbml_w.start_tag(c::tag_table as usize); ii.visit_ids(&mut SideTableEncodingIdVisitor { ecx: ecx, - rbml_w: rbml_w + rbml_w: rbml_w, }); rbml_w.end_tag(); } -fn encode_side_tables_for_id(ecx: &e::EncodeContext, - rbml_w: &mut Encoder, - id: ast::NodeId) { +fn encode_side_tables_for_id(ecx: &e::EncodeContext, rbml_w: &mut Encoder, id: ast::NodeId) { let tcx = ecx.tcx; debug!("Encoding side tables for id {}", id); @@ -743,9 +734,8 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext, if let Some(fv) = tcx.freevars.borrow().get(&id) { rbml_w.tag(c::tag_table_freevars, |rbml_w| { rbml_w.id(id); - rbml_w.emit_from_vec(fv, |rbml_w, fv_entry| { - Ok(encode_freevar_entry(rbml_w, fv_entry)) - }); + rbml_w.emit_from_vec(fv, + |rbml_w, fv_entry| Ok(encode_freevar_entry(rbml_w, fv_entry))); }); for freevar in fv { @@ -755,7 +745,7 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext, let var_id = freevar.def.var_id(); let upvar_id = ty::UpvarId { var_id: var_id, - closure_expr_id: id + closure_expr_id: id, }; let upvar_capture = tcx.tables .borrow() @@ -785,8 +775,7 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext, if let Some(method) = tcx.tables.borrow().method_map.get(&method_call) { rbml_w.tag(c::tag_table_method_map, |rbml_w| { rbml_w.id(id); - encode_method_callee(ecx, rbml_w, - method_call.autoderef, method) + encode_method_callee(ecx, rbml_w, method_call.autoderef, method) }) } } @@ -821,94 +810,108 @@ trait doc_decoder_helpers: Sized { } impl<'a> doc_decoder_helpers for rbml::Doc<'a> { - fn as_int(&self) -> isize { reader::doc_as_u64(*self) as isize } + fn as_int(&self) -> isize { + reader::doc_as_u64(*self) as isize + } fn opt_child(&self, tag: c::astencode_tag) -> Option> { reader::maybe_get_doc(*self, tag as usize) } } trait rbml_decoder_decoder_helpers<'tcx> { - fn read_ty_encoded<'a, 'b, F, R>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>, - f: F) -> R + fn read_ty_encoded<'a, 'b, F, R>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>, f: F) -> R where F: for<'x> FnOnce(&mut tydecode::TyDecoder<'x, 'tcx>) -> R; fn read_region(&mut self, dcx: &DecodeContext) -> ty::Region; fn read_ty<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) -> Ty<'tcx>; fn read_tys<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) -> Vec>; - fn read_trait_ref<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) - -> ty::TraitRef<'tcx>; - fn read_poly_trait_ref<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) + fn read_trait_ref<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) -> ty::TraitRef<'tcx>; + fn read_poly_trait_ref<'a, 'b>(&mut self, + dcx: &DecodeContext<'a, 'b, 'tcx>) -> ty::PolyTraitRef<'tcx>; - fn read_predicate<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) - -> ty::Predicate<'tcx>; - fn read_existential_bounds<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) + fn read_predicate<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) -> ty::Predicate<'tcx>; + fn read_existential_bounds<'a, 'b>(&mut self, + dcx: &DecodeContext<'a, 'b, 'tcx>) -> ty::ExistentialBounds<'tcx>; - fn read_substs<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) - -> subst::Substs<'tcx>; - fn read_upvar_capture(&mut self, dcx: &DecodeContext) - -> ty::UpvarCapture; - fn read_auto_adjustment<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) + fn read_substs<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) -> subst::Substs<'tcx>; + fn read_upvar_capture(&mut self, dcx: &DecodeContext) -> ty::UpvarCapture; + fn read_auto_adjustment<'a, 'b>(&mut self, + dcx: &DecodeContext<'a, 'b, 'tcx>) -> adjustment::AutoAdjustment<'tcx>; - fn read_cast_kind<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) - -> cast::CastKind; - fn read_auto_deref_ref<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) + fn read_cast_kind<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) -> cast::CastKind; + fn read_auto_deref_ref<'a, 'b>(&mut self, + dcx: &DecodeContext<'a, 'b, 'tcx>) -> adjustment::AutoDerefRef<'tcx>; - fn read_autoref<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) + fn read_autoref<'a, 'b>(&mut self, + dcx: &DecodeContext<'a, 'b, 'tcx>) -> adjustment::AutoRef<'tcx>; // Versions of the type reading functions that don't need the full // DecodeContext. - fn read_ty_nodcx<'a>(&mut self, tcx: TyCtxt<'a, 'tcx, 'tcx>, - cdata: &cstore::crate_metadata) -> Ty<'tcx>; - fn read_tys_nodcx<'a>(&mut self, tcx: TyCtxt<'a, 'tcx, 'tcx>, - cdata: &cstore::crate_metadata) -> Vec>; - fn read_substs_nodcx<'a>(&mut self, tcx: TyCtxt<'a, 'tcx, 'tcx>, + fn read_ty_nodcx<'a>(&mut self, + tcx: TyCtxt<'a, 'tcx, 'tcx>, + cdata: &cstore::crate_metadata) + -> Ty<'tcx>; + fn read_tys_nodcx<'a>(&mut self, + tcx: TyCtxt<'a, 'tcx, 'tcx>, + cdata: &cstore::crate_metadata) + -> Vec>; + fn read_substs_nodcx<'a>(&mut self, + tcx: TyCtxt<'a, 'tcx, 'tcx>, cdata: &cstore::crate_metadata) -> subst::Substs<'tcx>; } impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> { - fn read_ty_nodcx<'b>(&mut self, tcx: TyCtxt<'b, 'tcx, 'tcx>, + fn read_ty_nodcx<'b>(&mut self, + tcx: TyCtxt<'b, 'tcx, 'tcx>, cdata: &cstore::crate_metadata) -> Ty<'tcx> { self.read_opaque(|_, doc| { - Ok( - tydecode::TyDecoder::with_doc(tcx, cdata.cnum, doc, - &mut |id| decoder::translate_def_id(cdata, id)) - .parse_ty()) - }).unwrap() + Ok(tydecode::TyDecoder::with_doc(tcx, + cdata.cnum, + doc, + &mut |id| decoder::translate_def_id(cdata, id)) + .parse_ty()) + }) + .unwrap() } - fn read_tys_nodcx<'b>(&mut self, tcx: TyCtxt<'b, 'tcx, 'tcx>, - cdata: &cstore::crate_metadata) -> Vec> { - self.read_to_vec(|this| Ok(this.read_ty_nodcx(tcx, cdata)) ) + fn read_tys_nodcx<'b>(&mut self, + tcx: TyCtxt<'b, 'tcx, 'tcx>, + cdata: &cstore::crate_metadata) + -> Vec> { + self.read_to_vec(|this| Ok(this.read_ty_nodcx(tcx, cdata))) .unwrap() .into_iter() .collect() } - fn read_substs_nodcx<'b>(&mut self, tcx: TyCtxt<'b, 'tcx, 'tcx>, + fn read_substs_nodcx<'b>(&mut self, + tcx: TyCtxt<'b, 'tcx, 'tcx>, cdata: &cstore::crate_metadata) - -> subst::Substs<'tcx> - { + -> subst::Substs<'tcx> { self.read_opaque(|_, doc| { - Ok( - tydecode::TyDecoder::with_doc(tcx, cdata.cnum, doc, - &mut |id| decoder::translate_def_id(cdata, id)) - .parse_substs()) - }).unwrap() + Ok(tydecode::TyDecoder::with_doc(tcx, + cdata.cnum, + doc, + &mut |id| decoder::translate_def_id(cdata, id)) + .parse_substs()) + }) + .unwrap() } fn read_ty_encoded<'b, 'c, F, R>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>, op: F) -> R - where F: for<'x> FnOnce(&mut tydecode::TyDecoder<'x,'tcx>) -> R + where F: for<'x> FnOnce(&mut tydecode::TyDecoder<'x, 'tcx>) -> R { return self.read_opaque(|_, doc| { - debug!("read_ty_encoded({})", type_string(doc)); - Ok(op( - &mut tydecode::TyDecoder::with_doc( - dcx.tcx, dcx.cdata.cnum, doc, - &mut |d| convert_def_id(dcx, d)))) - }).unwrap(); + debug!("read_ty_encoded({})", type_string(doc)); + Ok(op(&mut tydecode::TyDecoder::with_doc(dcx.tcx, + dcx.cdata.cnum, + doc, + &mut |d| convert_def_id(dcx, d)))) + }) + .unwrap(); fn type_string(doc: rbml::Doc) -> String { let mut str = String::new(); @@ -929,143 +932,160 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> { return self.read_ty_encoded(dcx, |decoder| decoder.parse_ty()); } - fn read_tys<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) - -> Vec> { + fn read_tys<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) -> Vec> { self.read_to_vec(|this| Ok(this.read_ty(dcx))).unwrap().into_iter().collect() } - fn read_trait_ref<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) - -> ty::TraitRef<'tcx> { + fn read_trait_ref<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) -> ty::TraitRef<'tcx> { self.read_ty_encoded(dcx, |decoder| decoder.parse_trait_ref()) } - fn read_poly_trait_ref<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) + fn read_poly_trait_ref<'b, 'c>(&mut self, + dcx: &DecodeContext<'b, 'c, 'tcx>) -> ty::PolyTraitRef<'tcx> { ty::Binder(self.read_ty_encoded(dcx, |decoder| decoder.parse_trait_ref())) } - fn read_predicate<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) - -> ty::Predicate<'tcx> - { + fn read_predicate<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) -> ty::Predicate<'tcx> { self.read_ty_encoded(dcx, |decoder| decoder.parse_predicate()) } - fn read_existential_bounds<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) - -> ty::ExistentialBounds<'tcx> - { + fn read_existential_bounds<'b, 'c>(&mut self, + dcx: &DecodeContext<'b, 'c, 'tcx>) + -> ty::ExistentialBounds<'tcx> { self.read_ty_encoded(dcx, |decoder| decoder.parse_existential_bounds()) } - fn read_substs<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) - -> subst::Substs<'tcx> { + fn read_substs<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) -> subst::Substs<'tcx> { self.read_opaque(|_, doc| { - Ok(tydecode::TyDecoder::with_doc(dcx.tcx, dcx.cdata.cnum, doc, - &mut |d| convert_def_id(dcx, d)) - .parse_substs()) - }).unwrap() + Ok(tydecode::TyDecoder::with_doc(dcx.tcx, + dcx.cdata.cnum, + doc, + &mut |d| convert_def_id(dcx, d)) + .parse_substs()) + }) + .unwrap() } fn read_upvar_capture(&mut self, dcx: &DecodeContext) -> ty::UpvarCapture { self.read_enum("UpvarCapture", |this| { - let variants = ["ByValue", "ByRef"]; - this.read_enum_variant(&variants, |this, i| { - Ok(match i { - 1 => ty::UpvarCapture::ByValue, - 2 => ty::UpvarCapture::ByRef(ty::UpvarBorrow { - kind: this.read_enum_variant_arg(0, - |this| Decodable::decode(this)).unwrap(), - region: this.read_enum_variant_arg(1, - |this| Ok(this.read_region(dcx))).unwrap() - }), - _ => bug!("bad enum variant for ty::UpvarCapture") + let variants = ["ByValue", "ByRef"]; + this.read_enum_variant(&variants, |this, i| { + Ok(match i { + 1 => ty::UpvarCapture::ByValue, + 2 => { + ty::UpvarCapture::ByRef(ty::UpvarBorrow { + kind: this.read_enum_variant_arg(0, |this| Decodable::decode(this)) + .unwrap(), + region: this.read_enum_variant_arg(1, |this| { + Ok(this.read_region(dcx)) + }) + .unwrap(), + }) + } + _ => bug!("bad enum variant for ty::UpvarCapture"), + }) }) }) - }).unwrap() + .unwrap() } - fn read_auto_adjustment<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) + fn read_auto_adjustment<'b, 'c>(&mut self, + dcx: &DecodeContext<'b, 'c, 'tcx>) -> adjustment::AutoAdjustment<'tcx> { self.read_enum("AutoAdjustment", |this| { - let variants = ["AdjustReifyFnPointer", "AdjustUnsafeFnPointer", - "AdjustMutToConstPointer", "AdjustDerefRef"]; - this.read_enum_variant(&variants, |this, i| { - Ok(match i { - 1 => adjustment::AdjustReifyFnPointer, - 2 => adjustment::AdjustUnsafeFnPointer, - 3 => adjustment::AdjustMutToConstPointer, - 4 => { - let auto_deref_ref: adjustment::AutoDerefRef = - this.read_enum_variant_arg(0, - |this| Ok(this.read_auto_deref_ref(dcx))).unwrap(); - - adjustment::AdjustDerefRef(auto_deref_ref) - } - _ => bug!("bad enum variant for adjustment::AutoAdjustment") + let variants = ["AdjustReifyFnPointer", + "AdjustUnsafeFnPointer", + "AdjustMutToConstPointer", + "AdjustDerefRef"]; + this.read_enum_variant(&variants, |this, i| { + Ok(match i { + 1 => adjustment::AdjustReifyFnPointer, + 2 => adjustment::AdjustUnsafeFnPointer, + 3 => adjustment::AdjustMutToConstPointer, + 4 => { + let auto_deref_ref: adjustment::AutoDerefRef = + this.read_enum_variant_arg(0, |this| { + Ok(this.read_auto_deref_ref(dcx)) + }) + .unwrap(); + + adjustment::AdjustDerefRef(auto_deref_ref) + } + _ => bug!("bad enum variant for adjustment::AutoAdjustment"), + }) }) }) - }).unwrap() + .unwrap() } - fn read_auto_deref_ref<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) + fn read_auto_deref_ref<'b, 'c>(&mut self, + dcx: &DecodeContext<'b, 'c, 'tcx>) -> adjustment::AutoDerefRef<'tcx> { self.read_struct("AutoDerefRef", 2, |this| { - Ok(adjustment::AutoDerefRef { - autoderefs: this.read_struct_field("autoderefs", 0, |this| { - Decodable::decode(this) - }).unwrap(), - autoref: this.read_struct_field("autoref", 1, |this| { - this.read_option(|this, b| { - if b { - Ok(Some(this.read_autoref(dcx))) - } else { - Ok(None) - } - }) - }).unwrap(), - unsize: this.read_struct_field("unsize", 2, |this| { - this.read_option(|this, b| { - if b { - Ok(Some(this.read_ty(dcx))) - } else { - Ok(None) - } - }) - }).unwrap(), + Ok(adjustment::AutoDerefRef { + autoderefs: this.read_struct_field("autoderefs", + 0, + |this| Decodable::decode(this)) + .unwrap(), + autoref: this.read_struct_field("autoref", 1, |this| { + this.read_option(|this, b| { + if b { + Ok(Some(this.read_autoref(dcx))) + } else { + Ok(None) + } + }) + }) + .unwrap(), + unsize: this.read_struct_field("unsize", 2, |this| { + this.read_option(|this, b| { + if b { + Ok(Some(this.read_ty(dcx))) + } else { + Ok(None) + } + }) + }) + .unwrap(), + }) }) - }).unwrap() + .unwrap() } - fn read_autoref<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) + fn read_autoref<'b, 'c>(&mut self, + dcx: &DecodeContext<'b, 'c, 'tcx>) -> adjustment::AutoRef<'tcx> { self.read_enum("AutoRef", |this| { - let variants = ["AutoPtr", "AutoUnsafe"]; - this.read_enum_variant(&variants, |this, i| { - Ok(match i { - 0 => { - let r: ty::Region = - this.read_enum_variant_arg(0, |this| { - Ok(this.read_region(dcx)) - }).unwrap(); - let m: hir::Mutability = - this.read_enum_variant_arg(1, |this| { - Decodable::decode(this) - }).unwrap(); - - adjustment::AutoPtr(dcx.tcx.mk_region(r), m) - } - 1 => { - let m: hir::Mutability = - this.read_enum_variant_arg(0, |this| Decodable::decode(this)).unwrap(); + let variants = ["AutoPtr", "AutoUnsafe"]; + this.read_enum_variant(&variants, |this, i| { + Ok(match i { + 0 => { + let r: ty::Region = this.read_enum_variant_arg(0, |this| { + Ok(this.read_region(dcx)) + }) + .unwrap(); + let m: hir::Mutability = this.read_enum_variant_arg(1, |this| { + Decodable::decode(this) + }) + .unwrap(); + + adjustment::AutoPtr(dcx.tcx.mk_region(r), m) + } + 1 => { + let m: hir::Mutability = this.read_enum_variant_arg(0, |this| { + Decodable::decode(this) + }) + .unwrap(); - adjustment::AutoUnsafe(m) - } - _ => bug!("bad enum variant for adjustment::AutoRef") + adjustment::AutoUnsafe(m) + } + _ => bug!("bad enum variant for adjustment::AutoRef"), + }) }) }) - }).unwrap() + .unwrap() } - fn read_cast_kind<'b, 'c>(&mut self, _dcx: &DecodeContext<'b, 'c, 'tcx>) - -> cast::CastKind - { + fn read_cast_kind<'b, 'c>(&mut self, _dcx: &DecodeContext<'b, 'c, 'tcx>) -> cast::CastKind { Decodable::decode(self).unwrap() } } @@ -1102,16 +1122,13 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> { // // It'd be really nice to refactor the type repr to not include // def-ids so that all these distinctions were unnecessary. -fn convert_def_id(dcx: &DecodeContext, - did: DefId) - -> DefId { +fn convert_def_id(dcx: &DecodeContext, did: DefId) -> DefId { let r = dcx.tr_def_id(did); debug!("convert_def_id(did={:?})={:?}", did, r); return r; } -fn decode_side_tables(dcx: &DecodeContext, - ast_doc: rbml::Doc) { +fn decode_side_tables(dcx: &DecodeContext, ast_doc: rbml::Doc) { let tbl_doc = ast_doc.get(c::tag_table as usize); for (tag, entry_doc) in reader::docs(tbl_doc) { let mut entry_dsr = reader::Decoder::new(entry_doc); @@ -1120,7 +1137,9 @@ fn decode_side_tables(dcx: &DecodeContext, debug!(">> Side table document with tag 0x{:x} \ found for id {} (orig {})", - tag, id, id0); + tag, + id, + id0); let tag = tag as u32; let decoded_tag: Option = c::astencode_tag::from_u32(tag); match decoded_tag { @@ -1133,35 +1152,37 @@ fn decode_side_tables(dcx: &DecodeContext, match value { c::tag_table_def => { let def = decode_def(dcx, val_dsr); - dcx.tcx.def_map.borrow_mut().insert(id, def::PathResolution { - base_def: def, - depth: 0 - }); + dcx.tcx.def_map.borrow_mut().insert(id, + def::PathResolution { + base_def: def, + depth: 0, + }); } c::tag_table_node_type => { let ty = val_dsr.read_ty(dcx); - debug!("inserting ty for node {}: {:?}", - id, ty); + debug!("inserting ty for node {}: {:?}", id, ty); dcx.tcx.node_type_insert(id, ty); } c::tag_table_item_subst => { let item_substs = ty::ItemSubsts { - substs: dcx.tcx.mk_substs(val_dsr.read_substs(dcx)) + substs: dcx.tcx.mk_substs(val_dsr.read_substs(dcx)), }; - dcx.tcx.tables.borrow_mut().item_substs.insert( - id, item_substs); + dcx.tcx.tables.borrow_mut().item_substs.insert(id, item_substs); } c::tag_table_freevars => { let fv_info = val_dsr.read_to_vec(|val_dsr| { - Ok(val_dsr.read_freevar_entry(dcx)) - }).unwrap().into_iter().collect(); + Ok(val_dsr.read_freevar_entry(dcx)) + }) + .unwrap() + .into_iter() + .collect(); dcx.tcx.freevars.borrow_mut().insert(id, fv_info); } c::tag_table_upvar_capture_map => { let var_id: ast::NodeId = Decodable::decode(val_dsr).unwrap(); let upvar_id = ty::UpvarId { var_id: dcx.tr_id(var_id), - closure_expr_id: id + closure_expr_id: id, }; let ub = val_dsr.read_upvar_capture(dcx); dcx.tcx.tables.borrow_mut().upvar_capture_map.insert(upvar_id, ub); @@ -1170,18 +1191,16 @@ fn decode_side_tables(dcx: &DecodeContext, let (autoderef, method) = val_dsr.read_method_callee(dcx); let method_call = ty::MethodCall { expr_id: id, - autoderef: autoderef + autoderef: autoderef, }; dcx.tcx.tables.borrow_mut().method_map.insert(method_call, method); } c::tag_table_adjustments => { - let adj = - val_dsr.read_auto_adjustment(dcx); + let adj = val_dsr.read_auto_adjustment(dcx); dcx.tcx.tables.borrow_mut().adjustments.insert(id, adj); } c::tag_table_cast_kinds => { - let cast_kind = - val_dsr.read_cast_kind(dcx); + let cast_kind = val_dsr.read_cast_kind(dcx); dcx.tcx.cast_kinds.borrow_mut().insert(id, cast_kind); } c::tag_table_const_qualif => { @@ -1202,12 +1221,9 @@ fn decode_side_tables(dcx: &DecodeContext, // copy the tcache entries from the original item to the new // inlined item fn copy_item_types(dcx: &DecodeContext, ii: &InlinedItem, orig_did: DefId) { - fn copy_item_type(dcx: &DecodeContext, - inlined_id: ast::NodeId, - remote_did: DefId) { + fn copy_item_type(dcx: &DecodeContext, inlined_id: ast::NodeId, remote_did: DefId) { let inlined_did = dcx.tcx.map.local_def_id(inlined_id); - dcx.tcx.register_item_type(inlined_did, - dcx.tcx.lookup_item_type(remote_did)); + dcx.tcx.register_item_type(inlined_did, dcx.tcx.lookup_item_type(remote_did)); } // copy the entry for the item itself @@ -1215,7 +1231,7 @@ fn copy_item_types(dcx: &DecodeContext, ii: &InlinedItem, orig_did: DefId) { &InlinedItem::Item(ref i) => i.id, &InlinedItem::TraitItem(_, ref ti) => ti.id, &InlinedItem::ImplItem(_, ref ii) => ii.id, - &InlinedItem::Foreign(ref fi) => fi.id + &InlinedItem::Foreign(ref fi) => fi.id, }; copy_item_type(dcx, item_node_id, orig_did); @@ -1224,20 +1240,20 @@ fn copy_item_types(dcx: &DecodeContext, ii: &InlinedItem, orig_did: DefId) { match item.node { hir::ItemEnum(ref def, _) => { let orig_def = dcx.tcx.lookup_adt_def(orig_did); - for (i_variant, orig_variant) in - def.variants.iter().zip(orig_def.variants.iter()) - { + for (i_variant, orig_variant) in def.variants.iter().zip(orig_def.variants.iter()) { debug!("astencode: copying variant {:?} => {:?}", - orig_variant.did, i_variant.node.data.id()); + orig_variant.did, + i_variant.node.data.id()); copy_item_type(dcx, i_variant.node.data.id(), orig_variant.did); } } hir::ItemStruct(ref def, _) => { if !def.is_struct() { - let ctor_did = dcx.tcx.lookup_adt_def(orig_did) - .struct_variant().did; - debug!("astencode: copying ctor {:?} => {:?}", ctor_did, - def.id()); + let ctor_did = dcx.tcx + .lookup_adt_def(orig_did) + .struct_variant() + .did; + debug!("astencode: copying ctor {:?} => {:?}", ctor_did, def.id()); copy_item_type(dcx, def.id(), ctor_did); } } @@ -1287,14 +1303,18 @@ impl FakeExtCtxt for parse::ParseSess { expn_id: codemap::NO_EXPANSION, } } - fn cfg(&self) -> ast::CrateConfig { Vec::new() } + fn cfg(&self) -> ast::CrateConfig { + Vec::new() + } fn ident_of(&self, st: &str) -> ast::Ident { parse::token::str_to_ident(st) } fn name_of(&self, st: &str) -> ast::Name { parse::token::intern(st) } - fn parse_sess(&self) -> &parse::ParseSess { self } + fn parse_sess(&self) -> &parse::ParseSess { + self + } } #[cfg(test)] @@ -1342,7 +1362,8 @@ fn test_basic() { with_testing_context(|lcx| { roundtrip(lcx.lower_item("e_item!(&cx, fn foo() {} - ).unwrap())); + ) + .unwrap())); }); } @@ -1352,7 +1373,8 @@ fn test_smalltalk() { with_testing_context(|lcx| { roundtrip(lcx.lower_item("e_item!(&cx, fn foo() -> isize { 3 + 4 } // first smalltalk program ever executed. - ).unwrap())); + ) + .unwrap())); }); } @@ -1365,7 +1387,8 @@ fn test_more() { let z = x + y; return z; } - ).unwrap())); + ) + .unwrap())); }); } @@ -1377,7 +1400,8 @@ fn test_simplification() { fn eq_int(a: isize, b: isize) -> bool { a == b } return alist {eq_fn: eq_int, data: Vec::new()}; } - ).unwrap(); + ) + .unwrap(); let cx = mk_ctxt(); with_testing_context(|lcx| { let hir_item = lcx.lower_item(&item); @@ -1387,13 +1411,13 @@ fn test_simplification() { fn new_int_alist() -> alist { return alist {eq_fn: eq_int, data: Vec::new()}; } - ).unwrap()))); + ) + .unwrap()))); match (item_out, item_exp) { (InlinedItem::Item(item_out), InlinedItem::Item(item_exp)) => { - assert!(pprust::item_to_string(&item_out) == - pprust::item_to_string(&item_exp)); + assert!(pprust::item_to_string(&item_out) == pprust::item_to_string(&item_exp)); } - _ => bug!() + _ => bug!(), } }); } diff --git a/src/librustc_metadata/common.rs b/src/librustc_metadata/common.rs index 74f97de265898..aa0c139a23eee 100644 --- a/src/librustc_metadata/common.rs +++ b/src/librustc_metadata/common.rs @@ -91,14 +91,13 @@ pub const tag_disr_val: usize = 0x3c; pub const tag_item_field: usize = 0x41; // GAP 0x42 pub const tag_item_variances: usize = 0x43; -/* - trait items contain tag_item_trait_item elements, - impl items contain tag_item_impl_item elements, and classes - have both. That's because some code treats classes like traits, - and other code treats them like impls. Because classes can contain - both, tag_item_trait_item and tag_item_impl_item have to be two - different tags. - */ +// trait items contain tag_item_trait_item elements, +// impl items contain tag_item_impl_item elements, and classes +// have both. That's because some code treats classes like traits, +// and other code treats them like impls. Because classes can contain +// both, tag_item_trait_item and tag_item_impl_item have to be two +// different tags. +// pub const tag_item_impl_item: usize = 0x44; pub const tag_item_trait_method_explicit_self: usize = 0x45; @@ -245,10 +244,8 @@ pub const tag_items_data_parent_impl: usize = 0xa9; pub const tag_rustc_version: usize = 0x10f; pub fn rustc_version() -> String { - format!( - "rustc {}", - option_env!("CFG_VERSION").unwrap_or("unknown version") - ) + format!("rustc {}", + option_env!("CFG_VERSION").unwrap_or("unknown version")) } pub const tag_panic_strategy: usize = 0x114; diff --git a/src/librustc_metadata/cstore.rs b/src/librustc_metadata/cstore.rs index 2e1bdf21c9a5a..3278794e185ed 100644 --- a/src/librustc_metadata/cstore.rs +++ b/src/librustc_metadata/cstore.rs @@ -21,14 +21,14 @@ use index; use loader; use rustc::dep_graph::DepGraph; -use rustc::hir::def_id::{DefIndex, DefId}; +use rustc::hir::def_id::{DefId, DefIndex}; use rustc::hir::map::DefKey; use rustc::hir::svh::Svh; -use rustc::middle::cstore::{ExternCrate}; +use rustc::middle::cstore::ExternCrate; use rustc::session::config::PanicStrategy; -use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet, DefIdMap}; +use rustc::util::nodemap::{DefIdMap, FnvHashMap, NodeMap, NodeSet}; -use std::cell::{RefCell, Ref, Cell}; +use std::cell::{Cell, Ref, RefCell}; use std::rc::Rc; use std::path::PathBuf; use flate::Bytes; @@ -37,8 +37,8 @@ use syntax::attr; use syntax::codemap; use syntax::parse::token::IdentInterner; -pub use middle::cstore::{NativeLibraryKind, LinkagePreference}; -pub use middle::cstore::{NativeStatic, NativeFramework, NativeUnknown}; +pub use middle::cstore::{LinkagePreference, NativeLibraryKind}; +pub use middle::cstore::{NativeFramework, NativeStatic, NativeUnknown}; pub use middle::cstore::{CrateSource, LinkMeta}; // A map from external crate numbers (as decoded from some crate file) to @@ -60,7 +60,7 @@ pub struct ImportedFileMap { /// The end of this FileMap within the codemap of its original crate pub original_end_pos: codemap::BytePos, /// The imported FileMap's representation within the local codemap - pub translated_filemap: Rc + pub translated_filemap: Rc, } pub struct crate_metadata { @@ -108,8 +108,7 @@ pub struct CStore { } impl CStore { - pub fn new(dep_graph: &DepGraph, - intr: Rc) -> CStore { + pub fn new(dep_graph: &DepGraph, intr: Rc) -> CStore { CStore { dep_graph: dep_graph.clone(), metas: RefCell::new(FnvHashMap()), @@ -140,8 +139,8 @@ impl CStore { self.metas.borrow_mut().insert(cnum, data); } - pub fn iter_crate_data(&self, mut i: I) where - I: FnMut(ast::CrateNum, &Rc), + pub fn iter_crate_data(&self, mut i: I) + where I: FnMut(ast::CrateNum, &Rc) { for (&k, v) in self.metas.borrow().iter() { i(k, v); @@ -149,12 +148,14 @@ impl CStore { } /// Like `iter_crate_data`, but passes source paths (if available) as well. - pub fn iter_crate_data_origins(&self, mut i: I) where - I: FnMut(ast::CrateNum, &crate_metadata, Option), + pub fn iter_crate_data_origins(&self, mut i: I) + where I: FnMut(ast::CrateNum, &crate_metadata, Option) { for (&k, v) in self.metas.borrow().iter() { let origin = self.opt_used_crate_source(k); - origin.as_ref().map(|cs| { assert!(k == cs.cnum); }); + origin.as_ref().map(|cs| { + assert!(k == cs.cnum); + }); i(k, &v, origin); } } @@ -166,10 +167,12 @@ impl CStore { } } - pub fn opt_used_crate_source(&self, cnum: ast::CrateNum) - -> Option { - self.used_crate_sources.borrow_mut() - .iter().find(|source| source.cnum == cnum).cloned() + pub fn opt_used_crate_source(&self, cnum: ast::CrateNum) -> Option { + self.used_crate_sources + .borrow_mut() + .iter() + .find(|source| source.cnum == cnum) + .cloned() } pub fn reset(&self) { @@ -190,12 +193,14 @@ impl CStore { // In order to get this left-to-right dependency ordering, we perform a // topological sort of all crates putting the leaves at the right-most // positions. - pub fn do_get_used_crates(&self, prefer: LinkagePreference) + pub fn do_get_used_crates(&self, + prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option)> { let mut ordering = Vec::new(); - fn visit(cstore: &CStore, cnum: ast::CrateNum, - ordering: &mut Vec) { - if ordering.contains(&cnum) { return } + fn visit(cstore: &CStore, cnum: ast::CrateNum, ordering: &mut Vec) { + if ordering.contains(&cnum) { + return; + } let meta = cstore.get_crate_data(cnum); for (_, &dep) in meta.cnum_map.borrow().iter() { visit(cstore, dep, ordering); @@ -207,13 +212,21 @@ impl CStore { } info!("topological ordering: {:?}", ordering); ordering.reverse(); - let mut libs = self.used_crate_sources.borrow() - .iter() - .map(|src| (src.cnum, match prefer { - LinkagePreference::RequireDynamic => src.dylib.clone().map(|p| p.0), - LinkagePreference::RequireStatic => src.rlib.clone().map(|p| p.0), - })) - .collect::>(); + let mut libs = self.used_crate_sources + .borrow() + .iter() + .map(|src| { + (src.cnum, + match prefer { + LinkagePreference::RequireDynamic => { + src.dylib.clone().map(|p| p.0) + } + LinkagePreference::RequireStatic => { + src.rlib.clone().map(|p| p.0) + } + }) + }) + .collect::>(); libs.sort_by(|&(a, _), &(b, _)| { let a = ordering.iter().position(|x| *x == a); let b = ordering.iter().position(|x| *x == b); @@ -227,9 +240,7 @@ impl CStore { self.used_libraries.borrow_mut().push((lib, kind)); } - pub fn get_used_libraries<'a>(&'a self) - -> &'a RefCell> { + pub fn get_used_libraries<'a>(&'a self) -> &'a RefCell> { &self.used_libraries } @@ -239,13 +250,11 @@ impl CStore { } } - pub fn get_used_link_args<'a>(&'a self) -> &'a RefCell > { + pub fn get_used_link_args<'a>(&'a self) -> &'a RefCell> { &self.used_link_args } - pub fn add_extern_mod_stmt_cnum(&self, - emod_id: ast::NodeId, - cnum: ast::CrateNum) { + pub fn add_extern_mod_stmt_cnum(&self, emod_id: ast::NodeId, cnum: ast::CrateNum) { self.extern_mod_crate_map.borrow_mut().insert(emod_id, cnum); } @@ -257,20 +266,26 @@ impl CStore { self.statically_included_foreign_items.borrow().contains(&id) } - pub fn do_extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option - { + pub fn do_extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option { self.extern_mod_crate_map.borrow().get(&emod_id).cloned() } } impl crate_metadata { - pub fn data<'a>(&'a self) -> &'a [u8] { self.data.as_slice() } - pub fn name(&self) -> &str { decoder::get_crate_name(self.data()) } - pub fn hash(&self) -> Svh { decoder::get_crate_hash(self.data()) } + pub fn data<'a>(&'a self) -> &'a [u8] { + self.data.as_slice() + } + pub fn name(&self) -> &str { + decoder::get_crate_name(self.data()) + } + pub fn hash(&self) -> Svh { + decoder::get_crate_hash(self.data()) + } pub fn disambiguator(&self) -> &str { decoder::get_crate_disambiguator(self.data()) } - pub fn imported_filemaps<'a>(&'a self, codemap: &codemap::CodeMap) + pub fn imported_filemaps<'a>(&'a self, + codemap: &codemap::CodeMap) -> Ref<'a, Vec> { let filemaps = self.codemap_import_info.borrow(); if filemaps.is_empty() { @@ -319,12 +334,11 @@ impl MetadataBlob { if slice.len() < 4 { &[] // corrupt metadata } else { - let len = (((slice[0] as u32) << 24) | - ((slice[1] as u32) << 16) | + let len = (((slice[0] as u32) << 24) | ((slice[1] as u32) << 16) | ((slice[2] as u32) << 8) | ((slice[3] as u32) << 0)) as usize; if len + 4 <= slice.len() { - &slice[4.. len + 4] + &slice[4..len + 4] } else { &[] // corrupt or old metadata } diff --git a/src/librustc_metadata/index.rs b/src/librustc_metadata/index.rs index b850073462f5a..6910983e26849 100644 --- a/src/librustc_metadata/index.rs +++ b/src/librustc_metadata/index.rs @@ -31,7 +31,10 @@ impl Index { /// Given the RBML doc representing the index, save the offests /// for later. pub fn from_rbml(index: rbml::Doc) -> Index { - Index { data_start: index.start, data_end: index.end } + Index { + data_start: index.start, + data_end: index.end, + } } /// Given the metadata, extract out the offset of a particular @@ -41,8 +44,7 @@ impl Index { let words = bytes_to_words(&bytes[self.data_start..self.data_end]); let index = def_index.as_usize(); - debug!("lookup_item: index={:?} words.len={:?}", - index, words.len()); + debug!("lookup_item: index={:?} words.len={:?}", index, words.len()); let position = u32::from_be(words[index]); if position == u32::MAX { @@ -68,9 +70,7 @@ pub struct IndexData { impl IndexData { pub fn new(max_index: usize) -> IndexData { - IndexData { - positions: vec![u32::MAX; max_index] - } + IndexData { positions: vec![u32::MAX; max_index] } } pub fn record(&mut self, def_id: DefId, position: u64) { @@ -86,7 +86,9 @@ impl IndexData { assert!(self.positions[item] == u32::MAX, "recorded position for item {:?} twice, first at {:?} and now at {:?}", - item, self.positions[item], position); + item, + self.positions[item], + position); self.positions[item] = position; } @@ -102,7 +104,7 @@ impl IndexData { /// these be merged?) pub struct DenseIndex { start: usize, - end: usize + end: usize, } impl DenseIndex { @@ -111,10 +113,10 @@ impl DenseIndex { data.get(ix as usize).map(|d| u32::from_be(*d)) } pub fn from_buf(buf: &[u8], start: usize, end: usize) -> Self { - assert!((end-start)%4 == 0 && start <= end && end <= buf.len()); + assert!((end - start) % 4 == 0 && start <= end && end <= buf.len()); DenseIndex { start: start, - end: end + end: end, } } } @@ -131,15 +133,10 @@ pub fn write_dense_index(entries: Vec, buf: &mut Cursor>) { } fn write_be_u32(w: &mut W, u: u32) { - let _ = w.write_all(&[ - (u >> 24) as u8, - (u >> 16) as u8, - (u >> 8) as u8, - (u >> 0) as u8, - ]); + let _ = w.write_all(&[(u >> 24) as u8, (u >> 16) as u8, (u >> 8) as u8, (u >> 0) as u8]); } fn bytes_to_words(b: &[u8]) -> &[u32] { assert!(b.len() % 4 == 0); - unsafe { slice::from_raw_parts(b.as_ptr() as *const u32, b.len()/4) } + unsafe { slice::from_raw_parts(b.as_ptr() as *const u32, b.len() / 4) } }