Skip to content

Commit 1f90f4f

Browse files
authored
Rollup merge of rust-lang#93721 - jyn514:less-macro-special-casing, r=petrochenkov
rustdoc: Special-case macro lookups less Previously, rustdoc had 3 fallbacks it used: 1. `resolve_macro_path` 2. `all_macros` 3. `resolve_str_path_error` Ideally, it would only use `resolve_str_path_error`, to be consistent with other namespaces. Unfortunately, that doesn't consider macros that aren't defined at module scope; consider for instance ```rust { struct S; macro_rules! mac { () => {} } // `mac`'s scope starts here /// `mac` <- `resolve_str_path_error` won't see this struct Z; //`mac`'s scope ends here } ``` This changes it to only use `all_macros` and `resolve_str_path_error`, and gives `resolve_str_path_error` precedence over `all_macros` in case there are two macros with the same name in the same module. This is a smaller version of rust-lang#91427. r? `@petrochenkov`
2 parents 7d5e2ac + f026550 commit 1f90f4f

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+5-19
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
//!
33
//! [RFC 1946]: https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md
44
5-
use rustc_ast as ast;
65
use rustc_data_structures::{fx::FxHashMap, stable_set::FxHashSet};
76
use rustc_errors::{Applicability, DiagnosticBuilder};
8-
use rustc_expand::base::SyntaxExtensionKind;
97
use rustc_hir::def::{
108
DefKind,
119
Namespace::{self, *},
@@ -14,7 +12,6 @@ use rustc_hir::def::{
1412
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_ID};
1513
use rustc_middle::ty::{DefIdTree, Ty, TyCtxt};
1614
use rustc_middle::{bug, span_bug, ty};
17-
use rustc_resolve::ParentScope;
1815
use rustc_session::lint::Lint;
1916
use rustc_span::hygiene::MacroKind;
2017
use rustc_span::symbol::{sym, Ident, Symbol};
@@ -486,23 +483,9 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
486483
path_str: &'a str,
487484
module_id: DefId,
488485
) -> Result<Res, ResolutionFailure<'a>> {
489-
let path = ast::Path::from_ident(Ident::from_str(path_str));
490486
self.cx.enter_resolver(|resolver| {
491-
// FIXME(jynelson): does this really need 3 separate lookups?
492-
if let Ok((Some(ext), res)) = resolver.resolve_macro_path(
493-
&path,
494-
None,
495-
&ParentScope::module(resolver.graph_root(), resolver),
496-
false,
497-
false,
498-
) {
499-
if let SyntaxExtensionKind::LegacyBang { .. } = ext.kind {
500-
return Ok(res.try_into().unwrap());
501-
}
502-
}
503-
if let Some(&res) = resolver.all_macros().get(&Symbol::intern(path_str)) {
504-
return Ok(res.try_into().unwrap());
505-
}
487+
// NOTE: this needs 2 separate lookups because `resolve_str_path_error` doesn't take
488+
// lexical scope into account (it ignores all macros not defined at the mod-level)
506489
debug!("resolving {} as a macro in the module {:?}", path_str, module_id);
507490
if let Ok((_, res)) =
508491
resolver.resolve_str_path_error(DUMMY_SP, path_str, MacroNS, module_id)
@@ -512,6 +495,9 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
512495
return Ok(res);
513496
}
514497
}
498+
if let Some(&res) = resolver.all_macros().get(&Symbol::intern(path_str)) {
499+
return Ok(res.try_into().unwrap());
500+
}
515501
Err(ResolutionFailure::NotResolved {
516502
module_id,
517503
partial_res: None,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// check-pass
2+
#![allow(rustdoc::private_intra_doc_links)]
3+
4+
macro_rules! foo {
5+
() => {};
6+
}
7+
8+
/// [foo!]
9+
pub fn baz() {}

0 commit comments

Comments
 (0)