mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 20:09:19 +00:00
hir: resolve associated items in docs (excl. type aliases)
This commit is contained in:
parent
d987137b4e
commit
fe6f931ac2
3 changed files with 102 additions and 12 deletions
|
@ -1,5 +1,7 @@
|
||||||
//! Attributes & documentation for hir types.
|
//! Attributes & documentation for hir types.
|
||||||
|
|
||||||
|
use std::ops::ControlFlow;
|
||||||
|
|
||||||
use base_db::FileId;
|
use base_db::FileId;
|
||||||
use hir_def::{
|
use hir_def::{
|
||||||
attr::AttrsWithOwner,
|
attr::AttrsWithOwner,
|
||||||
|
@ -13,13 +15,13 @@ use hir_expand::{
|
||||||
name::Name,
|
name::Name,
|
||||||
span_map::{RealSpanMap, SpanMapRef},
|
span_map::{RealSpanMap, SpanMapRef},
|
||||||
};
|
};
|
||||||
use hir_ty::db::HirDatabase;
|
use hir_ty::{db::HirDatabase, method_resolution};
|
||||||
use syntax::{ast, AstNode};
|
use syntax::{ast, AstNode};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Adt, AsAssocItem, AssocItem, BuiltinType, Const, ConstParam, DocLinkDef, Enum, ExternCrateDecl,
|
Adt, AsAssocItem, AssocItem, BuiltinType, Const, ConstParam, DocLinkDef, Enum, ExternCrateDecl,
|
||||||
Field, Function, GenericParam, Impl, LifetimeParam, Macro, Module, ModuleDef, Static, Struct,
|
Field, Function, GenericParam, HasCrate, Impl, LifetimeParam, Macro, Module, ModuleDef, Static,
|
||||||
Trait, TraitAlias, TypeAlias, TypeParam, Union, Variant, VariantDef,
|
Struct, Trait, TraitAlias, Type, TypeAlias, TypeParam, Union, Variant, VariantDef,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait HasAttrs {
|
pub trait HasAttrs {
|
||||||
|
@ -205,8 +207,14 @@ fn resolve_assoc_or_field(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// FIXME: Resolve associated items here, e.g. `Option::map`. Note that associated items take
|
// Resolve inherent items first, then trait items, then fields.
|
||||||
// precedence over fields.
|
if let Some(assoc_item_def) = resolve_assoc_item(db, &ty, &name, ns) {
|
||||||
|
return Some(assoc_item_def);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(impl_trait_item_def) = resolve_impl_trait_item(db, resolver, &ty, &name, ns) {
|
||||||
|
return Some(impl_trait_item_def);
|
||||||
|
}
|
||||||
|
|
||||||
let variant_def = match ty.as_adt()? {
|
let variant_def = match ty.as_adt()? {
|
||||||
Adt::Struct(it) => it.into(),
|
Adt::Struct(it) => it.into(),
|
||||||
|
@ -216,6 +224,69 @@ fn resolve_assoc_or_field(
|
||||||
resolve_field(db, variant_def, name, ns)
|
resolve_field(db, variant_def, name, ns)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn resolve_assoc_item(
|
||||||
|
db: &dyn HirDatabase,
|
||||||
|
ty: &Type,
|
||||||
|
name: &Name,
|
||||||
|
ns: Option<Namespace>,
|
||||||
|
) -> Option<DocLinkDef> {
|
||||||
|
ty.iterate_assoc_items(db, ty.krate(db), move |assoc_item| {
|
||||||
|
if assoc_item.name(db)? != *name {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
as_module_def_if_namespace_matches(assoc_item, ns)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_impl_trait_item(
|
||||||
|
db: &dyn HirDatabase,
|
||||||
|
resolver: Resolver,
|
||||||
|
ty: &Type,
|
||||||
|
name: &Name,
|
||||||
|
ns: Option<Namespace>,
|
||||||
|
) -> Option<DocLinkDef> {
|
||||||
|
let canonical = ty.canonical();
|
||||||
|
let krate = ty.krate(db);
|
||||||
|
let environment = resolver.generic_def().map_or_else(
|
||||||
|
|| crate::TraitEnvironment::empty(krate.id).into(),
|
||||||
|
|d| db.trait_environment(d),
|
||||||
|
);
|
||||||
|
let traits_in_scope = resolver.traits_in_scope(db.upcast());
|
||||||
|
|
||||||
|
let mut result = None;
|
||||||
|
|
||||||
|
// `ty.iterate_path_candidates()` require a scope, which is not available when resolving
|
||||||
|
// attributes here. Use path resolution directly instead.
|
||||||
|
//
|
||||||
|
// FIXME: resolve type aliases (which are not yielded by iterate_path_candidates)
|
||||||
|
method_resolution::iterate_path_candidates(
|
||||||
|
&canonical,
|
||||||
|
db,
|
||||||
|
environment,
|
||||||
|
&traits_in_scope,
|
||||||
|
method_resolution::VisibleFromModule::None,
|
||||||
|
Some(name),
|
||||||
|
&mut |assoc_item_id| {
|
||||||
|
let assoc_item: AssocItem = assoc_item_id.into();
|
||||||
|
|
||||||
|
debug_assert_eq!(assoc_item.name(db).as_ref(), Some(name));
|
||||||
|
|
||||||
|
// If two traits in scope define the same item, Rustdoc links to no specific trait (for
|
||||||
|
// instance, given two methods `a`, Rustdoc simply links to `method.a` with no
|
||||||
|
// disambiguation) so we just pick the first one we find as well.
|
||||||
|
result = as_module_def_if_namespace_matches(assoc_item, ns);
|
||||||
|
|
||||||
|
if result.is_some() {
|
||||||
|
ControlFlow::Break(())
|
||||||
|
} else {
|
||||||
|
ControlFlow::Continue(())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
fn resolve_field(
|
fn resolve_field(
|
||||||
db: &dyn HirDatabase,
|
db: &dyn HirDatabase,
|
||||||
def: VariantDef,
|
def: VariantDef,
|
||||||
|
@ -228,6 +299,19 @@ fn resolve_field(
|
||||||
def.fields(db).into_iter().find(|f| f.name(db) == name).map(DocLinkDef::Field)
|
def.fields(db).into_iter().find(|f| f.name(db) == name).map(DocLinkDef::Field)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn as_module_def_if_namespace_matches(
|
||||||
|
assoc_item: AssocItem,
|
||||||
|
ns: Option<Namespace>,
|
||||||
|
) -> Option<DocLinkDef> {
|
||||||
|
let (def, expected_ns) = match assoc_item {
|
||||||
|
AssocItem::Function(it) => (ModuleDef::Function(it), Namespace::Values),
|
||||||
|
AssocItem::Const(it) => (ModuleDef::Const(it), Namespace::Values),
|
||||||
|
AssocItem::TypeAlias(it) => (ModuleDef::TypeAlias(it), Namespace::Types),
|
||||||
|
};
|
||||||
|
|
||||||
|
(ns.unwrap_or(expected_ns) == expected_ns).then(|| DocLinkDef::ModuleDef(def))
|
||||||
|
}
|
||||||
|
|
||||||
fn modpath_from_str(db: &dyn HirDatabase, link: &str) -> Option<ModPath> {
|
fn modpath_from_str(db: &dyn HirDatabase, link: &str) -> Option<ModPath> {
|
||||||
// FIXME: this is not how we should get a mod path here.
|
// FIXME: this is not how we should get a mod path here.
|
||||||
let try_get_modpath = |link: &str| {
|
let try_get_modpath = |link: &str| {
|
||||||
|
|
|
@ -4121,6 +4121,10 @@ impl Type {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn canonical(&self) -> Canonical<Ty> {
|
||||||
|
hir_ty::replace_errors_with_variables(&self.ty)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns types that this type dereferences to (including this type itself). The returned
|
/// Returns types that this type dereferences to (including this type itself). The returned
|
||||||
/// iterator won't yield the same type more than once even if the deref chain contains a cycle.
|
/// iterator won't yield the same type more than once even if the deref chain contains a cycle.
|
||||||
pub fn autoderef(&self, db: &dyn HirDatabase) -> impl Iterator<Item = Type> + '_ {
|
pub fn autoderef(&self, db: &dyn HirDatabase) -> impl Iterator<Item = Type> + '_ {
|
||||||
|
|
|
@ -462,14 +462,15 @@ mod module {}
|
||||||
fn doc_links_inherent_impl_items() {
|
fn doc_links_inherent_impl_items() {
|
||||||
check_doc_links(
|
check_doc_links(
|
||||||
r#"
|
r#"
|
||||||
// /// [`Struct::CONST`]
|
/// [`Struct::CONST`]
|
||||||
// /// [`Struct::function`]
|
/// [`Struct::function`]
|
||||||
/// FIXME #9694
|
|
||||||
struct Struct$0;
|
struct Struct$0;
|
||||||
|
|
||||||
impl Struct {
|
impl Struct {
|
||||||
const CONST: () = ();
|
const CONST: () = ();
|
||||||
|
// ^^^^^ Struct::CONST
|
||||||
fn function() {}
|
fn function() {}
|
||||||
|
// ^^^^^^^^ Struct::function
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
|
@ -482,12 +483,13 @@ fn doc_links_trait_impl_items() {
|
||||||
trait Trait {
|
trait Trait {
|
||||||
type Type;
|
type Type;
|
||||||
const CONST: usize;
|
const CONST: usize;
|
||||||
|
// ^^^^^ Struct::CONST
|
||||||
fn function();
|
fn function();
|
||||||
|
// ^^^^^^^^ Struct::function
|
||||||
}
|
}
|
||||||
// /// [`Struct::Type`]
|
// FIXME #9694: [`Struct::Type`]
|
||||||
// /// [`Struct::CONST`]
|
/// [`Struct::CONST`]
|
||||||
// /// [`Struct::function`]
|
/// [`Struct::function`]
|
||||||
/// FIXME #9694
|
|
||||||
struct Struct$0;
|
struct Struct$0;
|
||||||
|
|
||||||
impl Trait for Struct {
|
impl Trait for Struct {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue