From 09bc7ca74dc920a01db32b2a29f70eb9f10c9853 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 26 Feb 2020 13:22:46 +0100 Subject: Reduce visibility --- crates/ra_hir/src/from_id.rs | 1 + crates/ra_hir/src/semantics.rs | 72 +++++++++++++++++++++++++++++--- crates/ra_hir/src/source_binder.rs | 84 +++++++------------------------------- 3 files changed, 81 insertions(+), 76 deletions(-) diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs index c16c17072..3aa7c4870 100644 --- a/crates/ra_hir/src/from_id.rs +++ b/crates/ra_hir/src/from_id.rs @@ -40,6 +40,7 @@ from_id![ (hir_def::ConstId, crate::Const), (hir_def::FunctionId, crate::Function), (hir_def::ImplId, crate::ImplBlock), + (hir_def::TypeParamId, crate::TypeParam), (hir_expand::MacroDefId, crate::MacroDef) ]; diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 22a7e7588..9fedb7657 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -4,16 +4,16 @@ use std::{cell::RefCell, fmt, iter::successors}; use hir_def::{ resolver::{self, HasResolver, Resolver}, - TraitId, + DefWithBodyId, TraitId, }; use ra_db::{FileId, FileRange}; -use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextUnit}; +use ra_syntax::{ast, match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextUnit}; use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ db::HirDatabase, source_analyzer::{resolve_hir_path, ReferenceDescriptor, SourceAnalyzer}, - source_binder::{ChildContainer, SourceBinder, ToDef}, + source_binder::{ChildContainer, SourceBinder}, Function, HirFileId, InFile, Local, MacroDef, Module, Name, Origin, Path, PathResolution, ScopeDef, StructField, Trait, Type, TypeParam, VariantDef, }; @@ -129,9 +129,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { // pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option; pub fn to_def(&self, src: &T) -> Option { - let src = self.find_file(src.syntax().clone()).with_value(src.clone()); - let mut sb = self.sb.borrow_mut(); - T::to_def(self.db, &mut sb, src) + T::to_def(self, src) } pub fn to_module_def(&self, file: FileId) -> Option { @@ -227,6 +225,68 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { } } +pub trait ToDef: Sized + AstNode + 'static { + type Def; + fn to_def(sema: &Semantics, src: &Self) -> Option; +} + +macro_rules! to_def_impls { + ($(($def:path, $ast:path)),* ,) => {$( + impl ToDef for $ast { + type Def = $def; + fn to_def(sema: &Semantics, src: &Self) + -> Option + { + let src = sema.find_file(src.syntax().clone()).with_value(src); + sema.sb.borrow_mut().to_id(sema.db, src.cloned()).map(Into::into) + } + } + )*} +} + +to_def_impls![ + (crate::Module, ast::Module), + (crate::Struct, ast::StructDef), + (crate::Enum, ast::EnumDef), + (crate::Union, ast::UnionDef), + (crate::Trait, ast::TraitDef), + (crate::ImplBlock, ast::ImplBlock), + (crate::TypeAlias, ast::TypeAliasDef), + (crate::Const, ast::ConstDef), + (crate::Static, ast::StaticDef), + (crate::Function, ast::FnDef), + (crate::StructField, ast::RecordFieldDef), + (crate::EnumVariant, ast::EnumVariant), + (crate::TypeParam, ast::TypeParam), + (crate::MacroDef, ast::MacroCall), // this one is dubious, not all calls are macros +]; + +impl ToDef for ast::BindPat { + type Def = Local; + + fn to_def(sema: &Semantics, src: &Self) -> Option { + let src = sema.find_file(src.syntax().clone()).with_value(src); + let file_id = src.file_id; + let mut sb = sema.sb.borrow_mut(); + let db = sema.db; + let parent: DefWithBodyId = src.value.syntax().ancestors().find_map(|it| { + let res = match_ast! { + match it { + ast::ConstDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() }, + ast::StaticDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() }, + ast::FnDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() }, + _ => return None, + } + }; + Some(res) + })?; + let (_body, source_map) = db.body_with_source_map(parent); + let src = src.cloned().map(ast::Pat::from); + let pat_id = source_map.node_pat(src.as_ref())?; + Some(Local { parent: parent.into(), pat_id }) + } +} + fn find_root(node: &SyntaxNode) -> SyntaxNode { node.ancestors().last().unwrap() } diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 0b8a641f9..4353e25ac 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -6,7 +6,7 @@ use hir_def::{ dyn_map::DynMap, keys::{self, Key}, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, - StaticId, StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId, + StaticId, StructFieldId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId, }; use hir_expand::{name::AsName, AstId, InFile, MacroDefId, MacroDefKind}; use ra_db::FileId; @@ -17,9 +17,9 @@ use ra_syntax::{ }; use rustc_hash::FxHashMap; -use crate::{db::HirDatabase, Local, Module, TypeParam}; +use crate::{db::HirDatabase, Module}; -pub struct SourceBinder { +pub(crate) struct SourceBinder { child_by_source_cache: FxHashMap, } @@ -38,7 +38,11 @@ impl SourceBinder { Some(Module { id: ModuleId { krate, local_id } }) } - fn to_id(&mut self, db: &impl HirDatabase, src: InFile) -> Option { + pub(crate) fn to_id( + &mut self, + db: &impl HirDatabase, + src: InFile, + ) -> Option { T::to_id(db, self, src) } @@ -118,42 +122,6 @@ pub(crate) trait ToId: Sized { ) -> Option; } -pub trait ToDef: Sized + AstNode + 'static { - type Def; - fn to_def( - db: &DB, - sb: &mut SourceBinder, - src: InFile, - ) -> Option; -} - -macro_rules! to_def_impls { - ($(($def:path, $ast:path)),* ,) => {$( - impl ToDef for $ast { - type Def = $def; - fn to_def(db: &DB, sb: &mut SourceBinder, src: InFile) - -> Option - { sb.to_id(db, src).map(Into::into) } - } - )*} -} - -to_def_impls![ - (crate::Module, ast::Module), - (crate::Struct, ast::StructDef), - (crate::Enum, ast::EnumDef), - (crate::Union, ast::UnionDef), - (crate::Trait, ast::TraitDef), - (crate::ImplBlock, ast::ImplBlock), - (crate::TypeAlias, ast::TypeAliasDef), - (crate::Const, ast::ConstDef), - (crate::Static, ast::StaticDef), - (crate::Function, ast::FnDef), - (crate::StructField, ast::RecordFieldDef), - (crate::EnumVariant, ast::EnumVariant), - (crate::MacroDef, ast::MacroCall), // this one is dubious, not all calls are macros -]; - #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub(crate) enum ChildContainer { DefWithBodyId(DefWithBodyId), @@ -245,37 +213,14 @@ impl ToId for ast::MacroCall { } } -impl ToDef for ast::BindPat { - type Def = Local; +impl ToId for ast::TypeParam { + type ID = TypeParamId; - fn to_def(db: &DB, sb: &mut SourceBinder, src: InFile) -> Option { - let file_id = src.file_id; - let parent: DefWithBodyId = src.value.syntax().ancestors().find_map(|it| { - let res = match_ast! { - match it { - ast::ConstDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() }, - ast::StaticDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() }, - ast::FnDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() }, - _ => return None, - } - }; - Some(res) - })?; - let (_body, source_map) = db.body_with_source_map(parent); - let src = src.map(ast::Pat::from); - let pat_id = source_map.node_pat(src.as_ref())?; - Some(Local { parent: parent.into(), pat_id }) - } -} - -impl ToDef for ast::TypeParam { - type Def = TypeParam; - - fn to_def( + fn to_id( db: &DB, sb: &mut SourceBinder, - src: InFile, - ) -> Option { + src: InFile, + ) -> Option { let file_id = src.file_id; let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| { let res = match_ast! { @@ -291,8 +236,7 @@ impl ToDef for ast::TypeParam { }; Some(res) })?; - let &id = sb.child_by_source(db, parent.into())[keys::TYPE_PARAM].get(&src)?; - Some(TypeParam { id }) + sb.child_by_source(db, parent.into())[keys::TYPE_PARAM].get(&src).copied() } } -- cgit v1.2.3 From ade0176c20f7e4159a0cb81ae8034acacc915310 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 26 Feb 2020 13:24:46 +0100 Subject: Remove dead code --- crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/source_analyzer.rs | 32 ++++++++------------------------ 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 004a2185f..4a85e7e36 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -47,7 +47,7 @@ pub use crate::{ }, has_source::HasSource, semantics::{original_range, Semantics, SemanticsScope}, - source_analyzer::{PathResolution, ScopeEntryWithSyntax}, + source_analyzer::PathResolution, }; pub use hir_def::{ diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index bff1ecd14..b655e2c32 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -25,8 +25,8 @@ use ra_syntax::{ }; use crate::{ - db::HirDatabase, Adt, Const, EnumVariant, Function, Local, MacroDef, Name, Path, Static, - Struct, Trait, Type, TypeAlias, TypeParam, + db::HirDatabase, Adt, Const, EnumVariant, Function, Local, MacroDef, Path, Static, Struct, + Trait, Type, TypeAlias, TypeParam, }; /// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of @@ -53,22 +53,6 @@ pub enum PathResolution { AssocItem(crate::AssocItem), } -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct ScopeEntryWithSyntax { - pub(crate) name: Name, - pub(crate) ptr: Either, AstPtr>, -} - -impl ScopeEntryWithSyntax { - pub fn name(&self) -> &Name { - &self.name - } - - pub fn ptr(&self) -> Either, AstPtr> { - self.ptr - } -} - #[derive(Debug)] pub struct ReferenceDescriptor { pub range: TextRange, @@ -235,16 +219,16 @@ impl SourceAnalyzer { resolve_hir_path(db, &self.resolver, &hir_path) } - fn resolve_local_name(&self, name_ref: &ast::NameRef) -> Option { + fn resolve_local_name( + &self, + name_ref: &ast::NameRef, + ) -> Option, AstPtr>> { let name = name_ref.as_name(); let source_map = self.body_source_map.as_ref()?; let scopes = self.scopes.as_ref()?; let scope = scope_for(scopes, source_map, InFile::new(self.file_id, name_ref.syntax()))?; let entry = scopes.resolve_name_in_scope(scope, &name)?; - Some(ScopeEntryWithSyntax { - name: entry.name().clone(), - ptr: source_map.pat_syntax(entry.pat())?.value, - }) + Some(source_map.pat_syntax(entry.pat())?.value) } // FIXME: we only use this in `inline_local_variable` assist, ideally, we @@ -258,7 +242,7 @@ impl SourceAnalyzer { .filter_map(ast::NameRef::cast) .filter(|name_ref| match self.resolve_local_name(&name_ref) { None => false, - Some(entry) => entry.ptr() == ptr, + Some(d_ptr) => d_ptr == ptr, }) .map(|name_ref| ReferenceDescriptor { name: name_ref.text().to_string(), -- cgit v1.2.3