diff options
| author | bors <bors@rust-lang.org> | 2024-03-04 09:02:32 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-03-04 09:02:32 +0000 |
| commit | d8feb908beb9585645891bf06233d15ec4857cc1 (patch) | |
| tree | 36b4a92e2b7b2c0ffef71c936e4f35e56652ec33 | |
| parent | 0b7d4cc6ff80b943953750e3bea782267bd22ff6 (diff) | |
| parent | fac8a14de874d36aed74e3e29022342f77b0c01d (diff) | |
| download | rust-d8feb908beb9585645891bf06233d15ec4857cc1.tar.gz rust-d8feb908beb9585645891bf06233d15ec4857cc1.zip | |
Auto merge of #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)
| -rw-r--r-- | crates/hir/src/semantics.rs | 71 | ||||
| -rw-r--r-- | crates/hir/src/semantics/source_to_def.rs | 2 | ||||
| -rw-r--r-- | crates/ide-assists/src/handlers/generate_delegate_trait.rs | 2 | ||||
| -rw-r--r-- | crates/ide-assists/src/handlers/move_from_mod_rs.rs | 2 | ||||
| -rw-r--r-- | crates/ide-assists/src/handlers/move_to_mod_rs.rs | 2 | ||||
| -rw-r--r-- | crates/ide-db/src/helpers.rs | 2 | ||||
| -rw-r--r-- | crates/ide-diagnostics/src/handlers/missing_fields.rs | 2 | ||||
| -rw-r--r-- | crates/ide-diagnostics/src/lib.rs | 2 | ||||
| -rw-r--r-- | crates/ide/src/parent_module.rs | 2 | ||||
| -rw-r--r-- | crates/ide/src/rename.rs | 2 | ||||
| -rw-r--r-- | crates/ide/src/runnables.rs | 2 | ||||
| -rw-r--r-- | crates/ide/src/syntax_highlighting.rs | 2 |
12 files changed, 71 insertions, 22 deletions
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index cfda8d4f937..ca8bd65510b 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -38,10 +38,11 @@ use crate::{ db::HirDatabase, semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, source_analyzer::{resolve_hir_path, SourceAnalyzer}, - Access, Adjust, Adjustment, AutoBorrow, BindingMode, BuiltinAttr, Callable, ConstParam, Crate, - DeriveHelper, Field, Function, HasSource, HirFileId, Impl, InFile, Label, LifetimeParam, Local, - Macro, Module, ModuleDef, Name, OverloadedDeref, Path, ScopeDef, Struct, ToolModule, Trait, - TupleField, Type, TypeAlias, TypeParam, VariantDef, + Access, Adjust, Adjustment, Adt, AutoBorrow, BindingMode, BuiltinAttr, Callable, Const, + ConstParam, Crate, DeriveHelper, Enum, Field, Function, HasSource, HirFileId, Impl, InFile, + Label, LifetimeParam, Local, Macro, Module, ModuleDef, Name, OverloadedDeref, Path, ScopeDef, + Static, Struct, ToolModule, Trait, TraitAlias, TupleField, Type, TypeAlias, TypeParam, Union, + Variant, VariantDef, }; pub enum DescendPreference { @@ -223,21 +224,69 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.resolve_variant(record_lit).map(VariantDef::from) } - pub fn to_module_def(&self, file: FileId) -> Option<Module> { - self.imp.to_module_def(file).next() + pub fn file_to_module_def(&self, file: FileId) -> Option<Module> { + self.imp.file_to_module_defs(file).next() } - pub fn to_module_defs(&self, file: FileId) -> impl Iterator<Item = Module> { - self.imp.to_module_def(file) + pub fn file_to_module_defs(&self, file: FileId) -> impl Iterator<Item = Module> { + self.imp.file_to_module_defs(file) } - pub fn to_struct_def(&self, s: &ast::Struct) -> Option<Struct> { - self.imp.to_def(s).map(Struct::from) + pub fn to_adt_def(&self, a: &ast::Adt) -> Option<Adt> { + self.imp.to_def(a).map(Adt::from) + } + + pub fn to_const_def(&self, c: &ast::Const) -> Option<Const> { + self.imp.to_def(c).map(Const::from) + } + + pub fn to_enum_def(&self, e: &ast::Enum) -> Option<Enum> { + self.imp.to_def(e).map(Enum::from) + } + + pub fn to_enum_variant_def(&self, v: &ast::Variant) -> Option<Variant> { + self.imp.to_def(v).map(Variant::from) + } + + pub fn to_fn_def(&self, f: &ast::Fn) -> Option<Function> { + self.imp.to_def(f).map(Function::from) } pub fn to_impl_def(&self, i: &ast::Impl) -> Option<Impl> { self.imp.to_def(i).map(Impl::from) } + + pub fn to_macro_def(&self, m: &ast::Macro) -> Option<Macro> { + self.imp.to_def(m).map(Macro::from) + } + + pub fn to_module_def(&self, m: &ast::Module) -> Option<Module> { + self.imp.to_def(m).map(Module::from) + } + + pub fn to_static_def(&self, s: &ast::Static) -> Option<Static> { + self.imp.to_def(s).map(Static::from) + } + + pub fn to_struct_def(&self, s: &ast::Struct) -> Option<Struct> { + self.imp.to_def(s).map(Struct::from) + } + + pub fn to_trait_alias_def(&self, t: &ast::TraitAlias) -> Option<TraitAlias> { + self.imp.to_def(t).map(TraitAlias::from) + } + + pub fn to_trait_def(&self, t: &ast::Trait) -> Option<Trait> { + self.imp.to_def(t).map(Trait::from) + } + + pub fn to_type_alias_def(&self, t: &ast::TypeAlias) -> Option<TypeAlias> { + self.imp.to_def(t).map(TypeAlias::from) + } + + pub fn to_union_def(&self, u: &ast::Union) -> Option<Union> { + self.imp.to_def(u).map(Union::from) + } } impl<'db> SemanticsImpl<'db> { @@ -1241,7 +1290,7 @@ impl<'db> SemanticsImpl<'db> { T::to_def(self, src) } - fn to_module_def(&self, file: FileId) -> impl Iterator<Item = Module> { + fn file_to_module_defs(&self, file: FileId) -> impl Iterator<Item = Module> { self.with_ctx(|ctx| ctx.file_to_def(file)).into_iter().map(Module::from) } diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs index ef4ed90ce35..4733ea5a35b 100644 --- a/crates/hir/src/semantics/source_to_def.rs +++ b/crates/hir/src/semantics/source_to_def.rs @@ -118,7 +118,7 @@ pub(super) struct SourceToDefCtx<'a, 'b> { impl SourceToDefCtx<'_, '_> { pub(super) fn file_to_def(&self, file: FileId) -> SmallVec<[ModuleId; 1]> { - let _p = tracing::span!(tracing::Level::INFO, "SourceBinder::to_module_def"); + let _p = tracing::span!(tracing::Level::INFO, "SourceBinder::file_to_module_def"); let mut mods = SmallVec::new(); for &crate_id in self.db.relevant_crates(file).iter() { // FIXME: inner items diff --git a/crates/ide-assists/src/handlers/generate_delegate_trait.rs b/crates/ide-assists/src/handlers/generate_delegate_trait.rs index 7a60287f923..e0bcd78c99b 100644 --- a/crates/ide-assists/src/handlers/generate_delegate_trait.rs +++ b/crates/ide-assists/src/handlers/generate_delegate_trait.rs @@ -116,7 +116,7 @@ impl Field { ) -> Option<Field> { let db = ctx.sema.db; - let module = ctx.sema.to_module_def(ctx.file_id())?; + let module = ctx.sema.file_to_module_def(ctx.file_id())?; let (name, range, ty) = match f { Either::Left(f) => { diff --git a/crates/ide-assists/src/handlers/move_from_mod_rs.rs b/crates/ide-assists/src/handlers/move_from_mod_rs.rs index 917d0b3671e..a256f60c421 100644 --- a/crates/ide-assists/src/handlers/move_from_mod_rs.rs +++ b/crates/ide-assists/src/handlers/move_from_mod_rs.rs @@ -25,7 +25,7 @@ use crate::{ // ``` pub(crate) fn move_from_mod_rs(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?; - let module = ctx.sema.to_module_def(ctx.file_id())?; + let module = ctx.sema.file_to_module_def(ctx.file_id())?; // Enable this assist if the user select all "meaningful" content in the source file let trimmed_selected_range = trimmed_text_range(&source_file, ctx.selection_trimmed()); let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range()); diff --git a/crates/ide-assists/src/handlers/move_to_mod_rs.rs b/crates/ide-assists/src/handlers/move_to_mod_rs.rs index b73270cd05f..a8a124eebb6 100644 --- a/crates/ide-assists/src/handlers/move_to_mod_rs.rs +++ b/crates/ide-assists/src/handlers/move_to_mod_rs.rs @@ -25,7 +25,7 @@ use crate::{ // ``` pub(crate) fn move_to_mod_rs(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?; - let module = ctx.sema.to_module_def(ctx.file_id())?; + let module = ctx.sema.file_to_module_def(ctx.file_id())?; // Enable this assist if the user select all "meaningful" content in the source file let trimmed_selected_range = trimmed_text_range(&source_file, ctx.selection_trimmed()); let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range()); diff --git a/crates/ide-db/src/helpers.rs b/crates/ide-db/src/helpers.rs index 0b5ad7060e0..4ac8a7c4c4a 100644 --- a/crates/ide-db/src/helpers.rs +++ b/crates/ide-db/src/helpers.rs @@ -64,7 +64,7 @@ pub fn visit_file_defs( cb: &mut dyn FnMut(Definition), ) { let db = sema.db; - let module = match sema.to_module_def(file_id) { + let module = match sema.file_to_module_def(file_id) { Some(it) => it, None => return, }; diff --git a/crates/ide-diagnostics/src/handlers/missing_fields.rs b/crates/ide-diagnostics/src/handlers/missing_fields.rs index 09daefd084d..f92ba576d3a 100644 --- a/crates/ide-diagnostics/src/handlers/missing_fields.rs +++ b/crates/ide-diagnostics/src/handlers/missing_fields.rs @@ -200,7 +200,7 @@ fn get_default_constructor( } } - let krate = ctx.sema.to_module_def(d.file.original_file(ctx.sema.db))?.krate(); + let krate = ctx.sema.file_to_module_def(d.file.original_file(ctx.sema.db))?.krate(); let module = krate.root_module(); // Look for a ::new() associated function diff --git a/crates/ide-diagnostics/src/lib.rs b/crates/ide-diagnostics/src/lib.rs index 9f4368b04e7..6c72dbb5edf 100644 --- a/crates/ide-diagnostics/src/lib.rs +++ b/crates/ide-diagnostics/src/lib.rs @@ -315,7 +315,7 @@ pub fn diagnostics( handlers::json_is_not_rust::json_in_items(&sema, &mut res, file_id, &node, config); } - let module = sema.to_module_def(file_id); + let module = sema.file_to_module_def(file_id); let ctx = DiagnosticsContext { config, sema, resolve }; if module.is_none() { diff --git a/crates/ide/src/parent_module.rs b/crates/ide/src/parent_module.rs index f67aea2d5b9..ce7a6779e27 100644 --- a/crates/ide/src/parent_module.rs +++ b/crates/ide/src/parent_module.rs @@ -48,7 +48,7 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<Na .flat_map(|module| NavigationTarget::from_module_to_decl(db, module)) .collect(), None => sema - .to_module_defs(position.file_id) + .file_to_module_defs(position.file_id) .flat_map(|module| NavigationTarget::from_module_to_decl(db, module)) .collect(), } diff --git a/crates/ide/src/rename.rs b/crates/ide/src/rename.rs index f78153df38b..8c2ae327c7f 100644 --- a/crates/ide/src/rename.rs +++ b/crates/ide/src/rename.rs @@ -156,7 +156,7 @@ pub(crate) fn will_rename_file( new_name_stem: &str, ) -> Option<SourceChange> { let sema = Semantics::new(db); - let module = sema.to_module_def(file_id)?; + let module = sema.file_to_module_def(file_id)?; let def = Definition::Module(module); let mut change = if is_raw_identifier(new_name_stem) { def.rename(&sema, &SmolStr::from_iter(["r#", new_name_stem])).ok()? diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index ae107a96040..5fe46444ff4 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs @@ -178,7 +178,7 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> { } }); - sema.to_module_defs(file_id) + sema.file_to_module_defs(file_id) .map(|it| runnable_mod_outline_definition(&sema, it)) .for_each(|it| add_opt(it, None)); diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index dfcbaf54d4f..d2bd3bab14e 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -223,7 +223,7 @@ fn traverse( krate: hir::Crate, range_to_highlight: TextRange, ) { - let is_unlinked = sema.to_module_def(file_id).is_none(); + let is_unlinked = sema.file_to_module_def(file_id).is_none(); let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default(); enum AttrOrDerive { |
