Skip to content

Commit a574434

Browse files
committed
Simplify NameClass::classify
1 parent 37a8770 commit a574434

File tree

3 files changed

+100
-114
lines changed

3 files changed

+100
-114
lines changed

crates/hir/src/semantics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@ to_def_impls![
11301130
(crate::TypeParam, ast::TypeParam, type_param_to_def),
11311131
(crate::LifetimeParam, ast::LifetimeParam, lifetime_param_to_def),
11321132
(crate::ConstParam, ast::ConstParam, const_param_to_def),
1133+
(crate::GenericParam, ast::GenericParam, generic_param_to_def),
11331134
(crate::MacroDef, ast::Macro, macro_to_def),
11341135
(crate::Local, ast::IdentPat, bind_pat_to_def),
11351136
(crate::Local, ast::SelfParam, self_param_to_def),

crates/hir/src/semantics/source_to_def.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ use hir_def::{
9292
expr::{LabelId, PatId},
9393
keys::{self, Key},
9494
AdtId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId,
95-
GenericDefId, ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
96-
TypeParamId, UnionId, VariantId,
95+
GenericDefId, GenericParamId, ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId,
96+
TypeAliasId, TypeParamId, UnionId, VariantId,
9797
};
9898
use hir_expand::{name::AsName, AstId, HirFileId, MacroCallId, MacroDefId, MacroDefKind};
9999
use rustc_hash::FxHashMap;
@@ -299,6 +299,23 @@ impl SourceToDefCtx<'_, '_> {
299299
dyn_map[keys::CONST_PARAM].get(&src).copied()
300300
}
301301

302+
pub(super) fn generic_param_to_def(
303+
&mut self,
304+
InFile { file_id, value }: InFile<ast::GenericParam>,
305+
) -> Option<GenericParamId> {
306+
match value {
307+
ast::GenericParam::ConstParam(it) => {
308+
self.const_param_to_def(InFile::new(file_id, it)).map(GenericParamId::ConstParamId)
309+
}
310+
ast::GenericParam::LifetimeParam(it) => self
311+
.lifetime_param_to_def(InFile::new(file_id, it))
312+
.map(GenericParamId::LifetimeParamId),
313+
ast::GenericParam::TypeParam(it) => {
314+
self.type_param_to_def(InFile::new(file_id, it)).map(GenericParamId::TypeParamId)
315+
}
316+
}
317+
}
318+
302319
pub(super) fn macro_to_def(&mut self, src: InFile<ast::Macro>) -> Option<MacroDefId> {
303320
let makro = self.dyn_map(src.as_ref()).and_then(|it| it[keys::MACRO].get(&src).copied());
304321
if let res @ Some(_) = makro {

crates/ide_db/src/defs.rs

Lines changed: 80 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -224,125 +224,93 @@ impl NameClass {
224224

225225
let parent = name.syntax().parent()?;
226226

227-
if let Some(bind_pat) = ast::IdentPat::cast(parent.clone()) {
228-
if let Some(def) = sema.resolve_bind_pat_to_const(&bind_pat) {
227+
let def = if let Some(item) = ast::Item::cast(parent.clone()) {
228+
match item {
229+
ast::Item::MacroRules(it) => {
230+
Definition::Macro(sema.to_def(&ast::Macro::MacroRules(it))?)
231+
}
232+
ast::Item::MacroDef(it) => {
233+
Definition::Macro(sema.to_def(&ast::Macro::MacroDef(it))?)
234+
}
235+
ast::Item::Const(it) => Definition::Const(sema.to_def(&it)?),
236+
ast::Item::Fn(it) => Definition::Function(sema.to_def(&it)?),
237+
ast::Item::Module(it) => Definition::Module(sema.to_def(&it)?),
238+
ast::Item::Static(it) => Definition::Static(sema.to_def(&it)?),
239+
ast::Item::Trait(it) => Definition::Trait(sema.to_def(&it)?),
240+
ast::Item::TypeAlias(it) => Definition::TypeAlias(sema.to_def(&it)?),
241+
ast::Item::Enum(it) => Definition::Adt(hir::Adt::Enum(sema.to_def(&it)?)),
242+
ast::Item::Struct(it) => Definition::Adt(hir::Adt::Struct(sema.to_def(&it)?)),
243+
ast::Item::Union(it) => Definition::Adt(hir::Adt::Union(sema.to_def(&it)?)),
244+
_ => return None,
245+
}
246+
} else if let Some(it) = ast::IdentPat::cast(parent.clone()) {
247+
if let Some(def) = sema.resolve_bind_pat_to_const(&it) {
229248
return Some(NameClass::ConstReference(Definition::from(def)));
230249
}
231-
}
232250

233-
match_ast! {
234-
match parent {
235-
ast::Rename(it) => {
236-
if let Some(use_tree) = it.syntax().parent().and_then(ast::UseTree::cast) {
237-
let path = use_tree.path()?;
238-
let path_segment = path.segment()?;
239-
let name_ref = path_segment.name_ref()?;
240-
let name_ref = if name_ref.self_token().is_some() {
241-
use_tree
242-
.syntax()
243-
.parent()
244-
.as_ref()
245-
// Skip over UseTreeList
246-
.and_then(|it| {
247-
let use_tree = it.parent().and_then(ast::UseTree::cast)?;
248-
let path = use_tree.path()?;
249-
let path_segment = path.segment()?;
250-
path_segment.name_ref()
251-
}).unwrap_or(name_ref)
252-
} else {
253-
name_ref
254-
};
255-
let name_ref_class = NameRefClass::classify(sema, &name_ref)?;
256-
257-
Some(NameClass::Definition(match name_ref_class {
258-
NameRefClass::Definition(def) => def,
259-
NameRefClass::FieldShorthand { local_ref: _, field_ref } => {
260-
Definition::Field(field_ref)
261-
}
262-
}))
263-
} else {
264-
let extern_crate = it.syntax().parent().and_then(ast::ExternCrate::cast)?;
265-
let krate = sema.resolve_extern_crate(&extern_crate)?;
266-
let root_module = krate.root_module(sema.db);
267-
Some(NameClass::Definition(Definition::Module(root_module)))
251+
let local = sema.to_def(&it)?;
252+
let pat_parent = it.syntax().parent();
253+
if let Some(record_pat_field) = pat_parent.and_then(ast::RecordPatField::cast) {
254+
if record_pat_field.name_ref().is_none() {
255+
if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) {
256+
return Some(NameClass::PatFieldShorthand {
257+
local_def: local,
258+
field_ref: field,
259+
});
268260
}
269-
},
270-
ast::IdentPat(it) => {
271-
let local = sema.to_def(&it)?;
261+
}
262+
}
272263

273-
if let Some(record_pat_field) = it.syntax().parent().and_then(ast::RecordPatField::cast) {
274-
if record_pat_field.name_ref().is_none() {
275-
if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) {
276-
return Some(NameClass::PatFieldShorthand { local_def: local, field_ref: field });
277-
}
278-
}
279-
}
264+
Definition::Local(local)
265+
} else if let Some(it) = ast::Rename::cast(parent.clone()) {
266+
if let Some(use_tree) = it.syntax().parent().and_then(ast::UseTree::cast) {
267+
let path = use_tree.path()?;
268+
let path_segment = path.segment()?;
269+
let name_ref = path_segment.name_ref()?;
270+
let name_ref = if name_ref.self_token().is_some() {
271+
use_tree
272+
.syntax()
273+
.parent()
274+
.as_ref()
275+
// Skip over UseTreeList
276+
.and_then(|it| {
277+
let use_tree = it.parent().and_then(ast::UseTree::cast)?;
278+
let path = use_tree.path()?;
279+
let path_segment = path.segment()?;
280+
path_segment.name_ref()
281+
})
282+
.unwrap_or(name_ref)
283+
} else {
284+
name_ref
285+
};
286+
let name_ref_class = NameRefClass::classify(sema, &name_ref)?;
280287

281-
Some(NameClass::Definition(Definition::Local(local)))
282-
},
283-
ast::SelfParam(it) => {
284-
let def = sema.to_def(&it)?;
285-
Some(NameClass::Definition(Definition::Local(def)))
286-
},
287-
ast::RecordField(it) => {
288-
let field: hir::Field = sema.to_def(&it)?;
289-
Some(NameClass::Definition(Definition::Field(field)))
290-
},
291-
ast::Module(it) => {
292-
let def = sema.to_def(&it)?;
293-
Some(NameClass::Definition(Definition::Module(def)))
294-
},
295-
ast::Struct(it) => {
296-
let def: hir::Struct = sema.to_def(&it)?;
297-
Some(NameClass::Definition(Definition::Adt(def.into())))
298-
},
299-
ast::Union(it) => {
300-
let def: hir::Union = sema.to_def(&it)?;
301-
Some(NameClass::Definition(Definition::Adt(def.into())))
302-
},
303-
ast::Enum(it) => {
304-
let def: hir::Enum = sema.to_def(&it)?;
305-
Some(NameClass::Definition(Definition::Adt(def.into())))
306-
},
307-
ast::Trait(it) => {
308-
let def: hir::Trait = sema.to_def(&it)?;
309-
Some(NameClass::Definition(Definition::Trait(def)))
310-
},
311-
ast::Static(it) => {
312-
let def: hir::Static = sema.to_def(&it)?;
313-
Some(NameClass::Definition(Definition::Static(def)))
314-
},
315-
ast::Variant(it) => {
316-
let def: hir::Variant = sema.to_def(&it)?;
317-
Some(NameClass::Definition(Definition::Variant(def)))
318-
},
319-
ast::Fn(it) => {
320-
let def: hir::Function = sema.to_def(&it)?;
321-
Some(NameClass::Definition(Definition::Function(def)))
322-
},
323-
ast::Const(it) => {
324-
let def: hir::Const = sema.to_def(&it)?;
325-
Some(NameClass::Definition(Definition::Const(def)))
326-
},
327-
ast::TypeAlias(it) => {
328-
let def: hir::TypeAlias = sema.to_def(&it)?;
329-
Some(NameClass::Definition(Definition::TypeAlias(def)))
330-
},
331-
ast::Macro(it) => {
332-
let def = sema.to_def(&it)?;
333-
Some(NameClass::Definition(Definition::Macro(def)))
334-
},
335-
ast::TypeParam(it) => {
336-
let def = sema.to_def(&it)?;
337-
Some(NameClass::Definition(Definition::GenericParam(def.into())))
338-
},
339-
ast::ConstParam(it) => {
340-
let def = sema.to_def(&it)?;
341-
Some(NameClass::Definition(Definition::GenericParam(def.into())))
342-
},
343-
_ => None,
288+
match name_ref_class {
289+
NameRefClass::Definition(def) => def,
290+
NameRefClass::FieldShorthand { local_ref: _, field_ref } => {
291+
Definition::Field(field_ref)
292+
}
293+
}
294+
} else {
295+
let extern_crate = it.syntax().parent().and_then(ast::ExternCrate::cast)?;
296+
let krate = sema.resolve_extern_crate(&extern_crate)?;
297+
let root_module = krate.root_module(sema.db);
298+
Definition::Module(root_module)
344299
}
345-
}
300+
} else {
301+
match_ast! {
302+
match parent {
303+
ast::SelfParam(it) => Definition::Local(sema.to_def(&it)?),
304+
ast::RecordField(it) => Definition::Field(sema.to_def(&it)?),
305+
ast::Variant(it) => Definition::Variant(sema.to_def(&it)?),
306+
ast::TypeParam(it) => Definition::GenericParam(sema.to_def(&it)?.into()),
307+
ast::ConstParam(it) => Definition::GenericParam(sema.to_def(&it)?.into()),
308+
_ => return None,
309+
}
310+
}
311+
};
312+
313+
Some(NameClass::Definition(def))
346314
}
347315

348316
pub fn classify_lifetime(

0 commit comments

Comments
 (0)