internal: Remove PathResolution::AssocItem

This commit is contained in:
Lukas Wirth 2022-04-01 18:32:05 +02:00
parent a1d684e951
commit c290e68ff9
7 changed files with 28 additions and 36 deletions

View File

@ -30,9 +30,9 @@
db::HirDatabase, db::HirDatabase,
semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx},
source_analyzer::{resolve_hir_path, SourceAnalyzer}, source_analyzer::{resolve_hir_path, SourceAnalyzer},
Access, AssocItem, BuiltinAttr, Callable, ConstParam, Crate, Field, Function, HasSource, Access, BuiltinAttr, Callable, ConstParam, Crate, Field, Function, HasSource, HirFileId, Impl,
HirFileId, Impl, InFile, Label, LifetimeParam, Local, Macro, Module, ModuleDef, Name, Path, InFile, Label, LifetimeParam, Local, Macro, Module, ModuleDef, Name, Path, ScopeDef,
ScopeDef, ToolModule, Trait, Type, TypeAlias, TypeParam, VariantDef, ToolModule, Trait, Type, TypeAlias, TypeParam, VariantDef,
}; };
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -46,7 +46,6 @@ pub enum PathResolution {
/// A const parameter /// A const parameter
ConstParam(ConstParam), ConstParam(ConstParam),
SelfType(Impl), SelfType(Impl),
AssocItem(AssocItem),
BuiltinAttr(BuiltinAttr), BuiltinAttr(BuiltinAttr),
ToolModule(ToolModule), ToolModule(ToolModule),
} }
@ -76,10 +75,6 @@ pub(crate) fn in_type_ns(&self) -> Option<TypeNs> {
| PathResolution::ConstParam(_) => None, | PathResolution::ConstParam(_) => None,
PathResolution::TypeParam(param) => Some(TypeNs::GenericParam((*param).into())), PathResolution::TypeParam(param) => Some(TypeNs::GenericParam((*param).into())),
PathResolution::SelfType(impl_def) => Some(TypeNs::SelfType((*impl_def).into())), PathResolution::SelfType(impl_def) => Some(TypeNs::SelfType((*impl_def).into())),
PathResolution::AssocItem(AssocItem::Const(_) | AssocItem::Function(_)) => None,
PathResolution::AssocItem(AssocItem::TypeAlias(alias)) => {
Some(TypeNs::TypeAliasId((*alias).into()))
}
} }
} }
} }

View File

@ -40,8 +40,9 @@
}; };
use crate::{ use crate::{
db::HirDatabase, semantics::PathResolution, Adt, BuiltinAttr, BuiltinType, Const, Field, db::HirDatabase, semantics::PathResolution, Adt, AssocItem, BuiltinAttr, BuiltinType, Const,
Function, Local, Macro, ModuleDef, Static, Struct, ToolModule, Trait, Type, TypeAlias, Variant, Field, Function, Local, Macro, ModuleDef, Static, Struct, ToolModule, Trait, Type, TypeAlias,
Variant,
}; };
use base_db::CrateId; use base_db::CrateId;
@ -302,7 +303,7 @@ pub(crate) fn resolve_path(
let expr_id = self.expr_id(db, &path_expr.into())?; let expr_id = self.expr_id(db, &path_expr.into())?;
let infer = self.infer.as_ref()?; let infer = self.infer.as_ref()?;
if let Some(assoc) = infer.assoc_resolutions_for_expr(expr_id) { if let Some(assoc) = infer.assoc_resolutions_for_expr(expr_id) {
return Some(PathResolution::AssocItem(assoc.into())); return Some(PathResolution::Def(AssocItem::from(assoc).into()));
} }
if let Some(VariantId::EnumVariantId(variant)) = if let Some(VariantId::EnumVariantId(variant)) =
infer.variant_resolution_for_expr(expr_id) infer.variant_resolution_for_expr(expr_id)
@ -313,7 +314,7 @@ pub(crate) fn resolve_path(
} else if let Some(path_pat) = parent().and_then(ast::PathPat::cast) { } else if let Some(path_pat) = parent().and_then(ast::PathPat::cast) {
let pat_id = self.pat_id(&path_pat.into())?; let pat_id = self.pat_id(&path_pat.into())?;
if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_pat(pat_id) { if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_pat(pat_id) {
return Some(PathResolution::AssocItem(assoc.into())); return Some(PathResolution::Def(AssocItem::from(assoc).into()));
} }
if let Some(VariantId::EnumVariantId(variant)) = if let Some(VariantId::EnumVariantId(variant)) =
self.infer.as_ref()?.variant_resolution_for_pat(pat_id) self.infer.as_ref()?.variant_resolution_for_pat(pat_id)

View File

@ -198,9 +198,6 @@ fn signature_help_for_generics(
| hir::PathResolution::Def(hir::ModuleDef::Macro(_)) | hir::PathResolution::Def(hir::ModuleDef::Macro(_))
| hir::PathResolution::Def(hir::ModuleDef::Module(_)) | hir::PathResolution::Def(hir::ModuleDef::Module(_))
| hir::PathResolution::Def(hir::ModuleDef::Static(_)) => return None, | hir::PathResolution::Def(hir::ModuleDef::Static(_)) => return None,
hir::PathResolution::AssocItem(hir::AssocItem::Function(it)) => it.into(),
hir::PathResolution::AssocItem(hir::AssocItem::TypeAlias(it)) => it.into(),
hir::PathResolution::AssocItem(hir::AssocItem::Const(_)) => return None,
hir::PathResolution::BuiltinAttr(_) hir::PathResolution::BuiltinAttr(_)
| hir::PathResolution::ToolModule(_) | hir::PathResolution::ToolModule(_)
| hir::PathResolution::Local(_) | hir::PathResolution::Local(_)

View File

@ -185,7 +185,6 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
}?; }?;
let function = match ctx.sema.resolve_path(&path)? { let function = match ctx.sema.resolve_path(&path)? {
PathResolution::Def(hir::ModuleDef::Function(f)) => f, PathResolution::Def(hir::ModuleDef::Function(f)) => f,
PathResolution::AssocItem(hir::AssocItem::Function(f)) => f,
_ => return None, _ => return None,
}; };
(function, format!("Inline `{}`", path)) (function, format!("Inline `{}`", path))

View File

@ -482,14 +482,6 @@ impl From<PathResolution> for Definition {
fn from(path_resolution: PathResolution) -> Self { fn from(path_resolution: PathResolution) -> Self {
match path_resolution { match path_resolution {
PathResolution::Def(def) => def.into(), PathResolution::Def(def) => def.into(),
PathResolution::AssocItem(item) => {
let def: ModuleDef = match item {
hir::AssocItem::Function(it) => it.into(),
hir::AssocItem::Const(it) => it.into(),
hir::AssocItem::TypeAlias(it) => it.into(),
};
def.into()
}
PathResolution::Local(local) => Definition::Local(local), PathResolution::Local(local) => Definition::Local(local),
PathResolution::TypeParam(par) => Definition::GenericParam(par.into()), PathResolution::TypeParam(par) => Definition::GenericParam(par.into()),
PathResolution::ConstParam(par) => Definition::GenericParam(par.into()), PathResolution::ConstParam(par) => Definition::GenericParam(par.into()),

View File

@ -2,7 +2,7 @@
use crate::helpers::mod_path_to_ast; use crate::helpers::mod_path_to_ast;
use either::Either; use either::Either;
use hir::{HirDisplay, SemanticsScope}; use hir::{AsAssocItem, HirDisplay, SemanticsScope};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use syntax::{ use syntax::{
ast::{self, AstNode}, ast::{self, AstNode},
@ -197,7 +197,7 @@ fn transform_path(&self, path: ast::Path) -> Option<()> {
} }
} }
} }
hir::PathResolution::Def(def) => { hir::PathResolution::Def(def) if def.as_assoc_item(self.source_scope.db).is_none() => {
if let hir::ModuleDef::Trait(_) = def { if let hir::ModuleDef::Trait(_) = def {
if matches!(path.segment()?.kind()?, ast::PathSegmentKind::Type { .. }) { if matches!(path.segment()?.kind()?, ast::PathSegmentKind::Type { .. }) {
// `speculative_resolve` resolves segments like `<T as // `speculative_resolve` resolves segments like `<T as
@ -222,7 +222,7 @@ fn transform_path(&self, path: ast::Path) -> Option<()> {
hir::PathResolution::Local(_) hir::PathResolution::Local(_)
| hir::PathResolution::ConstParam(_) | hir::PathResolution::ConstParam(_)
| hir::PathResolution::SelfType(_) | hir::PathResolution::SelfType(_)
| hir::PathResolution::AssocItem(_) | hir::PathResolution::Def(_)
| hir::PathResolution::BuiltinAttr(_) | hir::PathResolution::BuiltinAttr(_)
| hir::PathResolution::ToolModule(_) => (), | hir::PathResolution::ToolModule(_) => (),
} }

View File

@ -2,6 +2,7 @@
use crate::errors::error; use crate::errors::error;
use crate::{parsing, SsrError}; use crate::{parsing, SsrError};
use hir::AsAssocItem;
use ide_db::base_db::FilePosition; use ide_db::base_db::FilePosition;
use parsing::Placeholder; use parsing::Placeholder;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
@ -82,14 +83,17 @@ fn resolve_pattern_tree(&self, pattern: SyntaxNode) -> Result<ResolvedPattern, S
.filter_map(|(path_node, resolved)| { .filter_map(|(path_node, resolved)| {
if let Some(grandparent) = path_node.parent().and_then(|parent| parent.parent()) { if let Some(grandparent) = path_node.parent().and_then(|parent| parent.parent()) {
if let Some(call_expr) = ast::CallExpr::cast(grandparent.clone()) { if let Some(call_expr) = ast::CallExpr::cast(grandparent.clone()) {
if let hir::PathResolution::AssocItem(hir::AssocItem::Function(function)) = if let hir::PathResolution::Def(hir::ModuleDef::Function(function)) =
resolved.resolution resolved.resolution
{ {
let qualifier_type = self.resolution_scope.qualifier_type(path_node); if function.as_assoc_item(self.resolution_scope.scope.db).is_some() {
return Some(( let qualifier_type =
grandparent, self.resolution_scope.qualifier_type(path_node);
UfcsCallInfo { call_expr, function, qualifier_type }, return Some((
)); grandparent,
UfcsCallInfo { call_expr, function, qualifier_type },
));
}
} }
} }
} }
@ -162,7 +166,9 @@ fn path_contains_placeholder(&self, path: &ast::Path) -> bool {
fn ok_to_use_path_resolution(&self, resolution: &hir::PathResolution) -> bool { fn ok_to_use_path_resolution(&self, resolution: &hir::PathResolution) -> bool {
match resolution { match resolution {
hir::PathResolution::AssocItem(hir::AssocItem::Function(function)) => { hir::PathResolution::Def(hir::ModuleDef::Function(function))
if function.as_assoc_item(self.resolution_scope.scope.db).is_some() =>
{
if function.self_param(self.resolution_scope.scope.db).is_some() { if function.self_param(self.resolution_scope.scope.db).is_some() {
// If we don't use this path resolution, then we won't be able to match method // If we don't use this path resolution, then we won't be able to match method
// calls. e.g. `Foo::bar($s)` should match `x.bar()`. // calls. e.g. `Foo::bar($s)` should match `x.bar()`.
@ -172,7 +178,9 @@ fn ok_to_use_path_resolution(&self, resolution: &hir::PathResolution) -> bool {
false false
} }
} }
hir::PathResolution::AssocItem(_) => { hir::PathResolution::Def(
def @ (hir::ModuleDef::Const(_) | hir::ModuleDef::TypeAlias(_)),
) if def.as_assoc_item(self.resolution_scope.scope.db).is_some() => {
// Not a function. Could be a constant or an associated type. // Not a function. Could be a constant or an associated type.
cov_mark::hit!(replace_associated_trait_constant); cov_mark::hit!(replace_associated_trait_constant);
false false
@ -229,7 +237,7 @@ fn resolve_path(&self, path: &ast::Path) -> Option<hir::PathResolution> {
|assoc_item| { |assoc_item| {
let item_name = assoc_item.name(self.scope.db)?; let item_name = assoc_item.name(self.scope.db)?;
if item_name.to_smol_str().as_str() == name.text() { if item_name.to_smol_str().as_str() == name.text() {
Some(hir::PathResolution::AssocItem(assoc_item)) Some(hir::PathResolution::Def(assoc_item.into()))
} else { } else {
None None
} }