Skip to content

Commit 9bc8f1f

Browse files
committed
Store names in TraitData
1 parent 4a07923 commit 9bc8f1f

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -737,14 +737,11 @@ impl Trait {
737737
}
738738

739739
pub fn items(self, db: &impl DefDatabase) -> Vec<AssocItem> {
740-
db.trait_data(self.id).items.iter().map(|it| (*it).into()).collect()
740+
db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect()
741741
}
742742

743743
pub fn associated_type_by_name(self, db: &impl DefDatabase, name: &Name) -> Option<TypeAlias> {
744-
let trait_data = db.trait_data(self.id);
745-
let res =
746-
trait_data.associated_types().map(TypeAlias::from).find(|t| &t.name(db) == name)?;
747-
Some(res)
744+
db.trait_data(self.id).associated_type_by_name(name).map(TypeAlias::from)
748745
}
749746

750747
pub fn associated_type_by_name_including_super_traits(

crates/ra_hir/src/ty/method_resolution.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ fn iterate_trait_method_candidates<T>(
261261
// trait, but if we find out it doesn't, we'll skip the rest of the
262262
// iteration
263263
let mut known_implemented = false;
264-
for &item in data.items.iter() {
265-
if !is_valid_candidate(db, name, mode, item.into()) {
264+
for (_name, item) in data.items.iter() {
265+
if !is_valid_candidate(db, name, mode, (*item).into()) {
266266
continue;
267267
}
268268
if !known_implemented {
@@ -272,7 +272,7 @@ fn iterate_trait_method_candidates<T>(
272272
}
273273
}
274274
known_implemented = true;
275-
if let Some(result) = callback(&ty.value, item.into()) {
275+
if let Some(result) = callback(&ty.value, (*item).into()) {
276276
return Some(result);
277277
}
278278
}

crates/ra_hir_def/src/data.rs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl TypeAliasData {
8787
#[derive(Debug, Clone, PartialEq, Eq)]
8888
pub struct TraitData {
8989
pub name: Option<Name>,
90-
pub items: Vec<AssocItemId>,
90+
pub items: Vec<(Name, AssocItemId)>,
9191
pub auto: bool,
9292
}
9393

@@ -97,28 +97,42 @@ impl TraitData {
9797
let name = src.value.name().map(|n| n.as_name());
9898
let auto = src.value.is_auto();
9999
let ast_id_map = db.ast_id_map(src.file_id);
100+
101+
let container = ContainerId::TraitId(tr);
100102
let items = if let Some(item_list) = src.value.item_list() {
101103
item_list
102104
.impl_items()
103105
.map(|item_node| match item_node {
104-
ast::ImplItem::FnDef(it) => FunctionLoc {
105-
container: ContainerId::TraitId(tr),
106-
ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
106+
ast::ImplItem::FnDef(it) => {
107+
let name = it.name().map(|it| it.as_name()).unwrap_or_else(Name::missing);
108+
let def = FunctionLoc {
109+
container,
110+
ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
111+
}
112+
.intern(db)
113+
.into();
114+
(name, def)
107115
}
108-
.intern(db)
109-
.into(),
110-
ast::ImplItem::ConstDef(it) => ConstLoc {
111-
container: ContainerId::TraitId(tr),
112-
ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
116+
ast::ImplItem::ConstDef(it) => {
117+
let name = it.name().map(|it| it.as_name()).unwrap_or_else(Name::missing);
118+
let def = ConstLoc {
119+
container,
120+
ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
121+
}
122+
.intern(db)
123+
.into();
124+
(name, def)
113125
}
114-
.intern(db)
115-
.into(),
116-
ast::ImplItem::TypeAliasDef(it) => TypeAliasLoc {
117-
container: ContainerId::TraitId(tr),
118-
ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
126+
ast::ImplItem::TypeAliasDef(it) => {
127+
let name = it.name().map(|it| it.as_name()).unwrap_or_else(Name::missing);
128+
let def = TypeAliasLoc {
129+
container,
130+
ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
131+
}
132+
.intern(db)
133+
.into();
134+
(name, def)
119135
}
120-
.intern(db)
121-
.into(),
122136
})
123137
.collect()
124138
} else {
@@ -128,11 +142,18 @@ impl TraitData {
128142
}
129143

130144
pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
131-
self.items.iter().filter_map(|item| match item {
145+
self.items.iter().filter_map(|(_name, item)| match item {
132146
AssocItemId::TypeAliasId(t) => Some(*t),
133147
_ => None,
134148
})
135149
}
150+
151+
pub fn associated_type_by_name(&self, name: &Name) -> Option<TypeAliasId> {
152+
self.items.iter().find_map(|(item_name, item)| match item {
153+
AssocItemId::TypeAliasId(t) if item_name == name => Some(*t),
154+
_ => None,
155+
})
156+
}
136157
}
137158

138159
#[derive(Debug, Clone, PartialEq, Eq)]

0 commit comments

Comments
 (0)