From 0bc7d285189caaffc13e4d6856baf895f72ed80c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 18:41:50 +0300 Subject: refactor $crate handling Introduce proper hygiene module, which should grow quite a bit eventually. --- crates/ra_hir/src/expr/lower.rs | 4 +- crates/ra_hir/src/impl_block.rs | 12 ++--- crates/ra_hir_def/src/attr.rs | 18 ++----- crates/ra_hir_def/src/hygiene.rs | 40 ++++++++++++++ crates/ra_hir_def/src/lib.rs | 1 + crates/ra_hir_def/src/nameres/raw.rs | 31 +++++------ crates/ra_hir_def/src/path.rs | 101 +++++++++++++++++------------------ 7 files changed, 118 insertions(+), 89 deletions(-) create mode 100644 crates/ra_hir_def/src/hygiene.rs diff --git a/crates/ra_hir/src/expr/lower.rs b/crates/ra_hir/src/expr/lower.rs index ad029b868..575d78198 100644 --- a/crates/ra_hir/src/expr/lower.rs +++ b/crates/ra_hir/src/expr/lower.rs @@ -1,6 +1,7 @@ //! FIXME: write short doc here use hir_def::{ + hygiene::Hygiene, name::{self, AsName, Name}, path::GenericArgs, type_ref::TypeRef, @@ -597,7 +598,8 @@ where } fn parse_path(&mut self, path: ast::Path) -> Option { - Path::from_src(Source { ast: path, file_id: self.current_file_id }, self.db) + let hygiene = Hygiene::new(self.db, self.current_file_id); + Path::from_src(path, &hygiene) } } diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 518330713..9e4a40017 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -3,7 +3,7 @@ use rustc_hash::FxHashMap; use std::sync::Arc; -use hir_def::{attr::Attr, type_ref::TypeRef}; +use hir_def::{attr::Attr, hygiene::Hygiene, type_ref::TypeRef}; use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; use ra_cfg::CfgOptions; use ra_syntax::{ @@ -227,10 +227,11 @@ impl ModuleImplBlocks { owner: &dyn ast::ModuleItemOwner, file_id: HirFileId, ) { + let hygiene = Hygiene::new(db, file_id); for item in owner.items_with_macros() { match item { ast::ItemOrMacro::Item(ast::ModuleItem::ImplBlock(impl_block_ast)) => { - let attrs = Attr::from_attrs_owner(file_id, &impl_block_ast, db); + let attrs = Attr::from_attrs_owner(&impl_block_ast, &hygiene); if attrs.map_or(false, |attrs| { attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false)) }) { @@ -247,7 +248,7 @@ impl ModuleImplBlocks { } ast::ItemOrMacro::Item(_) => (), ast::ItemOrMacro::Macro(macro_call) => { - let attrs = Attr::from_attrs_owner(file_id, ¯o_call, db); + let attrs = Attr::from_attrs_owner(¯o_call, &hygiene); if attrs.map_or(false, |attrs| { attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false)) }) { @@ -256,9 +257,8 @@ impl ModuleImplBlocks { //FIXME: we should really cut down on the boilerplate required to process a macro let ast_id = AstId::new(file_id, db.ast_id_map(file_id).ast_id(¯o_call)); - if let Some(path) = macro_call - .path() - .and_then(|path| Path::from_src(Source { ast: path, file_id }, db)) + if let Some(path) = + macro_call.path().and_then(|path| Path::from_src(path, &hygiene)) { if let Some(def) = self.module.resolver(db).resolve_path_as_macro(db, &path) { diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 248f03cdf..71f92adc2 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -2,7 +2,6 @@ use std::sync::Arc; -use hir_expand::db::AstDatabase; use mbe::ast_to_token_tree; use ra_cfg::CfgOptions; use ra_syntax::{ @@ -11,7 +10,7 @@ use ra_syntax::{ }; use tt::Subtree; -use crate::{path::Path, HirFileId, Source}; +use crate::{hygiene::Hygiene, path::Path}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct Attr { @@ -26,11 +25,8 @@ pub enum AttrInput { } impl Attr { - pub(crate) fn from_src( - Source { file_id, ast }: Source, - db: &impl AstDatabase, - ) -> Option { - let path = Path::from_src(Source { file_id, ast: ast.path()? }, db)?; + pub(crate) fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option { + let path = Path::from_src(ast.path()?, hygiene)?; let input = match ast.input() { None => None, Some(ast::AttrInput::Literal(lit)) => { @@ -46,17 +42,13 @@ impl Attr { Some(Attr { path, input }) } - pub fn from_attrs_owner( - file_id: HirFileId, - owner: &dyn AttrsOwner, - db: &impl AstDatabase, - ) -> Option> { + pub fn from_attrs_owner(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Option> { let mut attrs = owner.attrs().peekable(); if attrs.peek().is_none() { // Avoid heap allocation return None; } - Some(attrs.flat_map(|ast| Attr::from_src(Source { file_id, ast }, db)).collect()) + Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect()) } pub fn is_simple_atom(&self, name: &str) -> bool { diff --git a/crates/ra_hir_def/src/hygiene.rs b/crates/ra_hir_def/src/hygiene.rs new file mode 100644 index 000000000..e1ae58a3b --- /dev/null +++ b/crates/ra_hir_def/src/hygiene.rs @@ -0,0 +1,40 @@ +//! This modules handles hygiene information. +//! +//! Specifically, `ast` + `Hygiene` allows you to create a `Name`. Note that, at +//! this moment, this is horribly incomplete and handles only `$crate`. +// Should this be moved to `hir_expand`? Seems like it. + +use hir_expand::{db::AstDatabase, HirFileId}; +use ra_db::CrateId; +use ra_syntax::ast; + +use crate::{ + either::Either, + name::{AsName, Name}, +}; + +#[derive(Debug)] +pub struct Hygiene { + // This is what `$crate` expands to + def_crate: Option, +} + +impl Hygiene { + pub fn new(db: &impl AstDatabase, file_id: HirFileId) -> Hygiene { + Hygiene { def_crate: file_id.macro_crate(db) } + } + + pub(crate) fn new_unhygienic() -> Hygiene { + Hygiene { def_crate: None } + } + + // FIXME: this should just return name + pub(crate) fn name_ref_to_name(&self, name_ref: ast::NameRef) -> Either { + if let Some(def_crate) = self.def_crate { + if name_ref.text() == "$crate" { + return Either::B(def_crate); + } + } + Either::A(name_ref.as_name()) + } +} diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 95d503325..61ccdb30d 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -13,6 +13,7 @@ pub mod attr; pub mod name; pub mod path; pub mod type_ref; +pub mod hygiene; // FIXME: this should be private pub mod nameres; diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 86b4fef96..636364628 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -13,6 +13,7 @@ use crate::{ attr::Attr, db::DefDatabase2, either::Either, + hygiene::Hygiene, name::{AsName, Name}, path::Path, FileAstId, HirFileId, ModuleSource, Source, @@ -78,7 +79,7 @@ impl RawItems { source_ast_id_map: db.ast_id_map(file_id), source_map: ImportSourceMap::default(), file_id, - db, + hygiene: Hygiene::new(db, file_id), }; if let Some(node) = db.parse_or_expand(file_id) { if let Some(source_file) = ast::SourceFile::cast(node.clone()) { @@ -204,15 +205,15 @@ pub struct MacroData { pub export: bool, } -struct RawItemsCollector { +struct RawItemsCollector { raw_items: RawItems, source_ast_id_map: Arc, source_map: ImportSourceMap, file_id: HirFileId, - db: DB, + hygiene: Hygiene, } -impl RawItemsCollector<&DB> { +impl RawItemsCollector { fn process_module(&mut self, current_module: Option, body: impl ast::ModuleItemOwner) { for item_or_macro in body.items_with_macros() { match item_or_macro { @@ -309,9 +310,10 @@ impl RawItemsCollector<&DB> { let is_prelude = use_item.has_atom_attr("prelude_import"); let attrs = self.parse_attrs(&use_item); + let mut buf = Vec::new(); Path::expand_use_item( Source { ast: use_item, file_id: self.file_id }, - self.db, + &self.hygiene, |path, use_tree, is_glob, alias| { let import_data = ImportData { path, @@ -321,14 +323,12 @@ impl RawItemsCollector<&DB> { is_extern_crate: false, is_macro_use: false, }; - self.push_import( - current_module, - attrs.clone(), - import_data, - Either::A(AstPtr::new(use_tree)), - ); + buf.push((import_data, Either::A(AstPtr::new(use_tree)))); }, - ) + ); + for (import_data, ptr) in buf { + self.push_import(current_module, attrs.clone(), import_data, ptr); + } } fn add_extern_crate_item( @@ -361,10 +361,7 @@ impl RawItemsCollector<&DB> { fn add_macro(&mut self, current_module: Option, m: ast::MacroCall) { let attrs = self.parse_attrs(&m); - let path = match m - .path() - .and_then(|path| Path::from_src(Source { ast: path, file_id: self.file_id }, self.db)) - { + let path = match m.path().and_then(|path| Path::from_src(path, &self.hygiene)) { Some(it) => it, _ => return, }; @@ -402,6 +399,6 @@ impl RawItemsCollector<&DB> { } fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs { - Attr::from_attrs_owner(self.file_id, item, self.db) + Attr::from_attrs_owner(item, &self.hygiene) } } diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index fe060437d..39f394c3f 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -2,7 +2,6 @@ use std::{iter, sync::Arc}; -use hir_expand::db::AstDatabase; use ra_db::CrateId; use ra_syntax::{ ast::{self, NameOwner, TypeAscriptionOwner}, @@ -10,6 +9,8 @@ use ra_syntax::{ }; use crate::{ + either::Either, + hygiene::Hygiene, name::{self, AsName, Name}, type_ref::TypeRef, Source, @@ -68,11 +69,11 @@ impl Path { /// Calls `cb` with all paths, represented by this use item. pub fn expand_use_item( item_src: Source, - db: &impl AstDatabase, + hygiene: &Hygiene, mut cb: impl FnMut(Path, &ast::UseTree, bool, Option), ) { if let Some(tree) = item_src.ast.use_tree() { - expand_use_tree(None, tree, &|| item_src.file_id.macro_crate(db), &mut cb); + expand_use_tree(None, tree, hygiene, &mut cb); } } @@ -89,17 +90,12 @@ impl Path { /// Converts an `ast::Path` to `Path`. Works with use trees. /// DEPRECATED: It does not handle `$crate` from macro call. pub fn from_ast(path: ast::Path) -> Option { - Path::parse(path, &|| None) + Path::from_src(path, &Hygiene::new_unhygienic()) } /// Converts an `ast::Path` to `Path`. Works with use trees. /// It correctly handles `$crate` based path from macro call. - pub fn from_src(source: Source, db: &impl AstDatabase) -> Option { - let file_id = source.file_id; - Path::parse(source.ast, &|| file_id.macro_crate(db)) - } - - fn parse(mut path: ast::Path, macro_crate: &impl Fn() -> Option) -> Option { + pub fn from_src(mut path: ast::Path, hygiene: &Hygiene) -> Option { let mut kind = PathKind::Plain; let mut segments = Vec::new(); loop { @@ -110,26 +106,28 @@ impl Path { } match segment.kind()? { - ast::PathSegmentKind::Name(name) => { - if name.text() == "$crate" { - if let Some(macro_crate) = macro_crate() { - kind = PathKind::DollarCrate(macro_crate); + ast::PathSegmentKind::Name(name_ref) => { + // FIXME: this should just return name + match hygiene.name_ref_to_name(name_ref) { + Either::A(name) => { + let args = segment + .type_arg_list() + .and_then(GenericArgs::from_ast) + .or_else(|| { + GenericArgs::from_fn_like_path_ast( + segment.param_list(), + segment.ret_type(), + ) + }) + .map(Arc::new); + let segment = PathSegment { name, args_and_bindings: args }; + segments.push(segment); + } + Either::B(crate_id) => { + kind = PathKind::DollarCrate(crate_id); break; } } - - let args = segment - .type_arg_list() - .and_then(GenericArgs::from_ast) - .or_else(|| { - GenericArgs::from_fn_like_path_ast( - segment.param_list(), - segment.ret_type(), - ) - }) - .map(Arc::new); - let segment = PathSegment { name: name.as_name(), args_and_bindings: args }; - segments.push(segment); } ast::PathSegmentKind::Type { type_ref, trait_ref } => { assert!(path.qualifier().is_none()); // this can only occur at the first segment @@ -143,7 +141,7 @@ impl Path { } // >::Foo desugars to Trait::Foo Some(trait_ref) => { - let path = Path::parse(trait_ref.path()?, macro_crate)?; + let path = Path::from_src(trait_ref.path()?, hygiene)?; kind = path.kind; let mut prefix_segments = path.segments; prefix_segments.reverse(); @@ -294,7 +292,7 @@ impl From for Path { fn expand_use_tree( prefix: Option, tree: ast::UseTree, - macro_crate: &impl Fn() -> Option, + hygiene: &Hygiene, cb: &mut impl FnMut(Path, &ast::UseTree, bool, Option), ) { if let Some(use_tree_list) = tree.use_tree_list() { @@ -303,13 +301,13 @@ fn expand_use_tree( None => prefix, // E.g. `use something::{inner}` (prefix is `None`, path is `something`) // or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`) - Some(path) => match convert_path(prefix, path, macro_crate) { + Some(path) => match convert_path(prefix, path, hygiene) { Some(it) => Some(it), None => return, // FIXME: report errors somewhere }, }; for child_tree in use_tree_list.use_trees() { - expand_use_tree(prefix.clone(), child_tree, macro_crate, cb); + expand_use_tree(prefix.clone(), child_tree, hygiene, cb); } } else { let alias = tree.alias().and_then(|a| a.name()).map(|a| a.as_name()); @@ -326,7 +324,7 @@ fn expand_use_tree( } } } - if let Some(path) = convert_path(prefix, ast_path, macro_crate) { + if let Some(path) = convert_path(prefix, ast_path, hygiene) { let is_glob = tree.has_star(); cb(path, &tree, is_glob, alias) } @@ -336,37 +334,36 @@ fn expand_use_tree( } } -fn convert_path( - prefix: Option, - path: ast::Path, - macro_crate: &impl Fn() -> Option, -) -> Option { +fn convert_path(prefix: Option, path: ast::Path, hygiene: &Hygiene) -> Option { let prefix = if let Some(qual) = path.qualifier() { - Some(convert_path(prefix, qual, macro_crate)?) + Some(convert_path(prefix, qual, hygiene)?) } else { prefix }; let segment = path.segment()?; let res = match segment.kind()? { - ast::PathSegmentKind::Name(name) => { - if name.text() == "$crate" { - if let Some(krate) = macro_crate() { + ast::PathSegmentKind::Name(name_ref) => { + match hygiene.name_ref_to_name(name_ref) { + Either::A(name) => { + // no type args in use + let mut res = prefix.unwrap_or_else(|| Path { + kind: PathKind::Plain, + segments: Vec::with_capacity(1), + }); + res.segments.push(PathSegment { + name, + args_and_bindings: None, // no type args in use + }); + res + } + Either::B(crate_id) => { return Some(Path::from_simple_segments( - PathKind::DollarCrate(krate), + PathKind::DollarCrate(crate_id), iter::empty(), - )); + )) } } - - // no type args in use - let mut res = prefix - .unwrap_or_else(|| Path { kind: PathKind::Plain, segments: Vec::with_capacity(1) }); - res.segments.push(PathSegment { - name: name.as_name(), - args_and_bindings: None, // no type args in use - }); - res } ast::PathSegmentKind::CrateKw => { if prefix.is_some() { -- cgit v1.2.3 From b05d6e53fb0e9a008dc2e1220b1201818e63ed2d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 18:50:10 +0300 Subject: push either to hir_expand --- crates/ra_hir/src/lib.rs | 17 ++++++------ crates/ra_hir_def/src/either.rs | 54 ------------------------------------ crates/ra_hir_def/src/hygiene.rs | 6 ++-- crates/ra_hir_def/src/lib.rs | 1 - crates/ra_hir_def/src/nameres/raw.rs | 11 +++----- crates/ra_hir_def/src/path.rs | 2 +- crates/ra_hir_expand/src/either.rs | 54 ++++++++++++++++++++++++++++++++++++ crates/ra_hir_expand/src/lib.rs | 1 + 8 files changed, 70 insertions(+), 76 deletions(-) delete mode 100644 crates/ra_hir_def/src/either.rs create mode 100644 crates/ra_hir_expand/src/either.rs diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index f765490b0..989818c0e 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -60,6 +60,13 @@ use crate::{ids::MacroFileKind, resolve::Resolver}; pub use crate::{ adt::VariantDef, + code_model::{ + docs::{DocDef, Docs, Documentation}, + src::{HasBodySource, HasSource, Source}, + Adt, AssocItem, BuiltinType, Const, ConstData, Container, Crate, CrateDependency, + DefWithBody, Enum, EnumVariant, FieldSource, FnData, Function, HasBody, MacroDef, Module, + ModuleDef, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, + }, expr::ExprScopes, from_source::FromSource, generics::{GenericDef, GenericParam, GenericParams, HasGenericParams}, @@ -73,17 +80,9 @@ pub use crate::{ }, }; -pub use self::code_model::{ - docs::{DocDef, Docs, Documentation}, - src::{HasBodySource, HasSource, Source}, - Adt, AssocItem, BuiltinType, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, - Enum, EnumVariant, FieldSource, FnData, Function, HasBody, MacroDef, Module, ModuleDef, - ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, -}; - pub use hir_def::{ - either::Either, name::Name, path::{Path, PathKind}, type_ref::Mutability, }; +pub use hir_expand::either::Either; diff --git a/crates/ra_hir_def/src/either.rs b/crates/ra_hir_def/src/either.rs deleted file mode 100644 index 83583ef8b..000000000 --- a/crates/ra_hir_def/src/either.rs +++ /dev/null @@ -1,54 +0,0 @@ -//! FIXME: write short doc here - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum Either { - A(A), - B(B), -} - -impl Either { - pub fn either(self, f1: F1, f2: F2) -> R - where - F1: FnOnce(A) -> R, - F2: FnOnce(B) -> R, - { - match self { - Either::A(a) => f1(a), - Either::B(b) => f2(b), - } - } - pub fn map(self, f1: F1, f2: F2) -> Either - where - F1: FnOnce(A) -> U, - F2: FnOnce(B) -> V, - { - match self { - Either::A(a) => Either::A(f1(a)), - Either::B(b) => Either::B(f2(b)), - } - } - pub fn map_a(self, f: F) -> Either - where - F: FnOnce(A) -> U, - { - self.map(f, |it| it) - } - pub fn a(self) -> Option { - match self { - Either::A(it) => Some(it), - Either::B(_) => None, - } - } - pub fn b(self) -> Option { - match self { - Either::A(_) => None, - Either::B(it) => Some(it), - } - } - pub fn as_ref(&self) -> Either<&A, &B> { - match self { - Either::A(it) => Either::A(it), - Either::B(it) => Either::B(it), - } - } -} diff --git a/crates/ra_hir_def/src/hygiene.rs b/crates/ra_hir_def/src/hygiene.rs index e1ae58a3b..f51c46fcb 100644 --- a/crates/ra_hir_def/src/hygiene.rs +++ b/crates/ra_hir_def/src/hygiene.rs @@ -4,14 +4,12 @@ //! this moment, this is horribly incomplete and handles only `$crate`. // Should this be moved to `hir_expand`? Seems like it. +use hir_expand::either::Either; use hir_expand::{db::AstDatabase, HirFileId}; use ra_db::CrateId; use ra_syntax::ast; -use crate::{ - either::Either, - name::{AsName, Name}, -}; +use crate::name::{AsName, Name}; #[derive(Debug)] pub struct Hygiene { diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 61ccdb30d..0de728dc1 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -8,7 +8,6 @@ //! actually true. pub mod db; -pub mod either; pub mod attr; pub mod name; pub mod path; diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 636364628..f1896c0cc 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -2,7 +2,7 @@ use std::{ops::Index, sync::Arc}; -use hir_expand::{ast_id_map::AstIdMap, db::AstDatabase}; +use hir_expand::{ast_id_map::AstIdMap, db::AstDatabase, either::Either}; use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; use ra_syntax::{ ast::{self, AttrsOwner, NameOwner}, @@ -12,7 +12,6 @@ use ra_syntax::{ use crate::{ attr::Attr, db::DefDatabase2, - either::Either, hygiene::Hygiene, name::{AsName, Name}, path::Path, @@ -41,10 +40,8 @@ pub struct ImportSourceMap { type ImportSourcePtr = Either, AstPtr>; type ImportSource = Either; -impl ImportSourcePtr { - fn to_node(self, file: &SourceFile) -> ImportSource { - self.map(|ptr| ptr.to_node(file.syntax()), |ptr| ptr.to_node(file.syntax())) - } +fn to_node(ptr: ImportSourcePtr, file: &SourceFile) -> ImportSource { + ptr.map(|ptr| ptr.to_node(file.syntax()), |ptr| ptr.to_node(file.syntax())) } impl ImportSourceMap { @@ -58,7 +55,7 @@ impl ImportSourceMap { ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), }; - self.map[import].to_node(&file) + to_node(self.map[import], &file) } } diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 39f394c3f..8d57e7761 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -2,6 +2,7 @@ use std::{iter, sync::Arc}; +use hir_expand::either::Either; use ra_db::CrateId; use ra_syntax::{ ast::{self, NameOwner, TypeAscriptionOwner}, @@ -9,7 +10,6 @@ use ra_syntax::{ }; use crate::{ - either::Either, hygiene::Hygiene, name::{self, AsName, Name}, type_ref::TypeRef, diff --git a/crates/ra_hir_expand/src/either.rs b/crates/ra_hir_expand/src/either.rs new file mode 100644 index 000000000..83583ef8b --- /dev/null +++ b/crates/ra_hir_expand/src/either.rs @@ -0,0 +1,54 @@ +//! FIXME: write short doc here + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum Either { + A(A), + B(B), +} + +impl Either { + pub fn either(self, f1: F1, f2: F2) -> R + where + F1: FnOnce(A) -> R, + F2: FnOnce(B) -> R, + { + match self { + Either::A(a) => f1(a), + Either::B(b) => f2(b), + } + } + pub fn map(self, f1: F1, f2: F2) -> Either + where + F1: FnOnce(A) -> U, + F2: FnOnce(B) -> V, + { + match self { + Either::A(a) => Either::A(f1(a)), + Either::B(b) => Either::B(f2(b)), + } + } + pub fn map_a(self, f: F) -> Either + where + F: FnOnce(A) -> U, + { + self.map(f, |it| it) + } + pub fn a(self) -> Option { + match self { + Either::A(it) => Some(it), + Either::B(_) => None, + } + } + pub fn b(self) -> Option { + match self { + Either::A(_) => None, + Either::B(it) => Some(it), + } + } + pub fn as_ref(&self) -> Either<&A, &B> { + match self { + Either::A(it) => Either::A(it), + Either::B(it) => Either::B(it), + } + } +} diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 3c0ef8f1c..6359b2b4d 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -6,6 +6,7 @@ pub mod db; pub mod ast_id_map; +pub mod either; use std::hash::{Hash, Hasher}; -- cgit v1.2.3 From 872ac566bfc6cf43ac55354cf5223b962dbc1d92 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 18:56:20 +0300 Subject: push name down to hir_expand --- crates/ra_hir/src/adt.rs | 3 +- crates/ra_hir/src/code_model.rs | 8 +- crates/ra_hir/src/expr/lower.rs | 8 +- crates/ra_hir/src/from_source.rs | 2 +- crates/ra_hir/src/generics.rs | 2 +- crates/ra_hir/src/lib.rs | 3 +- crates/ra_hir/src/nameres/collector.rs | 3 +- crates/ra_hir/src/resolve.rs | 2 +- crates/ra_hir/src/source_binder.rs | 3 +- crates/ra_hir/src/traits.rs | 3 +- crates/ra_hir/src/ty/autoderef.rs | 2 +- crates/ra_hir/src/ty/infer.rs | 2 +- crates/ra_hir/src/ty/infer/expr.rs | 6 +- crates/ra_hir/src/ty/traits/chalk.rs | 3 +- crates/ra_hir/src/type_alias.rs | 7 +- crates/ra_hir_def/src/hygiene.rs | 10 ++- crates/ra_hir_def/src/lib.rs | 1 - crates/ra_hir_def/src/name.rs | 142 --------------------------------- crates/ra_hir_def/src/nameres/raw.rs | 15 ++-- crates/ra_hir_def/src/path.rs | 15 ++-- crates/ra_hir_expand/src/lib.rs | 1 + crates/ra_hir_expand/src/name.rs | 142 +++++++++++++++++++++++++++++++++ 22 files changed, 191 insertions(+), 192 deletions(-) delete mode 100644 crates/ra_hir_def/src/name.rs create mode 100644 crates/ra_hir_expand/src/name.rs diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index d16b3a1cc..97424b39e 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -3,7 +3,8 @@ use std::sync::Arc; -use hir_def::{name::AsName, type_ref::TypeRef}; +use hir_def::type_ref::TypeRef; +use hir_expand::name::AsName; use ra_arena::{impl_arena_id, Arena, RawId}; use ra_syntax::ast::{self, NameOwner, StructKind, TypeAscriptionOwner}; diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index d865c972e..a6ce23dd1 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -6,13 +6,13 @@ pub(crate) mod docs; use std::sync::Arc; use hir_def::{ - name::{ - self, AsName, BOOL, CHAR, F32, F64, I128, I16, I32, I64, I8, ISIZE, SELF_TYPE, STR, U128, - U16, U32, U64, U8, USIZE, - }, type_ref::{Mutability, TypeRef}, CrateModuleId, ModuleId, }; +use hir_expand::name::{ + self, AsName, BOOL, CHAR, F32, F64, I128, I16, I32, I64, I8, ISIZE, SELF_TYPE, STR, U128, U16, + U32, U64, U8, USIZE, +}; use ra_db::{CrateId, Edition}; use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; diff --git a/crates/ra_hir/src/expr/lower.rs b/crates/ra_hir/src/expr/lower.rs index 575d78198..241ad68fd 100644 --- a/crates/ra_hir/src/expr/lower.rs +++ b/crates/ra_hir/src/expr/lower.rs @@ -1,11 +1,7 @@ //! FIXME: write short doc here -use hir_def::{ - hygiene::Hygiene, - name::{self, AsName, Name}, - path::GenericArgs, - type_ref::TypeRef, -}; +use hir_def::{hygiene::Hygiene, path::GenericArgs, type_ref::TypeRef}; +use hir_expand::name::{self, AsName, Name}; use ra_arena::Arena; use ra_syntax::{ ast::{ diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index b9fbaa367..a9de01455 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use hir_def::name::AsName; +use hir_expand::name::AsName; use ra_syntax::ast::{self, AstNode, NameOwner}; use crate::{ diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs index 9d5d18564..52e1fbf29 100644 --- a/crates/ra_hir/src/generics.rs +++ b/crates/ra_hir/src/generics.rs @@ -6,10 +6,10 @@ use std::sync::Arc; use hir_def::{ - name::{self, AsName}, path::Path, type_ref::{TypeBound, TypeRef}, }; +use hir_expand::name::{self, AsName}; use ra_syntax::ast::{self, DefaultTypeParamOwner, NameOwner, TypeBoundsOwner, TypeParamsOwner}; use crate::{ diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 989818c0e..603b0c3dc 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -81,8 +81,7 @@ pub use crate::{ }; pub use hir_def::{ - name::Name, path::{Path, PathKind}, type_ref::Mutability, }; -pub use hir_expand::either::Either; +pub use hir_expand::{either::Either, name::Name}; diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index 2f342870b..e2e13805a 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -1,6 +1,7 @@ //! FIXME: write short doc here -use hir_def::{attr::Attr, name, nameres::raw}; +use hir_def::{attr::Attr, nameres::raw}; +use hir_expand::name; use ra_cfg::CfgOptions; use ra_db::FileId; use ra_syntax::{ast, SmolStr}; diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 2a783b61e..f77c9df9f 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -2,10 +2,10 @@ use std::sync::Arc; use hir_def::{ - name::{self, Name}, path::{Path, PathKind}, CrateModuleId, }; +use hir_expand::name::{self, Name}; use rustc_hash::FxHashSet; use crate::{ diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 544433a0a..01f51ba5d 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -7,7 +7,8 @@ //! purely for "IDE needs". use std::sync::Arc; -use hir_def::{name::AsName, path::known}; +use hir_def::path::known; +use hir_expand::name::AsName; use ra_db::FileId; use ra_syntax::{ ast::{self, AstNode}, diff --git a/crates/ra_hir/src/traits.rs b/crates/ra_hir/src/traits.rs index 514c813ab..1a45dacba 100644 --- a/crates/ra_hir/src/traits.rs +++ b/crates/ra_hir/src/traits.rs @@ -2,7 +2,8 @@ use std::sync::Arc; -use hir_def::name::AsName; +use hir_expand::name::AsName; + use ra_syntax::ast::{self, NameOwner}; use rustc_hash::FxHashMap; diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs index 03c45546d..3645ee831 100644 --- a/crates/ra_hir/src/ty/autoderef.rs +++ b/crates/ra_hir/src/ty/autoderef.rs @@ -5,7 +5,7 @@ use std::iter::successors; -use hir_def::name; +use hir_expand::name; use log::{info, warn}; use super::{traits::Solution, Canonical, Substs, Ty, TypeWalk}; diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 7466ee341..6694467a3 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -22,10 +22,10 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; use rustc_hash::FxHashMap; use hir_def::{ - name, path::known, type_ref::{Mutability, TypeRef}, }; +use hir_expand::name; use ra_arena::map::ArenaMap; use ra_prof::profile; use test_utils::tested_by; diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs index bc6437b44..fed52df39 100644 --- a/crates/ra_hir/src/ty/infer/expr.rs +++ b/crates/ra_hir/src/ty/infer/expr.rs @@ -3,10 +3,8 @@ use std::iter::{repeat, repeat_with}; use std::sync::Arc; -use hir_def::{ - name, - path::{GenericArg, GenericArgs}, -}; +use hir_def::path::{GenericArg, GenericArgs}; +use hir_expand::name; use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; use crate::{ diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index 2dd4c2fae..39ef92182 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -9,7 +9,8 @@ use chalk_ir::{ }; use chalk_rust_ir::{AssociatedTyDatum, ImplDatum, StructDatum, TraitDatum}; -use hir_def::name; +use hir_expand::name; + use ra_db::salsa::{InternId, InternKey}; use super::{Canonical, ChalkContext, Impl, Obligation}; diff --git a/crates/ra_hir/src/type_alias.rs b/crates/ra_hir/src/type_alias.rs index 87126ee7f..078e6295e 100644 --- a/crates/ra_hir/src/type_alias.rs +++ b/crates/ra_hir/src/type_alias.rs @@ -2,10 +2,9 @@ use std::sync::Arc; -use hir_def::{ - name::{AsName, Name}, - type_ref::TypeRef, -}; +use hir_def::type_ref::TypeRef; +use hir_expand::name::{AsName, Name}; + use ra_syntax::ast::NameOwner; use crate::{ diff --git a/crates/ra_hir_def/src/hygiene.rs b/crates/ra_hir_def/src/hygiene.rs index f51c46fcb..94de2c57c 100644 --- a/crates/ra_hir_def/src/hygiene.rs +++ b/crates/ra_hir_def/src/hygiene.rs @@ -4,13 +4,15 @@ //! this moment, this is horribly incomplete and handles only `$crate`. // Should this be moved to `hir_expand`? Seems like it. -use hir_expand::either::Either; -use hir_expand::{db::AstDatabase, HirFileId}; +use hir_expand::{ + db::AstDatabase, + either::Either, + name::{AsName, Name}, + HirFileId, +}; use ra_db::CrateId; use ra_syntax::ast; -use crate::name::{AsName, Name}; - #[derive(Debug)] pub struct Hygiene { // This is what `$crate` expands to diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 0de728dc1..5135dda56 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -9,7 +9,6 @@ pub mod db; pub mod attr; -pub mod name; pub mod path; pub mod type_ref; pub mod hygiene; diff --git a/crates/ra_hir_def/src/name.rs b/crates/ra_hir_def/src/name.rs deleted file mode 100644 index 720896ee8..000000000 --- a/crates/ra_hir_def/src/name.rs +++ /dev/null @@ -1,142 +0,0 @@ -//! FIXME: write short doc here - -use std::fmt; - -use ra_syntax::{ast, SmolStr}; - -/// `Name` is a wrapper around string, which is used in hir for both references -/// and declarations. In theory, names should also carry hygiene info, but we are -/// not there yet! -#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub struct Name(Repr); - -#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] -enum Repr { - Text(SmolStr), - TupleField(usize), -} - -impl fmt::Display for Name { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match &self.0 { - Repr::Text(text) => fmt::Display::fmt(&text, f), - Repr::TupleField(idx) => fmt::Display::fmt(&idx, f), - } - } -} - -impl Name { - /// Note: this is private to make creating name from random string hard. - /// Hopefully, this should allow us to integrate hygiene cleaner in the - /// future, and to switch to interned representation of names. - const fn new_text(text: SmolStr) -> Name { - Name(Repr::Text(text)) - } - - pub fn new_tuple_field(idx: usize) -> Name { - Name(Repr::TupleField(idx)) - } - - /// Shortcut to create inline plain text name - const fn new_inline_ascii(len: usize, text: &[u8]) -> Name { - Name::new_text(SmolStr::new_inline_from_ascii(len, text)) - } - - /// Resolve a name from the text of token. - fn resolve(raw_text: &SmolStr) -> Name { - let raw_start = "r#"; - if raw_text.as_str().starts_with(raw_start) { - Name::new_text(SmolStr::new(&raw_text[raw_start.len()..])) - } else { - Name::new_text(raw_text.clone()) - } - } - - pub fn missing() -> Name { - Name::new_text("[missing name]".into()) - } - - pub fn as_tuple_index(&self) -> Option { - match self.0 { - Repr::TupleField(idx) => Some(idx), - _ => None, - } - } -} - -pub trait AsName { - fn as_name(&self) -> Name; -} - -impl AsName for ast::NameRef { - fn as_name(&self) -> Name { - match self.as_tuple_field() { - Some(idx) => Name::new_tuple_field(idx), - None => Name::resolve(self.text()), - } - } -} - -impl AsName for ast::Name { - fn as_name(&self) -> Name { - Name::resolve(self.text()) - } -} - -impl AsName for ast::FieldKind { - fn as_name(&self) -> Name { - match self { - ast::FieldKind::Name(nr) => nr.as_name(), - ast::FieldKind::Index(idx) => Name::new_tuple_field(idx.text().parse().unwrap()), - } - } -} - -impl AsName for ra_db::Dependency { - fn as_name(&self) -> Name { - Name::new_text(self.name.clone()) - } -} - -// Primitives -pub const ISIZE: Name = Name::new_inline_ascii(5, b"isize"); -pub const I8: Name = Name::new_inline_ascii(2, b"i8"); -pub const I16: Name = Name::new_inline_ascii(3, b"i16"); -pub const I32: Name = Name::new_inline_ascii(3, b"i32"); -pub const I64: Name = Name::new_inline_ascii(3, b"i64"); -pub const I128: Name = Name::new_inline_ascii(4, b"i128"); -pub const USIZE: Name = Name::new_inline_ascii(5, b"usize"); -pub const U8: Name = Name::new_inline_ascii(2, b"u8"); -pub const U16: Name = Name::new_inline_ascii(3, b"u16"); -pub const U32: Name = Name::new_inline_ascii(3, b"u32"); -pub const U64: Name = Name::new_inline_ascii(3, b"u64"); -pub const U128: Name = Name::new_inline_ascii(4, b"u128"); -pub const F32: Name = Name::new_inline_ascii(3, b"f32"); -pub const F64: Name = Name::new_inline_ascii(3, b"f64"); -pub const BOOL: Name = Name::new_inline_ascii(4, b"bool"); -pub const CHAR: Name = Name::new_inline_ascii(4, b"char"); -pub const STR: Name = Name::new_inline_ascii(3, b"str"); - -// Special names -pub const SELF_PARAM: Name = Name::new_inline_ascii(4, b"self"); -pub const SELF_TYPE: Name = Name::new_inline_ascii(4, b"Self"); -pub const MACRO_RULES: Name = Name::new_inline_ascii(11, b"macro_rules"); - -// Components of known path (value or mod name) -pub const STD: Name = Name::new_inline_ascii(3, b"std"); -pub const ITER: Name = Name::new_inline_ascii(4, b"iter"); -pub const OPS: Name = Name::new_inline_ascii(3, b"ops"); -pub const FUTURE: Name = Name::new_inline_ascii(6, b"future"); -pub const RESULT: Name = Name::new_inline_ascii(6, b"result"); -pub const BOXED: Name = Name::new_inline_ascii(5, b"boxed"); - -// Components of known path (type name) -pub const INTO_ITERATOR_TYPE: Name = Name::new_inline_ascii(12, b"IntoIterator"); -pub const ITEM_TYPE: Name = Name::new_inline_ascii(4, b"Item"); -pub const TRY_TYPE: Name = Name::new_inline_ascii(3, b"Try"); -pub const OK_TYPE: Name = Name::new_inline_ascii(2, b"Ok"); -pub const FUTURE_TYPE: Name = Name::new_inline_ascii(6, b"Future"); -pub const RESULT_TYPE: Name = Name::new_inline_ascii(6, b"Result"); -pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output"); -pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target"); -pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box"); diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index f1896c0cc..56831e409 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -2,7 +2,12 @@ use std::{ops::Index, sync::Arc}; -use hir_expand::{ast_id_map::AstIdMap, db::AstDatabase, either::Either}; +use hir_expand::{ + ast_id_map::AstIdMap, + db::AstDatabase, + either::Either, + name::{AsName, Name}, +}; use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; use ra_syntax::{ ast::{self, AttrsOwner, NameOwner}, @@ -10,12 +15,8 @@ use ra_syntax::{ }; use crate::{ - attr::Attr, - db::DefDatabase2, - hygiene::Hygiene, - name::{AsName, Name}, - path::Path, - FileAstId, HirFileId, ModuleSource, Source, + attr::Attr, db::DefDatabase2, hygiene::Hygiene, path::Path, FileAstId, HirFileId, ModuleSource, + Source, }; /// `RawItems` is a set of top-level items in a file (except for impls). diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 8d57e7761..d0b842a6b 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -2,19 +2,17 @@ use std::{iter, sync::Arc}; -use hir_expand::either::Either; +use hir_expand::{ + either::Either, + name::{self, AsName, Name}, +}; use ra_db::CrateId; use ra_syntax::{ ast::{self, NameOwner, TypeAscriptionOwner}, AstNode, }; -use crate::{ - hygiene::Hygiene, - name::{self, AsName, Name}, - type_ref::TypeRef, - Source, -}; +use crate::{hygiene::Hygiene, type_ref::TypeRef, Source}; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Path { @@ -392,8 +390,9 @@ fn convert_path(prefix: Option, path: ast::Path, hygiene: &Hygiene) -> Opt } pub mod known { + use hir_expand::name; + use super::{Path, PathKind}; - use crate::name; pub fn std_iter_into_iterator() -> Path { Path::from_simple_segments( diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 6359b2b4d..cf28de3d8 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -7,6 +7,7 @@ pub mod db; pub mod ast_id_map; pub mod either; +pub mod name; use std::hash::{Hash, Hasher}; diff --git a/crates/ra_hir_expand/src/name.rs b/crates/ra_hir_expand/src/name.rs new file mode 100644 index 000000000..720896ee8 --- /dev/null +++ b/crates/ra_hir_expand/src/name.rs @@ -0,0 +1,142 @@ +//! FIXME: write short doc here + +use std::fmt; + +use ra_syntax::{ast, SmolStr}; + +/// `Name` is a wrapper around string, which is used in hir for both references +/// and declarations. In theory, names should also carry hygiene info, but we are +/// not there yet! +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct Name(Repr); + +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] +enum Repr { + Text(SmolStr), + TupleField(usize), +} + +impl fmt::Display for Name { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match &self.0 { + Repr::Text(text) => fmt::Display::fmt(&text, f), + Repr::TupleField(idx) => fmt::Display::fmt(&idx, f), + } + } +} + +impl Name { + /// Note: this is private to make creating name from random string hard. + /// Hopefully, this should allow us to integrate hygiene cleaner in the + /// future, and to switch to interned representation of names. + const fn new_text(text: SmolStr) -> Name { + Name(Repr::Text(text)) + } + + pub fn new_tuple_field(idx: usize) -> Name { + Name(Repr::TupleField(idx)) + } + + /// Shortcut to create inline plain text name + const fn new_inline_ascii(len: usize, text: &[u8]) -> Name { + Name::new_text(SmolStr::new_inline_from_ascii(len, text)) + } + + /// Resolve a name from the text of token. + fn resolve(raw_text: &SmolStr) -> Name { + let raw_start = "r#"; + if raw_text.as_str().starts_with(raw_start) { + Name::new_text(SmolStr::new(&raw_text[raw_start.len()..])) + } else { + Name::new_text(raw_text.clone()) + } + } + + pub fn missing() -> Name { + Name::new_text("[missing name]".into()) + } + + pub fn as_tuple_index(&self) -> Option { + match self.0 { + Repr::TupleField(idx) => Some(idx), + _ => None, + } + } +} + +pub trait AsName { + fn as_name(&self) -> Name; +} + +impl AsName for ast::NameRef { + fn as_name(&self) -> Name { + match self.as_tuple_field() { + Some(idx) => Name::new_tuple_field(idx), + None => Name::resolve(self.text()), + } + } +} + +impl AsName for ast::Name { + fn as_name(&self) -> Name { + Name::resolve(self.text()) + } +} + +impl AsName for ast::FieldKind { + fn as_name(&self) -> Name { + match self { + ast::FieldKind::Name(nr) => nr.as_name(), + ast::FieldKind::Index(idx) => Name::new_tuple_field(idx.text().parse().unwrap()), + } + } +} + +impl AsName for ra_db::Dependency { + fn as_name(&self) -> Name { + Name::new_text(self.name.clone()) + } +} + +// Primitives +pub const ISIZE: Name = Name::new_inline_ascii(5, b"isize"); +pub const I8: Name = Name::new_inline_ascii(2, b"i8"); +pub const I16: Name = Name::new_inline_ascii(3, b"i16"); +pub const I32: Name = Name::new_inline_ascii(3, b"i32"); +pub const I64: Name = Name::new_inline_ascii(3, b"i64"); +pub const I128: Name = Name::new_inline_ascii(4, b"i128"); +pub const USIZE: Name = Name::new_inline_ascii(5, b"usize"); +pub const U8: Name = Name::new_inline_ascii(2, b"u8"); +pub const U16: Name = Name::new_inline_ascii(3, b"u16"); +pub const U32: Name = Name::new_inline_ascii(3, b"u32"); +pub const U64: Name = Name::new_inline_ascii(3, b"u64"); +pub const U128: Name = Name::new_inline_ascii(4, b"u128"); +pub const F32: Name = Name::new_inline_ascii(3, b"f32"); +pub const F64: Name = Name::new_inline_ascii(3, b"f64"); +pub const BOOL: Name = Name::new_inline_ascii(4, b"bool"); +pub const CHAR: Name = Name::new_inline_ascii(4, b"char"); +pub const STR: Name = Name::new_inline_ascii(3, b"str"); + +// Special names +pub const SELF_PARAM: Name = Name::new_inline_ascii(4, b"self"); +pub const SELF_TYPE: Name = Name::new_inline_ascii(4, b"Self"); +pub const MACRO_RULES: Name = Name::new_inline_ascii(11, b"macro_rules"); + +// Components of known path (value or mod name) +pub const STD: Name = Name::new_inline_ascii(3, b"std"); +pub const ITER: Name = Name::new_inline_ascii(4, b"iter"); +pub const OPS: Name = Name::new_inline_ascii(3, b"ops"); +pub const FUTURE: Name = Name::new_inline_ascii(6, b"future"); +pub const RESULT: Name = Name::new_inline_ascii(6, b"result"); +pub const BOXED: Name = Name::new_inline_ascii(5, b"boxed"); + +// Components of known path (type name) +pub const INTO_ITERATOR_TYPE: Name = Name::new_inline_ascii(12, b"IntoIterator"); +pub const ITEM_TYPE: Name = Name::new_inline_ascii(4, b"Item"); +pub const TRY_TYPE: Name = Name::new_inline_ascii(3, b"Try"); +pub const OK_TYPE: Name = Name::new_inline_ascii(2, b"Ok"); +pub const FUTURE_TYPE: Name = Name::new_inline_ascii(6, b"Future"); +pub const RESULT_TYPE: Name = Name::new_inline_ascii(6, b"Result"); +pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output"); +pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target"); +pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box"); -- cgit v1.2.3 From ab559f170ee02e3bdd9aeeb55933bb143b520c34 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 19:10:53 +0300 Subject: move hygiene to hir_expand --- crates/ra_hir/src/expr/lower.rs | 7 ++++-- crates/ra_hir/src/impl_block.rs | 3 ++- crates/ra_hir_def/src/attr.rs | 3 ++- crates/ra_hir_def/src/hygiene.rs | 40 ------------------------------- crates/ra_hir_def/src/lib.rs | 1 - crates/ra_hir_def/src/nameres/raw.rs | 6 ++--- crates/ra_hir_def/src/path.rs | 3 ++- crates/ra_hir_expand/src/hygiene.rs | 46 ++++++++++++++++++++++++++++++++++++ crates/ra_hir_expand/src/lib.rs | 12 +--------- 9 files changed, 60 insertions(+), 61 deletions(-) delete mode 100644 crates/ra_hir_def/src/hygiene.rs create mode 100644 crates/ra_hir_expand/src/hygiene.rs diff --git a/crates/ra_hir/src/expr/lower.rs b/crates/ra_hir/src/expr/lower.rs index 241ad68fd..6463dd65e 100644 --- a/crates/ra_hir/src/expr/lower.rs +++ b/crates/ra_hir/src/expr/lower.rs @@ -1,7 +1,10 @@ //! FIXME: write short doc here -use hir_def::{hygiene::Hygiene, path::GenericArgs, type_ref::TypeRef}; -use hir_expand::name::{self, AsName, Name}; +use hir_def::{path::GenericArgs, type_ref::TypeRef}; +use hir_expand::{ + hygiene::Hygiene, + name::{self, AsName, Name}, +}; use ra_arena::Arena; use ra_syntax::{ ast::{ diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 9e4a40017..b1a014074 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -3,7 +3,8 @@ use rustc_hash::FxHashMap; use std::sync::Arc; -use hir_def::{attr::Attr, hygiene::Hygiene, type_ref::TypeRef}; +use hir_def::{attr::Attr, type_ref::TypeRef}; +use hir_expand::hygiene::Hygiene; use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; use ra_cfg::CfgOptions; use ra_syntax::{ diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 71f92adc2..0e961ca12 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -2,6 +2,7 @@ use std::sync::Arc; +use hir_expand::hygiene::Hygiene; use mbe::ast_to_token_tree; use ra_cfg::CfgOptions; use ra_syntax::{ @@ -10,7 +11,7 @@ use ra_syntax::{ }; use tt::Subtree; -use crate::{hygiene::Hygiene, path::Path}; +use crate::path::Path; #[derive(Debug, Clone, PartialEq, Eq)] pub struct Attr { diff --git a/crates/ra_hir_def/src/hygiene.rs b/crates/ra_hir_def/src/hygiene.rs deleted file mode 100644 index 94de2c57c..000000000 --- a/crates/ra_hir_def/src/hygiene.rs +++ /dev/null @@ -1,40 +0,0 @@ -//! This modules handles hygiene information. -//! -//! Specifically, `ast` + `Hygiene` allows you to create a `Name`. Note that, at -//! this moment, this is horribly incomplete and handles only `$crate`. -// Should this be moved to `hir_expand`? Seems like it. - -use hir_expand::{ - db::AstDatabase, - either::Either, - name::{AsName, Name}, - HirFileId, -}; -use ra_db::CrateId; -use ra_syntax::ast; - -#[derive(Debug)] -pub struct Hygiene { - // This is what `$crate` expands to - def_crate: Option, -} - -impl Hygiene { - pub fn new(db: &impl AstDatabase, file_id: HirFileId) -> Hygiene { - Hygiene { def_crate: file_id.macro_crate(db) } - } - - pub(crate) fn new_unhygienic() -> Hygiene { - Hygiene { def_crate: None } - } - - // FIXME: this should just return name - pub(crate) fn name_ref_to_name(&self, name_ref: ast::NameRef) -> Either { - if let Some(def_crate) = self.def_crate { - if name_ref.text() == "$crate" { - return Either::B(def_crate); - } - } - Either::A(name_ref.as_name()) - } -} diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 5135dda56..7a6c7b301 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -11,7 +11,6 @@ pub mod db; pub mod attr; pub mod path; pub mod type_ref; -pub mod hygiene; // FIXME: this should be private pub mod nameres; diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 56831e409..86c05d602 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -6,6 +6,7 @@ use hir_expand::{ ast_id_map::AstIdMap, db::AstDatabase, either::Either, + hygiene::Hygiene, name::{AsName, Name}, }; use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; @@ -14,10 +15,7 @@ use ra_syntax::{ AstNode, AstPtr, SourceFile, }; -use crate::{ - attr::Attr, db::DefDatabase2, hygiene::Hygiene, path::Path, FileAstId, HirFileId, ModuleSource, - Source, -}; +use crate::{attr::Attr, db::DefDatabase2, path::Path, FileAstId, HirFileId, ModuleSource, Source}; /// `RawItems` is a set of top-level items in a file (except for impls). /// diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index d0b842a6b..ddabc7ca6 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -4,6 +4,7 @@ use std::{iter, sync::Arc}; use hir_expand::{ either::Either, + hygiene::Hygiene, name::{self, AsName, Name}, }; use ra_db::CrateId; @@ -12,7 +13,7 @@ use ra_syntax::{ AstNode, }; -use crate::{hygiene::Hygiene, type_ref::TypeRef, Source}; +use crate::{type_ref::TypeRef, Source}; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Path { diff --git a/crates/ra_hir_expand/src/hygiene.rs b/crates/ra_hir_expand/src/hygiene.rs new file mode 100644 index 000000000..77428ec99 --- /dev/null +++ b/crates/ra_hir_expand/src/hygiene.rs @@ -0,0 +1,46 @@ +//! This modules handles hygiene information. +//! +//! Specifically, `ast` + `Hygiene` allows you to create a `Name`. Note that, at +//! this moment, this is horribly incomplete and handles only `$crate`. +use ra_db::CrateId; +use ra_syntax::ast; + +use crate::{ + db::AstDatabase, + either::Either, + name::{AsName, Name}, + HirFileId, HirFileIdRepr, +}; + +#[derive(Debug)] +pub struct Hygiene { + // This is what `$crate` expands to + def_crate: Option, +} + +impl Hygiene { + pub fn new(db: &impl AstDatabase, file_id: HirFileId) -> Hygiene { + let def_crate = match file_id.0 { + HirFileIdRepr::FileId(_) => None, + HirFileIdRepr::MacroFile(macro_file) => { + let loc = db.lookup_intern_macro(macro_file.macro_call_id); + Some(loc.def.krate) + } + }; + Hygiene { def_crate } + } + + pub fn new_unhygienic() -> Hygiene { + Hygiene { def_crate: None } + } + + // FIXME: this should just return name + pub fn name_ref_to_name(&self, name_ref: ast::NameRef) -> Either { + if let Some(def_crate) = self.def_crate { + if name_ref.text() == "$crate" { + return Either::B(def_crate); + } + } + Either::A(name_ref.as_name()) + } +} diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index cf28de3d8..5a0e5a19c 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -8,6 +8,7 @@ pub mod db; pub mod ast_id_map; pub mod either; pub mod name; +pub mod hygiene; use std::hash::{Hash, Hasher}; @@ -61,17 +62,6 @@ impl HirFileId { } } } - - /// Get the crate which the macro lives in, if it is a macro file. - pub fn macro_crate(self, db: &dyn db::AstDatabase) -> Option { - match self.0 { - HirFileIdRepr::FileId(_) => None, - HirFileIdRepr::MacroFile(macro_file) => { - let loc = db.lookup_intern_macro(macro_file.macro_call_id); - Some(loc.def.krate) - } - } - } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -- cgit v1.2.3 From cf4f7eb56660cfff355cb6bd41d5c17f7d19571b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 19:15:41 +0300 Subject: improve compile time a bit --- crates/ra_hir_def/src/path.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index ddabc7ca6..04039376f 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -292,7 +292,7 @@ fn expand_use_tree( prefix: Option, tree: ast::UseTree, hygiene: &Hygiene, - cb: &mut impl FnMut(Path, &ast::UseTree, bool, Option), + cb: &mut dyn FnMut(Path, &ast::UseTree, bool, Option), ) { if let Some(use_tree_list) = tree.use_tree_list() { let prefix = match tree.path() { -- cgit v1.2.3