Remove TraitItem and ImplItem in favor of AssocItem

This commit is contained in:
Florian Diebold 2019-09-16 22:01:13 +02:00
parent 53a932509d
commit a040fde3ae
9 changed files with 46 additions and 93 deletions

View File

@ -1,7 +1,7 @@
use std::{collections::HashSet, fmt::Write, path::Path, time::Instant};
use ra_db::SourceDatabase;
use ra_hir::{Crate, HasBodySource, HasSource, HirDisplay, ImplItem, ModuleDef, Ty, TypeWalk};
use ra_hir::{AssocItem, Crate, HasBodySource, HasSource, HirDisplay, ModuleDef, Ty, TypeWalk};
use ra_syntax::AstNode;
use crate::{Result, Verbosity};
@ -47,7 +47,7 @@ pub fn run(
for impl_block in module.impl_blocks(db) {
for item in impl_block.items(db) {
num_decls += 1;
if let ImplItem::Method(f) = item {
if let AssocItem::Function(f) = item {
funcs.push(f);
}
}

View File

@ -23,7 +23,7 @@ use crate::{
},
nameres::{CrateModuleId, ImportId, ModuleScope, Namespace},
resolve::{Resolver, TypeNs},
traits::{TraitData, TraitItem},
traits::TraitData,
ty::{
primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness},
InferenceResult, TraitRef,
@ -269,7 +269,7 @@ impl Module {
for impl_block in self.impl_blocks(db) {
for item in impl_block.items(db) {
if let crate::ImplItem::Method(f) = item {
if let AssocItem::Function(f) = item {
f.diagnostics(db, sink);
}
}
@ -853,7 +853,7 @@ impl Trait {
self.trait_data(db).name().clone()
}
pub fn items(self, db: &impl DefDatabase) -> Vec<TraitItem> {
pub fn items(self, db: &impl DefDatabase) -> Vec<AssocItem> {
self.trait_data(db).items().to_vec()
}
@ -906,7 +906,7 @@ impl Trait {
.items()
.iter()
.filter_map(|item| match item {
TraitItem::TypeAlias(t) => Some(*t),
AssocItem::TypeAlias(t) => Some(*t),
_ => None,
})
.find(|t| &t.name(db) == name)
@ -1030,23 +1030,8 @@ pub enum AssocItem {
Const(Const),
TypeAlias(TypeAlias),
}
impl From<TraitItem> for AssocItem {
fn from(t: TraitItem) -> Self {
match t {
TraitItem::Function(f) => AssocItem::Function(f),
TraitItem::Const(c) => AssocItem::Const(c),
TraitItem::TypeAlias(t) => AssocItem::TypeAlias(t),
}
}
}
impl From<crate::ImplItem> for AssocItem {
fn from(i: crate::ImplItem) -> Self {
match i {
crate::ImplItem::Method(f) => AssocItem::Function(f),
crate::ImplItem::Const(c) => AssocItem::Const(c),
crate::ImplItem::TypeAlias(t) => AssocItem::TypeAlias(t),
}
}
}
// FIXME: not every function, ... is actually an assoc item. maybe we should make
// sure that you can only turn actual assoc items into AssocItems. This would
// require not implementing From, and instead having some checked way of
// casting them, and somehow making the constructors private, which would be annoying.
impl_froms!(AssocItem: Function, Const, TypeAlias);

View File

@ -15,7 +15,7 @@ use crate::{
resolve::Resolver,
ty::Ty,
type_ref::TypeRef,
Const, Function, HasSource, HirFileId, Source, TraitRef, TypeAlias,
AssocItem, Const, Function, HasSource, HirFileId, Source, TraitRef, TypeAlias,
};
#[derive(Debug, Default, PartialEq, Eq)]
@ -56,7 +56,7 @@ impl HasSource for ImplBlock {
impl ImplBlock {
pub(crate) fn containing(
module_impl_blocks: Arc<ModuleImplBlocks>,
item: ImplItem,
item: AssocItem,
) -> Option<ImplBlock> {
let impl_id = *module_impl_blocks.impls_by_def.get(&item)?;
Some(ImplBlock { module: module_impl_blocks.module, impl_id })
@ -91,7 +91,7 @@ impl ImplBlock {
TraitRef::from_hir(db, &self.resolver(db), &self.target_trait(db)?, Some(target_ty))
}
pub fn items(&self, db: &impl DefDatabase) -> Vec<ImplItem> {
pub fn items(&self, db: &impl DefDatabase) -> Vec<AssocItem> {
db.impls_in_module(self.module).impls[self.impl_id].items().to_vec()
}
@ -113,7 +113,7 @@ impl ImplBlock {
pub struct ImplData {
target_trait: Option<TypeRef>,
target_type: TypeRef,
items: Vec<ImplItem>,
items: Vec<AssocItem>,
negative: bool,
}
@ -151,27 +151,11 @@ impl ImplData {
&self.target_type
}
pub fn items(&self) -> &[ImplItem] {
pub fn items(&self) -> &[AssocItem] {
&self.items
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
//FIXME: rename to ImplDef?
pub enum ImplItem {
Method(Function),
Const(Const),
TypeAlias(TypeAlias),
// Existential
}
impl_froms!(ImplItem: Const, TypeAlias);
impl From<Function> for ImplItem {
fn from(func: Function) -> ImplItem {
ImplItem::Method(func)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ImplId(pub RawId);
impl_arena_id!(ImplId);
@ -185,7 +169,7 @@ impl_arena_id!(ImplId);
pub struct ModuleImplBlocks {
pub(crate) module: Module,
pub(crate) impls: Arena<ImplId, ImplData>,
impls_by_def: FxHashMap<ImplItem, ImplId>,
impls_by_def: FxHashMap<AssocItem, ImplId>,
}
impl ModuleImplBlocks {

View File

@ -69,7 +69,7 @@ pub use self::{
expr::ExprScopes,
generics::{GenericParam, GenericParams, HasGenericParams},
ids::{HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile},
impl_block::{ImplBlock, ImplItem},
impl_block::ImplBlock,
name::Name,
nameres::{ImportId, Namespace, PerNs},
path::{Path, PathKind},

View File

@ -9,13 +9,13 @@ use crate::{
db::{AstDatabase, DefDatabase},
ids::LocationCtx,
name::AsName,
Const, Function, HasSource, Module, Name, Trait, TypeAlias,
AssocItem, Const, Function, HasSource, Module, Name, Trait, TypeAlias,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TraitData {
name: Option<Name>,
items: Vec<TraitItem>,
items: Vec<AssocItem>,
auto: bool,
}
@ -48,7 +48,7 @@ impl TraitData {
&self.name
}
pub(crate) fn items(&self) -> &[TraitItem] {
pub(crate) fn items(&self) -> &[AssocItem] {
&self.items
}
@ -57,22 +57,9 @@ impl TraitData {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TraitItem {
Function(Function),
Const(Const),
TypeAlias(TypeAlias),
// Existential
}
// FIXME: not every function, ... is actually a trait item. maybe we should make
// sure that you can only turn actual trait items into TraitItems. This would
// require not implementing From, and instead having some checked way of
// casting them.
impl_froms!(TraitItem: Function, Const, TypeAlias);
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TraitItemsIndex {
traits_by_def: FxHashMap<TraitItem, Trait>,
traits_by_def: FxHashMap<AssocItem, Trait>,
}
impl TraitItemsIndex {
@ -88,7 +75,7 @@ impl TraitItemsIndex {
index
}
pub(crate) fn get_parent_trait(&self, item: TraitItem) -> Option<Trait> {
pub(crate) fn get_parent_trait(&self, item: AssocItem) -> Option<Trait> {
self.traits_by_def.get(&item).cloned()
}
}

View File

@ -576,34 +576,32 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
// Find impl
// FIXME: consider trait candidates
let def = ty.clone().iterate_impl_items(self.db, krate, |item| match item {
crate::ImplItem::Method(func) => {
let item = ty.clone().iterate_impl_items(self.db, krate, |item| match item {
AssocItem::Function(func) => {
if segment.name == func.name(self.db) {
Some(ValueNs::Function(func))
Some(AssocItem::Function(func))
} else {
None
}
}
crate::ImplItem::Const(konst) => {
AssocItem::Const(konst) => {
if konst.name(self.db).map_or(false, |n| n == segment.name) {
Some(ValueNs::Const(konst))
Some(AssocItem::Const(konst))
} else {
None
}
}
crate::ImplItem::TypeAlias(_) => None,
AssocItem::TypeAlias(_) => None,
})?;
let def = match item {
AssocItem::Function(f) => ValueNs::Function(f),
AssocItem::Const(c) => ValueNs::Const(c),
AssocItem::TypeAlias(_) => unreachable!(),
};
let substs = self.find_self_types(&def, ty);
self.write_assoc_resolution(
id,
match def {
ValueNs::Function(f) => AssocItem::Function(f),
ValueNs::Const(c) => AssocItem::Const(c),
_ => unreachable!(),
},
);
self.write_assoc_resolution(id, item);
Some((def, substs))
}

View File

@ -11,13 +11,12 @@ use super::{autoderef, lower, Canonical, InEnvironment, TraitEnvironment, TraitR
use crate::{
db::HirDatabase,
generics::HasGenericParams,
impl_block::{ImplBlock, ImplId, ImplItem},
impl_block::{ImplBlock, ImplId},
nameres::CrateModuleId,
resolve::Resolver,
traits::TraitItem,
ty::primitive::{FloatBitness, UncertainFloatTy, UncertainIntTy},
ty::{Ty, TypeCtor},
Crate, Function, Module, Name, Trait,
AssocItem, Crate, Function, Module, Name, Trait,
};
/// This is used as a key for indexing impls.
@ -232,7 +231,7 @@ fn iterate_trait_method_candidates<T>(
// iteration
let mut known_implemented = inherently_implemented;
for item in data.items() {
if let TraitItem::Function(m) = *item {
if let AssocItem::Function(m) = *item {
let data = m.data(db);
if name.map_or(true, |name| data.name() == name) && data.has_self_param() {
if !known_implemented {
@ -264,7 +263,7 @@ fn iterate_inherent_methods<T>(
for impl_block in impls.lookup_impl_blocks(&ty.value) {
for item in impl_block.items(db) {
if let ImplItem::Method(f) = item {
if let AssocItem::Function(f) = item {
let data = f.data(db);
if name.map_or(true, |name| data.name() == name) && data.has_self_param() {
if let Some(result) = callback(&ty.value, f) {
@ -304,7 +303,7 @@ impl Ty {
self,
db: &impl HirDatabase,
krate: Crate,
mut callback: impl FnMut(ImplItem) -> Option<T>,
mut callback: impl FnMut(AssocItem) -> Option<T>,
) -> Option<T> {
for krate in def_crates(db, krate, &self)? {
let impls = db.impls_in_crate(krate);

View File

@ -21,7 +21,7 @@ use crate::{
ApplicationTy, CallableDef, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor,
TypeWalk,
},
Crate, HasGenericParams, ImplBlock, ImplItem, Trait, TypeAlias,
AssocItem, Crate, HasGenericParams, ImplBlock, Trait, TypeAlias,
};
/// This represents a trait whose name we could not resolve.
@ -496,7 +496,7 @@ pub(crate) fn trait_datum_query(
.items(db)
.into_iter()
.filter_map(|trait_item| match trait_item {
crate::traits::TraitItem::TypeAlias(type_alias) => Some(type_alias),
crate::AssocItem::TypeAlias(type_alias) => Some(type_alias),
_ => None,
})
.map(|type_alias| type_alias.to_chalk(db))
@ -616,7 +616,7 @@ pub(crate) fn impl_datum_query(
.items(db)
.into_iter()
.filter_map(|item| match item {
ImplItem::TypeAlias(t) => Some(t),
AssocItem::TypeAlias(t) => Some(t),
_ => None,
})
.filter_map(|t| {

View File

@ -52,14 +52,14 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
if let Some(krate) = krate {
ty.iterate_impl_items(ctx.db, krate, |item| {
match item {
hir::ImplItem::Method(func) => {
hir::AssocItem::Function(func) => {
let data = func.data(ctx.db);
if !data.has_self_param() {
acc.add_function(ctx, func);
}
}
hir::ImplItem::Const(ct) => acc.add_const(ctx, ct),
hir::ImplItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
}
None::<()>
});