Skip to content

Commit 6fdeecf

Browse files
michaelwoeristernikomatsakis
authored andcommitted
CrateStore: Allow for custom def_id_to_string mappings in encode_type().
1 parent 64a13a4 commit 6fdeecf

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

src/librustc/middle/cstore.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,11 @@ pub trait CrateStore<'tcx> : Any {
236236
// utility functions
237237
fn metadata_filename(&self) -> &str;
238238
fn metadata_section_name(&self, target: &Target) -> &str;
239-
fn encode_type(&self, tcx: &TyCtxt<'tcx>, ty: Ty<'tcx>) -> Vec<u8>;
239+
fn encode_type(&self,
240+
tcx: &TyCtxt<'tcx>,
241+
ty: Ty<'tcx>,
242+
def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String)
243+
-> Vec<u8>;
240244
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>;
241245
fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource;
242246
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum>;
@@ -419,8 +423,13 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
419423
// utility functions
420424
fn metadata_filename(&self) -> &str { unimplemented!() }
421425
fn metadata_section_name(&self, target: &Target) -> &str { unimplemented!() }
422-
fn encode_type(&self, tcx: &TyCtxt<'tcx>, ty: Ty<'tcx>) -> Vec<u8>
423-
{ unimplemented!() }
426+
fn encode_type(&self,
427+
tcx: &TyCtxt<'tcx>,
428+
ty: Ty<'tcx>,
429+
def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String)
430+
-> Vec<u8> {
431+
unimplemented!()
432+
}
424433
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>
425434
{ vec![] }
426435
fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource { unimplemented!() }

src/librustc_metadata/csearch.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,13 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
478478
{
479479
loader::meta_section_name(target)
480480
}
481-
fn encode_type(&self, tcx: &TyCtxt<'tcx>, ty: Ty<'tcx>) -> Vec<u8>
481+
fn encode_type(&self,
482+
tcx: &TyCtxt<'tcx>,
483+
ty: Ty<'tcx>,
484+
def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String)
485+
-> Vec<u8>
482486
{
483-
encoder::encoded_ty(tcx, ty)
487+
encoder::encoded_ty(tcx, ty, def_id_to_string)
484488
}
485489

486490
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>

src/librustc_metadata/encoder.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub fn def_to_u64(did: DefId) -> u64 {
143143
(did.krate as u64) << 32 | (did.index.as_usize() as u64)
144144
}
145145

146-
pub fn def_to_string(did: DefId) -> String {
146+
pub fn def_to_string(_tcx: &TyCtxt, did: DefId) -> String {
147147
format!("{}:{}", did.krate, did.index.as_usize())
148148
}
149149

@@ -2078,11 +2078,14 @@ fn encode_metadata_inner(rbml_w: &mut Encoder,
20782078
}
20792079

20802080
// Get the encoded string for a type
2081-
pub fn encoded_ty<'tcx>(tcx: &TyCtxt<'tcx>, t: Ty<'tcx>) -> Vec<u8> {
2081+
pub fn encoded_ty<'tcx>(tcx: &TyCtxt<'tcx>,
2082+
t: Ty<'tcx>,
2083+
def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String)
2084+
-> Vec<u8> {
20822085
let mut wr = Cursor::new(Vec::new());
20832086
tyencode::enc_ty(&mut wr, &tyencode::ctxt {
20842087
diag: tcx.sess.diagnostic(),
2085-
ds: def_to_string,
2088+
ds: def_id_to_string,
20862089
tcx: tcx,
20872090
abbrevs: &RefCell::new(FnvHashMap())
20882091
}, t);

src/librustc_metadata/tyencode.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use encoder;
3737
pub struct ctxt<'a, 'tcx: 'a> {
3838
pub diag: &'a Handler,
3939
// Def -> str Callback:
40-
pub ds: fn(DefId) -> String,
40+
pub ds: fn(&TyCtxt<'tcx>, DefId) -> String,
4141
// The type context.
4242
pub tcx: &'a TyCtxt<'tcx>,
4343
pub abbrevs: &'a abbrev_map<'tcx>
@@ -99,7 +99,7 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx
9999
};
100100
}
101101
ty::TyEnum(def, substs) => {
102-
write!(w, "t[{}|", (cx.ds)(def.did));
102+
write!(w, "t[{}|", (cx.ds)(cx.tcx, def.did));
103103
enc_substs(w, cx, substs);
104104
write!(w, "]");
105105
}
@@ -137,7 +137,7 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx
137137
}
138138
ty::TyFnDef(def_id, substs, f) => {
139139
write!(w, "F");
140-
write!(w, "{}|", (cx.ds)(def_id));
140+
write!(w, "{}|", (cx.ds)(cx.tcx, def_id));
141141
enc_substs(w, cx, substs);
142142
enc_bare_fn_ty(w, cx, f);
143143
}
@@ -152,12 +152,12 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx
152152
write!(w, "p[{}|{}|{}]", idx, space.to_uint(), name);
153153
}
154154
ty::TyStruct(def, substs) => {
155-
write!(w, "a[{}|", (cx.ds)(def.did));
155+
write!(w, "a[{}|", (cx.ds)(cx.tcx, def.did));
156156
enc_substs(w, cx, substs);
157157
write!(w, "]");
158158
}
159159
ty::TyClosure(def, ref substs) => {
160-
write!(w, "k[{}|", (cx.ds)(def));
160+
write!(w, "k[{}|", (cx.ds)(cx.tcx, def));
161161
enc_substs(w, cx, &substs.func_substs);
162162
for ty in &substs.upvar_tys {
163163
enc_ty(w, cx, ty);
@@ -310,7 +310,7 @@ fn enc_bound_region(w: &mut Cursor<Vec<u8>>, cx: &ctxt, br: ty::BoundRegion) {
310310
}
311311
ty::BrNamed(d, name) => {
312312
write!(w, "[{}|{}]",
313-
(cx.ds)(d),
313+
(cx.ds)(cx.tcx, d),
314314
name);
315315
}
316316
ty::BrFresh(id) => {
@@ -324,7 +324,7 @@ fn enc_bound_region(w: &mut Cursor<Vec<u8>>, cx: &ctxt, br: ty::BoundRegion) {
324324

325325
pub fn enc_trait_ref<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
326326
s: ty::TraitRef<'tcx>) {
327-
write!(w, "{}|", (cx.ds)(s.def_id));
327+
write!(w, "{}|", (cx.ds)(cx.tcx, s.def_id));
328328
enc_substs(w, cx, s.substs);
329329
}
330330

@@ -408,16 +408,16 @@ pub fn enc_existential_bounds<'a,'tcx>(w: &mut Cursor<Vec<u8>>,
408408
pub fn enc_type_param_def<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
409409
v: &ty::TypeParameterDef<'tcx>) {
410410
write!(w, "{}:{}|{}|{}|{}|",
411-
v.name, (cx.ds)(v.def_id),
412-
v.space.to_uint(), v.index, (cx.ds)(v.default_def_id));
411+
v.name, (cx.ds)(cx.tcx, v.def_id),
412+
v.space.to_uint(), v.index, (cx.ds)(cx.tcx, v.default_def_id));
413413
enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));
414414
enc_object_lifetime_default(w, cx, v.object_lifetime_default);
415415
}
416416

417417
pub fn enc_region_param_def(w: &mut Cursor<Vec<u8>>, cx: &ctxt,
418418
v: &ty::RegionParameterDef) {
419419
write!(w, "{}:{}|{}|{}|",
420-
v.name, (cx.ds)(v.def_id),
420+
v.name, (cx.ds)(cx.tcx, v.def_id),
421421
v.space.to_uint(), v.index);
422422
for &r in &v.bounds {
423423
write!(w, "R");
@@ -477,7 +477,7 @@ pub fn enc_predicate<'a, 'tcx>(w: &mut Cursor<Vec<u8>>,
477477
enc_ty(w, cx, data);
478478
}
479479
ty::Predicate::ObjectSafe(trait_def_id) => {
480-
write!(w, "O{}|", (cx.ds)(trait_def_id));
480+
write!(w, "O{}|", (cx.ds)(cx.tcx, trait_def_id));
481481
}
482482
}
483483
}

src/librustc_trans/back/link.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use session::search_paths::PathKind;
2222
use session::Session;
2323
use middle::cstore::{self, CrateStore, LinkMeta};
2424
use middle::cstore::{LinkagePreference, NativeLibraryKind};
25+
use middle::def_id::DefId;
2526
use middle::dependency_format::Linkage;
2627
use middle::ty::{Ty, TyCtxt};
2728
use rustc::front::map::DefPath;
@@ -200,6 +201,9 @@ fn truncated_hash_result(symbol_hasher: &mut Sha256) -> String {
200201
output[.. 8].to_hex().to_string()
201202
}
202203

204+
pub fn def_to_string(_tcx: &ty::ctxt, did: DefId) -> String {
205+
format!("{}:{}", did.krate, did.index.as_usize())
206+
}
203207

204208
// This calculates STH for a symbol, as defined above
205209
fn symbol_hash<'tcx>(tcx: &TyCtxt<'tcx>,
@@ -218,7 +222,7 @@ fn symbol_hash<'tcx>(tcx: &TyCtxt<'tcx>,
218222
symbol_hasher.input_str(&meta[..]);
219223
}
220224
symbol_hasher.input_str("-");
221-
symbol_hasher.input(&tcx.sess.cstore.encode_type(tcx, t));
225+
symbol_hasher.input(&tcx.sess.cstore.encode_type(tcx, t, def_to_string));
222226
// Prefix with 'h' so that it never blends into adjacent digits
223227
let mut hash = String::from("h");
224228
hash.push_str(&truncated_hash_result(symbol_hasher));

0 commit comments

Comments
 (0)