Skip to content

Commit f9ebf1e

Browse files
authored
Rollup merge of #86523 - LeSeulArtichaut:macros-disambiguators, r=jyn514
Improvements to intra-doc link macro disambiguators A few small improvements around macro disambiguators: - display the link text as it was entered: previously `[macro!()]` would be displayed without the parantheses (fixes #86309) - support `!{}` and `![]` as macro disambiguators (fixes #86310) r? `@jyn514` cc `@Manishearth` `@camelid`
2 parents 6023ac2 + f387e8c commit f9ebf1e

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+12-27
Original file line numberDiff line numberDiff line change
@@ -992,9 +992,9 @@ fn preprocess_link<'a>(
992992
}
993993

994994
// Parse and strip the disambiguator from the link, if present.
995-
let (path_str, disambiguator) = match Disambiguator::from_str(&link) {
996-
Ok(Some((d, path))) => (path.trim(), Some(d)),
997-
Ok(None) => (link.trim(), None),
995+
let (disambiguator, path_str, link_text) = match Disambiguator::from_str(&link) {
996+
Ok(Some((d, path, link_text))) => (Some(d), path.trim(), link_text.trim()),
997+
Ok(None) => (None, link.trim(), link.trim()),
998998
Err((err_msg, relative_range)) => {
999999
// Only report error if we would not have ignored this link. See issue #83859.
10001000
if !should_ignore_link_with_disambiguators(link) {
@@ -1012,11 +1012,6 @@ fn preprocess_link<'a>(
10121012
return None;
10131013
}
10141014

1015-
// We stripped `()` and `!` when parsing the disambiguator.
1016-
// Add them back to be displayed, but not prefix disambiguators.
1017-
let link_text =
1018-
disambiguator.map(|d| d.display_for(path_str)).unwrap_or_else(|| path_str.to_owned());
1019-
10201015
// Strip generics from the path.
10211016
let path_str = if path_str.contains(['<', '>'].as_slice()) {
10221017
match strip_generics_from_path(&path_str) {
@@ -1046,7 +1041,7 @@ fn preprocess_link<'a>(
10461041
path_str,
10471042
disambiguator,
10481043
extra_fragment: extra_fragment.map(String::from),
1049-
link_text,
1044+
link_text: link_text.to_owned(),
10501045
}))
10511046
}
10521047

@@ -1554,24 +1549,12 @@ enum Disambiguator {
15541549
}
15551550

15561551
impl Disambiguator {
1557-
/// The text that should be displayed when the path is rendered as HTML.
1558-
///
1559-
/// NOTE: `path` is not the original link given by the user, but a name suitable for passing to `resolve`.
1560-
fn display_for(&self, path: &str) -> String {
1561-
match self {
1562-
// FIXME: this will have different output if the user had `m!()` originally.
1563-
Self::Kind(DefKind::Macro(MacroKind::Bang)) => format!("{}!", path),
1564-
Self::Kind(DefKind::Fn) => format!("{}()", path),
1565-
_ => path.to_owned(),
1566-
}
1567-
}
1568-
1569-
/// Given a link, parse and return `(disambiguator, path_str)`.
1552+
/// Given a link, parse and return `(disambiguator, path_str, link_text)`.
15701553
///
15711554
/// This returns `Ok(Some(...))` if a disambiguator was found,
15721555
/// `Ok(None)` if no disambiguator was found, or `Err(...)`
15731556
/// if there was a problem with the disambiguator.
1574-
fn from_str(link: &str) -> Result<Option<(Self, &str)>, (String, Range<usize>)> {
1557+
fn from_str(link: &str) -> Result<Option<(Self, &str, &str)>, (String, Range<usize>)> {
15751558
use Disambiguator::{Kind, Namespace as NS, Primitive};
15761559

15771560
if let Some(idx) = link.find('@') {
@@ -1592,18 +1575,20 @@ impl Disambiguator {
15921575
"prim" | "primitive" => Primitive,
15931576
_ => return Err((format!("unknown disambiguator `{}`", prefix), 0..idx)),
15941577
};
1595-
Ok(Some((d, &rest[1..])))
1578+
Ok(Some((d, &rest[1..], &rest[1..])))
15961579
} else {
15971580
let suffixes = [
15981581
("!()", DefKind::Macro(MacroKind::Bang)),
1582+
("!{}", DefKind::Macro(MacroKind::Bang)),
1583+
("![]", DefKind::Macro(MacroKind::Bang)),
15991584
("()", DefKind::Fn),
16001585
("!", DefKind::Macro(MacroKind::Bang)),
16011586
];
16021587
for (suffix, kind) in suffixes {
1603-
if let Some(link) = link.strip_suffix(suffix) {
1588+
if let Some(path_str) = link.strip_suffix(suffix) {
16041589
// Avoid turning `!` or `()` into an empty string
1605-
if !link.is_empty() {
1606-
return Ok(Some((Kind(kind), link)));
1590+
if !path_str.is_empty() {
1591+
return Ok(Some((Kind(kind), path_str, link)));
16071592
}
16081593
}
16091594
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![crate_name = "foo"]
2+
#![deny(rustdoc::broken_intra_doc_links)]
3+
4+
//! [foo!()]
5+
// @has foo/index.html '//a[@href="macro.foo.html"]' 'foo!()'
6+
7+
//! [foo!{}]
8+
// @has - '//a[@href="macro.foo.html"]' 'foo!{}'
9+
10+
//! [foo![]](foo![])
11+
// @has - '//a[@href="macro.foo.html"]' 'foo![]'
12+
13+
//! [foo1](foo!())
14+
// @has - '//a[@href="macro.foo.html"]' 'foo1'
15+
16+
//! [foo2](foo!{})
17+
// @has - '//a[@href="macro.foo.html"]' 'foo2'
18+
19+
//! [foo3](foo![])
20+
// @has - '//a[@href="macro.foo.html"]' 'foo3'
21+
22+
#[macro_export]
23+
macro_rules! foo {
24+
() => {};
25+
}

0 commit comments

Comments
 (0)