From 6fdeecf62f413171ad9762bb42b2d794eb4f4967 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Wed, 10 Feb 2016 10:04:45 -0500 Subject: [PATCH] CrateStore: Allow for custom def_id_to_string mappings in encode_type(). --- src/librustc/middle/cstore.rs | 15 ++++++++++++--- src/librustc_metadata/csearch.rs | 8 ++++++-- src/librustc_metadata/encoder.rs | 9 ++++++--- src/librustc_metadata/tyencode.rs | 22 +++++++++++----------- src/librustc_trans/back/link.rs | 6 +++++- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/librustc/middle/cstore.rs b/src/librustc/middle/cstore.rs index f85d8741384..08b87e83a6c 100644 --- a/src/librustc/middle/cstore.rs +++ b/src/librustc/middle/cstore.rs @@ -236,7 +236,11 @@ fn maybe_get_item_mir(&self, tcx: &TyCtxt<'tcx>, def: DefId) // utility functions fn metadata_filename(&self) -> &str; fn metadata_section_name(&self, target: &Target) -> &str; - fn encode_type(&self, tcx: &TyCtxt<'tcx>, ty: Ty<'tcx>) -> Vec; + fn encode_type(&self, + tcx: &TyCtxt<'tcx>, + ty: Ty<'tcx>, + def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String) + -> Vec; fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option)>; fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource; fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option; @@ -419,8 +423,13 @@ fn used_link_args(&self) -> Vec { vec![] } // utility functions fn metadata_filename(&self) -> &str { unimplemented!() } fn metadata_section_name(&self, target: &Target) -> &str { unimplemented!() } - fn encode_type(&self, tcx: &TyCtxt<'tcx>, ty: Ty<'tcx>) -> Vec - { unimplemented!() } + fn encode_type(&self, + tcx: &TyCtxt<'tcx>, + ty: Ty<'tcx>, + def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String) + -> Vec { + unimplemented!() + } fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option)> { vec![] } fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource { unimplemented!() } diff --git a/src/librustc_metadata/csearch.rs b/src/librustc_metadata/csearch.rs index 9ac7216165c..62318f13a8a 100644 --- a/src/librustc_metadata/csearch.rs +++ b/src/librustc_metadata/csearch.rs @@ -478,9 +478,13 @@ fn metadata_section_name(&self, target: &Target) -> &str { loader::meta_section_name(target) } - fn encode_type(&self, tcx: &TyCtxt<'tcx>, ty: Ty<'tcx>) -> Vec + fn encode_type(&self, + tcx: &TyCtxt<'tcx>, + ty: Ty<'tcx>, + def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String) + -> Vec { - encoder::encoded_ty(tcx, ty) + encoder::encoded_ty(tcx, ty, def_id_to_string) } fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option)> diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index e677ea962f9..a082b0f21f1 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -143,7 +143,7 @@ pub fn def_to_u64(did: DefId) -> u64 { (did.krate as u64) << 32 | (did.index.as_usize() as u64) } -pub fn def_to_string(did: DefId) -> String { +pub fn def_to_string(_tcx: &TyCtxt, did: DefId) -> String { format!("{}:{}", did.krate, did.index.as_usize()) } @@ -2078,11 +2078,14 @@ struct Stats { } // Get the encoded string for a type -pub fn encoded_ty<'tcx>(tcx: &TyCtxt<'tcx>, t: Ty<'tcx>) -> Vec { +pub fn encoded_ty<'tcx>(tcx: &TyCtxt<'tcx>, + t: Ty<'tcx>, + def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String) + -> Vec { let mut wr = Cursor::new(Vec::new()); tyencode::enc_ty(&mut wr, &tyencode::ctxt { diag: tcx.sess.diagnostic(), - ds: def_to_string, + ds: def_id_to_string, tcx: tcx, abbrevs: &RefCell::new(FnvHashMap()) }, t); diff --git a/src/librustc_metadata/tyencode.rs b/src/librustc_metadata/tyencode.rs index 4718732c8a0..67e77ba3315 100644 --- a/src/librustc_metadata/tyencode.rs +++ b/src/librustc_metadata/tyencode.rs @@ -37,7 +37,7 @@ pub struct ctxt<'a, 'tcx: 'a> { pub diag: &'a Handler, // Def -> str Callback: - pub ds: fn(DefId) -> String, + pub ds: fn(&TyCtxt<'tcx>, DefId) -> String, // The type context. pub tcx: &'a TyCtxt<'tcx>, pub abbrevs: &'a abbrev_map<'tcx> @@ -99,7 +99,7 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Cursor>, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx }; } ty::TyEnum(def, substs) => { - write!(w, "t[{}|", (cx.ds)(def.did)); + write!(w, "t[{}|", (cx.ds)(cx.tcx, def.did)); enc_substs(w, cx, substs); write!(w, "]"); } @@ -137,7 +137,7 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Cursor>, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx } ty::TyFnDef(def_id, substs, f) => { write!(w, "F"); - write!(w, "{}|", (cx.ds)(def_id)); + write!(w, "{}|", (cx.ds)(cx.tcx, def_id)); enc_substs(w, cx, substs); enc_bare_fn_ty(w, cx, f); } @@ -152,12 +152,12 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Cursor>, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx write!(w, "p[{}|{}|{}]", idx, space.to_uint(), name); } ty::TyStruct(def, substs) => { - write!(w, "a[{}|", (cx.ds)(def.did)); + write!(w, "a[{}|", (cx.ds)(cx.tcx, def.did)); enc_substs(w, cx, substs); write!(w, "]"); } ty::TyClosure(def, ref substs) => { - write!(w, "k[{}|", (cx.ds)(def)); + write!(w, "k[{}|", (cx.ds)(cx.tcx, def)); enc_substs(w, cx, &substs.func_substs); for ty in &substs.upvar_tys { enc_ty(w, cx, ty); @@ -310,7 +310,7 @@ fn enc_bound_region(w: &mut Cursor>, cx: &ctxt, br: ty::BoundRegion) { } ty::BrNamed(d, name) => { write!(w, "[{}|{}]", - (cx.ds)(d), + (cx.ds)(cx.tcx, d), name); } ty::BrFresh(id) => { @@ -324,7 +324,7 @@ fn enc_bound_region(w: &mut Cursor>, cx: &ctxt, br: ty::BoundRegion) { pub fn enc_trait_ref<'a, 'tcx>(w: &mut Cursor>, cx: &ctxt<'a, 'tcx>, s: ty::TraitRef<'tcx>) { - write!(w, "{}|", (cx.ds)(s.def_id)); + write!(w, "{}|", (cx.ds)(cx.tcx, s.def_id)); enc_substs(w, cx, s.substs); } @@ -408,8 +408,8 @@ pub fn enc_existential_bounds<'a,'tcx>(w: &mut Cursor>, pub fn enc_type_param_def<'a, 'tcx>(w: &mut Cursor>, cx: &ctxt<'a, 'tcx>, v: &ty::TypeParameterDef<'tcx>) { write!(w, "{}:{}|{}|{}|{}|", - v.name, (cx.ds)(v.def_id), - v.space.to_uint(), v.index, (cx.ds)(v.default_def_id)); + v.name, (cx.ds)(cx.tcx, v.def_id), + v.space.to_uint(), v.index, (cx.ds)(cx.tcx, v.default_def_id)); enc_opt(w, v.default, |w, t| enc_ty(w, cx, t)); enc_object_lifetime_default(w, cx, v.object_lifetime_default); } @@ -417,7 +417,7 @@ pub fn enc_type_param_def<'a, 'tcx>(w: &mut Cursor>, cx: &ctxt<'a, 'tcx> pub fn enc_region_param_def(w: &mut Cursor>, cx: &ctxt, v: &ty::RegionParameterDef) { write!(w, "{}:{}|{}|{}|", - v.name, (cx.ds)(v.def_id), + v.name, (cx.ds)(cx.tcx, v.def_id), v.space.to_uint(), v.index); for &r in &v.bounds { write!(w, "R"); @@ -477,7 +477,7 @@ pub fn enc_predicate<'a, 'tcx>(w: &mut Cursor>, enc_ty(w, cx, data); } ty::Predicate::ObjectSafe(trait_def_id) => { - write!(w, "O{}|", (cx.ds)(trait_def_id)); + write!(w, "O{}|", (cx.ds)(cx.tcx, trait_def_id)); } } } diff --git a/src/librustc_trans/back/link.rs b/src/librustc_trans/back/link.rs index acb458f8cc6..20fc74caad9 100644 --- a/src/librustc_trans/back/link.rs +++ b/src/librustc_trans/back/link.rs @@ -22,6 +22,7 @@ use session::Session; use middle::cstore::{self, CrateStore, LinkMeta}; use middle::cstore::{LinkagePreference, NativeLibraryKind}; +use middle::def_id::DefId; use middle::dependency_format::Linkage; use middle::ty::{Ty, TyCtxt}; use rustc::front::map::DefPath; @@ -200,6 +201,9 @@ fn truncated_hash_result(symbol_hasher: &mut Sha256) -> String { output[.. 8].to_hex().to_string() } +pub fn def_to_string(_tcx: &ty::ctxt, did: DefId) -> String { + format!("{}:{}", did.krate, did.index.as_usize()) +} // This calculates STH for a symbol, as defined above fn symbol_hash<'tcx>(tcx: &TyCtxt<'tcx>, @@ -218,7 +222,7 @@ fn symbol_hash<'tcx>(tcx: &TyCtxt<'tcx>, symbol_hasher.input_str(&meta[..]); } symbol_hasher.input_str("-"); - symbol_hasher.input(&tcx.sess.cstore.encode_type(tcx, t)); + symbol_hasher.input(&tcx.sess.cstore.encode_type(tcx, t, def_to_string)); // Prefix with 'h' so that it never blends into adjacent digits let mut hash = String::from("h"); hash.push_str(&truncated_hash_result(symbol_hasher)); -- 2.44.0