6630: followup to "hir_expand: propagate expansion errors" r=jonas-schievink a=jonas-schievink

https://github.com/rust-analyzer/rust-analyzer/pull/6625

bors r+ 🤖

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
bors[bot] 2020-11-24 21:01:33 +00:00 committed by GitHub
commit 5478aaebfe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 29 deletions

View file

@ -11,7 +11,7 @@ pub use hir_def::db::{
}; };
pub use hir_expand::db::{ pub use hir_expand::db::{
AstDatabase, AstDatabaseStorage, AstIdMapQuery, InternEagerExpansionQuery, InternMacroQuery, AstDatabase, AstDatabaseStorage, AstIdMapQuery, InternEagerExpansionQuery, InternMacroQuery,
MacroArgTextQuery, MacroDefQuery, MacroExpandQuery, ParseMacroQuery, MacroArgTextQuery, MacroDefQuery, MacroExpandQuery, ParseMacroExpansionQuery,
}; };
pub use hir_ty::db::*; pub use hir_ty::db::*;

View file

@ -88,7 +88,7 @@ pub trait AstDatabase: SourceDatabase {
#[salsa::transparent] #[salsa::transparent]
fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>; fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>; fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>;
fn parse_macro( fn parse_macro_expansion(
&self, &self,
macro_file: MacroFile, macro_file: MacroFile,
) -> MacroResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>; ) -> MacroResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>;
@ -138,16 +138,13 @@ pub fn expand_hypothetical(
Some((node.syntax_node(), token)) Some((node.syntax_node(), token))
} }
pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> { fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
let map = let map =
db.parse_or_expand(file_id).map_or_else(AstIdMap::default, |it| AstIdMap::from_source(&it)); db.parse_or_expand(file_id).map_or_else(AstIdMap::default, |it| AstIdMap::from_source(&it));
Arc::new(map) Arc::new(map)
} }
pub(crate) fn macro_def( fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
db: &dyn AstDatabase,
id: MacroDefId,
) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
match id.kind { match id.kind {
MacroDefKind::Declarative => { MacroDefKind::Declarative => {
let macro_call = id.ast_id?.to_node(db); 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<GreenNode> { fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
let id = match id { let id = match id {
MacroCallId::LazyMacro(id) => id, MacroCallId::LazyMacro(id) => id,
MacroCallId::EagerMacro(_id) => { MacroCallId::EagerMacro(_id) => {
@ -191,16 +188,13 @@ pub(crate) fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<Gr
Some(arg.green().clone()) Some(arg.green().clone())
} }
pub(crate) fn macro_arg( fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
db: &dyn AstDatabase,
id: MacroCallId,
) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
let arg = db.macro_arg_text(id)?; let arg = db.macro_arg_text(id)?;
let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg))?; let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg))?;
Some(Arc::new((tt, tmap))) Some(Arc::new((tt, tmap)))
} }
pub(crate) fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> MacroResult<Arc<tt::Subtree>> { fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> MacroResult<Arc<tt::Subtree>> {
macro_expand_with_arg(db, id, None) 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)) } 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, db: &dyn AstDatabase,
id: MacroCallId, id: MacroCallId,
) -> Result<tt::Subtree, mbe::ExpandError> { ) -> Result<tt::Subtree, mbe::ExpandError> {
@ -285,23 +279,23 @@ pub(crate) fn expand_proc_macro(
expander.expand(db, lazy_id, &macro_arg.0) expander.expand(db, lazy_id, &macro_arg.0)
} }
pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> { fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> {
match file_id.0 { match file_id.0 {
HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()), HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()),
HirFileIdRepr::MacroFile(macro_file) => { 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
} }
} }
} }
pub(crate) fn parse_macro( fn parse_macro_expansion(
db: &dyn AstDatabase, db: &dyn AstDatabase,
macro_file: MacroFile, macro_file: MacroFile,
) -> MacroResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)> { ) -> MacroResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)> {
parse_macro_with_arg(db, macro_file, None) parse_macro_with_arg(db, macro_file, None)
} }
pub fn parse_macro_with_arg( fn parse_macro_with_arg(
db: &dyn AstDatabase, db: &dyn AstDatabase,
macro_file: MacroFile, macro_file: MacroFile,
arg: Option<Arc<(tt::Subtree, mbe::TokenMap)>>, arg: Option<Arc<(tt::Subtree, mbe::TokenMap)>>,
@ -359,11 +353,7 @@ pub fn parse_macro_with_arg(
match result.error { match result.error {
Some(error) => { Some(error) => {
// FIXME: // Safety check for recursive identity macro.
// In future, we should propagate the actual error with recovery information
// instead of ignore the error here.
// Safe check for recurisve identity macro
let node = parse.syntax_node(); let node = parse.syntax_node();
let file: HirFileId = macro_file.into(); let file: HirFileId = macro_file.into();
let call_node = match file.call_node(db) { let call_node = match file.call_node(db) {
@ -374,7 +364,7 @@ pub fn parse_macro_with_arg(
}; };
if !diff(&node, &call_node.value).is_empty() { 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 { } else {
return MacroResult::error(error); return MacroResult::error(error);
} }

View file

@ -144,7 +144,7 @@ impl HirFileId {
let def_tt = loc.def.ast_id?.to_node(db).token_tree()?; let def_tt = loc.def.ast_id?.to_node(db).token_tree()?;
let macro_def = db.macro_def(loc.def)?; 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)?; let macro_arg = db.macro_arg(macro_file.macro_call_id)?;
Some(ExpansionInfo { Some(ExpansionInfo {

View file

@ -19,7 +19,7 @@ fn syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
ide_db::base_db::ParseQuery.in_db(db).entries::<SyntaxTreeStats>() ide_db::base_db::ParseQuery.in_db(db).entries::<SyntaxTreeStats>()
} }
fn macro_syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats { fn macro_syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
hir::db::ParseMacroQuery.in_db(db).entries::<SyntaxTreeStats>() hir::db::ParseMacroExpansionQuery.in_db(db).entries::<SyntaxTreeStats>()
} }
// Feature: Status // Feature: Status

View file

@ -76,7 +76,7 @@ impl RootDatabase {
let sweep = SweepStrategy::default().discard_values().sweep_all_revisions(); let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
base_db::ParseQuery.in_db(self).sweep(sweep); 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 // Macros do take significant space, but less then the syntax trees
// self.query(hir::db::MacroDefQuery).sweep(sweep); // self.query(hir::db::MacroDefQuery).sweep(sweep);
@ -143,7 +143,7 @@ impl RootDatabase {
hir::db::AstIdMapQuery hir::db::AstIdMapQuery
hir::db::MacroArgTextQuery hir::db::MacroArgTextQuery
hir::db::MacroDefQuery hir::db::MacroDefQuery
hir::db::ParseMacroQuery hir::db::ParseMacroExpansionQuery
hir::db::MacroExpandQuery hir::db::MacroExpandQuery
// DefDatabase // DefDatabase

View file

@ -113,7 +113,7 @@ impl RootDatabase {
pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) { pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
let lru_capacity = lru_capacity.unwrap_or(base_db::DEFAULT_LRU_CAP); let lru_capacity = lru_capacity.unwrap_or(base_db::DEFAULT_LRU_CAP);
base_db::ParseQuery.in_db_mut(self).set_lru_capacity(lru_capacity); 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); hir::db::MacroExpandQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
} }
} }