diff --git a/compiler/rustc_resolve/src/rustdoc.rs b/compiler/rustc_resolve/src/rustdoc.rs index 5e4b66018e42e..b8853c1744c92 100644 --- a/compiler/rustc_resolve/src/rustdoc.rs +++ b/compiler/rustc_resolve/src/rustdoc.rs @@ -265,9 +265,9 @@ fn strip_generics_from_path_segment(segment: Vec) -> Result Result { +pub fn strip_generics_from_path(path_str: &str) -> Result, MalformedGenerics> { if !path_str.contains(['<', '>']) { - return Ok(path_str.to_string()); + return Ok(path_str.into()); } let mut stripped_segments = vec![]; let mut path = path_str.chars().peekable(); @@ -322,7 +322,11 @@ pub fn strip_generics_from_path(path_str: &str) -> Result bool { /// Simplified version of the corresponding function in rustdoc. /// If the rustdoc version returns a successful result, this function must return the same result. /// Otherwise this function may return anything. -fn preprocess_link(link: &str) -> String { +fn preprocess_link(link: &str) -> Box { let link = link.replace('`', ""); let link = link.split('#').next().unwrap(); let link = link.trim(); @@ -345,7 +349,7 @@ fn preprocess_link(link: &str) -> String { let link = link.strip_suffix("{}").unwrap_or(link); let link = link.strip_suffix("[]").unwrap_or(link); let link = if link != "!" { link.strip_suffix('!').unwrap_or(link) } else { link }; - strip_generics_from_path(link).unwrap_or_else(|_| link.to_string()) + strip_generics_from_path(link).unwrap_or_else(|_| link.into()) } /// Keep inline and reference links `[]`, @@ -365,7 +369,7 @@ pub fn may_be_doc_link(link_type: LinkType) -> bool { /// Simplified version of `preprocessed_markdown_links` from rustdoc. /// Must return at least the same links as it, but may add some more links on top of that. -pub(crate) fn attrs_to_preprocessed_links(attrs: &[ast::Attribute]) -> Vec { +pub(crate) fn attrs_to_preprocessed_links(attrs: &[ast::Attribute]) -> Vec> { let (doc_fragments, _) = attrs_to_doc_fragments(attrs.iter().map(|attr| (attr, None)), true); let doc = prepare_to_doc_link_resolution(&doc_fragments).into_values().next().unwrap(); diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index fc1396e86f6b1..27d18aad7a349 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1017,12 +1017,12 @@ pub(crate) fn collapse_doc_fragments(doc_strings: &[DocFragment]) -> String { #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) struct ItemLink { /// The original link written in the markdown - pub(crate) link: String, + pub(crate) link: Box, /// The link text displayed in the HTML. /// /// This may not be the same as `link` if there was a disambiguator /// in an intra-doc link (e.g. \[`fn@f`\]) - pub(crate) link_text: String, + pub(crate) link_text: Box, /// The `DefId` of the Item whose **HTML Page** contains the item being /// linked to. This will be different to `item_id` on item's that don't /// have their own page, such as struct fields and enum variants. @@ -1035,9 +1035,9 @@ pub struct RenderedLink { /// The text the link was original written as. /// /// This could potentially include disambiguators and backticks. - pub(crate) original_text: String, + pub(crate) original_text: Box, /// The text to display in the HTML - pub(crate) new_text: String, + pub(crate) new_text: Box, /// The URL to put in the `href` pub(crate) href: String, /// The tooltip. diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 6bdd9db9bfaf4..0e4c5ed683682 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -772,14 +772,21 @@ pub(crate) fn link_tooltip(did: DefId, fragment: &Option, cx: &Cont let Some((fqp, shortty)) = cache.paths.get(&did) .or_else(|| cache.external_paths.get(&did)) else { return String::new() }; - let fqp = fqp.iter().map(|sym| sym.as_str()).join("::"); + let mut buf = Buffer::new(); if let &Some(UrlFragment::Item(id)) = fragment { - let name = cx.tcx().item_name(id); - let descr = cx.tcx().def_descr(id); - format!("{descr} {fqp}::{name}") - } else { - format!("{shortty} {fqp}") + write!(buf, "{} ", cx.tcx().def_descr(id)); + for component in fqp { + write!(buf, "{component}::"); + } + write!(buf, "{}", cx.tcx().item_name(id)); + } else if !fqp.is_empty() { + let mut fqp_it = fqp.into_iter(); + write!(buf, "{shortty} {}", fqp_it.next().unwrap()); + for component in fqp_it { + write!(buf, "::{component}"); + } } + buf.into_inner() } /// Used to render a [`clean::Path`]. diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 89f1ad71134ca..fe446ae3c164c 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -978,7 +978,7 @@ impl Markdown<'_> { let mut replacer = |broken_link: BrokenLink<'_>| { links .iter() - .find(|link| link.original_text.as_str() == &*broken_link.reference) + .find(|link| &*link.original_text == &*broken_link.reference) .map(|link| (link.href.as_str().into(), link.tooltip.as_str().into())) }; @@ -1061,7 +1061,7 @@ impl MarkdownSummaryLine<'_> { let mut replacer = |broken_link: BrokenLink<'_>| { links .iter() - .find(|link| link.original_text.as_str() == &*broken_link.reference) + .find(|link| &*link.original_text == &*broken_link.reference) .map(|link| (link.href.as_str().into(), link.tooltip.as_str().into())) }; @@ -1108,7 +1108,7 @@ fn markdown_summary_with_limit( let mut replacer = |broken_link: BrokenLink<'_>| { link_names .iter() - .find(|link| link.original_text.as_str() == &*broken_link.reference) + .find(|link| &*link.original_text == &*broken_link.reference) .map(|link| (link.href.as_str().into(), link.tooltip.as_str().into())) }; @@ -1189,7 +1189,7 @@ pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> Strin let mut replacer = |broken_link: BrokenLink<'_>| { link_names .iter() - .find(|link| link.original_text.as_str() == &*broken_link.reference) + .find(|link| &*link.original_text == &*broken_link.reference) .map(|link| (link.href.as_str().into(), link.tooltip.as_str().into())) }; diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index d5e9010eb4ed9..18c45fd699155 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -38,7 +38,7 @@ impl JsonRenderer<'_> { Some(UrlFragment::UserWritten(_)) | None => *page_id, }; - (link.clone(), id_from_item_default(id.into(), self.tcx)) + (String::from(&**link), id_from_item_default(id.into(), self.tcx)) }) .collect(); let docs = item.attrs.collapsed_doc_value(); diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 920a3b22f25a2..cbfc581389c27 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -228,7 +228,7 @@ struct ResolutionInfo { item_id: ItemId, module_id: DefId, dis: Option, - path_str: String, + path_str: Box, extra_fragment: Option, } @@ -849,10 +849,10 @@ impl PreprocessingError { #[derive(Clone)] struct PreprocessingInfo { - path_str: String, + path_str: Box, disambiguator: Option, extra_fragment: Option, - link_text: String, + link_text: Box, } // Not a typedef to avoid leaking several private structures from this module. @@ -937,7 +937,7 @@ fn preprocess_link( path_str, disambiguator, extra_fragment: extra_fragment.map(|frag| frag.to_owned()), - link_text: link_text.to_owned(), + link_text: Box::::from(link_text), })) } @@ -993,7 +993,7 @@ impl LinkCollector<'_, '_> { item_id: item.item_id, module_id, dis: disambiguator, - path_str: path_str.to_owned(), + path_str: path_str.clone(), extra_fragment: extra_fragment.clone(), }, diag_info.clone(), // this struct should really be Copy, but Range is not :( @@ -1067,7 +1067,7 @@ impl LinkCollector<'_, '_> { } res.def_id(self.cx.tcx).map(|page_id| ItemLink { - link: ori_link.link.clone(), + link: Box::::from(&*ori_link.link), link_text: link_text.clone(), page_id, fragment, @@ -1091,7 +1091,7 @@ impl LinkCollector<'_, '_> { let page_id = clean::register_res(self.cx, rustc_hir::def::Res::Def(kind, id)); Some(ItemLink { - link: ori_link.link.clone(), + link: Box::::from(&*ori_link.link), link_text: link_text.clone(), page_id, fragment, diff --git a/src/librustdoc/passes/lint/html_tags.rs b/src/librustdoc/passes/lint/html_tags.rs index eac362b37b209..4f72df5a5cda0 100644 --- a/src/librustdoc/passes/lint/html_tags.rs +++ b/src/librustdoc/passes/lint/html_tags.rs @@ -113,7 +113,7 @@ pub(crate) fn visit_item(cx: &DocContext<'_>, item: &Item) { if let Some(link) = link_names.iter().find(|link| *link.original_text == *broken_link.reference) { - Some((link.href.as_str().into(), link.new_text.as_str().into())) + Some((link.href.as_str().into(), link.new_text.to_string().into())) } else if matches!( &broken_link.link_type, LinkType::Reference | LinkType::ReferenceUnknown