Ignore "non-real" type Res in rustdoc intra doc link resolution

This commit is contained in:
Guillaume Gomez 2023-05-23 15:33:34 +02:00
parent cda5becc27
commit 234927e098
2 changed files with 9 additions and 8 deletions

View file

@ -17,7 +17,7 @@ use rustc_ast::*;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_errors::{Applicability, DiagnosticArgValue, DiagnosticId, IntoDiagnosticArg}; use rustc_errors::{Applicability, DiagnosticArgValue, DiagnosticId, IntoDiagnosticArg};
use rustc_hir::def::Namespace::{self, *}; use rustc_hir::def::Namespace::{self, *};
use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, PartialRes, PerNS}; use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, NonMacroAttrKind, PartialRes, PerNS};
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::{BindingAnnotation, PrimTy, TraitCandidate}; use rustc_hir::{BindingAnnotation, PrimTy, TraitCandidate};
use rustc_middle::middle::resolve_bound_vars::Set1; use rustc_middle::middle::resolve_bound_vars::Set1;
@ -4287,12 +4287,12 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
} }
} }
fn resolve_and_cache_rustdoc_path(&mut self, path_str: &str, ns: Namespace) -> bool { fn resolve_and_cache_rustdoc_path(&mut self, path_str: &str, ns: Namespace) -> Option<Res> {
// FIXME: This caching may be incorrect in case of multiple `macro_rules` // FIXME: This caching may be incorrect in case of multiple `macro_rules`
// items with the same name in the same module. // items with the same name in the same module.
// Also hygiene is not considered. // Also hygiene is not considered.
let mut doc_link_resolutions = std::mem::take(&mut self.r.doc_link_resolutions); let mut doc_link_resolutions = std::mem::take(&mut self.r.doc_link_resolutions);
let res = doc_link_resolutions let res = *doc_link_resolutions
.entry(self.parent_scope.module.nearest_parent_mod().expect_local()) .entry(self.parent_scope.module.nearest_parent_mod().expect_local())
.or_default() .or_default()
.entry((Symbol::intern(path_str), ns)) .entry((Symbol::intern(path_str), ns))
@ -4307,8 +4307,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
return None; return None;
} }
res res
}) });
.is_some();
self.r.doc_link_resolutions = doc_link_resolutions; self.r.doc_link_resolutions = doc_link_resolutions;
res res
} }
@ -4343,8 +4342,10 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
let mut any_resolved = false; let mut any_resolved = false;
let mut need_assoc = false; let mut need_assoc = false;
for ns in [TypeNS, ValueNS, MacroNS] { for ns in [TypeNS, ValueNS, MacroNS] {
if self.resolve_and_cache_rustdoc_path(&path_str, ns) { if let Some(res) = self.resolve_and_cache_rustdoc_path(&path_str, ns) {
any_resolved = true; // Rustdoc ignores tool attribute resolutions and attempts
// to resolve their prefixes for diagnostics.
any_resolved = !matches!(res, Res::NonMacroAttr(NonMacroAttrKind::Tool));
} else if ns != MacroNS { } else if ns != MacroNS {
need_assoc = true; need_assoc = true;
} }

View file

@ -149,7 +149,7 @@ impl TryFrom<ResolveRes> for Res {
Def(kind, id) => Ok(Res::Def(kind, id)), Def(kind, id) => Ok(Res::Def(kind, id)),
PrimTy(prim) => Ok(Res::Primitive(PrimitiveType::from_hir(prim))), PrimTy(prim) => Ok(Res::Primitive(PrimitiveType::from_hir(prim))),
// e.g. `#[derive]` // e.g. `#[derive]`
NonMacroAttr(..) | Err => Result::Err(()), ToolMod | NonMacroAttr(..) | Err => Result::Err(()),
other => bug!("unrecognized res {:?}", other), other => bug!("unrecognized res {:?}", other),
} }
} }