diff --git a/src/tools/rust-analyzer/crates/hir-def/src/dyn_map.rs b/src/tools/rust-analyzer/crates/hir-def/src/dyn_map.rs index a59bbf7e221..22005695af6 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/dyn_map.rs +++ b/src/tools/rust-analyzer/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 = crate::dyn_map::Key, V, AstPtrPolicy>; + + pub const BLOCK: Key = Key::new(); + pub const FUNCTION: Key = Key::new(); + pub const CONST: Key = Key::new(); + pub const STATIC: Key = Key::new(); + pub const TYPE_ALIAS: Key = Key::new(); + pub const IMPL: Key = Key::new(); + pub const TRAIT: Key = Key::new(); + pub const TRAIT_ALIAS: Key = Key::new(); + pub const STRUCT: Key = Key::new(); + pub const UNION: Key = Key::new(); + pub const ENUM: Key = Key::new(); + pub const EXTERN_CRATE: Key = Key::new(); + pub const USE: Key = Key::new(); + + pub const ENUM_VARIANT: Key = Key::new(); + pub const TUPLE_FIELD: Key = Key::new(); + pub const RECORD_FIELD: Key = Key::new(); + pub const TYPE_PARAM: Key = Key::new(); + pub const CONST_PARAM: Key = Key::new(); + pub const LIFETIME_PARAM: Key = Key::new(); + + pub const MACRO_RULES: Key = Key::new(); + pub const MACRO2: Key = Key::new(); + pub const PROC_MACRO: Key = Key::new(); + pub const MACRO_CALL: Key = Key::new(); + pub const ATTR_MACRO_CALL: Key = Key::new(); + pub const DERIVE_MACRO_CALL: Key]>)> = + 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 { + _phantom: PhantomData<(AST, ID)>, + } + + impl Policy for AstPtrPolicy { + type K = AstPtr; + type V = ID; + fn insert(map: &mut DynMap, key: AstPtr, value: ID) { + map.map + .entry::, ID>>() + .or_insert_with(Default::default) + .insert(key, value); + } + fn get<'a>(map: &'a DynMap, key: &AstPtr) -> Option<&'a ID> { + map.map.get::, ID>>()?.get(key) + } + fn is_empty(map: &DynMap) -> bool { + map.map.get::, ID>>().map_or(true, |it| it.is_empty()) + } + } +} use std::{ hash::Hash, diff --git a/src/tools/rust-analyzer/crates/hir-def/src/dyn_map/keys.rs b/src/tools/rust-analyzer/crates/hir-def/src/dyn_map/keys.rs deleted file mode 100644 index 9d330a7bf1c..00000000000 --- a/src/tools/rust-analyzer/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 = crate::dyn_map::Key, V, AstPtrPolicy>; - -pub const BLOCK: Key = Key::new(); -pub const FUNCTION: Key = Key::new(); -pub const CONST: Key = Key::new(); -pub const STATIC: Key = Key::new(); -pub const TYPE_ALIAS: Key = Key::new(); -pub const IMPL: Key = Key::new(); -pub const TRAIT: Key = Key::new(); -pub const TRAIT_ALIAS: Key = Key::new(); -pub const STRUCT: Key = Key::new(); -pub const UNION: Key = Key::new(); -pub const ENUM: Key = Key::new(); -pub const EXTERN_CRATE: Key = Key::new(); -pub const USE: Key = Key::new(); - -pub const ENUM_VARIANT: Key = Key::new(); -pub const TUPLE_FIELD: Key = Key::new(); -pub const RECORD_FIELD: Key = Key::new(); -pub const TYPE_PARAM: Key = Key::new(); -pub const CONST_PARAM: Key = Key::new(); -pub const LIFETIME_PARAM: Key = Key::new(); - -pub const MACRO_RULES: Key = Key::new(); -pub const MACRO2: Key = Key::new(); -pub const PROC_MACRO: Key = Key::new(); -pub const MACRO_CALL: Key = Key::new(); -pub const ATTR_MACRO_CALL: Key = Key::new(); -pub const DERIVE_MACRO_CALL: Key]>)> = - 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 { - _phantom: PhantomData<(AST, ID)>, -} - -impl Policy for AstPtrPolicy { - type K = AstPtr; - type V = ID; - fn insert(map: &mut DynMap, key: AstPtr, value: ID) { - map.map - .entry::, ID>>() - .or_insert_with(Default::default) - .insert(key, value); - } - fn get<'a>(map: &'a DynMap, key: &AstPtr) -> Option<&'a ID> { - map.map.get::, ID>>()?.get(key) - } - fn is_empty(map: &DynMap) -> bool { - map.map.get::, ID>>().map_or(true, |it| it.is_empty()) - } -} diff --git a/src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs b/src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs index a04f12cab76..e96e38eceeb 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs @@ -361,9 +361,7 @@ pub(crate) fn macro_invoc(&self, call: AstId) -> Option impl Iterator, &MacroCallId)> { + pub fn iter_macro_invoc(&self) -> impl Iterator, &MacroCallId)> { self.macro_invocations.iter() } } @@ -401,9 +399,7 @@ pub(crate) fn add_macro_invoc(&mut self, call: AstId, call_id: M self.macro_invocations.insert(call, call_id); } - pub(crate) fn attr_macro_invocs( - &self, - ) -> impl Iterator, MacroCallId)> + '_ { + pub fn attr_macro_invocs(&self) -> impl Iterator, MacroCallId)> + '_ { self.attr_macros.iter().map(|(k, v)| (*k, *v)) } @@ -440,7 +436,7 @@ pub(crate) fn init_derive_attribute( }); } - pub(crate) fn derive_macro_invocs( + pub fn derive_macro_invocs( &self, ) -> impl Iterator< Item = ( diff --git a/src/tools/rust-analyzer/crates/hir-def/src/lib.rs b/src/tools/rust-analyzer/crates/hir-def/src/lib.rs index f6ed826f04c..e73c2ee6f6c 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/lib.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/lib.rs @@ -47,7 +47,6 @@ pub mod nameres; -pub mod child_by_source; pub mod src; pub mod find_path; @@ -354,9 +353,9 @@ pub struct ProcMacroLoc { pub struct BlockId(ra_salsa::InternId); #[derive(Debug, Hash, PartialEq, Eq, Clone)] pub struct BlockLoc { - ast_id: AstId, + pub ast_id: AstId, /// The containing module. - module: ModuleId, + pub module: ModuleId, } impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block); @@ -935,7 +934,7 @@ pub enum GenericDefId { ); impl GenericDefId { - fn file_id_and_params_of( + pub fn file_id_and_params_of( self, db: &dyn DefDatabase, ) -> (HirFileId, Option) { diff --git a/src/tools/rust-analyzer/crates/hir/src/semantics.rs b/src/tools/rust-analyzer/crates/hir/src/semantics.rs index 29c2660554b..9d3f8e5fba4 100644 --- a/src/tools/rust-analyzer/crates/hir/src/semantics.rs +++ b/src/tools/rust-analyzer/crates/hir/src/semantics.rs @@ -1,5 +1,6 @@ //! See `Semantics`. +mod child_by_source; mod source_to_def; use std::{ diff --git a/src/tools/rust-analyzer/crates/hir-def/src/child_by_source.rs b/src/tools/rust-analyzer/crates/hir/src/semantics/child_by_source.rs similarity index 99% rename from src/tools/rust-analyzer/crates/hir-def/src/child_by_source.rs rename to src/tools/rust-analyzer/crates/hir/src/semantics/child_by_source.rs index 0438278ca27..ec65ea9a9a8 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/child_by_source.rs +++ b/src/tools/rust-analyzer/crates/hir/src/semantics/child_by_source.rs @@ -8,7 +8,7 @@ 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 @@ 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/src/tools/rust-analyzer/crates/hir/src/semantics/source_to_def.rs b/src/tools/rust-analyzer/crates/hir/src/semantics/source_to_def.rs index 5357e824d09..08333c2d76c 100644 --- a/src/tools/rust-analyzer/crates/hir/src/semantics/source_to_def.rs +++ b/src/tools/rust-analyzer/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 @@ 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/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs index f01b4ea0fd4..83f4a6b123c 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs +++ b/src/tools/rust-analyzer/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/src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs index 45c723d09d4..1f1b6478d36 100644 --- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs +++ b/src/tools/rust-analyzer/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); } }