From ab559f170ee02e3bdd9aeeb55933bb143b520c34 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 19:10:53 +0300 Subject: [PATCH] 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/lib.rs | 1 - crates/ra_hir_def/src/nameres/raw.rs | 6 ++--- crates/ra_hir_def/src/path.rs | 3 ++- .../src/hygiene.rs | 22 ++++++++++++------- crates/ra_hir_expand/src/lib.rs | 12 +--------- 8 files changed, 28 insertions(+), 29 deletions(-) rename crates/{ra_hir_def => ra_hir_expand}/src/hygiene.rs (63%) diff --git a/crates/ra_hir/src/expr/lower.rs b/crates/ra_hir/src/expr/lower.rs index 241ad68fd8b..6463dd65e1e 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 9e4a4001770..b1a0140745c 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 71f92adc297..0e961ca12af 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 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/lib.rs b/crates/ra_hir_def/src/lib.rs index 5135dda56a2..7a6c7b30125 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -11,7 +11,6 @@ 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 56831e409fd..86c05d6028e 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -6,6 +6,7 @@ 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 @@ 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 d0b842a6b1b..ddabc7ca668 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -4,6 +4,7 @@ use hir_expand::{ either::Either, + hygiene::Hygiene, name::{self, AsName, Name}, }; use ra_db::CrateId; @@ -12,7 +13,7 @@ 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_def/src/hygiene.rs b/crates/ra_hir_expand/src/hygiene.rs similarity index 63% rename from crates/ra_hir_def/src/hygiene.rs rename to crates/ra_hir_expand/src/hygiene.rs index 94de2c57c21..77428ec9904 100644 --- a/crates/ra_hir_def/src/hygiene.rs +++ b/crates/ra_hir_expand/src/hygiene.rs @@ -2,16 +2,15 @@ //! //! 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 ra_db::CrateId; +use ra_syntax::ast; -use hir_expand::{ +use crate::{ db::AstDatabase, either::Either, name::{AsName, Name}, - HirFileId, + HirFileId, HirFileIdRepr, }; -use ra_db::CrateId; -use ra_syntax::ast; #[derive(Debug)] pub struct Hygiene { @@ -21,15 +20,22 @@ pub struct Hygiene { impl Hygiene { pub fn new(db: &impl AstDatabase, file_id: HirFileId) -> Hygiene { - Hygiene { def_crate: file_id.macro_crate(db) } + 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(crate) fn new_unhygienic() -> Hygiene { + pub 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 { + 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); diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index cf28de3d8cc..5a0e5a19c53 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -8,6 +8,7 @@ pub mod ast_id_map; pub mod either; pub mod name; +pub mod hygiene; use std::hash::{Hash, Hasher}; @@ -61,17 +62,6 @@ pub fn original_file(self, db: &dyn db::AstDatabase) -> FileId { } } } - - /// 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)]