From d963042ca9da93be8d5922ce46ea26dc6a79c929 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 27 Dec 2018 20:07:21 +0300 Subject: introduce hir::Name --- crates/ra_hir/src/module/imp.rs | 19 ++++++++----------- crates/ra_hir/src/module/nameres.rs | 25 ++++++++++++++----------- crates/ra_hir/src/module/nameres/tests.rs | 7 ++++--- 3 files changed, 26 insertions(+), 25 deletions(-) (limited to 'crates/ra_hir/src/module') diff --git a/crates/ra_hir/src/module/imp.rs b/crates/ra_hir/src/module/imp.rs index 748fdb64e..eded85a63 100644 --- a/crates/ra_hir/src/module/imp.rs +++ b/crates/ra_hir/src/module/imp.rs @@ -1,16 +1,13 @@ use std::sync::Arc; -use ra_syntax::{ - ast::{self, NameOwner}, - SmolStr, -}; +use ra_syntax::ast::{self, NameOwner}; use relative_path::RelativePathBuf; use rustc_hash::{FxHashMap, FxHashSet}; use arrayvec::ArrayVec; use ra_db::{SourceRoot, SourceRootId, Cancelable, FileId}; use crate::{ - HirDatabase, + HirDatabase, Name, AsName, }; use super::{ @@ -20,12 +17,12 @@ use super::{ #[derive(Clone, Hash, PartialEq, Eq, Debug)] pub enum Submodule { - Declaration(SmolStr), - Definition(SmolStr, ModuleSource), + Declaration(Name), + Definition(Name, ModuleSource), } impl Submodule { - fn name(&self) -> &SmolStr { + fn name(&self) -> &Name { match self { Submodule::Declaration(name) => name, Submodule::Definition(name, _) => name, @@ -35,14 +32,14 @@ impl Submodule { pub(crate) fn modules<'a>( root: impl ast::ModuleItemOwner<'a>, -) -> impl Iterator)> { +) -> impl Iterator)> { root.items() .filter_map(|item| match item { ast::ModuleItem::Module(m) => Some(m), _ => None, }) .filter_map(|module| { - let name = module.name()?.text(); + let name = module.name()?.as_name(); Some((name, module)) }) } @@ -155,7 +152,7 @@ fn build_subtree( fn resolve_submodule( db: &impl HirDatabase, source: ModuleSource, - name: &SmolStr, + name: &Name, ) -> (Vec, Option) { // FIXME: handle submodules of inline modules properly let file_id = source.file_id(); diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index 98cd225dd..68eb02a98 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs @@ -14,14 +14,12 @@ //! modifications (that is, typing inside a function shold not change IMIs), //! such that the results of name resolution can be preserved unless the module //! structure itself is modified. -use std::{ - sync::Arc, -}; +use std::sync::Arc; use rustc_hash::FxHashMap; use ra_syntax::{ TextRange, - SmolStr, SyntaxKind::{self, *}, + SyntaxKind::{self, *}, ast::{self, AstNode} }; use ra_db::SourceRootId; @@ -32,6 +30,7 @@ use crate::{ SourceItemId, SourceFileItemId, SourceFileItems, Path, PathKind, HirDatabase, Crate, + Name, AsName, module::{Module, ModuleId, ModuleTree}, }; @@ -45,14 +44,14 @@ pub struct ItemMap { #[derive(Debug, Default, PartialEq, Eq, Clone)] pub struct ModuleScope { - items: FxHashMap, + items: FxHashMap, } impl ModuleScope { - pub fn entries<'a>(&'a self) -> impl Iterator + 'a { + pub fn entries<'a>(&'a self) -> impl Iterator + 'a { self.items.iter() } - pub fn get(&self, name: &SmolStr) -> Option<&Resolution> { + pub fn get(&self, name: &Name) -> Option<&Resolution> { self.items.get(name) } } @@ -72,7 +71,7 @@ pub struct InputModuleItems { #[derive(Debug, PartialEq, Eq)] struct ModuleItem { id: SourceFileItemId, - name: SmolStr, + name: Name, kind: SyntaxKind, vis: Vis, } @@ -260,7 +259,7 @@ impl InputModuleItems { impl ModuleItem { fn new<'a>(file_items: &SourceFileItems, item: impl ast::NameOwner<'a>) -> Option { - let name = item.name()?.text(); + let name = item.name()?.as_name(); let kind = item.syntax().kind(); let vis = Vis::Other; let id = file_items.id_of_unchecked(item.syntax()); @@ -328,7 +327,11 @@ where for dep in krate.dependencies(self.db) { if let Some(module) = dep.krate.root_module(self.db)? { let def_id = module.def_id(self.db); - self.add_module_item(&mut module_items, dep.name, PerNs::types(def_id)); + self.add_module_item( + &mut module_items, + dep.name.clone(), + PerNs::types(def_id), + ); } } }; @@ -389,7 +392,7 @@ where Ok(()) } - fn add_module_item(&self, module_items: &mut ModuleScope, name: SmolStr, def_id: PerNs) { + fn add_module_item(&self, module_items: &mut ModuleScope, name: Name, def_id: PerNs) { let resolution = Resolution { def_id, import: None, diff --git a/crates/ra_hir/src/module/nameres/tests.rs b/crates/ra_hir/src/module/nameres/tests.rs index 03ea5c1d6..165ac81c8 100644 --- a/crates/ra_hir/src/module/nameres/tests.rs +++ b/crates/ra_hir/src/module/nameres/tests.rs @@ -9,6 +9,7 @@ use crate::{ self as hir, db::HirDatabase, mock::MockDatabase, + Name, }; fn item_map(fixture: &str) -> (Arc, hir::ModuleId) { @@ -38,7 +39,7 @@ fn item_map_smoke_test() { pub struct Baz; ", ); - let name = SmolStr::from("Baz"); + let name = Name::new(SmolStr::from("Baz")); let resolution = &item_map.per_module[&module_id].items[&name]; assert!(resolution.def_id.take_types().is_some()); } @@ -57,7 +58,7 @@ fn test_self() { pub struct Baz; ", ); - let name = SmolStr::from("Baz"); + let name = Name::new(SmolStr::from("Baz")); let resolution = &item_map.per_module[&module_id].items[&name]; assert!(resolution.def_id.take_types().is_some()); } @@ -90,7 +91,7 @@ fn item_map_across_crates() { let module_id = module.module_id; let item_map = db.item_map(source_root).unwrap(); - let name = SmolStr::from("Baz"); + let name = Name::new(SmolStr::from("Baz")); let resolution = &item_map.per_module[&module_id].items[&name]; assert!(resolution.def_id.take_types().is_some()); } -- cgit v1.2.3