Skip to content

Commit c9ab68c

Browse files
Select upstream monomorphizations in a stable way.
1 parent 476e69f commit c9ab68c

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/librustc_trans/back/symbol_export.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ use std::sync::Arc;
1414
use monomorphize::Instance;
1515
use rustc::hir;
1616
use rustc::hir::TransFnAttrFlags;
17-
use rustc::hir::def_id::CrateNum;
18-
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
17+
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE, CRATE_DEF_INDEX};
18+
use rustc::ich::Fingerprint;
1919
use rustc::middle::exported_symbols::{SymbolExportLevel, ExportedSymbol, metadata_symbol_name};
2020
use rustc::session::config;
2121
use rustc::ty::{TyCtxt, SymbolName};
2222
use rustc::ty::maps::Providers;
2323
use rustc::ty::subst::Substs;
2424
use rustc::util::nodemap::{FxHashMap, DefIdMap};
2525
use rustc_allocator::ALLOCATOR_METHODS;
26+
use rustc_data_structures::indexed_vec::IndexVec;
27+
use syntax::attr;
28+
use std::collections::hash_map::Entry::*;
2629

2730
pub type ExportedSymbols = FxHashMap<
2831
CrateNum,
@@ -268,12 +271,39 @@ fn upstream_monomorphizations_provider<'a, 'tcx>(
268271

269272
let mut instances = DefIdMap();
270273

274+
let cnum_stable_ids: IndexVec<CrateNum, Fingerprint> = {
275+
let mut cnum_stable_ids = IndexVec::from_elem_n(Fingerprint::ZERO,
276+
cnums.len() + 1);
277+
278+
for &cnum in cnums.iter() {
279+
cnum_stable_ids[cnum] = tcx.def_path_hash(DefId {
280+
krate: cnum,
281+
index: CRATE_DEF_INDEX,
282+
}).0;
283+
}
284+
285+
cnum_stable_ids
286+
};
287+
271288
for &cnum in cnums.iter() {
272289
for &(ref exported_symbol, _) in tcx.exported_symbols(cnum).iter() {
273290
if let &ExportedSymbol::Generic(def_id, substs) = exported_symbol {
274-
instances.entry(def_id)
275-
.or_insert_with(|| FxHashMap())
276-
.insert(substs, cnum);
291+
let substs_map = instances.entry(def_id)
292+
.or_insert_with(|| FxHashMap());
293+
294+
match substs_map.entry(substs) {
295+
Occupied(mut e) => {
296+
// If there are multiple monomorphizations available,
297+
// we select one deterministically.
298+
let other_cnum = *e.get();
299+
if cnum_stable_ids[other_cnum] > cnum_stable_ids[cnum] {
300+
e.insert(cnum);
301+
}
302+
}
303+
Vacant(e) => {
304+
e.insert(cnum);
305+
}
306+
}
277307
}
278308
}
279309
}

0 commit comments

Comments
 (0)