Skip to content

Commit dad194a

Browse files
Allow for representing exported monomorphizations in crate metadata.
1 parent d3b5451 commit dad194a

File tree

8 files changed

+89
-39
lines changed

8 files changed

+89
-39
lines changed

src/librustc/middle/exported_symbols.rs

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
// except according to those terms.
1010

1111
use hir::def_id::{DefId, LOCAL_CRATE};
12+
use ich::StableHashingContext;
13+
use rustc_data_structures::stable_hasher::{StableHasher, HashStable,
14+
StableHasherResult};
1215
use std::cmp;
16+
use std::mem;
1317
use ty;
18+
use ty::subst::Substs;
1419

1520
/// The SymbolExportLevel of a symbols specifies from which kinds of crates
1621
/// the symbol will be exported. `C` symbols will be exported from any
@@ -40,56 +45,89 @@ impl SymbolExportLevel {
4045
}
4146

4247
#[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
43-
pub enum ExportedSymbol {
48+
pub enum ExportedSymbol<'tcx> {
4449
NonGeneric(DefId),
50+
Generic(DefId, &'tcx Substs<'tcx>),
4551
NoDefId(ty::SymbolName),
4652
}
4753

48-
impl ExportedSymbol {
49-
pub fn symbol_name(&self, tcx: ty::TyCtxt) -> ty::SymbolName {
54+
impl<'tcx> ExportedSymbol<'tcx> {
55+
pub fn symbol_name(&self,
56+
tcx: ty::TyCtxt<'_, 'tcx, '_>)
57+
-> ty::SymbolName {
5058
match *self {
5159
ExportedSymbol::NonGeneric(def_id) => {
5260
tcx.symbol_name(ty::Instance::mono(tcx, def_id))
5361
}
62+
ExportedSymbol::Generic(def_id, substs) => {
63+
tcx.symbol_name(ty::Instance::new(def_id, substs))
64+
}
5465
ExportedSymbol::NoDefId(symbol_name) => {
5566
symbol_name
5667
}
5768
}
5869
}
5970

60-
pub fn compare_stable(&self, tcx: ty::TyCtxt, other: &ExportedSymbol) -> cmp::Ordering {
71+
pub fn compare_stable(&self,
72+
tcx: ty::TyCtxt<'_, 'tcx, '_>,
73+
other: &ExportedSymbol<'tcx>)
74+
-> cmp::Ordering {
6175
match *self {
62-
ExportedSymbol::NonGeneric(self_def_id) => {
63-
match *other {
64-
ExportedSymbol::NonGeneric(other_def_id) => {
65-
tcx.def_path_hash(self_def_id).cmp(&tcx.def_path_hash(other_def_id))
66-
}
67-
ExportedSymbol::NoDefId(_) => {
68-
cmp::Ordering::Less
69-
}
76+
ExportedSymbol::NonGeneric(self_def_id) => match *other {
77+
ExportedSymbol::NonGeneric(other_def_id) => {
78+
tcx.def_path_hash(self_def_id).cmp(&tcx.def_path_hash(other_def_id))
79+
}
80+
ExportedSymbol::Generic(..) |
81+
ExportedSymbol::NoDefId(_) => {
82+
cmp::Ordering::Less
83+
}
84+
}
85+
ExportedSymbol::Generic(..) => match *other {
86+
ExportedSymbol::NonGeneric(_) => {
87+
cmp::Ordering::Greater
88+
}
89+
ExportedSymbol::Generic(..) => {
90+
self.symbol_name(tcx).cmp(&other.symbol_name(tcx))
91+
}
92+
ExportedSymbol::NoDefId(_) => {
93+
cmp::Ordering::Less
7094
}
7195
}
72-
ExportedSymbol::NoDefId(self_symbol_name) => {
73-
match *other {
74-
ExportedSymbol::NonGeneric(_) => {
75-
cmp::Ordering::Greater
76-
}
77-
ExportedSymbol::NoDefId(ref other_symbol_name) => {
78-
self_symbol_name.cmp(other_symbol_name)
79-
}
96+
ExportedSymbol::NoDefId(self_symbol_name) => match *other {
97+
ExportedSymbol::NonGeneric(_) |
98+
ExportedSymbol::Generic(..) => {
99+
cmp::Ordering::Greater
100+
}
101+
ExportedSymbol::NoDefId(ref other_symbol_name) => {
102+
self_symbol_name.cmp(other_symbol_name)
80103
}
81104
}
82105
}
83106
}
84107
}
85108

86-
impl_stable_hash_for!(enum self::ExportedSymbol {
87-
NonGeneric(def_id),
88-
NoDefId(symbol_name)
89-
});
90-
91109
pub fn metadata_symbol_name(tcx: ty::TyCtxt) -> String {
92110
format!("rust_metadata_{}_{}",
93111
tcx.original_crate_name(LOCAL_CRATE),
94112
tcx.crate_disambiguator(LOCAL_CRATE).to_fingerprint().to_hex())
95113
}
114+
115+
impl<'gcx> HashStable<StableHashingContext<'gcx>> for ExportedSymbol<'gcx> {
116+
fn hash_stable<W: StableHasherResult>(&self,
117+
hcx: &mut StableHashingContext<'gcx>,
118+
hasher: &mut StableHasher<W>) {
119+
mem::discriminant(self).hash_stable(hcx, hasher);
120+
match *self {
121+
ExportedSymbol::NonGeneric(def_id) => {
122+
def_id.hash_stable(hcx, hasher);
123+
}
124+
ExportedSymbol::Generic(def_id, substs) => {
125+
def_id.hash_stable(hcx, hasher);
126+
substs.hash_stable(hcx, hasher);
127+
}
128+
ExportedSymbol::NoDefId(symbol_name) => {
129+
symbol_name.hash_stable(hcx, hasher);
130+
}
131+
}
132+
}
133+
}

src/librustc/ty/maps/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ define_maps! { <'tcx>
381381
[] fn all_crate_nums: all_crate_nums_node(CrateNum) -> Lrc<Vec<CrateNum>>,
382382

383383
[] fn exported_symbols: ExportedSymbols(CrateNum)
384-
-> Arc<Vec<(ExportedSymbol, SymbolExportLevel)>>,
384+
-> Arc<Vec<(ExportedSymbol<'tcx>, SymbolExportLevel)>>,
385385
[] fn collect_and_partition_translation_items:
386386
collect_and_partition_translation_items_node(CrateNum)
387387
-> (Arc<DefIdSet>, Arc<Vec<Arc<CodegenUnit<'tcx>>>>),

src/librustc_metadata/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
268268
return Arc::new(Vec::new())
269269
}
270270

271-
Arc::new(cdata.exported_symbols())
271+
Arc::new(cdata.exported_symbols(tcx))
272272
}
273273

274274
wasm_custom_sections => { Lrc::new(cdata.wasm_custom_sections()) }

src/librustc_metadata/decoder.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,11 +1065,13 @@ impl<'a, 'tcx> CrateMetadata {
10651065
arg_names.decode(self).collect()
10661066
}
10671067

1068-
pub fn exported_symbols(&self) -> Vec<(ExportedSymbol, SymbolExportLevel)> {
1069-
self.root
1070-
.exported_symbols
1071-
.decode(self)
1072-
.collect()
1068+
pub fn exported_symbols(&self,
1069+
tcx: TyCtxt<'a, 'tcx, 'tcx>)
1070+
-> Vec<(ExportedSymbol<'tcx>, SymbolExportLevel)> {
1071+
let lazy_seq: LazySeq<(ExportedSymbol<'tcx>, SymbolExportLevel)> =
1072+
LazySeq::with_position_and_length(self.root.exported_symbols.position,
1073+
self.root.exported_symbols.len);
1074+
lazy_seq.decode((self, tcx)).collect()
10731075
}
10741076

10751077
pub fn wasm_custom_sections(&self) -> Vec<DefId> {

src/librustc_metadata/encoder.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,13 +1444,12 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
14441444
// definition (as that's not defined in this crate).
14451445
fn encode_exported_symbols(&mut self,
14461446
exported_symbols: &[(ExportedSymbol, SymbolExportLevel)])
1447-
-> LazySeq<(ExportedSymbol, SymbolExportLevel)> {
1448-
1447+
-> EncodedExportedSymbols {
14491448
// The metadata symbol name is special. It should not show up in
14501449
// downstream crates.
14511450
let metadata_symbol_name = SymbolName::new(&metadata_symbol_name(self.tcx));
14521451

1453-
self.lazy_seq(exported_symbols
1452+
let lazy_seq = self.lazy_seq(exported_symbols
14541453
.iter()
14551454
.filter(|&&(ref exported_symbol, _)| {
14561455
match *exported_symbol {
@@ -1460,7 +1459,12 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
14601459
_ => true,
14611460
}
14621461
})
1463-
.cloned())
1462+
.cloned());
1463+
1464+
EncodedExportedSymbols {
1465+
len: lazy_seq.len,
1466+
position: lazy_seq.position,
1467+
}
14641468
}
14651469

14661470
fn encode_wasm_custom_sections(&mut self, statics: &[DefId]) -> LazySeq<DefIndex> {

src/librustc_metadata/schema.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc::hir;
1515
use rustc::hir::def::{self, CtorKind};
1616
use rustc::hir::def_id::{DefIndex, DefId, CrateNum};
1717
use rustc::ich::StableHashingContext;
18-
use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
1918
use rustc::middle::cstore::{DepKind, LinkagePreference, NativeLibrary, ForeignModule};
2019
use rustc::middle::lang_items;
2120
use rustc::mir;
@@ -206,7 +205,7 @@ pub struct CrateRoot {
206205
pub codemap: LazySeq<syntax_pos::FileMap>,
207206
pub def_path_table: Lazy<hir::map::definitions::DefPathTable>,
208207
pub impls: LazySeq<TraitImpls>,
209-
pub exported_symbols: LazySeq<(ExportedSymbol, SymbolExportLevel)>,
208+
pub exported_symbols: EncodedExportedSymbols,
210209
pub wasm_custom_sections: LazySeq<DefIndex>,
211210

212211
pub index: LazySeq<index::Index>,
@@ -531,3 +530,9 @@ impl_stable_hash_for!(struct GeneratorData<'tcx> { layout });
531530
// Tags used for encoding Spans:
532531
pub const TAG_VALID_SPAN: u8 = 0;
533532
pub const TAG_INVALID_SPAN: u8 = 1;
533+
534+
#[derive(RustcEncodable, RustcDecodable)]
535+
pub struct EncodedExportedSymbols {
536+
pub position: usize,
537+
pub len: usize,
538+
}

src/librustc_trans/back/symbol_export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn is_reachable_non_generic_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
9494

9595
fn exported_symbols_provider_local<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
9696
cnum: CrateNum)
97-
-> Arc<Vec<(ExportedSymbol,
97+
-> Arc<Vec<(ExportedSymbol<'tcx>,
9898
SymbolExportLevel)>>
9999
{
100100
assert_eq!(cnum, LOCAL_CRATE);

src/librustc_trans/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#![cfg_attr(stage0, feature(conservative_impl_trait))]
3434
#![feature(optin_builtin_traits)]
3535
#![feature(inclusive_range_fields)]
36+
#![feature(underscore_lifetimes)]
3637

3738
use rustc::dep_graph::WorkProduct;
3839
use syntax_pos::symbol::Symbol;

0 commit comments

Comments
 (0)