diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index f88ae61bb0e..398e00c42b4 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -45,6 +45,9 @@ pub trait DefDatabase: SourceDatabase { #[salsa::invoke(crate::ids::macro_arg_query)] fn macro_arg(&self, macro_call: ids::MacroCallId) -> Option>; + #[salsa::invoke(crate::ids::macro_expand_query)] + fn macro_expand(&self, macro_call: ids::MacroCallId) -> Result, String>; + #[salsa::invoke(crate::ids::HirFileId::hir_parse_query)] fn hir_parse(&self, file_id: HirFileId) -> TreeArc; diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index f9b3f5443c1..692da289539 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -5,14 +5,13 @@ use rustc_hash::FxHashMap; use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; use ra_syntax::{ - SyntaxNodePtr, AstPtr, AstNode,TreeArc, + SyntaxNodePtr, AstPtr, AstNode, ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind,ArrayExprKind, TypeAscriptionOwner} }; use crate::{ Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId, name::AsName, - ids::{MacroCallId}, type_ref::{Mutability, TypeRef}, }; use crate::{path::GenericArgs, ty::primitive::{IntTy, UncertainIntTy, FloatTy, UncertainFloatTy}}; @@ -826,8 +825,8 @@ where .with_file_id(self.current_file_id); if let Some(call_id) = self.resolver.resolve_macro_call(self.db, path, ast_id) { - if let Some(arg) = self.db.macro_arg(call_id) { - if let Some(expr) = expand_macro_to_expr(self.db, call_id, &arg) { + if let Some(tt) = self.db.macro_expand(call_id).ok() { + if let Some(expr) = mbe::token_tree_to_expr(&tt).ok() { log::debug!("macro expansion {}", expr.syntax().debug_dump()); let old_file_id = std::mem::replace(&mut self.current_file_id, call_id.into()); @@ -998,16 +997,6 @@ where } } -fn expand_macro_to_expr( - db: &impl HirDatabase, - macro_call: MacroCallId, - arg: &tt::Subtree, -) -> Option> { - let rules = db.macro_def(macro_call.loc(db).def)?; - let expanded = rules.expand(&arg).ok()?; - mbe::token_tree_to_expr(&expanded).ok() -} - pub(crate) fn body_with_source_map_query( db: &impl HirDatabase, def: DefWithBody, diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index cf0e934a9e7..4102951c954 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -63,19 +63,22 @@ impl HirFileId { match file_id.0 { HirFileIdRepr::File(file_id) => db.parse(file_id), HirFileIdRepr::Macro(macro_call_id) => { - parse_macro(db, macro_call_id).unwrap_or_else(|err| { - // Note: - // The final goal we would like to make all parse_macro success, - // such that the following log will not call anyway. - log::warn!( - "fail on macro_parse: (reason: {}) {}", - err, - macro_call_id.debug_dump(db) - ); + match db.macro_expand(macro_call_id) { + Ok(tt) => mbe::token_tree_to_ast_item_list(&tt), + Err(err) => { + // Note: + // The final goal we would like to make all parse_macro success, + // such that the following log will not call anyway. + log::warn!( + "fail on macro_parse: (reason: {}) {}", + err, + macro_call_id.debug_dump(db) + ); - // returning an empty string looks fishy... - SourceFile::parse("") - }) + // returning an empty string looks fishy... + SourceFile::parse("") + } + } } } } @@ -124,23 +127,21 @@ pub(crate) fn macro_arg_query(db: &impl DefDatabase, id: MacroCallId) -> Option< Some(Arc::new(tt)) } -fn parse_macro( +pub(crate) fn macro_expand_query( db: &impl DefDatabase, - macro_call_id: MacroCallId, -) -> Result, String> { - let loc = macro_call_id.loc(db); - let macro_arg = db.macro_arg(macro_call_id).ok_or("Fail to args in to tt::TokenTree")?; + id: MacroCallId, +) -> Result, String> { + let loc = id.loc(db); + let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?; let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?; let tt = macro_rules.expand(¯o_arg).map_err(|err| format!("{:?}", err))?; - // Set a hard limit for the expanded tt let count = tt.count(); if count > 65536 { return Err(format!("Total tokens count exceed limit : count = {}", count)); } - - Ok(mbe::token_tree_to_ast_item_list(&tt)) + Ok(Arc::new(tt)) } macro_rules! impl_intern_key {