Unnamed repository; edit this file 'description' to name the repository.
Move child_by_source from hir-def to hir
Lukas Wirth 2024-10-31
parent 8244f30 · commit 70348fa
-rw-r--r--crates/hir-def/src/dyn_map.rs74
-rw-r--r--crates/hir-def/src/dyn_map/keys.rs72
-rw-r--r--crates/hir-def/src/item_scope.rs10
-rw-r--r--crates/hir-def/src/lib.rs7
-rw-r--r--crates/hir/src/semantics.rs1
-rw-r--r--crates/hir/src/semantics/child_by_source.rs (renamed from crates/hir-def/src/child_by_source.rs)4
-rw-r--r--crates/hir/src/semantics/source_to_def.rs6
-rw-r--r--crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs2
-rw-r--r--crates/ide-diagnostics/src/lib.rs2
9 files changed, 88 insertions, 90 deletions
diff --git a/crates/hir-def/src/dyn_map.rs b/crates/hir-def/src/dyn_map.rs
index a59bbf7e22..22005695af 100644
--- a/crates/hir-def/src/dyn_map.rs
+++ b/crates/hir-def/src/dyn_map.rs
@@ -21,7 +21,79 @@
//!
//! This is a work of fiction. Any similarities to Kotlin's `BindingContext` are
//! a coincidence.
-pub mod keys;
+
+pub mod keys {
+ use std::marker::PhantomData;
+
+ use hir_expand::{attrs::AttrId, MacroCallId};
+ use rustc_hash::FxHashMap;
+ use syntax::{ast, AstNode, AstPtr};
+
+ use crate::{
+ dyn_map::{DynMap, Policy},
+ BlockId, ConstId, EnumId, EnumVariantId, ExternCrateId, FieldId, FunctionId, ImplId,
+ LifetimeParamId, Macro2Id, MacroRulesId, ProcMacroId, StaticId, StructId, TraitAliasId,
+ TraitId, TypeAliasId, TypeOrConstParamId, UnionId, UseId,
+ };
+
+ pub type Key<K, V> = crate::dyn_map::Key<AstPtr<K>, V, AstPtrPolicy<K, V>>;
+
+ pub const BLOCK: Key<ast::BlockExpr, BlockId> = Key::new();
+ pub const FUNCTION: Key<ast::Fn, FunctionId> = Key::new();
+ pub const CONST: Key<ast::Const, ConstId> = Key::new();
+ pub const STATIC: Key<ast::Static, StaticId> = Key::new();
+ pub const TYPE_ALIAS: Key<ast::TypeAlias, TypeAliasId> = Key::new();
+ pub const IMPL: Key<ast::Impl, ImplId> = Key::new();
+ pub const TRAIT: Key<ast::Trait, TraitId> = Key::new();
+ pub const TRAIT_ALIAS: Key<ast::TraitAlias, TraitAliasId> = Key::new();
+ pub const STRUCT: Key<ast::Struct, StructId> = Key::new();
+ pub const UNION: Key<ast::Union, UnionId> = Key::new();
+ pub const ENUM: Key<ast::Enum, EnumId> = Key::new();
+ pub const EXTERN_CRATE: Key<ast::ExternCrate, ExternCrateId> = Key::new();
+ pub const USE: Key<ast::Use, UseId> = Key::new();
+
+ pub const ENUM_VARIANT: Key<ast::Variant, EnumVariantId> = Key::new();
+ pub const TUPLE_FIELD: Key<ast::TupleField, FieldId> = Key::new();
+ pub const RECORD_FIELD: Key<ast::RecordField, FieldId> = Key::new();
+ pub const TYPE_PARAM: Key<ast::TypeParam, TypeOrConstParamId> = Key::new();
+ pub const CONST_PARAM: Key<ast::ConstParam, TypeOrConstParamId> = Key::new();
+ pub const LIFETIME_PARAM: Key<ast::LifetimeParam, LifetimeParamId> = Key::new();
+
+ pub const MACRO_RULES: Key<ast::MacroRules, MacroRulesId> = Key::new();
+ pub const MACRO2: Key<ast::MacroDef, Macro2Id> = Key::new();
+ pub const PROC_MACRO: Key<ast::Fn, ProcMacroId> = Key::new();
+ pub const MACRO_CALL: Key<ast::MacroCall, MacroCallId> = Key::new();
+ pub const ATTR_MACRO_CALL: Key<ast::Item, MacroCallId> = Key::new();
+ pub const DERIVE_MACRO_CALL: Key<ast::Attr, (AttrId, MacroCallId, Box<[Option<MacroCallId>]>)> =
+ Key::new();
+
+ /// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
+ /// equal if they point to exactly the same object.
+ ///
+ /// In general, we do not guarantee that we have exactly one instance of a
+ /// syntax tree for each file. We probably should add such guarantee, but, for
+ /// the time being, we will use identity-less AstPtr comparison.
+ pub struct AstPtrPolicy<AST, ID> {
+ _phantom: PhantomData<(AST, ID)>,
+ }
+
+ impl<AST: AstNode + 'static, ID: 'static> Policy for AstPtrPolicy<AST, ID> {
+ type K = AstPtr<AST>;
+ type V = ID;
+ fn insert(map: &mut DynMap, key: AstPtr<AST>, value: ID) {
+ map.map
+ .entry::<FxHashMap<AstPtr<AST>, ID>>()
+ .or_insert_with(Default::default)
+ .insert(key, value);
+ }
+ fn get<'a>(map: &'a DynMap, key: &AstPtr<AST>) -> Option<&'a ID> {
+ map.map.get::<FxHashMap<AstPtr<AST>, ID>>()?.get(key)
+ }
+ fn is_empty(map: &DynMap) -> bool {
+ map.map.get::<FxHashMap<AstPtr<AST>, ID>>().map_or(true, |it| it.is_empty())
+ }
+ }
+}
use std::{
hash::Hash,
diff --git a/crates/hir-def/src/dyn_map/keys.rs b/crates/hir-def/src/dyn_map/keys.rs
deleted file mode 100644
index 9d330a7bf1..0000000000
--- a/crates/hir-def/src/dyn_map/keys.rs
+++ /dev/null
@@ -1,72 +0,0 @@
-//! keys to be used with `DynMap`
-
-use std::marker::PhantomData;
-
-use hir_expand::{attrs::AttrId, MacroCallId};
-use rustc_hash::FxHashMap;
-use syntax::{ast, AstNode, AstPtr};
-
-use crate::{
- dyn_map::{DynMap, Policy},
- BlockId, ConstId, EnumId, EnumVariantId, ExternCrateId, FieldId, FunctionId, ImplId,
- LifetimeParamId, Macro2Id, MacroRulesId, ProcMacroId, StaticId, StructId, TraitAliasId,
- TraitId, TypeAliasId, TypeOrConstParamId, UnionId, UseId,
-};
-
-pub type Key<K, V> = crate::dyn_map::Key<AstPtr<K>, V, AstPtrPolicy<K, V>>;
-
-pub const BLOCK: Key<ast::BlockExpr, BlockId> = Key::new();
-pub const FUNCTION: Key<ast::Fn, FunctionId> = Key::new();
-pub const CONST: Key<ast::Const, ConstId> = Key::new();
-pub const STATIC: Key<ast::Static, StaticId> = Key::new();
-pub const TYPE_ALIAS: Key<ast::TypeAlias, TypeAliasId> = Key::new();
-pub const IMPL: Key<ast::Impl, ImplId> = Key::new();
-pub const TRAIT: Key<ast::Trait, TraitId> = Key::new();
-pub const TRAIT_ALIAS: Key<ast::TraitAlias, TraitAliasId> = Key::new();
-pub const STRUCT: Key<ast::Struct, StructId> = Key::new();
-pub const UNION: Key<ast::Union, UnionId> = Key::new();
-pub const ENUM: Key<ast::Enum, EnumId> = Key::new();
-pub const EXTERN_CRATE: Key<ast::ExternCrate, ExternCrateId> = Key::new();
-pub const USE: Key<ast::Use, UseId> = Key::new();
-
-pub const ENUM_VARIANT: Key<ast::Variant, EnumVariantId> = Key::new();
-pub const TUPLE_FIELD: Key<ast::TupleField, FieldId> = Key::new();
-pub const RECORD_FIELD: Key<ast::RecordField, FieldId> = Key::new();
-pub const TYPE_PARAM: Key<ast::TypeParam, TypeOrConstParamId> = Key::new();
-pub const CONST_PARAM: Key<ast::ConstParam, TypeOrConstParamId> = Key::new();
-pub const LIFETIME_PARAM: Key<ast::LifetimeParam, LifetimeParamId> = Key::new();
-
-pub const MACRO_RULES: Key<ast::MacroRules, MacroRulesId> = Key::new();
-pub const MACRO2: Key<ast::MacroDef, Macro2Id> = Key::new();
-pub const PROC_MACRO: Key<ast::Fn, ProcMacroId> = Key::new();
-pub const MACRO_CALL: Key<ast::MacroCall, MacroCallId> = Key::new();
-pub const ATTR_MACRO_CALL: Key<ast::Item, MacroCallId> = Key::new();
-pub const DERIVE_MACRO_CALL: Key<ast::Attr, (AttrId, MacroCallId, Box<[Option<MacroCallId>]>)> =
- Key::new();
-
-/// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
-/// equal if they point to exactly the same object.
-///
-/// In general, we do not guarantee that we have exactly one instance of a
-/// syntax tree for each file. We probably should add such guarantee, but, for
-/// the time being, we will use identity-less AstPtr comparison.
-pub struct AstPtrPolicy<AST, ID> {
- _phantom: PhantomData<(AST, ID)>,
-}
-
-impl<AST: AstNode + 'static, ID: 'static> Policy for AstPtrPolicy<AST, ID> {
- type K = AstPtr<AST>;
- type V = ID;
- fn insert(map: &mut DynMap, key: AstPtr<AST>, value: ID) {
- map.map
- .entry::<FxHashMap<AstPtr<AST>, ID>>()
- .or_insert_with(Default::default)
- .insert(key, value);
- }
- fn get<'a>(map: &'a DynMap, key: &AstPtr<AST>) -> Option<&'a ID> {
- map.map.get::<FxHashMap<AstPtr<AST>, ID>>()?.get(key)
- }
- fn is_empty(map: &DynMap) -> bool {
- map.map.get::<FxHashMap<AstPtr<AST>, ID>>().map_or(true, |it| it.is_empty())
- }
-}
diff --git a/crates/hir-def/src/item_scope.rs b/crates/hir-def/src/item_scope.rs
index a04f12cab7..e96e38ecee 100644
--- a/crates/hir-def/src/item_scope.rs
+++ b/crates/hir-def/src/item_scope.rs
@@ -361,9 +361,7 @@ impl ItemScope {
self.macro_invocations.get(&call).copied()
}
- pub(crate) fn iter_macro_invoc(
- &self,
- ) -> impl Iterator<Item = (&AstId<ast::MacroCall>, &MacroCallId)> {
+ pub fn iter_macro_invoc(&self) -> impl Iterator<Item = (&AstId<ast::MacroCall>, &MacroCallId)> {
self.macro_invocations.iter()
}
}
@@ -401,9 +399,7 @@ impl ItemScope {
self.macro_invocations.insert(call, call_id);
}
- pub(crate) fn attr_macro_invocs(
- &self,
- ) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
+ pub fn attr_macro_invocs(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
self.attr_macros.iter().map(|(k, v)| (*k, *v))
}
@@ -440,7 +436,7 @@ impl ItemScope {
});
}
- pub(crate) fn derive_macro_invocs(
+ pub fn derive_macro_invocs(
&self,
) -> impl Iterator<
Item = (
diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs
index f6ed826f04..e73c2ee6f6 100644
--- a/crates/hir-def/src/lib.rs
+++ b/crates/hir-def/src/lib.rs
@@ -47,7 +47,6 @@ pub mod resolver;
pub mod nameres;
-pub mod child_by_source;
pub mod src;
pub mod find_path;
@@ -354,9 +353,9 @@ impl_loc!(ProcMacroLoc, id: Function, container: CrateRootModuleId);
pub struct BlockId(ra_salsa::InternId);
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub struct BlockLoc {
- ast_id: AstId<ast::BlockExpr>,
+ pub ast_id: AstId<ast::BlockExpr>,
/// The containing module.
- module: ModuleId,
+ pub module: ModuleId,
}
impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block);
@@ -935,7 +934,7 @@ impl_from!(
);
impl GenericDefId {
- fn file_id_and_params_of(
+ pub fn file_id_and_params_of(
self,
db: &dyn DefDatabase,
) -> (HirFileId, Option<ast::GenericParamList>) {
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs
index 29c2660554..9d3f8e5fba 100644
--- a/crates/hir/src/semantics.rs
+++ b/crates/hir/src/semantics.rs
@@ -1,5 +1,6 @@
//! See `Semantics`.
+mod child_by_source;
mod source_to_def;
use std::{
diff --git a/crates/hir-def/src/child_by_source.rs b/crates/hir/src/semantics/child_by_source.rs
index 0438278ca2..ec65ea9a9a 100644
--- a/crates/hir-def/src/child_by_source.rs
+++ b/crates/hir/src/semantics/child_by_source.rs
@@ -8,7 +8,7 @@ use either::Either;
use hir_expand::{attrs::collect_attrs, HirFileId};
use syntax::{ast, AstPtr};
-use crate::{
+use hir_def::{
db::DefDatabase,
dyn_map::{
keys::{self, Key},
@@ -23,7 +23,7 @@ use crate::{
VariantId,
};
-pub trait ChildBySource {
+pub(crate) trait ChildBySource {
fn child_by_source(&self, db: &dyn DefDatabase, file_id: HirFileId) -> DynMap {
let mut res = DynMap::default();
self.child_by_source_to(db, &mut res, file_id);
diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs
index 5357e824d0..08333c2d76 100644
--- a/crates/hir/src/semantics/source_to_def.rs
+++ b/crates/hir/src/semantics/source_to_def.rs
@@ -87,7 +87,6 @@
use either::Either;
use hir_def::{
- child_by_source::ChildBySource,
dyn_map::{
keys::{self, Key},
DynMap,
@@ -111,7 +110,10 @@ use syntax::{
AstNode, AstPtr, SyntaxNode,
};
-use crate::{db::HirDatabase, InFile, InlineAsmOperand, SemanticsImpl};
+use crate::{
+ db::HirDatabase, semantics::child_by_source::ChildBySource, InFile, InlineAsmOperand,
+ SemanticsImpl,
+};
#[derive(Default)]
pub(super) struct SourceToDefCache {
diff --git a/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs b/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs
index f01b4ea0fd..83f4a6b123 100644
--- a/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs
+++ b/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs
@@ -104,7 +104,7 @@ fn edit_struct_def(
ast::make::tokens::single_newline().text(),
);
edit.insert(tuple_fields_text_range.start(), w.syntax().text());
- if !w.syntax().last_token().is_some_and(|t| t.kind() == SyntaxKind::COMMA) {
+ if w.syntax().last_token().is_none_or(|t| t.kind() != SyntaxKind::COMMA) {
edit.insert(tuple_fields_text_range.start(), ",");
}
edit.insert(
diff --git a/crates/ide-diagnostics/src/lib.rs b/crates/ide-diagnostics/src/lib.rs
index 45c723d09d..1f1b6478d3 100644
--- a/crates/ide-diagnostics/src/lib.rs
+++ b/crates/ide-diagnostics/src/lib.rs
@@ -382,7 +382,7 @@ pub fn semantic_diagnostics(
// A bunch of parse errors in a file indicate some bigger structural parse changes in the
// file, so we skip semantic diagnostics so we can show these faster.
Some(m) => {
- if !db.parse_errors(file_id).as_deref().is_some_and(|es| es.len() >= 16) {
+ if db.parse_errors(file_id).as_deref().is_none_or(|es| es.len() < 16) {
m.diagnostics(db, &mut diags, config.style_lints);
}
}