From 795a6669f83274ce3ada1046e6c9284b8e900a98 Mon Sep 17 00:00:00 2001 From: Augie Fackler Date: Tue, 18 Mar 2025 16:32:09 -0400 Subject: [PATCH 1/3] rustc_resolve: fix instability in lib.rmeta contents rust-lang/rust@23032f31c91f2 accidentally introduced some nondeterminism in the ordering of lib.rmeta files, which we caught in our bazel-based builds only recently due to being further behind than normal. In my testing, this fixes the issue. --- Cargo.lock | 1 + compiler/rustc_resolve/Cargo.toml | 1 + compiler/rustc_resolve/src/rustdoc.rs | 3 ++- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 63a3f5dd03773..67ddac6032b44 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4316,6 +4316,7 @@ name = "rustc_resolve" version = "0.0.0" dependencies = [ "bitflags", + "itertools", "pulldown-cmark 0.11.3", "rustc_arena", "rustc_ast", diff --git a/compiler/rustc_resolve/Cargo.toml b/compiler/rustc_resolve/Cargo.toml index f4771f1af2cfd..9ea9c58cfd17a 100644 --- a/compiler/rustc_resolve/Cargo.toml +++ b/compiler/rustc_resolve/Cargo.toml @@ -6,6 +6,7 @@ edition = "2024" [dependencies] # tidy-alphabetical-start bitflags = "2.4.1" +itertools = "0.12" pulldown-cmark = { version = "0.11", features = ["html"], default-features = false } rustc_arena = { path = "../rustc_arena" } rustc_ast = { path = "../rustc_ast" } diff --git a/compiler/rustc_resolve/src/rustdoc.rs b/compiler/rustc_resolve/src/rustdoc.rs index 52aaab77ebed6..9fda1eb4dc453 100644 --- a/compiler/rustc_resolve/src/rustdoc.rs +++ b/compiler/rustc_resolve/src/rustdoc.rs @@ -1,6 +1,7 @@ use std::mem; use std::ops::Range; +use itertools::Itertools; use pulldown_cmark::{ BrokenLink, BrokenLinkCallback, CowStr, Event, LinkType, Options, Parser, Tag, }; @@ -454,7 +455,7 @@ fn parse_links<'md>(doc: &'md str) -> Vec> { } } - for (label, refdef) in event_iter.reference_definitions().iter() { + for (label, refdef) in event_iter.reference_definitions().iter().sorted_by_key(|x| x.0) { if !refids.contains(label) { links.push(preprocess_link(&refdef.dest)); } From 77eb97d9a983f213cff1df7e02a4a96273653482 Mon Sep 17 00:00:00 2001 From: Augie Fackler Date: Thu, 27 Mar 2025 11:59:04 -0400 Subject: [PATCH 2/3] librustdoc: also stabilize iteration order here --- src/librustdoc/html/markdown.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index b067dbf750e7d..7606128e9e888 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -1726,6 +1726,7 @@ pub(crate) fn markdown_links<'md, R>( md: &'md str, preprocess_link: impl Fn(MarkdownLink) -> Option, ) -> Vec { + use itertools::Itertools; if md.is_empty() { return vec![]; } @@ -1884,7 +1885,7 @@ pub(crate) fn markdown_links<'md, R>( let mut links = Vec::new(); let mut refdefs = FxIndexMap::default(); - for (label, refdef) in event_iter.reference_definitions().iter() { + for (label, refdef) in event_iter.reference_definitions().iter().sorted_by_key(|x| x.0) { refdefs.insert(label.to_string(), (false, refdef.dest.to_string(), refdef.span.clone())); } From 1437dec79942f1367b4cc7756e2f080261c34f3a Mon Sep 17 00:00:00 2001 From: Augie Fackler Date: Thu, 27 Mar 2025 12:39:48 -0400 Subject: [PATCH 3/3] rustc_resolve: prevent iteration of refids for completeness This came up in review, and it should help some future author not introduce non-deterministic output here. --- compiler/rustc_resolve/src/rustdoc.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_resolve/src/rustdoc.rs b/compiler/rustc_resolve/src/rustdoc.rs index 9fda1eb4dc453..a32fe6990160c 100644 --- a/compiler/rustc_resolve/src/rustdoc.rs +++ b/compiler/rustc_resolve/src/rustdoc.rs @@ -8,7 +8,8 @@ use pulldown_cmark::{ use rustc_ast as ast; use rustc_ast::attr::AttributeExt; use rustc_ast::util::comments::beautify_doc_string; -use rustc_data_structures::fx::{FxHashSet, FxIndexMap}; +use rustc_data_structures::fx::FxIndexMap; +use rustc_data_structures::unord::UnordSet; use rustc_middle::ty::TyCtxt; use rustc_span::def_id::DefId; use rustc_span::{DUMMY_SP, InnerSpan, Span, Symbol, kw, sym}; @@ -423,7 +424,7 @@ fn parse_links<'md>(doc: &'md str) -> Vec> { ); let mut links = Vec::new(); - let mut refids = FxHashSet::default(); + let mut refids = UnordSet::default(); while let Some(event) = event_iter.next() { match event {