Skip to content

Commit b4019de

Browse files
committed
rustdoc: Remove doc link resolution fallback to all macro_rules in the crate
1 parent a170f2b commit b4019de

File tree

3 files changed

+39
-47
lines changed

3 files changed

+39
-47
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+22-30
Original file line numberDiff line numberDiff line change
@@ -443,21 +443,6 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
443443
})
444444
}
445445

446-
/// HACK: Try to search the macro name in the list of all `macro_rules` items in the crate.
447-
/// Used when nothing else works, may often give an incorrect result.
448-
fn resolve_macro_rules(&self, path_str: &str, ns: Namespace) -> Option<Res> {
449-
if ns != MacroNS {
450-
return None;
451-
}
452-
453-
self.cx
454-
.resolver_caches
455-
.all_macro_rules
456-
.get(&Symbol::intern(path_str))
457-
.copied()
458-
.and_then(|res| res.try_into().ok())
459-
}
460-
461446
/// Convenience wrapper around `resolve_rustdoc_path`.
462447
///
463448
/// This also handles resolving `true` and `false` as booleans.
@@ -489,8 +474,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
489474
})
490475
})
491476
.and_then(|res| res.try_into().ok())
492-
.or_else(|| resolve_primitive(path_str, ns))
493-
.or_else(|| self.resolve_macro_rules(path_str, ns));
477+
.or_else(|| resolve_primitive(path_str, ns));
494478
debug!("{} resolved to {:?} in namespace {:?}", path_str, result, ns);
495479
result
496480
}
@@ -1391,11 +1375,7 @@ impl LinkCollector<'_, '_> {
13911375
}
13921376
}
13931377
}
1394-
resolution_failure(self, diag, path_str, disambiguator, smallvec![err]);
1395-
// This could just be a normal link or a broken link
1396-
// we could potentially check if something is
1397-
// "intra-doc-link-like" and warn in that case.
1398-
None
1378+
resolution_failure(self, diag, path_str, disambiguator, smallvec![err])
13991379
}
14001380
}
14011381
}
@@ -1423,15 +1403,13 @@ impl LinkCollector<'_, '_> {
14231403
let len = candidates.iter().filter(|res| res.is_ok()).count();
14241404

14251405
if len == 0 {
1426-
resolution_failure(
1406+
return resolution_failure(
14271407
self,
14281408
diag,
14291409
path_str,
14301410
disambiguator,
14311411
candidates.into_iter().filter_map(|res| res.err()).collect(),
14321412
);
1433-
// this could just be a normal link
1434-
return None;
14351413
}
14361414

14371415
if len == 1 {
@@ -1737,8 +1715,9 @@ fn resolution_failure(
17371715
path_str: &str,
17381716
disambiguator: Option<Disambiguator>,
17391717
kinds: SmallVec<[ResolutionFailure<'_>; 3]>,
1740-
) {
1718+
) -> Option<(Res, Option<DefId>)> {
17411719
let tcx = collector.cx.tcx;
1720+
let mut recovered_res = None;
17421721
report_diagnostic(
17431722
tcx,
17441723
BROKEN_INTRA_DOC_LINKS,
@@ -1826,11 +1805,22 @@ fn resolution_failure(
18261805
diag.note(&note);
18271806
}
18281807

1829-
// If the link has `::` in it, assume it was meant to be an intra-doc link.
1830-
// Otherwise, the `[]` might be unrelated.
1831-
// FIXME: don't show this for autolinks (`<>`), `()` style links, or reference links
18321808
if !path_str.contains("::") {
1833-
diag.help(r#"to escape `[` and `]` characters, add '\' before them like `\[` or `\]`"#);
1809+
if disambiguator.map_or(true, |d| d.ns() == MacroNS)
1810+
&& let Some(&res) = collector.cx.resolver_caches.all_macro_rules
1811+
.get(&Symbol::intern(path_str))
1812+
{
1813+
diag.note(format!(
1814+
"`macro_rules` named `{path_str}` exists in this crate, \
1815+
but it is not in scope at this link's location"
1816+
));
1817+
recovered_res = res.try_into().ok().map(|res| (res, None));
1818+
} else {
1819+
// If the link has `::` in it, assume it was meant to be an
1820+
// intra-doc link. Otherwise, the `[]` might be unrelated.
1821+
diag.help("to escape `[` and `]` characters, \
1822+
add '\\' before them like `\\[` or `\\]`");
1823+
}
18341824
}
18351825

18361826
continue;
@@ -1915,6 +1905,8 @@ fn resolution_failure(
19151905
}
19161906
},
19171907
);
1908+
1909+
recovered_res
19181910
}
19191911

19201912
fn report_multiple_anchors(cx: &DocContext<'_>, diag_info: DiagnosticInfo<'_>) {

src/test/rustdoc-ui/intra-doc/macro-rules-error.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ mod no_escape {
1010
}
1111
}
1212

13-
/// [before_but_limited_to_module] FIXME: This error should be reported
14-
// ERROR unresolved link to `before_but_limited_to_module`
15-
/// [after] FIXME: This error should be reported
16-
// ERROR unresolved link to `after`
17-
/// [str] FIXME: This error shouldn not be reported
18-
//~^ ERROR `str` is both a builtin type and a macro
13+
/// [before_but_limited_to_module]
14+
//~^ ERROR unresolved link to `before_but_limited_to_module`
15+
/// [after]
16+
//~^ ERROR unresolved link to `after`
17+
/// [str]
1918
fn check() {}
2019

2120
macro_rules! after {
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
error: `str` is both a builtin type and a macro
2-
--> $DIR/macro-rules-error.rs:17:6
1+
error: unresolved link to `before_but_limited_to_module`
2+
--> $DIR/macro-rules-error.rs:13:6
33
|
4-
LL | /// [str] FIXME: This error shouldn not be reported
5-
| ^^^ ambiguous link
4+
LL | /// [before_but_limited_to_module]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no item named `before_but_limited_to_module` in scope
66
|
77
note: the lint level is defined here
88
--> $DIR/macro-rules-error.rs:5:9
99
|
1010
LL | #![deny(rustdoc::broken_intra_doc_links)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
help: to link to the builtin type, prefix with `prim@`
12+
= note: `macro_rules` named `before_but_limited_to_module` exists in this crate, but it is not in scope at this link's location
13+
14+
error: unresolved link to `after`
15+
--> $DIR/macro-rules-error.rs:15:6
1316
|
14-
LL | /// [prim@str] FIXME: This error shouldn not be reported
15-
| +++++
16-
help: to link to the macro, add an exclamation mark
17+
LL | /// [after]
18+
| ^^^^^ no item named `after` in scope
1719
|
18-
LL | /// [str!] FIXME: This error shouldn not be reported
19-
| +
20+
= note: `macro_rules` named `after` exists in this crate, but it is not in scope at this link's location
2021

21-
error: aborting due to previous error
22+
error: aborting due to 2 previous errors
2223

0 commit comments

Comments
 (0)