Skip to content

Commit 01bcddb

Browse files
committed
crates is already deterministic
1 parent 5b9cc20 commit 01bcddb

File tree

3 files changed

+29
-43
lines changed

3 files changed

+29
-43
lines changed

compiler/rustc_metadata/src/dependency_format.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> {
277277
let all_crates_available_as_rlib = tcx
278278
.crates(())
279279
.iter()
280-
.cloned()
280+
.copied()
281281
.filter_map(|cnum| {
282282
if tcx.dep_kind(cnum).macros_only() {
283283
return None;
@@ -291,10 +291,11 @@ fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> {
291291

292292
// All crates are available in an rlib format, so we're just going to link
293293
// everything in explicitly so long as it's actually required.
294-
let last_crate = tcx.crates(()).len();
295-
let mut ret = (1..last_crate + 1)
296-
.map(|cnum| {
297-
if tcx.dep_kind(CrateNum::new(cnum)) == CrateDepKind::Explicit {
294+
let mut ret = tcx
295+
.crates(())
296+
.iter()
297+
.map(|&cnum| {
298+
if tcx.dep_kind(cnum) == CrateDepKind::Explicit {
298299
Linkage::Static
299300
} else {
300301
Linkage::NotLinked

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+20-34
Original file line numberDiff line numberDiff line change
@@ -304,17 +304,7 @@ pub fn provide(providers: &mut Providers) {
304304
// traversal, but not globally minimal across all crates.
305305
let bfs_queue = &mut VecDeque::new();
306306

307-
// Preferring shortest paths alone does not guarantee a
308-
// deterministic result; so sort by crate num to avoid
309-
// hashtable iteration non-determinism. This only makes
310-
// things as deterministic as crate-nums assignment is,
311-
// which is to say, its not deterministic in general. But
312-
// we believe that libstd is consistently assigned crate
313-
// num 1, so it should be enough to resolve #46112.
314-
let mut crates: Vec<CrateNum> = (*tcx.crates(())).to_owned();
315-
crates.sort();
316-
317-
for &cnum in crates.iter() {
307+
for &cnum in tcx.crates(()) {
318308
// Ignore crates without a corresponding local `extern crate` item.
319309
if tcx.missing_extern_crate_item(cnum) {
320310
continue;
@@ -323,35 +313,31 @@ pub fn provide(providers: &mut Providers) {
323313
bfs_queue.push_back(DefId { krate: cnum, index: CRATE_DEF_INDEX });
324314
}
325315

326-
// (restrict scope of mutable-borrow of `visible_parent_map`)
327-
{
328-
let visible_parent_map = &mut visible_parent_map;
329-
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &Export, parent: DefId| {
330-
if child.vis != ty::Visibility::Public {
331-
return;
332-
}
316+
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &Export, parent: DefId| {
317+
if child.vis != ty::Visibility::Public {
318+
return;
319+
}
333320

334-
if let Some(child) = child.res.opt_def_id() {
335-
match visible_parent_map.entry(child) {
336-
Entry::Occupied(mut entry) => {
337-
// If `child` is defined in crate `cnum`, ensure
338-
// that it is mapped to a parent in `cnum`.
339-
if child.is_local() && entry.get().is_local() {
340-
entry.insert(parent);
341-
}
342-
}
343-
Entry::Vacant(entry) => {
321+
if let Some(child) = child.res.opt_def_id() {
322+
match visible_parent_map.entry(child) {
323+
Entry::Occupied(mut entry) => {
324+
// If `child` is defined in crate `cnum`, ensure
325+
// that it is mapped to a parent in `cnum`.
326+
if child.is_local() && entry.get().is_local() {
344327
entry.insert(parent);
345-
bfs_queue.push_back(child);
346328
}
347329
}
330+
Entry::Vacant(entry) => {
331+
entry.insert(parent);
332+
bfs_queue.push_back(child);
333+
}
348334
}
349-
};
335+
}
336+
};
350337

351-
while let Some(def) = bfs_queue.pop_front() {
352-
for child in tcx.item_children(def).iter() {
353-
add_child(bfs_queue, child, def);
354-
}
338+
while let Some(def) = bfs_queue.pop_front() {
339+
for child in tcx.item_children(def).iter() {
340+
add_child(bfs_queue, child, def);
355341
}
356342
}
357343

compiler/rustc_metadata/src/rmeta/encoder.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1708,9 +1708,10 @@ impl EncodeContext<'a, 'tcx> {
17081708

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

1713-
let mut deps = crates
1712+
let deps = self
1713+
.tcx
1714+
.crates(())
17141715
.iter()
17151716
.map(|&cnum| {
17161717
let dep = CrateDep {
@@ -1724,8 +1725,6 @@ impl EncodeContext<'a, 'tcx> {
17241725
})
17251726
.collect::<Vec<_>>();
17261727

1727-
deps.sort_by_key(|&(cnum, _)| cnum);
1728-
17291728
{
17301729
// Sanity-check the crate numbers
17311730
let mut expected_cnum = 1;

0 commit comments

Comments
 (0)