Skip to content

Commit

Permalink
crates is already deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Sep 21, 2021
1 parent 5b9cc20 commit 01bcddb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 43 deletions.
11 changes: 6 additions & 5 deletions compiler/rustc_metadata/src/dependency_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> {
let all_crates_available_as_rlib = tcx
.crates(())
.iter()
.cloned()
.copied()
.filter_map(|cnum| {
if tcx.dep_kind(cnum).macros_only() {
return None;
Expand All @@ -291,10 +291,11 @@ fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> {

// All crates are available in an rlib format, so we're just going to link
// everything in explicitly so long as it's actually required.
let last_crate = tcx.crates(()).len();
let mut ret = (1..last_crate + 1)
.map(|cnum| {
if tcx.dep_kind(CrateNum::new(cnum)) == CrateDepKind::Explicit {
let mut ret = tcx
.crates(())
.iter()
.map(|&cnum| {
if tcx.dep_kind(cnum) == CrateDepKind::Explicit {
Linkage::Static
} else {
Linkage::NotLinked
Expand Down
54 changes: 20 additions & 34 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,17 +304,7 @@ pub fn provide(providers: &mut Providers) {
// traversal, but not globally minimal across all crates.
let bfs_queue = &mut VecDeque::new();

// Preferring shortest paths alone does not guarantee a
// deterministic result; so sort by crate num to avoid
// hashtable iteration non-determinism. This only makes
// things as deterministic as crate-nums assignment is,
// which is to say, its not deterministic in general. But
// we believe that libstd is consistently assigned crate
// num 1, so it should be enough to resolve #46112.
let mut crates: Vec<CrateNum> = (*tcx.crates(())).to_owned();
crates.sort();

for &cnum in crates.iter() {
for &cnum in tcx.crates(()) {
// Ignore crates without a corresponding local `extern crate` item.
if tcx.missing_extern_crate_item(cnum) {
continue;
Expand All @@ -323,35 +313,31 @@ pub fn provide(providers: &mut Providers) {
bfs_queue.push_back(DefId { krate: cnum, index: CRATE_DEF_INDEX });
}

// (restrict scope of mutable-borrow of `visible_parent_map`)
{
let visible_parent_map = &mut visible_parent_map;
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &Export, parent: DefId| {
if child.vis != ty::Visibility::Public {
return;
}
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &Export, parent: DefId| {
if child.vis != ty::Visibility::Public {
return;
}

if let Some(child) = child.res.opt_def_id() {
match visible_parent_map.entry(child) {
Entry::Occupied(mut entry) => {
// If `child` is defined in crate `cnum`, ensure
// that it is mapped to a parent in `cnum`.
if child.is_local() && entry.get().is_local() {
entry.insert(parent);
}
}
Entry::Vacant(entry) => {
if let Some(child) = child.res.opt_def_id() {
match visible_parent_map.entry(child) {
Entry::Occupied(mut entry) => {
// If `child` is defined in crate `cnum`, ensure
// that it is mapped to a parent in `cnum`.
if child.is_local() && entry.get().is_local() {
entry.insert(parent);
bfs_queue.push_back(child);
}
}
Entry::Vacant(entry) => {
entry.insert(parent);
bfs_queue.push_back(child);
}
}
};
}
};

while let Some(def) = bfs_queue.pop_front() {
for child in tcx.item_children(def).iter() {
add_child(bfs_queue, child, def);
}
while let Some(def) = bfs_queue.pop_front() {
for child in tcx.item_children(def).iter() {
add_child(bfs_queue, child, def);
}
}

Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1708,9 +1708,10 @@ impl EncodeContext<'a, 'tcx> {

fn encode_crate_deps(&mut self) -> Lazy<[CrateDep]> {
empty_proc_macro!(self);
let crates = self.tcx.crates(());

let mut deps = crates
let deps = self
.tcx
.crates(())
.iter()
.map(|&cnum| {
let dep = CrateDep {
Expand All @@ -1724,8 +1725,6 @@ impl EncodeContext<'a, 'tcx> {
})
.collect::<Vec<_>>();

deps.sort_by_key(|&(cnum, _)| cnum);

{
// Sanity-check the crate numbers
let mut expected_cnum = 1;
Expand Down

0 comments on commit 01bcddb

Please sign in to comment.