Skip to content

Commit 4cd5aff

Browse files
Fix handling of disambiguator suffixes for intra-doc links
1 parent 6a5b97a commit 4cd5aff

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -951,9 +951,9 @@ fn preprocess_link<'a>(
951951
}
952952

953953
// Parse and strip the disambiguator from the link, if present.
954-
let (path_str, disambiguator) = match Disambiguator::from_str(&link) {
955-
Ok(Some((d, path))) => (path.trim(), Some(d)),
956-
Ok(None) => (link.trim(), None),
954+
let (link_text, path_str, disambiguator) = match Disambiguator::from_str(&link) {
955+
Ok(Some((d, path, link_text))) => (link_text.trim(), path.trim(), Some(d)),
956+
Ok(None) => (link.trim(), link.trim(), None),
957957
Err((err_msg, relative_range)) => {
958958
// Only report error if we would not have ignored this link. See issue #83859.
959959
if !should_ignore_link_with_disambiguators(link) {
@@ -971,11 +971,6 @@ fn preprocess_link<'a>(
971971
return None;
972972
}
973973

974-
// We stripped `()` and `!` when parsing the disambiguator.
975-
// Add them back to be displayed, but not prefix disambiguators.
976-
let link_text =
977-
disambiguator.map(|d| d.display_for(path_str)).unwrap_or_else(|| path_str.to_owned());
978-
979974
// Strip generics from the path.
980975
let path_str = if path_str.contains(['<', '>'].as_slice()) {
981976
match strip_generics_from_path(&path_str) {
@@ -1005,7 +1000,7 @@ fn preprocess_link<'a>(
10051000
path_str,
10061001
disambiguator,
10071002
extra_fragment: extra_fragment.map(String::from),
1008-
link_text,
1003+
link_text: link_text.to_owned(),
10091004
}))
10101005
}
10111006

@@ -1513,24 +1508,12 @@ enum Disambiguator {
15131508
}
15141509

15151510
impl Disambiguator {
1516-
/// The text that should be displayed when the path is rendered as HTML.
1517-
///
1518-
/// NOTE: `path` is not the original link given by the user, but a name suitable for passing to `resolve`.
1519-
fn display_for(&self, path: &str) -> String {
1520-
match self {
1521-
// FIXME: this will have different output if the user had `m!()` originally.
1522-
Self::Kind(DefKind::Macro(MacroKind::Bang)) => format!("{}!", path),
1523-
Self::Kind(DefKind::Fn) => format!("{}()", path),
1524-
_ => path.to_owned(),
1525-
}
1526-
}
1527-
1528-
/// Given a link, parse and return `(disambiguator, path_str)`.
1511+
/// Given a link, parse and return `(disambiguator, path_str, link_text)`.
15291512
///
15301513
/// This returns `Ok(Some(...))` if a disambiguator was found,
15311514
/// `Ok(None)` if no disambiguator was found, or `Err(...)`
15321515
/// if there was a problem with the disambiguator.
1533-
fn from_str(link: &str) -> Result<Option<(Self, &str)>, (String, Range<usize>)> {
1516+
fn from_str(link: &str) -> Result<Option<(Self, &str, &str)>, (String, Range<usize>)> {
15341517
use Disambiguator::{Kind, Namespace as NS, Primitive};
15351518

15361519
if let Some(idx) = link.find('@') {
@@ -1551,18 +1534,18 @@ impl Disambiguator {
15511534
"prim" | "primitive" => Primitive,
15521535
_ => return Err((format!("unknown disambiguator `{}`", prefix), 0..idx)),
15531536
};
1554-
Ok(Some((d, &rest[1..])))
1537+
Ok(Some((d, &rest[1..], &rest[1..])))
15551538
} else {
15561539
let suffixes = [
15571540
("!()", DefKind::Macro(MacroKind::Bang)),
15581541
("()", DefKind::Fn),
15591542
("!", DefKind::Macro(MacroKind::Bang)),
15601543
];
15611544
for (suffix, kind) in suffixes {
1562-
if let Some(link) = link.strip_suffix(suffix) {
1545+
if let Some(path_str) = link.strip_suffix(suffix) {
15631546
// Avoid turning `!` or `()` into an empty string
1564-
if !link.is_empty() {
1565-
return Ok(Some((Kind(kind), link)));
1547+
if !path_str.is_empty() {
1548+
return Ok(Some((Kind(kind), path_str, link)));
15661549
}
15671550
}
15681551
}

0 commit comments

Comments
 (0)