From 888efe74a3b1f54fcb34e5c8c4f17dd2a381989f Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Fri, 18 Oct 2024 00:00:23 -0700 Subject: [PATCH 1/8] cg_llvm: Switch `llvm::add_global` to `&CStr` --- .../rustc_codegen_llvm/src/coverageinfo/mapgen.rs | 2 +- compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs | 12 +++++++----- compiler/rustc_codegen_llvm/src/llvm/mod.rs | 3 +-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs index c2c261da79b68..4075849323a48 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs @@ -132,7 +132,7 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) { .collect::>(); let initializer = cx.const_array(cx.type_ptr(), &name_globals); - let array = llvm::add_global(cx.llmod, cx.val_ty(initializer), "__llvm_coverage_names"); + let array = llvm::add_global(cx.llmod, cx.val_ty(initializer), c"__llvm_coverage_names"); llvm::set_global_constant(array, true); llvm::set_linkage(array, llvm::Linkage::InternalLinkage); llvm::set_initializer(array, initializer); diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs index d7d29eebf8593..4192dc39de0a4 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs @@ -1,4 +1,5 @@ use std::cell::RefCell; +use std::ffi::CString; use libc::c_uint; use rustc_codegen_ssa::traits::{ @@ -284,10 +285,10 @@ pub(crate) fn save_cov_data_to_mod<'ll, 'tcx>( cx: &CodegenCx<'ll, 'tcx>, cov_data_val: &'ll llvm::Value, ) { - let covmap_var_name = llvm::build_string(|s| unsafe { + let covmap_var_name = CString::new(llvm::build_byte_buffer(|s| unsafe { llvm::LLVMRustCoverageWriteMappingVarNameToString(s); - }) - .expect("Rust Coverage Mapping var name failed UTF-8 conversion"); + })) + .unwrap(); debug!("covmap var name: {:?}", covmap_var_name); let covmap_section_name = llvm::build_string(|s| unsafe { @@ -322,7 +323,8 @@ pub(crate) fn save_func_record_to_mod<'ll, 'tcx>( // of descriptions play distinct roles in LLVM IR; therefore, assign them different names (by // appending "u" to the end of the function record var name, to prevent `linkonce_odr` merging. let func_record_var_name = - format!("__covrec_{:X}{}", func_name_hash, if is_used { "u" } else { "" }); + CString::new(format!("__covrec_{:X}{}", func_name_hash, if is_used { "u" } else { "" })) + .unwrap(); debug!("function record var name: {:?}", func_record_var_name); debug!("function record section name: {:?}", covfun_section_name); @@ -334,7 +336,7 @@ pub(crate) fn save_func_record_to_mod<'ll, 'tcx>( llvm::set_section(llglobal, covfun_section_name); // LLVM's coverage mapping format specifies 8-byte alignment for items in this section. llvm::set_alignment(llglobal, Align::EIGHT); - llvm::set_comdat(cx.llmod, llglobal, &func_record_var_name); + llvm::set_comdat(cx.llmod, llglobal, func_record_var_name.to_str().unwrap()); cx.add_used_global(llglobal); } diff --git a/compiler/rustc_codegen_llvm/src/llvm/mod.rs b/compiler/rustc_codegen_llvm/src/llvm/mod.rs index d0db350a149e8..e790c6696fa38 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/mod.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/mod.rs @@ -217,8 +217,7 @@ pub fn set_section(llglobal: &Value, section_name: &str) { } } -pub fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name: &str) -> &'a Value { - let name_cstr = CString::new(name).expect("unexpected CString error"); +pub fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name_cstr: &CStr) -> &'a Value { unsafe { LLVMAddGlobal(llmod, ty, name_cstr.as_ptr()) } } From ebb842328af5ef19c98cc361e1a27dc8f87ff8cd Mon Sep 17 00:00:00 2001 From: Alona Enraght-Moony Date: Sat, 19 Oct 2024 14:37:11 +0000 Subject: [PATCH 2/8] rustdoc: Extract footnote logic into it's own module. --- src/librustdoc/html/markdown.rs | 84 ++--------------------- src/librustdoc/html/markdown/footnotes.rs | 82 ++++++++++++++++++++++ 2 files changed, 87 insertions(+), 79 deletions(-) create mode 100644 src/librustdoc/html/markdown/footnotes.rs diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 315b7742a4cf8..7826a5d8394a7 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -37,7 +37,7 @@ use std::sync::OnceLock; use pulldown_cmark::{ BrokenLink, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag, TagEnd, html, }; -use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; +use rustc_data_structures::fx::FxHashMap; use rustc_errors::{Diag, DiagMessage}; use rustc_hir::def_id::LocalDefId; use rustc_middle::ty::TyCtxt; @@ -57,6 +57,7 @@ use crate::html::length_limit::HtmlWithLimit; use crate::html::render::small_url_encode; use crate::html::toc::{Toc, TocBuilder}; +mod footnotes; #[cfg(test)] mod tests; @@ -646,81 +647,6 @@ impl<'a, I: Iterator>> Iterator for SummaryLine<'a, I> { } } -/// Moves all footnote definitions to the end and add back links to the -/// references. -struct Footnotes<'a, I> { - inner: I, - footnotes: FxIndexMap>, u16)>, -} - -impl<'a, I> Footnotes<'a, I> { - fn new(iter: I) -> Self { - Footnotes { inner: iter, footnotes: FxIndexMap::default() } - } - - fn get_entry(&mut self, key: &str) -> &mut (Vec>, u16) { - let new_id = self.footnotes.len() + 1; - let key = key.to_owned(); - self.footnotes.entry(key).or_insert((Vec::new(), new_id as u16)) - } -} - -impl<'a, I: Iterator>> Iterator for Footnotes<'a, I> { - type Item = SpannedEvent<'a>; - - fn next(&mut self) -> Option { - loop { - match self.inner.next() { - Some((Event::FootnoteReference(ref reference), range)) => { - let entry = self.get_entry(reference); - let reference = format!( - "{0}", - (*entry).1 - ); - return Some((Event::Html(reference.into()), range)); - } - Some((Event::Start(Tag::FootnoteDefinition(def)), _)) => { - let mut content = Vec::new(); - for (event, _) in &mut self.inner { - if let Event::End(TagEnd::FootnoteDefinition) = event { - break; - } - content.push(event); - } - let entry = self.get_entry(&def); - (*entry).0 = content; - } - Some(e) => return Some(e), - None => { - if !self.footnotes.is_empty() { - let mut v: Vec<_> = self.footnotes.drain(..).map(|(_, x)| x).collect(); - v.sort_by(|a, b| a.1.cmp(&b.1)); - let mut ret = String::from("

    "); - for (mut content, id) in v { - write!(ret, "
  1. ").unwrap(); - let mut is_paragraph = false; - if let Some(&Event::End(TagEnd::Paragraph)) = content.last() { - content.pop(); - is_paragraph = true; - } - html::push_html(&mut ret, content.into_iter()); - write!(ret, " ").unwrap(); - if is_paragraph { - ret.push_str("

    "); - } - ret.push_str("
  2. "); - } - ret.push_str("
"); - return Some((Event::Html(ret.into()), 0..0)); - } else { - return None; - } - } - } - } - } -} - /// A newtype that represents a relative line number in Markdown. /// /// In other words, this represents an offset from the first line of Markdown @@ -1408,7 +1334,7 @@ impl Markdown<'_> { let mut s = String::with_capacity(md.len() * 3 / 2); let p = HeadingLinks::new(p, None, ids, heading_offset); - let p = Footnotes::new(p); + let p = footnotes::Footnotes::new(p); let p = LinkReplacer::new(p.map(|(ev, _)| ev), links); let p = TableWrapper::new(p); let p = CodeBlocks::new(p, codes, edition, playground); @@ -1443,7 +1369,7 @@ impl MarkdownWithToc<'_> { { let p = HeadingLinks::new(p, Some(&mut toc), ids, HeadingOffset::H1); - let p = Footnotes::new(p); + let p = footnotes::Footnotes::new(p); let p = TableWrapper::new(p.map(|(ev, _)| ev)); let p = CodeBlocks::new(p, codes, edition, playground); html::push_html(&mut s, p); @@ -1476,7 +1402,7 @@ impl MarkdownItemInfo<'_> { let mut s = String::with_capacity(md.len() * 3 / 2); let p = HeadingLinks::new(p, None, ids, HeadingOffset::H1); - let p = Footnotes::new(p); + let p = footnotes::Footnotes::new(p); let p = TableWrapper::new(p.map(|(ev, _)| ev)); let p = p.filter(|event| { !matches!(event, Event::Start(Tag::Paragraph) | Event::End(TagEnd::Paragraph)) diff --git a/src/librustdoc/html/markdown/footnotes.rs b/src/librustdoc/html/markdown/footnotes.rs new file mode 100644 index 0000000000000..47e248b891c32 --- /dev/null +++ b/src/librustdoc/html/markdown/footnotes.rs @@ -0,0 +1,82 @@ +//! Markdown footnote handling. +use std::fmt::Write as _; + +use pulldown_cmark::{Event, Tag, TagEnd, html}; +use rustc_data_structures::fx::FxIndexMap; + +use super::SpannedEvent; + +/// Moves all footnote definitions to the end and add back links to the +/// references. +pub(super) struct Footnotes<'a, I> { + inner: I, + footnotes: FxIndexMap>, u16)>, +} + +impl<'a, I> Footnotes<'a, I> { + pub(super) fn new(iter: I) -> Self { + Footnotes { inner: iter, footnotes: FxIndexMap::default() } + } + + fn get_entry(&mut self, key: &str) -> &mut (Vec>, u16) { + let new_id = self.footnotes.len() + 1; + let key = key.to_owned(); + self.footnotes.entry(key).or_insert((Vec::new(), new_id as u16)) + } +} + +impl<'a, I: Iterator>> Iterator for Footnotes<'a, I> { + type Item = SpannedEvent<'a>; + + fn next(&mut self) -> Option { + loop { + match self.inner.next() { + Some((Event::FootnoteReference(ref reference), range)) => { + let entry = self.get_entry(reference); + let reference = format!( + "{0}", + (*entry).1 + ); + return Some((Event::Html(reference.into()), range)); + } + Some((Event::Start(Tag::FootnoteDefinition(def)), _)) => { + let mut content = Vec::new(); + for (event, _) in &mut self.inner { + if let Event::End(TagEnd::FootnoteDefinition) = event { + break; + } + content.push(event); + } + let entry = self.get_entry(&def); + (*entry).0 = content; + } + Some(e) => return Some(e), + None => { + if !self.footnotes.is_empty() { + let mut v: Vec<_> = self.footnotes.drain(..).map(|(_, x)| x).collect(); + v.sort_by(|a, b| a.1.cmp(&b.1)); + let mut ret = String::from("

    "); + for (mut content, id) in v { + write!(ret, "
  1. ").unwrap(); + let mut is_paragraph = false; + if let Some(&Event::End(TagEnd::Paragraph)) = content.last() { + content.pop(); + is_paragraph = true; + } + html::push_html(&mut ret, content.into_iter()); + write!(ret, " ").unwrap(); + if is_paragraph { + ret.push_str("

    "); + } + ret.push_str("
  2. "); + } + ret.push_str("
"); + return Some((Event::Html(ret.into()), 0..0)); + } else { + return None; + } + } + } + } + } +} From 45d61b0d2654373405e2a8d44289e883847873e4 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Fri, 18 Oct 2024 00:31:28 -0700 Subject: [PATCH 3/8] cg_llvm: Reuse LLVM-C Comdat support Migrate `llvm::set_comdat` and `llvm::SetUniqueComdat` to LLVM-C FFI. Note, now we can call `llvm::set_comdat` only when the target actually supports adding comdat. As this has no convenient LLVM-C API, we implement this as `TargetOptions::supports_comdat`. Co-authored-by: Stuart Cook --- .../rustc_codegen_llvm/src/coverageinfo/mod.rs | 5 ++++- compiler/rustc_codegen_llvm/src/intrinsic.rs | 4 +++- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 5 ++++- compiler/rustc_codegen_llvm/src/llvm/mod.rs | 17 +++++++++++------ compiler/rustc_codegen_llvm/src/mono_item.rs | 4 +++- compiler/rustc_target/src/spec/mod.rs | 7 +++++++ 6 files changed, 32 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs index 4192dc39de0a4..4f93e6ab1e52b 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs @@ -13,6 +13,7 @@ use rustc_middle::mir::coverage::CoverageKind; use rustc_middle::ty::Instance; use rustc_middle::ty::layout::HasTyCtxt; use rustc_target::abi::{Align, Size}; +use rustc_target::spec::HasTargetSpec; use tracing::{debug, instrument}; use crate::builder::Builder; @@ -336,7 +337,9 @@ pub(crate) fn save_func_record_to_mod<'ll, 'tcx>( llvm::set_section(llglobal, covfun_section_name); // LLVM's coverage mapping format specifies 8-byte alignment for items in this section. llvm::set_alignment(llglobal, Align::EIGHT); - llvm::set_comdat(cx.llmod, llglobal, func_record_var_name.to_str().unwrap()); + if cx.target_spec().supports_comdat() { + llvm::set_comdat(cx.llmod, llglobal, &func_record_var_name); + } cx.add_used_global(llglobal); } diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index bfe623e7fc354..64f1d21b43862 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -787,7 +787,9 @@ fn codegen_msvc_try<'ll>( let tydesc = bx.declare_global("__rust_panic_type_info", bx.val_ty(type_info)); unsafe { llvm::LLVMRustSetLinkage(tydesc, llvm::Linkage::LinkOnceODRLinkage); - llvm::SetUniqueComdat(bx.llmod, tydesc); + if bx.cx.tcx.sess.target.supports_comdat() { + llvm::SetUniqueComdat(bx.llmod, tydesc); + } llvm::LLVMSetInitializer(tydesc, type_info); } diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 661debbb9f126..d0034de06c726 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -646,6 +646,7 @@ unsafe extern "C" { pub type Attribute; pub type Metadata; pub type BasicBlock; + pub type Comdat; } #[repr(C)] pub struct Builder<'a>(InvariantOpaque<'a>); @@ -1490,6 +1491,9 @@ unsafe extern "C" { pub fn LLVMSetUnnamedAddress(Global: &Value, UnnamedAddr: UnnamedAddr); pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&ConstantInt>; + + pub fn LLVMGetOrInsertComdat(M: &Module, Name: *const c_char) -> &Comdat; + pub fn LLVMSetComdat(V: &Value, C: &Comdat); } #[link(name = "llvm-wrapper", kind = "static")] @@ -2320,7 +2324,6 @@ unsafe extern "C" { pub fn LLVMRustPositionBuilderAtStart<'a>(B: &Builder<'a>, BB: &'a BasicBlock); - pub fn LLVMRustSetComdat<'a>(M: &'a Module, V: &'a Value, Name: *const c_char, NameLen: size_t); pub fn LLVMRustSetModulePICLevel(M: &Module); pub fn LLVMRustSetModulePIELevel(M: &Module); pub fn LLVMRustSetModuleCodeModel(M: &Module, Model: CodeModel); diff --git a/compiler/rustc_codegen_llvm/src/llvm/mod.rs b/compiler/rustc_codegen_llvm/src/llvm/mod.rs index e790c6696fa38..b306396e15a80 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/mod.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/mod.rs @@ -178,10 +178,10 @@ pub fn SetFunctionCallConv(fn_: &Value, cc: CallConv) { // function. // For more details on COMDAT sections see e.g., https://www.airs.com/blog/archives/52 pub fn SetUniqueComdat(llmod: &Module, val: &Value) { - unsafe { - let name = get_value_name(val); - LLVMRustSetComdat(llmod, val, name.as_ptr().cast(), name.len()); - } + let name_buf = get_value_name(val).to_vec(); + let name = + CString::from_vec_with_nul(name_buf).or_else(|buf| CString::new(buf.into_bytes())).unwrap(); + set_comdat(llmod, val, &name); } pub fn SetUnnamedAddress(global: &Value, unnamed: UnnamedAddr) { @@ -251,9 +251,14 @@ pub fn set_alignment(llglobal: &Value, align: Align) { } } -pub fn set_comdat(llmod: &Module, llglobal: &Value, name: &str) { +/// Get the `name`d comdat from `llmod` and assign it to `llglobal`. +/// +/// Inserts the comdat into `llmod` if it does not exist. +/// It is an error to call this if the target does not support comdat. +pub fn set_comdat(llmod: &Module, llglobal: &Value, name: &CStr) { unsafe { - LLVMRustSetComdat(llmod, llglobal, name.as_ptr().cast(), name.len()); + let comdat = LLVMGetOrInsertComdat(llmod, name.as_ptr()); + LLVMSetComdat(llglobal, comdat); } } diff --git a/compiler/rustc_codegen_llvm/src/mono_item.rs b/compiler/rustc_codegen_llvm/src/mono_item.rs index 02e1995620bdb..bf6ef219873a5 100644 --- a/compiler/rustc_codegen_llvm/src/mono_item.rs +++ b/compiler/rustc_codegen_llvm/src/mono_item.rs @@ -64,7 +64,9 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> { unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) }; let attrs = self.tcx.codegen_fn_attrs(instance.def_id()); base::set_link_section(lldecl, attrs); - if linkage == Linkage::LinkOnceODR || linkage == Linkage::WeakODR { + if (linkage == Linkage::LinkOnceODR || linkage == Linkage::WeakODR) + && self.tcx.sess.target.supports_comdat() + { llvm::SetUniqueComdat(self.llmod, lldecl); } diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 82e11a3afce32..f4b45a0819525 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -2514,6 +2514,13 @@ fn add_link_args(link_args: &mut LinkArgs, flavor: LinkerFlavor, args: &[&'stati add_link_args_iter(link_args, flavor, args.iter().copied().map(Cow::Borrowed)) } +impl TargetOptions { + pub fn supports_comdat(&self) -> bool { + // XCOFF and MachO don't support COMDAT. + !self.is_like_aix && !self.is_like_osx + } +} + impl TargetOptions { fn link_args(flavor: LinkerFlavor, args: &[&'static str]) -> LinkArgs { let mut link_args = LinkArgs::new(); From 492760020e69f7d92867bf4fba418127d48464d3 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Fri, 18 Oct 2024 01:09:21 -0700 Subject: [PATCH 4/8] llvm: Delete LLVMRustSetComdat --- compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 72b03fa0560e6..910c27da95463 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -1658,16 +1658,6 @@ extern "C" void LLVMRustPositionBuilderAtStart(LLVMBuilderRef B, unwrap(B)->SetInsertPoint(unwrap(BB), Point); } -extern "C" void LLVMRustSetComdat(LLVMModuleRef M, LLVMValueRef V, - const char *Name, size_t NameLen) { - Triple TargetTriple = Triple(unwrap(M)->getTargetTriple()); - GlobalObject *GV = unwrap(V); - if (TargetTriple.supportsCOMDAT()) { - StringRef NameRef(Name, NameLen); - GV->setComdat(unwrap(M)->getOrInsertComdat(NameRef)); - } -} - enum class LLVMRustLinkage { ExternalLinkage = 0, AvailableExternallyLinkage = 1, From 6a834b4df8a45fafea84761b6e25ccdf75cec007 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Thu, 17 Oct 2024 12:59:04 -0700 Subject: [PATCH 5/8] compiler: Adopt rust-analyzer impls for `LayoutCalculatorError` --- compiler/rustc_abi/src/layout.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_abi/src/layout.rs b/compiler/rustc_abi/src/layout.rs index 6e1299944a09d..5ce5f14ce57cb 100644 --- a/compiler/rustc_abi/src/layout.rs +++ b/compiler/rustc_abi/src/layout.rs @@ -39,7 +39,7 @@ enum NicheBias { End, } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum LayoutCalculatorError { /// An unsized type was found in a location where a sized type was expected. /// @@ -56,6 +56,31 @@ pub enum LayoutCalculatorError { EmptyUnion, } +impl LayoutCalculatorError { + pub fn without_payload(&self) -> LayoutCalculatorError<()> { + match self { + LayoutCalculatorError::UnexpectedUnsized(_) => { + LayoutCalculatorError::UnexpectedUnsized(()) + } + LayoutCalculatorError::SizeOverflow => LayoutCalculatorError::SizeOverflow, + LayoutCalculatorError::EmptyUnion => LayoutCalculatorError::EmptyUnion, + } + } + + /// Format an untranslated diagnostic for this type + /// + /// Intended for use by rust-analyzer, as neither it nor `rustc_abi` depend on fluent infra. + pub fn fallback_fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + LayoutCalculatorError::UnexpectedUnsized(_) => { + "an unsized type was found where a sized type was expected" + } + LayoutCalculatorError::SizeOverflow => "size overflow", + LayoutCalculatorError::EmptyUnion => "type is a union with no fields", + }) + } +} + type LayoutCalculatorResult = Result, LayoutCalculatorError>; From e1d6164607d63df552ce2adf900c2c4cbf7f5ce3 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Thu, 17 Oct 2024 12:59:40 -0700 Subject: [PATCH 6/8] rust-analyzer: Nest `LayoutCalculatorError` in `hir_ty::LayoutError` --- src/tools/rust-analyzer/crates/hir-ty/src/layout.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/layout.rs b/src/tools/rust-analyzer/crates/hir-ty/src/layout.rs index 4cdc0db46a15f..bce6a80b5b071 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/layout.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/layout.rs @@ -72,6 +72,8 @@ pub type Variants = hir_def::layout::Variants), EmptyUnion, HasErrorConst, HasErrorType, @@ -90,6 +92,7 @@ impl std::error::Error for LayoutError {} impl fmt::Display for LayoutError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { + LayoutError::BadCalc(err) => err.fallback_fmt(f), LayoutError::EmptyUnion => write!(f, "type is an union with no fields"), LayoutError::HasErrorConst => write!(f, "type contains an unevaluatable const"), LayoutError::HasErrorType => write!(f, "type contains an error"), @@ -114,11 +117,7 @@ impl fmt::Display for LayoutError { impl From> for LayoutError { fn from(err: LayoutCalculatorError) -> Self { - match err { - LayoutCalculatorError::EmptyUnion => LayoutError::EmptyUnion, - LayoutCalculatorError::UnexpectedUnsized(_) => LayoutError::UnexpectedUnsized, - LayoutCalculatorError::SizeOverflow => LayoutError::SizeOverflow, - } + LayoutError::BadCalc(err.without_payload()) } } From 53974b178699468ec82452db691d646265765fab Mon Sep 17 00:00:00 2001 From: binarycat Date: Sat, 19 Oct 2024 13:11:26 -0500 Subject: [PATCH 7/8] compiletest: disambiguate html-tidy from rust tidy tool --- src/tools/compiletest/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/compiletest/src/main.rs b/src/tools/compiletest/src/main.rs index b0f87593f9532..b84dd956de062 100644 --- a/src/tools/compiletest/src/main.rs +++ b/src/tools/compiletest/src/main.rs @@ -19,7 +19,7 @@ fn main() { let config = Arc::new(parse_config(env::args().collect())); if !config.has_tidy && config.mode == Mode::Rustdoc { - eprintln!("warning: `tidy` is not installed; diffs will not be generated"); + eprintln!("warning: `tidy` (html-tidy.org) is not installed; diffs will not be generated"); } if !config.profiler_runtime && config.mode == Mode::CoverageRun { From 9435afc0fbd3f41375b676230aca6a945741dfa7 Mon Sep 17 00:00:00 2001 From: Alona Enraght-Moony Date: Sat, 19 Oct 2024 19:24:54 +0000 Subject: [PATCH 8/8] rustdoc: Refractor footnote handling --- src/librustdoc/html/markdown/footnotes.rs | 103 ++++++++++++++-------- 1 file changed, 67 insertions(+), 36 deletions(-) diff --git a/src/librustdoc/html/markdown/footnotes.rs b/src/librustdoc/html/markdown/footnotes.rs index 47e248b891c32..3f0e586b8e372 100644 --- a/src/librustdoc/html/markdown/footnotes.rs +++ b/src/librustdoc/html/markdown/footnotes.rs @@ -10,7 +10,14 @@ use super::SpannedEvent; /// references. pub(super) struct Footnotes<'a, I> { inner: I, - footnotes: FxIndexMap>, u16)>, + footnotes: FxIndexMap>, +} + +/// The definition of a single footnote. +struct FootnoteDef<'a> { + content: Vec>, + /// The number that appears in the footnote reference and list. + id: u16, } impl<'a, I> Footnotes<'a, I> { @@ -18,10 +25,15 @@ impl<'a, I> Footnotes<'a, I> { Footnotes { inner: iter, footnotes: FxIndexMap::default() } } - fn get_entry(&mut self, key: &str) -> &mut (Vec>, u16) { + fn get_entry(&mut self, key: &str) -> (&mut Vec>, u16) { let new_id = self.footnotes.len() + 1; let key = key.to_owned(); - self.footnotes.entry(key).or_insert((Vec::new(), new_id as u16)) + let FootnoteDef { content, id } = self + .footnotes + .entry(key) + .or_insert(FootnoteDef { content: Vec::new(), id: new_id as u16 }); + // Don't allow changing the ID of existing entrys, but allow changing the contents. + (content, *id) } } @@ -32,46 +44,28 @@ impl<'a, I: Iterator>> Iterator for Footnotes<'a, I> { loop { match self.inner.next() { Some((Event::FootnoteReference(ref reference), range)) => { - let entry = self.get_entry(reference); - let reference = format!( - "{0}", - (*entry).1 - ); + // When we see a reference (to a footnote we may not know) the definition of, + // reserve a number for it, and emit a link to that number. + let (_, id) = self.get_entry(reference); + let reference = + format!("{0}", id); return Some((Event::Html(reference.into()), range)); } Some((Event::Start(Tag::FootnoteDefinition(def)), _)) => { - let mut content = Vec::new(); - for (event, _) in &mut self.inner { - if let Event::End(TagEnd::FootnoteDefinition) = event { - break; - } - content.push(event); - } - let entry = self.get_entry(&def); - (*entry).0 = content; + // When we see a footnote definition, collect the assocated content, and store + // that for rendering later. + let content = collect_footnote_def(&mut self.inner); + let (entry_content, _) = self.get_entry(&def); + *entry_content = content; } Some(e) => return Some(e), None => { if !self.footnotes.is_empty() { - let mut v: Vec<_> = self.footnotes.drain(..).map(|(_, x)| x).collect(); - v.sort_by(|a, b| a.1.cmp(&b.1)); - let mut ret = String::from("

    "); - for (mut content, id) in v { - write!(ret, "
  1. ").unwrap(); - let mut is_paragraph = false; - if let Some(&Event::End(TagEnd::Paragraph)) = content.last() { - content.pop(); - is_paragraph = true; - } - html::push_html(&mut ret, content.into_iter()); - write!(ret, " ").unwrap(); - if is_paragraph { - ret.push_str("

    "); - } - ret.push_str("
  2. "); - } - ret.push_str("
"); - return Some((Event::Html(ret.into()), 0..0)); + // After all the markdown is emmited, emit an
then all the footnotes + // in a list. + let defs: Vec<_> = self.footnotes.drain(..).map(|(_, x)| x).collect(); + let defs_html = render_footnotes_defs(defs); + return Some((Event::Html(defs_html.into()), 0..0)); } else { return None; } @@ -80,3 +74,40 @@ impl<'a, I: Iterator>> Iterator for Footnotes<'a, I> { } } } + +fn collect_footnote_def<'a>(events: impl Iterator>) -> Vec> { + let mut content = Vec::new(); + for (event, _) in events { + if let Event::End(TagEnd::FootnoteDefinition) = event { + break; + } + content.push(event); + } + content +} + +fn render_footnotes_defs(mut footnotes: Vec>) -> String { + let mut ret = String::from("

    "); + + // Footnotes must listed in order of id, so the numbers the + // browser generated for
  1. are right. + footnotes.sort_by_key(|x| x.id); + + for FootnoteDef { mut content, id } in footnotes { + write!(ret, "
  2. ").unwrap(); + let mut is_paragraph = false; + if let Some(&Event::End(TagEnd::Paragraph)) = content.last() { + content.pop(); + is_paragraph = true; + } + html::push_html(&mut ret, content.into_iter()); + write!(ret, " ").unwrap(); + if is_paragraph { + ret.push_str("

    "); + } + ret.push_str("
  3. "); + } + ret.push_str("
"); + + ret +}