mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 21:28:51 +00:00
Differentiate method/tymethod by determining 'defaultness'
Currently a method only has defaultness if it is a provided trait method, but this will change when specialisation is available and may need to become a concept known to hir. I opted to go for a 'fewest changes' approach given specialisation is still under development.
This commit is contained in:
parent
62b76e7004
commit
c648884397
7 changed files with 31 additions and 9 deletions
|
@ -772,7 +772,14 @@ impl Function {
|
|||
hir_ty::diagnostics::validate_body(db, self.id.into(), sink)
|
||||
}
|
||||
|
||||
pub fn parent_def(self, db: &dyn HirDatabase) -> Option<MethodOwner> {
|
||||
/// Whether this function declaration has a definition.
|
||||
///
|
||||
/// This is false in the case of required (not provided) trait methods.
|
||||
pub fn has_body(self, db: &dyn HirDatabase) -> bool {
|
||||
db.function_data(self.id).has_body
|
||||
}
|
||||
|
||||
pub fn method_owner(self, db: &dyn HirDatabase) -> Option<MethodOwner> {
|
||||
match self.as_assoc_item(db).map(|assoc| assoc.container(db)) {
|
||||
Some(AssocItemContainer::Trait(t)) => Some(t.into()),
|
||||
Some(AssocItemContainer::ImplDef(imp)) => {
|
||||
|
|
|
@ -35,8 +35,8 @@ pub use crate::{
|
|||
code_model::{
|
||||
Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const,
|
||||
Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function,
|
||||
GenericDef, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static,
|
||||
Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
|
||||
GenericDef, HasVisibility, ImplDef, Local, MacroDef, MethodOwner, Module, ModuleDef,
|
||||
ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
|
||||
},
|
||||
has_source::HasSource,
|
||||
semantics::{original_range, PathResolution, Semantics, SemanticsScope},
|
||||
|
|
|
@ -25,6 +25,7 @@ pub struct FunctionData {
|
|||
/// True if the first param is `self`. This is relevant to decide whether this
|
||||
/// can be called as a method.
|
||||
pub has_self_param: bool,
|
||||
pub has_body: bool,
|
||||
pub is_unsafe: bool,
|
||||
pub is_varargs: bool,
|
||||
pub visibility: RawVisibility,
|
||||
|
@ -42,6 +43,7 @@ impl FunctionData {
|
|||
ret_type: func.ret_type.clone(),
|
||||
attrs: item_tree.attrs(ModItem::from(loc.id.value).into()).clone(),
|
||||
has_self_param: func.has_self_param,
|
||||
has_body: func.has_body,
|
||||
is_unsafe: func.is_unsafe,
|
||||
is_varargs: func.is_varargs,
|
||||
visibility: item_tree[func.visibility].clone(),
|
||||
|
|
|
@ -505,6 +505,7 @@ pub struct Function {
|
|||
pub visibility: RawVisibilityId,
|
||||
pub generic_params: GenericParamsId,
|
||||
pub has_self_param: bool,
|
||||
pub has_body: bool,
|
||||
pub is_unsafe: bool,
|
||||
pub params: Box<[TypeRef]>,
|
||||
pub is_varargs: bool,
|
||||
|
|
|
@ -330,12 +330,15 @@ impl Ctx {
|
|||
ret_type
|
||||
};
|
||||
|
||||
let has_body = func.body().is_some();
|
||||
|
||||
let ast_id = self.source_ast_id_map.ast_id(func);
|
||||
let mut res = Function {
|
||||
name,
|
||||
visibility,
|
||||
generic_params: GenericParamsId::EMPTY,
|
||||
has_self_param,
|
||||
has_body,
|
||||
is_unsafe: func.unsafe_token().is_some(),
|
||||
params: params.into_boxed_slice(),
|
||||
is_varargs,
|
||||
|
|
|
@ -13,7 +13,7 @@ use ide_db::{defs::Definition, RootDatabase};
|
|||
|
||||
use hir::{
|
||||
db::{DefDatabase, HirDatabase},
|
||||
Adt, AsName, AssocItem, Crate, Field, HasAttrs, ItemInNs, ModuleDef,
|
||||
Adt, AsName, AssocItem, Crate, Field, HasAttrs, ItemInNs, MethodOwner, ModuleDef,
|
||||
};
|
||||
use ide_db::{
|
||||
defs::{classify_name, classify_name_ref, Definition},
|
||||
|
@ -117,7 +117,7 @@ fn get_doc_link(db: &RootDatabase, definition: Definition) -> Option<String> {
|
|||
let target_def: ModuleDef = match definition {
|
||||
Definition::ModuleDef(moddef) => match moddef {
|
||||
ModuleDef::Function(f) => {
|
||||
f.parent_def(db).map(|mowner| mowner.into()).unwrap_or_else(|| f.clone().into())
|
||||
f.method_owner(db).map(|mowner| mowner.into()).unwrap_or_else(|| f.clone().into())
|
||||
}
|
||||
moddef => moddef,
|
||||
},
|
||||
|
@ -401,9 +401,18 @@ fn get_symbol_fragment(db: &dyn HirDatabase, field_or_assoc: &FieldOrAssocItem)
|
|||
Some(match field_or_assoc {
|
||||
FieldOrAssocItem::Field(field) => format!("#structfield.{}", field.name(db)),
|
||||
FieldOrAssocItem::AssocItem(assoc) => match assoc {
|
||||
// TODO: Rustdoc sometimes uses tymethod instead of method. This case needs to be investigated.
|
||||
AssocItem::Function(function) => format!("#method.{}", function.name(db)),
|
||||
// TODO: This might be the old method for documenting associated constants, i32::MAX uses a separate page...
|
||||
AssocItem::Function(function) => {
|
||||
let is_trait_method =
|
||||
matches!(function.method_owner(db), Some(MethodOwner::Trait(..)));
|
||||
// This distinction may get more complicated when specialisation is available.
|
||||
// In particular this decision is made based on whether a method 'has defaultness'.
|
||||
// Currently this is only the case for provided trait methods.
|
||||
if is_trait_method && !function.has_body(db) {
|
||||
format!("#tymethod.{}", function.name(db))
|
||||
} else {
|
||||
format!("#method.{}", function.name(db))
|
||||
}
|
||||
}
|
||||
AssocItem::Const(constant) => format!("#associatedconstant.{}", constant.name(db)?),
|
||||
AssocItem::TypeAlias(ty) => format!("#associatedtype.{}", ty.name(db)),
|
||||
},
|
||||
|
|
|
@ -425,7 +425,7 @@ export function openDocs(ctx: Ctx): Cmd {
|
|||
const client = ctx.client;
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
if (!editor || !client) {
|
||||
return
|
||||
return;
|
||||
};
|
||||
|
||||
const position = editor.selection.active;
|
||||
|
|
Loading…
Reference in a new issue