//!
//! [RFC 1946]: https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md
-use rustc_ast as ast;
use rustc_data_structures::{fx::FxHashMap, stable_set::FxHashSet};
use rustc_errors::{Applicability, DiagnosticBuilder};
-use rustc_expand::base::SyntaxExtensionKind;
-use rustc_hir as hir;
use rustc_hir::def::{
DefKind,
Namespace::{self, *},
PerNS,
};
-use rustc_hir::def_id::{CrateNum, DefId};
-use rustc_middle::ty::TyCtxt;
+use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_ID};
+use rustc_middle::ty::{DefIdTree, Ty, TyCtxt};
use rustc_middle::{bug, span_bug, ty};
-use rustc_resolve::ParentScope;
use rustc_session::lint::Lint;
-use rustc_span::hygiene::{MacroKind, SyntaxContext};
+use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::{BytePos, DUMMY_SP};
use smallvec::{smallvec, SmallVec};
use pulldown_cmark::LinkType;
use std::borrow::Cow;
-use std::cell::Cell;
use std::convert::{TryFrom, TryInto};
+use std::fmt::Write;
use std::mem;
use std::ops::Range;
use crate::visit::DocVisitor;
mod early;
-crate use early::load_intra_link_crates;
+crate use early::early_resolve_intra_doc_links;
crate const COLLECT_INTRA_DOC_LINKS: Pass = Pass {
name: "collect-intra-doc-links",
};
fn collect_intra_doc_links(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
- let mut collector = LinkCollector {
- cx,
- mod_ids: Vec::new(),
- kind_side_channel: Cell::new(None),
- visited_links: FxHashMap::default(),
- };
+ let mut collector =
+ LinkCollector { cx, mod_ids: Vec::new(), visited_links: FxHashMap::default() };
collector.visit_crate(&krate);
krate
}
Res::Primitive(_) => None,
}
}
+
+ /// Used for error reporting.
+ fn disambiguator_suggestion(self) -> Suggestion {
+ let kind = match self {
+ Res::Primitive(_) => return Suggestion::Prefix("prim"),
+ Res::Def(kind, _) => kind,
+ };
+ if kind == DefKind::Macro(MacroKind::Bang) {
+ return Suggestion::Macro;
+ } else if kind == DefKind::Fn || kind == DefKind::AssocFn {
+ return Suggestion::Function;
+ } else if kind == DefKind::Field {
+ return Suggestion::RemoveDisambiguator;
+ }
+
+ let prefix = match kind {
+ DefKind::Struct => "struct",
+ DefKind::Enum => "enum",
+ DefKind::Trait => "trait",
+ DefKind::Union => "union",
+ DefKind::Mod => "mod",
+ DefKind::Const | DefKind::ConstParam | DefKind::AssocConst | DefKind::AnonConst => {
+ "const"
+ }
+ DefKind::Static => "static",
+ DefKind::Macro(MacroKind::Derive) => "derive",
+ // Now handle things that don't have a specific disambiguator
+ _ => match kind
+ .ns()
+ .expect("tried to calculate a disambiguator for a def without a namespace?")
+ {
+ Namespace::TypeNS => "type",
+ Namespace::ValueNS => "value",
+ Namespace::MacroNS => "macro",
+ },
+ };
+
+ Suggestion::Prefix(prefix)
+ }
}
impl TryFrom<ResolveRes> for Res {
EmptyAngleBrackets,
}
-impl ResolutionFailure<'a> {
+impl ResolutionFailure<'_> {
/// This resolved fully (not just partially) but is erroneous for some other reason
///
/// Returns the full resolution of the link, if present.
RustdocAnchorConflict(Res),
}
+#[derive(Clone, Debug, Hash, PartialEq, Eq)]
+crate enum UrlFragment {
+ Item(ItemFragment),
+ UserWritten(String),
+}
+
+impl UrlFragment {
+ /// Render the fragment, including the leading `#`.
+ crate fn render(&self, s: &mut String, tcx: TyCtxt<'_>) -> std::fmt::Result {
+ match self {
+ UrlFragment::Item(frag) => frag.render(s, tcx),
+ UrlFragment::UserWritten(raw) => write!(s, "#{}", raw),
+ }
+ }
+}
+
+#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
+crate struct ItemFragment(FragmentKind, DefId);
+
+#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
+crate enum FragmentKind {
+ Method,
+ TyMethod,
+ AssociatedConstant,
+ AssociatedType,
+
+ StructField,
+ Variant,
+ VariantField,
+}
+
+impl ItemFragment {
+ /// Create a fragment for an associated item.
+ #[instrument(level = "debug")]
+ fn from_assoc_item(item: &ty::AssocItem) -> Self {
+ let def_id = item.def_id;
+ match item.kind {
+ ty::AssocKind::Fn => {
+ if item.defaultness.has_value() {
+ ItemFragment(FragmentKind::Method, def_id)
+ } else {
+ ItemFragment(FragmentKind::TyMethod, def_id)
+ }
+ }
+ ty::AssocKind::Const => ItemFragment(FragmentKind::AssociatedConstant, def_id),
+ ty::AssocKind::Type => ItemFragment(FragmentKind::AssociatedType, def_id),
+ }
+ }
+
+ /// Render the fragment, including the leading `#`.
+ crate fn render(&self, s: &mut String, tcx: TyCtxt<'_>) -> std::fmt::Result {
+ write!(s, "#")?;
+ match *self {
+ ItemFragment(kind, def_id) => {
+ let name = tcx.item_name(def_id);
+ match kind {
+ FragmentKind::Method => write!(s, "method.{}", name),
+ FragmentKind::TyMethod => write!(s, "tymethod.{}", name),
+ FragmentKind::AssociatedConstant => write!(s, "associatedconstant.{}", name),
+ FragmentKind::AssociatedType => write!(s, "associatedtype.{}", name),
+ FragmentKind::StructField => write!(s, "structfield.{}", name),
+ FragmentKind::Variant => write!(s, "variant.{}", name),
+ FragmentKind::VariantField => {
+ let variant = tcx.item_name(tcx.parent(def_id).unwrap());
+ write!(s, "variant.{}.field.{}", variant, name)
+ }
+ }
+ }
+ }
+ }
+}
+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
struct ResolutionInfo {
module_id: DefId,
#[derive(Clone, Debug, Hash)]
struct CachedLink {
- pub res: (Res, Option<String>),
- pub side_channel: Option<(DefKind, DefId)>,
+ res: (Res, Option<UrlFragment>),
}
struct LinkCollector<'a, 'tcx> {
/// The last module will be used if the parent scope of the current item is
/// unknown.
mod_ids: Vec<DefId>,
- /// This is used to store the kind of associated items,
- /// because `clean` and the disambiguator code expect them to be different.
- /// See the code for associated items on inherent impls for details.
- kind_side_channel: Cell<Option<(DefKind, DefId)>>,
/// Cache the resolved links so we can avoid resolving (and emitting errors for) the same link.
/// The link will be `None` if it could not be resolved (i.e. the error was cached).
visited_links: FxHashMap<ResolutionInfo, Option<CachedLink>>,
/// In particular, this will return an error whenever there aren't three
/// full path segments left in the link.
///
- /// [enum struct variant]: hir::VariantData::Struct
- fn variant_field(
+ /// [enum struct variant]: rustc_hir::VariantData::Struct
+ fn variant_field<'path>(
&self,
path_str: &'path str,
module_id: DefId,
- ) -> Result<(Res, Option<String>), ErrorKind<'path>> {
+ ) -> Result<(Res, Option<ItemFragment>), ErrorKind<'path>> {
let tcx = self.cx.tcx;
let no_res = || ResolutionFailure::NotResolved {
module_id,
debug!("looking for enum variant {}", path_str);
let mut split = path_str.rsplitn(3, "::");
- let (variant_field_str, variant_field_name) = split
+ let variant_field_name = split
.next()
- .map(|f| (f, Symbol::intern(f)))
+ .map(|f| Symbol::intern(f))
.expect("fold_item should ensure link is non-empty");
- let (variant_str, variant_name) =
+ let variant_name =
// we're not sure this is a variant at all, so use the full string
// If there's no second component, the link looks like `[path]`.
// So there's no partial res and we should say the whole link failed to resolve.
- split.next().map(|f| (f, Symbol::intern(f))).ok_or_else(no_res)?;
+ split.next().map(|f| Symbol::intern(f)).ok_or_else(no_res)?;
let path = split
.next()
.map(|f| f.to_owned())
.inherent_impls(did)
.iter()
.flat_map(|imp| tcx.associated_items(*imp).in_definition_order())
- .any(|item| item.ident.name == variant_name)
+ .any(|item| item.name == variant_name)
{
// This is just to let `fold_item` know that this shouldn't be considered;
// it's a bug for the error to make it to the user
}
match tcx.type_of(did).kind() {
ty::Adt(def, _) if def.is_enum() => {
- if def.all_fields().any(|item| item.ident.name == variant_field_name) {
- Ok((
- ty_res,
- Some(format!(
- "variant.{}.field.{}",
- variant_str, variant_field_name
- )),
- ))
+ if let Some(field) = def.all_fields().find(|f| f.name == variant_field_name)
+ {
+ Ok((ty_res, Some(ItemFragment(FragmentKind::VariantField, field.did))))
} else {
Err(ResolutionFailure::NotResolved {
module_id,
partial_res: Some(Res::Def(DefKind::Enum, def.did)),
- unresolved: variant_field_str.into(),
+ unresolved: variant_field_name.to_string().into(),
}
.into())
}
_ => Err(ResolutionFailure::NotResolved {
module_id,
partial_res: Some(ty_res),
- unresolved: variant_str.into(),
+ unresolved: variant_name.to_string().into(),
}
.into()),
}
prim_ty: PrimitiveType,
ns: Namespace,
item_name: Symbol,
- ) -> Option<(Res, String, Option<(DefKind, DefId)>)> {
+ ) -> Option<(Res, ItemFragment)> {
let tcx = self.cx.tcx;
prim_ty.impls(tcx).into_iter().find_map(|&impl_| {
tcx.associated_items(impl_)
.find_by_name_and_namespace(tcx, Ident::with_dummy_span(item_name), ns, impl_)
.map(|item| {
- let kind = item.kind;
- let out = match kind {
- ty::AssocKind::Fn => "method",
- ty::AssocKind::Const => "associatedconstant",
- ty::AssocKind::Type => "associatedtype",
- };
- let fragment = format!("{}.{}", out, item_name);
- (Res::Primitive(prim_ty), fragment, Some((kind.as_def_kind(), item.def_id)))
+ let fragment = ItemFragment::from_assoc_item(item);
+ (Res::Primitive(prim_ty), fragment)
})
})
}
path_str: &'a str,
module_id: DefId,
) -> Result<Res, ResolutionFailure<'a>> {
- let path = ast::Path::from_ident(Ident::from_str(path_str));
self.cx.enter_resolver(|resolver| {
- // FIXME(jynelson): does this really need 3 separate lookups?
- if let Ok((Some(ext), res)) = resolver.resolve_macro_path(
- &path,
- None,
- &ParentScope::module(resolver.graph_root(), resolver),
- false,
- false,
- ) {
- if let SyntaxExtensionKind::LegacyBang { .. } = ext.kind {
- return Ok(res.try_into().unwrap());
- }
- }
- if let Some(&res) = resolver.all_macros().get(&Symbol::intern(path_str)) {
- return Ok(res.try_into().unwrap());
- }
+ // NOTE: this needs 2 separate lookups because `resolve_str_path_error` doesn't take
+ // lexical scope into account (it ignores all macros not defined at the mod-level)
debug!("resolving {} as a macro in the module {:?}", path_str, module_id);
if let Ok((_, res)) =
resolver.resolve_str_path_error(DUMMY_SP, path_str, MacroNS, module_id)
return Ok(res);
}
}
+ if let Some(&res) = resolver.all_macros().get(&Symbol::intern(path_str)) {
+ return Ok(res.try_into().unwrap());
+ }
Err(ResolutionFailure::NotResolved {
module_id,
partial_res: None,
path_str: &'path str,
ns: Namespace,
module_id: DefId,
- extra_fragment: &Option<String>,
- ) -> Result<(Res, Option<String>), ErrorKind<'path>> {
+ user_fragment: &Option<String>,
+ ) -> Result<(Res, Option<UrlFragment>), ErrorKind<'path>> {
+ let (res, rustdoc_fragment) = self.resolve_inner(path_str, ns, module_id)?;
+ let chosen_fragment = match (user_fragment, rustdoc_fragment) {
+ (Some(_), Some(r_frag)) => {
+ let diag_res = match r_frag {
+ ItemFragment(_, did) => Res::Def(self.cx.tcx.def_kind(did), did),
+ };
+ let failure = AnchorFailure::RustdocAnchorConflict(diag_res);
+ return Err(ErrorKind::AnchorFailure(failure));
+ }
+ (Some(u_frag), None) => Some(UrlFragment::UserWritten(u_frag.clone())),
+ (None, Some(r_frag)) => Some(UrlFragment::Item(r_frag)),
+ (None, None) => None,
+ };
+ Ok((res, chosen_fragment))
+ }
+
+ fn resolve_inner<'path>(
+ &mut self,
+ path_str: &'path str,
+ ns: Namespace,
+ module_id: DefId,
+ ) -> Result<(Res, Option<ItemFragment>), ErrorKind<'path>> {
if let Some(res) = self.resolve_path(path_str, ns, module_id) {
match res {
// FIXME(#76467): make this fallthrough to lookup the associated
Res::Def(DefKind::AssocFn | DefKind::AssocConst, _) => assert_eq!(ns, ValueNS),
Res::Def(DefKind::AssocTy, _) => assert_eq!(ns, TypeNS),
Res::Def(DefKind::Variant, _) => {
- return handle_variant(self.cx, res, extra_fragment);
+ return handle_variant(self.cx, res);
}
// Not a trait item; just return what we found.
- _ => return Ok((res, extra_fragment.clone())),
+ _ => return Ok((res, None)),
}
}
resolve_primitive(&path_root, TypeNS)
.or_else(|| self.resolve_path(&path_root, TypeNS, module_id))
.and_then(|ty_res| {
- let (res, fragment, side_channel) =
+ let (res, fragment) =
self.resolve_associated_item(ty_res, item_name, ns, module_id)?;
- let result = if extra_fragment.is_some() {
- // NOTE: can never be a primitive since `side_channel.is_none()` only when `res`
- // is a trait (and the side channel DefId is always an associated item).
- let diag_res = side_channel.map_or(res, |(k, r)| Res::Def(k, r));
- Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(diag_res)))
- } else {
- // HACK(jynelson): `clean` expects the type, not the associated item
- // but the disambiguator logic expects the associated item.
- // Store the kind in a side channel so that only the disambiguator logic looks at it.
- if let Some((kind, id)) = side_channel {
- self.kind_side_channel.set(Some((kind, id)));
- }
- Ok((res, Some(fragment)))
- };
- Some(result)
+
+ Some(Ok((res, Some(fragment))))
})
.unwrap_or_else(|| {
if ns == Namespace::ValueNS {
})
}
- /// Returns:
- /// - None if no associated item was found
- /// - Some((_, _, Some(_))) if an item was found and should go through a side channel
- /// - Some((_, _, None)) otherwise
+ /// Convert a PrimitiveType to a Ty, where possible.
+ ///
+ /// This is used for resolving trait impls for primitives
+ fn primitive_type_to_ty(&mut self, prim: PrimitiveType) -> Option<Ty<'tcx>> {
+ use PrimitiveType::*;
+ let tcx = self.cx.tcx;
+
+ // FIXME: Only simple types are supported here, see if we can support
+ // other types such as Tuple, Array, Slice, etc.
+ // See https://github.com/rust-lang/rust/issues/90703#issuecomment-1004263455
+ Some(tcx.mk_ty(match prim {
+ Bool => ty::Bool,
+ Str => ty::Str,
+ Char => ty::Char,
+ Never => ty::Never,
+ I8 => ty::Int(ty::IntTy::I8),
+ I16 => ty::Int(ty::IntTy::I16),
+ I32 => ty::Int(ty::IntTy::I32),
+ I64 => ty::Int(ty::IntTy::I64),
+ I128 => ty::Int(ty::IntTy::I128),
+ Isize => ty::Int(ty::IntTy::Isize),
+ F32 => ty::Float(ty::FloatTy::F32),
+ F64 => ty::Float(ty::FloatTy::F64),
+ U8 => ty::Uint(ty::UintTy::U8),
+ U16 => ty::Uint(ty::UintTy::U16),
+ U32 => ty::Uint(ty::UintTy::U32),
+ U64 => ty::Uint(ty::UintTy::U64),
+ U128 => ty::Uint(ty::UintTy::U128),
+ Usize => ty::Uint(ty::UintTy::Usize),
+ _ => return None,
+ }))
+ }
+
+ /// Resolve an associated item, returning its containing page's `Res`
+ /// and the fragment targeting the associated item on its page.
fn resolve_associated_item(
&mut self,
root_res: Res,
item_name: Symbol,
ns: Namespace,
module_id: DefId,
- ) -> Option<(Res, String, Option<(DefKind, DefId)>)> {
+ ) -> Option<(Res, ItemFragment)> {
let tcx = self.cx.tcx;
match root_res {
- Res::Primitive(prim) => self.resolve_primitive_associated_item(prim, ns, item_name),
+ Res::Primitive(prim) => {
+ self.resolve_primitive_associated_item(prim, ns, item_name).or_else(|| {
+ let assoc_item = self
+ .primitive_type_to_ty(prim)
+ .map(|ty| {
+ resolve_associated_trait_item(ty, module_id, item_name, ns, self.cx)
+ })
+ .flatten();
+
+ assoc_item.map(|item| {
+ let fragment = ItemFragment::from_assoc_item(&item);
+ (root_res, fragment)
+ })
+ })
+ }
Res::Def(DefKind::TyAlias, did) => {
// Resolve the link on the type the alias points to.
// FIXME: if the associated item is defined directly on the type alias,
imp,
)
})
- .map(|item| (item.kind, item.def_id))
+ .copied()
// There should only ever be one associated item that matches from any inherent impl
.next()
// Check if item_name belongs to `impl SomeTrait for SomeItem`
// To handle that properly resolve() would have to support
// something like [`ambi_fn`](<SomeStruct as SomeTrait>::ambi_fn)
.or_else(|| {
- let kind =
- resolve_associated_trait_item(did, module_id, item_name, ns, self.cx);
- debug!("got associated item kind {:?}", kind);
- kind
+ resolve_associated_trait_item(
+ tcx.type_of(did),
+ module_id,
+ item_name,
+ ns,
+ self.cx,
+ )
});
- if let Some((kind, id)) = assoc_item {
- let out = match kind {
- ty::AssocKind::Fn => "method",
- ty::AssocKind::Const => "associatedconstant",
- ty::AssocKind::Type => "associatedtype",
- };
- // HACK(jynelson): `clean` expects the type, not the associated item
- // but the disambiguator logic expects the associated item.
- // Store the kind in a side channel so that only the disambiguator logic looks at it.
- return Some((
- root_res,
- format!("{}.{}", out, item_name),
- Some((kind.as_def_kind(), id)),
- ));
+ debug!("got associated item {:?}", assoc_item);
+
+ if let Some(item) = assoc_item {
+ let fragment = ItemFragment::from_assoc_item(&item);
+ return Some((root_res, fragment));
}
if ns != Namespace::ValueNS {
return None;
}
- debug!("looking for variants or fields named {} for {:?}", item_name, did);
+ debug!("looking for fields named {} for {:?}", item_name, did);
// FIXME: this doesn't really belong in `associated_item` (maybe `variant_field` is better?)
- // NOTE: it's different from variant_field because it resolves fields and variants,
+ // NOTE: it's different from variant_field because it only resolves struct fields,
// not variant fields (2 path segments, not 3).
+ //
+ // We need to handle struct (and union) fields in this code because
+ // syntactically their paths are identical to associated item paths:
+ // `module::Type::field` and `module::Type::Assoc`.
+ //
+ // On the other hand, variant fields can't be mistaken for associated
+ // items because they look like this: `module::Type::Variant::field`.
+ //
+ // Variants themselves don't need to be handled here, even though
+ // they also look like associated items (`module::Type::Variant`),
+ // because they are real Rust syntax (unlike the intra-doc links
+ // field syntax) and are handled by the compiler's resolver.
let def = match tcx.type_of(did).kind() {
- ty::Adt(def, _) => def,
+ ty::Adt(def, _) if !def.is_enum() => def,
_ => return None,
};
- let field = if def.is_enum() {
- def.all_fields().find(|item| item.ident.name == item_name)
- } else {
- def.non_enum_variant().fields.iter().find(|item| item.ident.name == item_name)
- }?;
- let kind = if def.is_enum() { DefKind::Variant } else { DefKind::Field };
- Some((
- root_res,
- format!(
- "{}.{}",
- if def.is_enum() { "variant" } else { "structfield" },
- field.ident
- ),
- Some((kind, field.did)),
- ))
+ let field =
+ def.non_enum_variant().fields.iter().find(|item| item.name == item_name)?;
+ Some((root_res, ItemFragment(FragmentKind::StructField, field.did)))
}
Res::Def(DefKind::Trait, did) => tcx
.associated_items(did)
.find_by_name_and_namespace(tcx, Ident::with_dummy_span(item_name), ns, did)
.map(|item| {
- let kind = match item.kind {
- ty::AssocKind::Const => "associatedconstant",
- ty::AssocKind::Type => "associatedtype",
- ty::AssocKind::Fn => {
- if item.defaultness.has_value() {
- "method"
- } else {
- "tymethod"
- }
- }
- };
-
+ let fragment = ItemFragment::from_assoc_item(item);
let res = Res::Def(item.kind.as_def_kind(), item.def_id);
- (res, format!("{}.{}", kind, item_name), None)
+ (res, fragment)
}),
_ => None,
}
) -> Option<Res> {
// resolve() can't be used for macro namespace
let result = match ns {
- Namespace::MacroNS => self.resolve_macro(path_str, module_id).map_err(ErrorKind::from),
+ Namespace::MacroNS => self
+ .resolve_macro(path_str, module_id)
+ .map(|res| (res, None))
+ .map_err(ErrorKind::from),
Namespace::TypeNS | Namespace::ValueNS => {
- self.resolve(path_str, ns, module_id, extra_fragment).map(|(res, _)| res)
+ self.resolve(path_str, ns, module_id, extra_fragment)
}
};
let res = match result {
- Ok(res) => Some(res),
+ Ok((res, frag)) => {
+ if let Some(UrlFragment::Item(ItemFragment(_, id))) = frag {
+ Some(Res::Def(self.cx.tcx.def_kind(id), id))
+ } else {
+ Some(res)
+ }
+ }
Err(ErrorKind::Resolve(box kind)) => kind.full_res(),
Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(res))) => Some(res),
Err(ErrorKind::AnchorFailure(AnchorFailure::MultipleAnchors)) => None,
};
- self.kind_side_channel.take().map(|(kind, id)| Res::Def(kind, id)).or(res)
+ res
}
}
/// Given `[std::io::Error::source]`, where `source` is unresolved, this would
/// find `std::error::Error::source` and return
/// `<io::Error as error::Error>::source`.
-fn resolve_associated_trait_item(
- did: DefId,
+fn resolve_associated_trait_item<'a>(
+ ty: Ty<'a>,
module: DefId,
item_name: Symbol,
ns: Namespace,
- cx: &mut DocContext<'_>,
-) -> Option<(ty::AssocKind, DefId)> {
+ cx: &mut DocContext<'a>,
+) -> Option<ty::AssocItem> {
// FIXME: this should also consider blanket impls (`impl<T> X for T`). Unfortunately
// `get_auto_trait_and_blanket_impls` is broken because the caching behavior is wrong. In the
// meantime, just don't look for these blanket impls.
// Next consider explicit impls: `impl MyTrait for MyType`
// Give precedence to inherent impls.
- let traits = traits_implemented_by(cx, did, module);
+ let traits = trait_impls_for(cx, ty, module);
debug!("considering traits {:?}", traits);
- let mut candidates = traits.iter().filter_map(|&trait_| {
+ let mut candidates = traits.iter().filter_map(|&(impl_, trait_)| {
cx.tcx
.associated_items(trait_)
.find_by_name_and_namespace(cx.tcx, Ident::with_dummy_span(item_name), ns, trait_)
- .map(|assoc| (assoc.kind, assoc.def_id))
+ .map(|trait_assoc| {
+ trait_assoc_to_impl_assoc_item(cx.tcx, impl_, trait_assoc.def_id)
+ .unwrap_or(trait_assoc)
+ })
});
// FIXME(#74563): warn about ambiguity
debug!("the candidates were {:?}", candidates.clone().collect::<Vec<_>>());
- candidates.next()
+ candidates.next().copied()
}
-/// Given a type, return all traits in scope in `module` implemented by that type.
+/// Find the associated item in the impl `impl_id` that corresponds to the
+/// trait associated item `trait_assoc_id`.
+///
+/// This function returns `None` if no associated item was found in the impl.
+/// This can occur when the trait associated item has a default value that is
+/// not overriden in the impl.
+///
+/// This is just a wrapper around [`TyCtxt::impl_item_implementor_ids()`] and
+/// [`TyCtxt::associated_item()`] (with some helpful logging added).
+#[instrument(level = "debug", skip(tcx))]
+fn trait_assoc_to_impl_assoc_item<'tcx>(
+ tcx: TyCtxt<'tcx>,
+ impl_id: DefId,
+ trait_assoc_id: DefId,
+) -> Option<&'tcx ty::AssocItem> {
+ let trait_to_impl_assoc_map = tcx.impl_item_implementor_ids(impl_id);
+ debug!(?trait_to_impl_assoc_map);
+ let impl_assoc_id = *trait_to_impl_assoc_map.get(&trait_assoc_id)?;
+ debug!(?impl_assoc_id);
+ let impl_assoc = tcx.associated_item(impl_assoc_id);
+ debug!(?impl_assoc);
+ Some(impl_assoc)
+}
+
+/// Given a type, return all trait impls in scope in `module` for that type.
+/// Returns a set of pairs of `(impl_id, trait_id)`.
///
/// NOTE: this cannot be a query because more traits could be available when more crates are compiled!
/// So it is not stable to serialize cross-crate.
-fn traits_implemented_by(cx: &mut DocContext<'_>, type_: DefId, module: DefId) -> FxHashSet<DefId> {
- let mut resolver = cx.resolver.borrow_mut();
- let in_scope_traits = cx.module_trait_cache.entry(module).or_insert_with(|| {
- resolver.access(|resolver| {
- let parent_scope = &ParentScope::module(resolver.expect_module(module), resolver);
- resolver
- .traits_in_scope(None, parent_scope, SyntaxContext::root(), None)
- .into_iter()
- .map(|candidate| candidate.def_id)
- .collect()
- })
- });
-
+#[instrument(level = "debug", skip(cx))]
+fn trait_impls_for<'a>(
+ cx: &mut DocContext<'a>,
+ ty: Ty<'a>,
+ module: DefId,
+) -> FxHashSet<(DefId, DefId)> {
let tcx = cx.tcx;
- let ty = tcx.type_of(type_);
- let iter = in_scope_traits.iter().flat_map(|&trait_| {
+ let iter = cx.resolver_caches.traits_in_scope[&module].iter().flat_map(|trait_candidate| {
+ let trait_ = trait_candidate.def_id;
trace!("considering explicit impl for trait {:?}", trait_);
// Look at each trait implementation to see if it's an impl for `did`
"comparing type {} with kind {:?} against type {:?}",
impl_type,
impl_type.kind(),
- type_
+ ty
);
// Fast path: if this is a primitive simple `==` will work
+ // NOTE: the `match` is necessary; see #92662.
+ // this allows us to ignore generics because the user input
+ // may not include the generic placeholders
+ // e.g. this allows us to match Foo (user comment) with Foo<T> (actual type)
let saw_impl = impl_type == ty
- || match impl_type.kind() {
- // Check if these are the same def_id
- ty::Adt(def, _) => {
- debug!("adt def_id: {:?}", def.did);
- def.did == type_
+ || match (impl_type.kind(), ty.kind()) {
+ (ty::Adt(impl_def, _), ty::Adt(ty_def, _)) => {
+ debug!("impl def_id: {:?}, ty def_id: {:?}", impl_def.did, ty_def.did);
+ impl_def.did == ty_def.did
}
- ty::Foreign(def_id) => *def_id == type_,
_ => false,
};
- if saw_impl { Some(trait_) } else { None }
+ if saw_impl { Some((impl_, trait_)) } else { None }
})
});
iter.collect()
impl<'a, 'tcx> DocVisitor for LinkCollector<'a, 'tcx> {
fn visit_item(&mut self, item: &Item) {
- use rustc_middle::ty::DefIdTree;
-
let parent_node =
item.def_id.as_def_id().and_then(|did| find_nearest_parent_module(self.cx.tcx, did));
if parent_node.is_some() {
{
self.cx.tcx.parent(did)
}
- Some(did) => match self.cx.tcx.parent(did) {
- // HACK(jynelson): `clean` marks associated types as `TypedefItem`, not as `AssocTypeItem`.
- // Fixing this breaks `fn render_deref_methods`.
- // As a workaround, see if the parent of the item is an `impl`; if so this must be an associated item,
- // regardless of what rustdoc wants to call it.
- Some(parent) => {
- let parent_kind = self.cx.tcx.def_kind(parent);
- Some(if parent_kind == DefKind::Impl { parent } else { did })
- }
- None => Some(did),
- },
+ Some(did) => Some(did),
};
// FIXME(jynelson): this shouldn't go through stringification, rustdoc should just use the DefId directly
return None;
}
- let stripped = ori_link.link.replace("`", "");
+ let stripped = ori_link.link.replace('`', "");
let mut parts = stripped.split('#');
let link = parts.next().unwrap();
Some(Ok(PreprocessingInfo {
path_str,
disambiguator,
- extra_fragment: extra_fragment.map(String::from),
+ extra_fragment: extra_fragment.map(|frag| frag.to_owned()),
link_text: link_text.to_owned(),
}))
}
let base_node =
if item.is_mod() && inner_docs { self.mod_ids.last().copied() } else { parent_node };
- let mut module_id = if let Some(id) = base_node {
- id
- } else {
+ let Some(mut module_id) = base_node else {
// This is a bug.
debug!("attempting to resolve item without parent module: {}", path_str);
resolution_failure(
module_id,
dis: disambiguator,
path_str: path_str.to_owned(),
- extra_fragment: extra_fragment.map(String::from),
+ extra_fragment,
},
diag_info.clone(), // this struct should really be Copy, but Range is not :(
matches!(ori_link.kind, LinkType::Reference | LinkType::Shortcut),
}
}
- let report_mismatch = |specified: Disambiguator, resolved: Disambiguator| {
- // The resolved item did not match the disambiguator; give a better error than 'not found'
- let msg = format!("incompatible link kind for `{}`", path_str);
- let callback = |diag: &mut DiagnosticBuilder<'_>, sp: Option<rustc_span::Span>| {
- let note = format!(
- "this link resolved to {} {}, which is not {} {}",
- resolved.article(),
- resolved.descr(),
- specified.article(),
- specified.descr()
- );
- if let Some(sp) = sp {
- diag.span_label(sp, ¬e);
- } else {
- diag.note(¬e);
- }
- suggest_disambiguator(resolved, diag, path_str, &ori_link.link, sp);
- };
- report_diagnostic(self.cx.tcx, BROKEN_INTRA_DOC_LINKS, &msg, &diag_info, callback);
- };
-
- let verify = |kind: DefKind, id: DefId| {
- let (kind, id) = self.kind_side_channel.take().unwrap_or((kind, id));
- debug!("intra-doc link to {} resolved to {:?} (id: {:?})", path_str, res, id);
-
- // Disallow e.g. linking to enums with `struct@`
- debug!("saw kind {:?} with disambiguator {:?}", kind, disambiguator);
- match (kind, disambiguator) {
- | (DefKind::Const | DefKind::ConstParam | DefKind::AssocConst | DefKind::AnonConst, Some(Disambiguator::Kind(DefKind::Const)))
- // NOTE: this allows 'method' to mean both normal functions and associated functions
- // This can't cause ambiguity because both are in the same namespace.
- | (DefKind::Fn | DefKind::AssocFn, Some(Disambiguator::Kind(DefKind::Fn)))
- // These are namespaces; allow anything in the namespace to match
- | (_, Some(Disambiguator::Namespace(_)))
- // If no disambiguator given, allow anything
- | (_, None)
- // All of these are valid, so do nothing
- => {}
- (actual, Some(Disambiguator::Kind(expected))) if actual == expected => {}
- (_, Some(specified @ Disambiguator::Kind(_) | specified @ Disambiguator::Primitive)) => {
- report_mismatch(specified, Disambiguator::Kind(kind));
- return None;
- }
- }
-
- // item can be non-local e.g. when using #[doc(primitive = "pointer")]
- if let Some((src_id, dst_id)) = id
- .as_local()
- // The `expect_def_id()` should be okay because `local_def_id_to_hir_id`
- // would presumably panic if a fake `DefIndex` were passed.
- .and_then(|dst_id| {
- item.def_id.expect_def_id().as_local().map(|src_id| (src_id, dst_id))
- })
- {
- if self.cx.tcx.privacy_access_levels(()).is_exported(src_id)
- && !self.cx.tcx.privacy_access_levels(()).is_exported(dst_id)
- {
- privacy_error(self.cx, &diag_info, path_str);
- }
- }
-
- Some(())
- };
-
match res {
Res::Primitive(prim) => {
- if let Some((kind, id)) = self.kind_side_channel.take() {
+ if let Some(UrlFragment::Item(ItemFragment(_, id))) = fragment {
// We're actually resolving an associated item of a primitive, so we need to
// verify the disambiguator (if any) matches the type of the associated item.
// This case should really follow the same flow as the `Res::Def` branch below,
// doesn't allow statements like `use str::trim;`, making this a (hopefully)
// valid omission. See https://github.com/rust-lang/rust/pull/80660#discussion_r551585677
// for discussion on the matter.
- verify(kind, id)?;
+ let kind = self.cx.tcx.def_kind(id);
+ self.verify_disambiguator(
+ path_str,
+ &ori_link,
+ kind,
+ id,
+ disambiguator,
+ item,
+ &diag_info,
+ )?;
// FIXME: it would be nice to check that the feature gate was enabled in the original crate, not just ignore it altogether.
// However I'm not sure how to check that across crates.
&& item.def_id.is_local()
&& !self.cx.tcx.features().intra_doc_pointers
{
- let span = super::source_span_for_markdown_range(
- self.cx.tcx,
- dox,
- &ori_link.range,
- &item.attrs,
- )
- .unwrap_or_else(|| item.attr_span(self.cx.tcx));
-
- rustc_session::parse::feature_err(
- &self.cx.tcx.sess.parse_sess,
- sym::intra_doc_pointers,
- span,
- "linking to associated items of raw pointers is experimental",
- )
- .note("rustdoc does not allow disambiguating between `*const` and `*mut`, and pointers are unstable until it does")
- .emit();
+ self.report_rawptr_assoc_feature_gate(dox, &ori_link, item);
}
} else {
match disambiguator {
Some(Disambiguator::Primitive | Disambiguator::Namespace(_)) | None => {}
Some(other) => {
- report_mismatch(other, Disambiguator::Primitive);
+ self.report_disambiguator_mismatch(
+ path_str, &ori_link, other, res, &diag_info,
+ );
return None;
}
}
})
}
Res::Def(kind, id) => {
- verify(kind, id)?;
+ let (kind_for_dis, id_for_dis) =
+ if let Some(UrlFragment::Item(ItemFragment(_, id))) = fragment {
+ (self.cx.tcx.def_kind(id), id)
+ } else {
+ (kind, id)
+ };
+ self.verify_disambiguator(
+ path_str,
+ &ori_link,
+ kind_for_dis,
+ id_for_dis,
+ disambiguator,
+ item,
+ &diag_info,
+ )?;
let id = clean::register_res(self.cx, rustc_hir::def::Res::Def(kind, id));
Some(ItemLink { link: ori_link.link, link_text, did: id, fragment })
}
}
}
+ fn verify_disambiguator(
+ &self,
+ path_str: &str,
+ ori_link: &MarkdownLink,
+ kind: DefKind,
+ id: DefId,
+ disambiguator: Option<Disambiguator>,
+ item: &Item,
+ diag_info: &DiagnosticInfo<'_>,
+ ) -> Option<()> {
+ debug!("intra-doc link to {} resolved to {:?}", path_str, (kind, id));
+
+ // Disallow e.g. linking to enums with `struct@`
+ debug!("saw kind {:?} with disambiguator {:?}", kind, disambiguator);
+ match (kind, disambiguator) {
+ | (DefKind::Const | DefKind::ConstParam | DefKind::AssocConst | DefKind::AnonConst, Some(Disambiguator::Kind(DefKind::Const)))
+ // NOTE: this allows 'method' to mean both normal functions and associated functions
+ // This can't cause ambiguity because both are in the same namespace.
+ | (DefKind::Fn | DefKind::AssocFn, Some(Disambiguator::Kind(DefKind::Fn)))
+ // These are namespaces; allow anything in the namespace to match
+ | (_, Some(Disambiguator::Namespace(_)))
+ // If no disambiguator given, allow anything
+ | (_, None)
+ // All of these are valid, so do nothing
+ => {}
+ (actual, Some(Disambiguator::Kind(expected))) if actual == expected => {}
+ (_, Some(specified @ Disambiguator::Kind(_) | specified @ Disambiguator::Primitive)) => {
+ self.report_disambiguator_mismatch(path_str,ori_link,specified, Res::Def(kind, id),diag_info);
+ return None;
+ }
+ }
+
+ // item can be non-local e.g. when using #[doc(primitive = "pointer")]
+ if let Some((src_id, dst_id)) = id
+ .as_local()
+ // The `expect_def_id()` should be okay because `local_def_id_to_hir_id`
+ // would presumably panic if a fake `DefIndex` were passed.
+ .and_then(|dst_id| {
+ item.def_id.expect_def_id().as_local().map(|src_id| (src_id, dst_id))
+ })
+ {
+ if self.cx.tcx.privacy_access_levels(()).is_exported(src_id)
+ && !self.cx.tcx.privacy_access_levels(()).is_exported(dst_id)
+ {
+ privacy_error(self.cx, diag_info, path_str);
+ }
+ }
+
+ Some(())
+ }
+
+ fn report_disambiguator_mismatch(
+ &self,
+ path_str: &str,
+ ori_link: &MarkdownLink,
+ specified: Disambiguator,
+ resolved: Res,
+ diag_info: &DiagnosticInfo<'_>,
+ ) {
+ // The resolved item did not match the disambiguator; give a better error than 'not found'
+ let msg = format!("incompatible link kind for `{}`", path_str);
+ let callback = |diag: &mut DiagnosticBuilder<'_>, sp: Option<rustc_span::Span>| {
+ let note = format!(
+ "this link resolved to {} {}, which is not {} {}",
+ resolved.article(),
+ resolved.descr(),
+ specified.article(),
+ specified.descr(),
+ );
+ if let Some(sp) = sp {
+ diag.span_label(sp, ¬e);
+ } else {
+ diag.note(¬e);
+ }
+ suggest_disambiguator(resolved, diag, path_str, &ori_link.link, sp);
+ };
+ report_diagnostic(self.cx.tcx, BROKEN_INTRA_DOC_LINKS, &msg, &diag_info, callback);
+ }
+
+ fn report_rawptr_assoc_feature_gate(&self, dox: &str, ori_link: &MarkdownLink, item: &Item) {
+ let span =
+ super::source_span_for_markdown_range(self.cx.tcx, dox, &ori_link.range, &item.attrs)
+ .unwrap_or_else(|| item.attr_span(self.cx.tcx));
+ rustc_session::parse::feature_err(
+ &self.cx.tcx.sess.parse_sess,
+ sym::intra_doc_pointers,
+ span,
+ "linking to associated items of raw pointers is experimental",
+ )
+ .note("rustdoc does not allow disambiguating between `*const` and `*mut`, and pointers are unstable until it does")
+ .emit();
+ }
+
fn resolve_with_disambiguator_cached(
&mut self,
key: ResolutionInfo,
diag: DiagnosticInfo<'_>,
cache_resolution_failure: bool,
- ) -> Option<(Res, Option<String>)> {
- // Try to look up both the result and the corresponding side channel value
+ ) -> Option<(Res, Option<UrlFragment>)> {
if let Some(ref cached) = self.visited_links.get(&key) {
match cached {
Some(cached) => {
- self.kind_side_channel.set(cached.side_channel);
return Some(cached.res.clone());
}
None if cache_resolution_failure => return None,
// Cache only if resolved successfully - don't silence duplicate errors
if let Some(res) = res {
// Store result for the actual namespace
- self.visited_links.insert(
- key,
- Some(CachedLink {
- res: res.clone(),
- side_channel: self.kind_side_channel.clone().into_inner(),
- }),
- );
+ self.visited_links.insert(key, Some(CachedLink { res: res.clone() }));
Some(res)
} else {
&mut self,
key: &ResolutionInfo,
diag: DiagnosticInfo<'_>,
- ) -> Option<(Res, Option<String>)> {
+ ) -> Option<(Res, Option<UrlFragment>)> {
let disambiguator = key.dis;
let path_str = &key.path_str;
let base_node = key.module_id;
let mut candidates = PerNS {
macro_ns: self
.resolve_macro(path_str, base_node)
- .map(|res| (res, extra_fragment.clone())),
+ .map(|res| (res, extra_fragment.clone().map(UrlFragment::UserWritten))),
type_ns: match self.resolve(path_str, TypeNS, base_node, extra_fragment) {
Ok(res) => {
debug!("got res in TypeNS: {:?}", res);
// Shouldn't happen but who knows?
Ok((res, Some(fragment)))
}
- (fragment, None) | (None, fragment) => Ok((res, fragment)),
+ (fragment, None) => Ok((res, fragment)),
+ (None, fragment) => {
+ Ok((res, fragment.map(UrlFragment::UserWritten)))
+ }
}
}
}
}
Some(MacroNS) => {
match self.resolve_macro(path_str, base_node) {
- Ok(res) => Some((res, extra_fragment.clone())),
+ Ok(res) => Some((res, extra_fragment.clone().map(UrlFragment::UserWritten))),
Err(mut kind) => {
// `resolve_macro` only looks in the macro namespace. Try to give a better error if possible.
for ns in [TypeNS, ValueNS] {
}
}
- fn from_res(res: Res) -> Self {
- match res {
- Res::Def(kind, _) => Disambiguator::Kind(kind),
- Res::Primitive(_) => Disambiguator::Primitive,
- }
- }
-
- /// Used for error reporting.
- fn suggestion(self) -> Suggestion {
- let kind = match self {
- Disambiguator::Primitive => return Suggestion::Prefix("prim"),
- Disambiguator::Kind(kind) => kind,
- Disambiguator::Namespace(_) => panic!("display_for cannot be used on namespaces"),
- };
- if kind == DefKind::Macro(MacroKind::Bang) {
- return Suggestion::Macro;
- } else if kind == DefKind::Fn || kind == DefKind::AssocFn {
- return Suggestion::Function;
- } else if kind == DefKind::Field {
- return Suggestion::RemoveDisambiguator;
- }
-
- let prefix = match kind {
- DefKind::Struct => "struct",
- DefKind::Enum => "enum",
- DefKind::Trait => "trait",
- DefKind::Union => "union",
- DefKind::Mod => "mod",
- DefKind::Const | DefKind::ConstParam | DefKind::AssocConst | DefKind::AnonConst => {
- "const"
- }
- DefKind::Static => "static",
- DefKind::Macro(MacroKind::Derive) => "derive",
- // Now handle things that don't have a specific disambiguator
- _ => match kind
- .ns()
- .expect("tried to calculate a disambiguator for a def without a namespace?")
- {
- Namespace::TypeNS => "type",
- Namespace::ValueNS => "value",
- Namespace::MacroNS => "macro",
- },
- };
-
- Suggestion::Prefix(prefix)
- }
-
fn ns(self) -> Namespace {
match self {
Self::Namespace(n) => n,
fn descr(self) -> &'static str {
match self {
Self::Namespace(n) => n.descr(),
- // HACK(jynelson): by looking at the source I saw the DefId we pass
- // for `expected.descr()` doesn't matter, since it's not a crate
- Self::Kind(k) => k.descr(DefId::local(hir::def_id::DefIndex::from_usize(0))),
+ // HACK(jynelson): the source of `DefKind::descr` only uses the DefId for
+ // printing "module" vs "crate" so using the wrong ID is not a huge problem
+ Self::Kind(k) => k.descr(CRATE_DEF_ID.to_def_id()),
Self::Primitive => "builtin type",
}
}
}
sugg
}
- Self::RemoveDisambiguator => return vec![(sp, path_str.into())],
+ Self::RemoveDisambiguator => vec![(sp, path_str.into())],
}
}
}
// If so, report it and say the first which failed; if not, say the first path segment didn't resolve.
let mut name = path_str;
'outer: loop {
- let (start, end) = if let Some(x) = split(name) {
- x
- } else {
+ let Some((start, end)) = split(name) else {
// avoid bug that marked [Quux::Z] as missing Z, not Quux
if partial_res.is_none() {
*unresolved = name.into();
ResolutionFailure::NotResolved { .. } => unreachable!("handled above"),
ResolutionFailure::Dummy => continue,
ResolutionFailure::WrongNamespace { res, expected_ns } => {
- if let Res::Def(kind, _) = res {
- let disambiguator = Disambiguator::Kind(kind);
- suggest_disambiguator(
- disambiguator,
- diag,
- path_str,
- diag_info.ori_link,
- sp,
- )
- }
+ suggest_disambiguator(res, diag, path_str, diag_info.ori_link, sp);
format!(
"this link resolves to {}, which is not in the {} namespace",
}
for res in candidates {
- let disambiguator = Disambiguator::from_res(res);
- suggest_disambiguator(disambiguator, diag, path_str, diag_info.ori_link, sp);
+ suggest_disambiguator(res, diag, path_str, diag_info.ori_link, sp);
}
});
}
/// In case of an ambiguity or mismatched disambiguator, suggest the correct
/// disambiguator.
fn suggest_disambiguator(
- disambiguator: Disambiguator,
+ res: Res,
diag: &mut DiagnosticBuilder<'_>,
path_str: &str,
ori_link: &str,
sp: Option<rustc_span::Span>,
) {
- let suggestion = disambiguator.suggestion();
- let help = format!("to link to the {}, {}", disambiguator.descr(), suggestion.descr());
+ let suggestion = res.disambiguator_suggestion();
+ let help = format!("to link to the {}, {}", res.descr(), suggestion.descr());
if let Some(sp) = sp {
let mut spans = suggestion.as_help_span(path_str, ori_link, sp);
let sym;
let item_name = match diag_info.item.name {
Some(name) => {
- sym = name.as_str();
- &*sym
+ sym = name;
+ sym.as_str()
}
None => "<unknown>",
};
fn handle_variant(
cx: &DocContext<'_>,
res: Res,
- extra_fragment: &Option<String>,
-) -> Result<(Res, Option<String>), ErrorKind<'static>> {
- use rustc_middle::ty::DefIdTree;
-
- if extra_fragment.is_some() {
- // NOTE: `res` can never be a primitive since this function is only called when `tcx.def_kind(res) == DefKind::Variant`.
- return Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(res)));
- }
+) -> Result<(Res, Option<ItemFragment>), ErrorKind<'static>> {
cx.tcx
.parent(res.def_id(cx.tcx))
.map(|parent| {
let parent_def = Res::Def(DefKind::Enum, parent);
let variant = cx.tcx.expect_variant_res(res.as_hir_res().unwrap());
- (parent_def, Some(format!("variant.{}", variant.ident.name)))
+ (parent_def, Some(ItemFragment(FragmentKind::Variant, variant.def_id)))
})
.ok_or_else(|| ResolutionFailure::NoParentItem.into())
}