From 9a3bb19d3274824d6648117ab32ac59358ff1f14 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 24 Nov 2020 21:52:19 +0100 Subject: [PATCH 1/3] Remove fixed FIXME, propagate errors better --- crates/hir_expand/src/db.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index fc512517cb..3fd9a90a8e 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs @@ -359,11 +359,7 @@ pub fn parse_macro_with_arg( match result.error { Some(error) => { - // FIXME: - // In future, we should propagate the actual error with recovery information - // instead of ignore the error here. - - // Safe check for recurisve identity macro + // Safety check for recursive identity macro. let node = parse.syntax_node(); let file: HirFileId = macro_file.into(); let call_node = match file.call_node(db) { @@ -374,7 +370,7 @@ pub fn parse_macro_with_arg( }; if !diff(&node, &call_node.value).is_empty() { - MacroResult { value: Some((parse, Arc::new(rev_token_map))), error: None } + MacroResult { value: Some((parse, Arc::new(rev_token_map))), error: Some(error) } } else { return MacroResult::error(error); } From f9d0d511011b7071c621ac3f7c64c4aaec7f2dfb Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 24 Nov 2020 21:55:08 +0100 Subject: [PATCH 2/3] hir_expand::db: reduce fn visibility --- crates/hir_expand/src/db.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index 3fd9a90a8e..36d3307a1f 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs @@ -138,16 +138,13 @@ pub fn expand_hypothetical( Some((node.syntax_node(), token)) } -pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc { +fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc { let map = db.parse_or_expand(file_id).map_or_else(AstIdMap::default, |it| AstIdMap::from_source(&it)); Arc::new(map) } -pub(crate) fn macro_def( - db: &dyn AstDatabase, - id: MacroDefId, -) -> Option> { +fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option> { match id.kind { MacroDefKind::Declarative => { let macro_call = id.ast_id?.to_node(db); @@ -178,7 +175,7 @@ pub(crate) fn macro_def( } } -pub(crate) fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option { +fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option { let id = match id { MacroCallId::LazyMacro(id) => id, MacroCallId::EagerMacro(_id) => { @@ -191,16 +188,13 @@ pub(crate) fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option Option> { +fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option> { let arg = db.macro_arg_text(id)?; let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg))?; Some(Arc::new((tt, tmap))) } -pub(crate) fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> MacroResult> { +fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> MacroResult> { macro_expand_with_arg(db, id, None) } @@ -258,7 +252,7 @@ fn macro_expand_with_arg( MacroResult { value: Some(Arc::new(tt)), error: err.map(|e| format!("{:?}", e)) } } -pub(crate) fn expand_proc_macro( +fn expand_proc_macro( db: &dyn AstDatabase, id: MacroCallId, ) -> Result { @@ -285,7 +279,7 @@ pub(crate) fn expand_proc_macro( expander.expand(db, lazy_id, ¯o_arg.0) } -pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option { +fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option { match file_id.0 { HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()), HirFileIdRepr::MacroFile(macro_file) => { @@ -294,14 +288,14 @@ pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Optio } } -pub(crate) fn parse_macro( +fn parse_macro( db: &dyn AstDatabase, macro_file: MacroFile, ) -> MacroResult<(Parse, Arc)> { parse_macro_with_arg(db, macro_file, None) } -pub fn parse_macro_with_arg( +fn parse_macro_with_arg( db: &dyn AstDatabase, macro_file: MacroFile, arg: Option>, From 9559bce311c3b6b7009b9cd09e52c75497c87035 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 24 Nov 2020 21:57:51 +0100 Subject: [PATCH 3/3] Rename `parse_macro` to `parse_macro_expansion` This does not parse macros, it expands a macro and parses the *result* --- crates/hir/src/db.rs | 2 +- crates/hir_expand/src/db.rs | 6 +++--- crates/hir_expand/src/lib.rs | 2 +- crates/ide/src/status.rs | 2 +- crates/ide_db/src/apply_change.rs | 4 ++-- crates/ide_db/src/lib.rs | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/hir/src/db.rs b/crates/hir/src/db.rs index 07333c453f..8c767b249d 100644 --- a/crates/hir/src/db.rs +++ b/crates/hir/src/db.rs @@ -11,7 +11,7 @@ pub use hir_def::db::{ }; pub use hir_expand::db::{ AstDatabase, AstDatabaseStorage, AstIdMapQuery, InternEagerExpansionQuery, InternMacroQuery, - MacroArgTextQuery, MacroDefQuery, MacroExpandQuery, ParseMacroQuery, + MacroArgTextQuery, MacroDefQuery, MacroExpandQuery, ParseMacroExpansionQuery, }; pub use hir_ty::db::*; diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index 36d3307a1f..a9099eb22a 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs @@ -88,7 +88,7 @@ pub trait AstDatabase: SourceDatabase { #[salsa::transparent] fn macro_arg(&self, id: MacroCallId) -> Option>; fn macro_def(&self, id: MacroDefId) -> Option>; - fn parse_macro( + fn parse_macro_expansion( &self, macro_file: MacroFile, ) -> MacroResult<(Parse, Arc)>; @@ -283,12 +283,12 @@ fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option Some(db.parse(file_id).tree().syntax().clone()), HirFileIdRepr::MacroFile(macro_file) => { - db.parse_macro(macro_file).map(|(it, _)| it.syntax_node()).value + db.parse_macro_expansion(macro_file).map(|(it, _)| it.syntax_node()).value } } } -fn parse_macro( +fn parse_macro_expansion( db: &dyn AstDatabase, macro_file: MacroFile, ) -> MacroResult<(Parse, Arc)> { diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 9fc697d6fa..83e09738b1 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs @@ -144,7 +144,7 @@ impl HirFileId { let def_tt = loc.def.ast_id?.to_node(db).token_tree()?; let macro_def = db.macro_def(loc.def)?; - let (parse, exp_map) = db.parse_macro(macro_file).value?; + let (parse, exp_map) = db.parse_macro_expansion(macro_file).value?; let macro_arg = db.macro_arg(macro_file.macro_call_id)?; Some(ExpansionInfo { diff --git a/crates/ide/src/status.rs b/crates/ide/src/status.rs index 8b4a1652e4..b75f88ed99 100644 --- a/crates/ide/src/status.rs +++ b/crates/ide/src/status.rs @@ -19,7 +19,7 @@ fn syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats { ide_db::base_db::ParseQuery.in_db(db).entries::() } fn macro_syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats { - hir::db::ParseMacroQuery.in_db(db).entries::() + hir::db::ParseMacroExpansionQuery.in_db(db).entries::() } // Feature: Status diff --git a/crates/ide_db/src/apply_change.rs b/crates/ide_db/src/apply_change.rs index da16fa21dc..987191fe32 100644 --- a/crates/ide_db/src/apply_change.rs +++ b/crates/ide_db/src/apply_change.rs @@ -76,7 +76,7 @@ impl RootDatabase { let sweep = SweepStrategy::default().discard_values().sweep_all_revisions(); base_db::ParseQuery.in_db(self).sweep(sweep); - hir::db::ParseMacroQuery.in_db(self).sweep(sweep); + hir::db::ParseMacroExpansionQuery.in_db(self).sweep(sweep); // Macros do take significant space, but less then the syntax trees // self.query(hir::db::MacroDefQuery).sweep(sweep); @@ -143,7 +143,7 @@ impl RootDatabase { hir::db::AstIdMapQuery hir::db::MacroArgTextQuery hir::db::MacroDefQuery - hir::db::ParseMacroQuery + hir::db::ParseMacroExpansionQuery hir::db::MacroExpandQuery // DefDatabase diff --git a/crates/ide_db/src/lib.rs b/crates/ide_db/src/lib.rs index 38ebdbf791..05139a6515 100644 --- a/crates/ide_db/src/lib.rs +++ b/crates/ide_db/src/lib.rs @@ -113,7 +113,7 @@ impl RootDatabase { pub fn update_lru_capacity(&mut self, lru_capacity: Option) { let lru_capacity = lru_capacity.unwrap_or(base_db::DEFAULT_LRU_CAP); base_db::ParseQuery.in_db_mut(self).set_lru_capacity(lru_capacity); - hir::db::ParseMacroQuery.in_db_mut(self).set_lru_capacity(lru_capacity); + hir::db::ParseMacroExpansionQuery.in_db_mut(self).set_lru_capacity(lru_capacity); hir::db::MacroExpandQuery.in_db_mut(self).set_lru_capacity(lru_capacity); } }