From 4cd5affcf2d4a9c708b4f20d4f3afaaa3e3c5b2a Mon Sep 17 00:00:00 2001 From: LeSeulArtichaut Date: Mon, 21 Jun 2021 20:26:14 +0200 Subject: [PATCH] Fix handling of disambiguator suffixes for intra-doc links --- .../passes/collect_intra_doc_links.rs | 37 +++++-------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 9db83c903abf..ce017995a3b8 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -951,9 +951,9 @@ fn preprocess_link<'a>( } // Parse and strip the disambiguator from the link, if present. - let (path_str, disambiguator) = match Disambiguator::from_str(&link) { - Ok(Some((d, path))) => (path.trim(), Some(d)), - Ok(None) => (link.trim(), None), + let (link_text, path_str, disambiguator) = match Disambiguator::from_str(&link) { + Ok(Some((d, path, link_text))) => (link_text.trim(), path.trim(), Some(d)), + Ok(None) => (link.trim(), link.trim(), None), Err((err_msg, relative_range)) => { // Only report error if we would not have ignored this link. See issue #83859. if !should_ignore_link_with_disambiguators(link) { @@ -971,11 +971,6 @@ fn preprocess_link<'a>( return None; } - // We stripped `()` and `!` when parsing the disambiguator. - // Add them back to be displayed, but not prefix disambiguators. - let link_text = - disambiguator.map(|d| d.display_for(path_str)).unwrap_or_else(|| path_str.to_owned()); - // Strip generics from the path. let path_str = if path_str.contains(['<', '>'].as_slice()) { match strip_generics_from_path(&path_str) { @@ -1005,7 +1000,7 @@ fn preprocess_link<'a>( path_str, disambiguator, extra_fragment: extra_fragment.map(String::from), - link_text, + link_text: link_text.to_owned(), })) } @@ -1513,24 +1508,12 @@ enum Disambiguator { } impl Disambiguator { - /// The text that should be displayed when the path is rendered as HTML. - /// - /// NOTE: `path` is not the original link given by the user, but a name suitable for passing to `resolve`. - fn display_for(&self, path: &str) -> String { - match self { - // FIXME: this will have different output if the user had `m!()` originally. - Self::Kind(DefKind::Macro(MacroKind::Bang)) => format!("{}!", path), - Self::Kind(DefKind::Fn) => format!("{}()", path), - _ => path.to_owned(), - } - } - - /// Given a link, parse and return `(disambiguator, path_str)`. + /// Given a link, parse and return `(disambiguator, path_str, link_text)`. /// /// This returns `Ok(Some(...))` if a disambiguator was found, /// `Ok(None)` if no disambiguator was found, or `Err(...)` /// if there was a problem with the disambiguator. - fn from_str(link: &str) -> Result, (String, Range)> { + fn from_str(link: &str) -> Result, (String, Range)> { use Disambiguator::{Kind, Namespace as NS, Primitive}; if let Some(idx) = link.find('@') { @@ -1551,7 +1534,7 @@ impl Disambiguator { "prim" | "primitive" => Primitive, _ => return Err((format!("unknown disambiguator `{}`", prefix), 0..idx)), }; - Ok(Some((d, &rest[1..]))) + Ok(Some((d, &rest[1..], &rest[1..]))) } else { let suffixes = [ ("!()", DefKind::Macro(MacroKind::Bang)), @@ -1559,10 +1542,10 @@ impl Disambiguator { ("!", DefKind::Macro(MacroKind::Bang)), ]; for (suffix, kind) in suffixes { - if let Some(link) = link.strip_suffix(suffix) { + if let Some(path_str) = link.strip_suffix(suffix) { // Avoid turning `!` or `()` into an empty string - if !link.is_empty() { - return Ok(Some((Kind(kind), link))); + if !path_str.is_empty() { + return Ok(Some((Kind(kind), path_str, link))); } } }