Skip to content

Commit d8feb90

Browse files
committed
Auto merge of rust-lang#16703 - regexident:sema-ast-to-hir, r=Veykril
Add more methods for resolving definitions from AST to their corresponding HIR types In order to be able to add these methods with consistent naming I had to also rename two existing methods that would otherwise be conflicting/confusing: `Semantics::to_module_def(&self, file: FileId) -> Option<Module>` (before) `Semantics::file_to_module_def(&self, file: FileId) -> Option<Module>` (after) `Semantics::to_module_defs(&self, file: FileId) -> impl Iterator<Item = Module>` (before) `Semantics::file_to_module_defs(&self, file: FileId) -> impl Iterator<Item = Module>` (after) (the PR is motivated by an outside use of the `ra_ap_hir` crate that would benefit from being able to walk a `hir::Function`'s AST, resolving its exprs/stmts/items to their HIR equivalents)
2 parents 0b7d4cc + fac8a14 commit d8feb90

File tree

12 files changed

+71
-22
lines changed

12 files changed

+71
-22
lines changed

crates/hir/src/semantics.rs

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ use crate::{
3838
db::HirDatabase,
3939
semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx},
4040
source_analyzer::{resolve_hir_path, SourceAnalyzer},
41-
Access, Adjust, Adjustment, AutoBorrow, BindingMode, BuiltinAttr, Callable, ConstParam, Crate,
42-
DeriveHelper, Field, Function, HasSource, HirFileId, Impl, InFile, Label, LifetimeParam, Local,
43-
Macro, Module, ModuleDef, Name, OverloadedDeref, Path, ScopeDef, Struct, ToolModule, Trait,
44-
TupleField, Type, TypeAlias, TypeParam, VariantDef,
41+
Access, Adjust, Adjustment, Adt, AutoBorrow, BindingMode, BuiltinAttr, Callable, Const,
42+
ConstParam, Crate, DeriveHelper, Enum, Field, Function, HasSource, HirFileId, Impl, InFile,
43+
Label, LifetimeParam, Local, Macro, Module, ModuleDef, Name, OverloadedDeref, Path, ScopeDef,
44+
Static, Struct, ToolModule, Trait, TraitAlias, TupleField, Type, TypeAlias, TypeParam, Union,
45+
Variant, VariantDef,
4546
};
4647

4748
pub enum DescendPreference {
@@ -223,21 +224,69 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
223224
self.imp.resolve_variant(record_lit).map(VariantDef::from)
224225
}
225226

226-
pub fn to_module_def(&self, file: FileId) -> Option<Module> {
227-
self.imp.to_module_def(file).next()
227+
pub fn file_to_module_def(&self, file: FileId) -> Option<Module> {
228+
self.imp.file_to_module_defs(file).next()
228229
}
229230

230-
pub fn to_module_defs(&self, file: FileId) -> impl Iterator<Item = Module> {
231-
self.imp.to_module_def(file)
231+
pub fn file_to_module_defs(&self, file: FileId) -> impl Iterator<Item = Module> {
232+
self.imp.file_to_module_defs(file)
232233
}
233234

234-
pub fn to_struct_def(&self, s: &ast::Struct) -> Option<Struct> {
235-
self.imp.to_def(s).map(Struct::from)
235+
pub fn to_adt_def(&self, a: &ast::Adt) -> Option<Adt> {
236+
self.imp.to_def(a).map(Adt::from)
237+
}
238+
239+
pub fn to_const_def(&self, c: &ast::Const) -> Option<Const> {
240+
self.imp.to_def(c).map(Const::from)
241+
}
242+
243+
pub fn to_enum_def(&self, e: &ast::Enum) -> Option<Enum> {
244+
self.imp.to_def(e).map(Enum::from)
245+
}
246+
247+
pub fn to_enum_variant_def(&self, v: &ast::Variant) -> Option<Variant> {
248+
self.imp.to_def(v).map(Variant::from)
249+
}
250+
251+
pub fn to_fn_def(&self, f: &ast::Fn) -> Option<Function> {
252+
self.imp.to_def(f).map(Function::from)
236253
}
237254

238255
pub fn to_impl_def(&self, i: &ast::Impl) -> Option<Impl> {
239256
self.imp.to_def(i).map(Impl::from)
240257
}
258+
259+
pub fn to_macro_def(&self, m: &ast::Macro) -> Option<Macro> {
260+
self.imp.to_def(m).map(Macro::from)
261+
}
262+
263+
pub fn to_module_def(&self, m: &ast::Module) -> Option<Module> {
264+
self.imp.to_def(m).map(Module::from)
265+
}
266+
267+
pub fn to_static_def(&self, s: &ast::Static) -> Option<Static> {
268+
self.imp.to_def(s).map(Static::from)
269+
}
270+
271+
pub fn to_struct_def(&self, s: &ast::Struct) -> Option<Struct> {
272+
self.imp.to_def(s).map(Struct::from)
273+
}
274+
275+
pub fn to_trait_alias_def(&self, t: &ast::TraitAlias) -> Option<TraitAlias> {
276+
self.imp.to_def(t).map(TraitAlias::from)
277+
}
278+
279+
pub fn to_trait_def(&self, t: &ast::Trait) -> Option<Trait> {
280+
self.imp.to_def(t).map(Trait::from)
281+
}
282+
283+
pub fn to_type_alias_def(&self, t: &ast::TypeAlias) -> Option<TypeAlias> {
284+
self.imp.to_def(t).map(TypeAlias::from)
285+
}
286+
287+
pub fn to_union_def(&self, u: &ast::Union) -> Option<Union> {
288+
self.imp.to_def(u).map(Union::from)
289+
}
241290
}
242291

243292
impl<'db> SemanticsImpl<'db> {
@@ -1241,7 +1290,7 @@ impl<'db> SemanticsImpl<'db> {
12411290
T::to_def(self, src)
12421291
}
12431292

1244-
fn to_module_def(&self, file: FileId) -> impl Iterator<Item = Module> {
1293+
fn file_to_module_defs(&self, file: FileId) -> impl Iterator<Item = Module> {
12451294
self.with_ctx(|ctx| ctx.file_to_def(file)).into_iter().map(Module::from)
12461295
}
12471296

crates/hir/src/semantics/source_to_def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub(super) struct SourceToDefCtx<'a, 'b> {
118118

119119
impl SourceToDefCtx<'_, '_> {
120120
pub(super) fn file_to_def(&self, file: FileId) -> SmallVec<[ModuleId; 1]> {
121-
let _p = tracing::span!(tracing::Level::INFO, "SourceBinder::to_module_def");
121+
let _p = tracing::span!(tracing::Level::INFO, "SourceBinder::file_to_module_def");
122122
let mut mods = SmallVec::new();
123123
for &crate_id in self.db.relevant_crates(file).iter() {
124124
// FIXME: inner items

crates/ide-assists/src/handlers/generate_delegate_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl Field {
116116
) -> Option<Field> {
117117
let db = ctx.sema.db;
118118

119-
let module = ctx.sema.to_module_def(ctx.file_id())?;
119+
let module = ctx.sema.file_to_module_def(ctx.file_id())?;
120120

121121
let (name, range, ty) = match f {
122122
Either::Left(f) => {

crates/ide-assists/src/handlers/move_from_mod_rs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
// ```
2626
pub(crate) fn move_from_mod_rs(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
2727
let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?;
28-
let module = ctx.sema.to_module_def(ctx.file_id())?;
28+
let module = ctx.sema.file_to_module_def(ctx.file_id())?;
2929
// Enable this assist if the user select all "meaningful" content in the source file
3030
let trimmed_selected_range = trimmed_text_range(&source_file, ctx.selection_trimmed());
3131
let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range());

crates/ide-assists/src/handlers/move_to_mod_rs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
// ```
2626
pub(crate) fn move_to_mod_rs(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
2727
let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?;
28-
let module = ctx.sema.to_module_def(ctx.file_id())?;
28+
let module = ctx.sema.file_to_module_def(ctx.file_id())?;
2929
// Enable this assist if the user select all "meaningful" content in the source file
3030
let trimmed_selected_range = trimmed_text_range(&source_file, ctx.selection_trimmed());
3131
let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range());

crates/ide-db/src/helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn visit_file_defs(
6464
cb: &mut dyn FnMut(Definition),
6565
) {
6666
let db = sema.db;
67-
let module = match sema.to_module_def(file_id) {
67+
let module = match sema.file_to_module_def(file_id) {
6868
Some(it) => it,
6969
None => return,
7070
};

crates/ide-diagnostics/src/handlers/missing_fields.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ fn get_default_constructor(
200200
}
201201
}
202202

203-
let krate = ctx.sema.to_module_def(d.file.original_file(ctx.sema.db))?.krate();
203+
let krate = ctx.sema.file_to_module_def(d.file.original_file(ctx.sema.db))?.krate();
204204
let module = krate.root_module();
205205

206206
// Look for a ::new() associated function

crates/ide-diagnostics/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ pub fn diagnostics(
315315
handlers::json_is_not_rust::json_in_items(&sema, &mut res, file_id, &node, config);
316316
}
317317

318-
let module = sema.to_module_def(file_id);
318+
let module = sema.file_to_module_def(file_id);
319319

320320
let ctx = DiagnosticsContext { config, sema, resolve };
321321
if module.is_none() {

crates/ide/src/parent_module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<Na
4848
.flat_map(|module| NavigationTarget::from_module_to_decl(db, module))
4949
.collect(),
5050
None => sema
51-
.to_module_defs(position.file_id)
51+
.file_to_module_defs(position.file_id)
5252
.flat_map(|module| NavigationTarget::from_module_to_decl(db, module))
5353
.collect(),
5454
}

crates/ide/src/rename.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ pub(crate) fn will_rename_file(
156156
new_name_stem: &str,
157157
) -> Option<SourceChange> {
158158
let sema = Semantics::new(db);
159-
let module = sema.to_module_def(file_id)?;
159+
let module = sema.file_to_module_def(file_id)?;
160160
let def = Definition::Module(module);
161161
let mut change = if is_raw_identifier(new_name_stem) {
162162
def.rename(&sema, &SmolStr::from_iter(["r#", new_name_stem])).ok()?

crates/ide/src/runnables.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
178178
}
179179
});
180180

181-
sema.to_module_defs(file_id)
181+
sema.file_to_module_defs(file_id)
182182
.map(|it| runnable_mod_outline_definition(&sema, it))
183183
.for_each(|it| add_opt(it, None));
184184

crates/ide/src/syntax_highlighting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ fn traverse(
223223
krate: hir::Crate,
224224
range_to_highlight: TextRange,
225225
) {
226-
let is_unlinked = sema.to_module_def(file_id).is_none();
226+
let is_unlinked = sema.file_to_module_def(file_id).is_none();
227227
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
228228

229229
enum AttrOrDerive {

0 commit comments

Comments
 (0)