aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-04-21 21:08:36 +0100
committerEdwin Cheng <[email protected]>2019-04-21 22:04:37 +0100
commit9a5b9638c1faf8fb4bbe4fbb0ea80c4386f65a21 (patch)
tree5afcf0a35738b42669b08341fa198c0fb571fdbf /crates/ra_hir
parentda05bbcfb1da752ef6d07eb1f4d9ac6ba187d573 (diff)
Add more information on parse_macro fail
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/ids.rs37
1 files changed, 27 insertions, 10 deletions
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index 2a1ed9b81..e771a311c 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -63,11 +63,15 @@ impl HirFileId {
63 match file_id.0 { 63 match file_id.0 {
64 HirFileIdRepr::File(file_id) => db.parse(file_id), 64 HirFileIdRepr::File(file_id) => db.parse(file_id),
65 HirFileIdRepr::Macro(macro_call_id) => { 65 HirFileIdRepr::Macro(macro_call_id) => {
66 parse_macro(db, macro_call_id).unwrap_or_else(|| { 66 parse_macro(db, macro_call_id).unwrap_or_else(|err| {
67 // Note: 67 // Note:
68 // The final goal we would like to make all parse_macro success, 68 // The final goal we would like to make all parse_macro success,
69 // such that the following log will not call anyway. 69 // such that the following log will not call anyway.
70 log::warn!("fail on macro_parse: {}", macro_call_id.debug_dump(db)); 70 log::warn!(
71 "fail on macro_parse: (reason: {}) {}",
72 err,
73 macro_call_id.debug_dump(db)
74 );
71 75
72 // returning an empty string looks fishy... 76 // returning an empty string looks fishy...
73 SourceFile::parse("") 77 SourceFile::parse("")
@@ -77,14 +81,20 @@ impl HirFileId {
77 } 81 }
78} 82}
79 83
80fn parse_macro(db: &impl DefDatabase, macro_call_id: MacroCallId) -> Option<TreeArc<SourceFile>> { 84fn parse_macro(
85 db: &impl DefDatabase,
86 macro_call_id: MacroCallId,
87) -> Result<TreeArc<SourceFile>, String> {
81 let loc = macro_call_id.loc(db); 88 let loc = macro_call_id.loc(db);
82 let macro_call = loc.ast_id.to_node(db); 89 let macro_call = loc.ast_id.to_node(db);
83 let (macro_arg, _) = macro_call.token_tree().and_then(mbe::ast_to_token_tree)?; 90 let (macro_arg, _) = macro_call
84 91 .token_tree()
85 let macro_rules = db.macro_def(loc.def)?; 92 .and_then(mbe::ast_to_token_tree)
86 let tt = macro_rules.expand(&macro_arg).ok()?; 93 .ok_or("Fail to args in to tt::TokenTree")?;
87 Some(mbe::token_tree_to_ast_item_list(&tt)) 94
95 let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?;
96 let tt = macro_rules.expand(&macro_arg).map_err(|err| format!("{:?}", err))?;
97 Ok(mbe::token_tree_to_ast_item_list(&tt))
88} 98}
89 99
90#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -311,11 +321,18 @@ impl MacroCallId {
311 pub fn debug_dump(&self, db: &impl DefDatabase) -> String { 321 pub fn debug_dump(&self, db: &impl DefDatabase) -> String {
312 let loc = self.clone().loc(db); 322 let loc = self.clone().loc(db);
313 let node = loc.ast_id.to_node(db); 323 let node = loc.ast_id.to_node(db);
314 let syntax_str = node.syntax().to_string(); 324 let syntax_str = node.syntax().text().chunks().collect::<Vec<_>>().join(" ");
315 325
316 // dump the file name 326 // dump the file name
317 let file_id: HirFileId = self.clone().into(); 327 let file_id: HirFileId = self.clone().into();
318 let original = file_id.original_file(db); 328 let original = file_id.original_file(db);
319 format!("macro call [file: {:#?}] : {}", db.file_relative_path(original), syntax_str) 329 let macro_rules = db.macro_def(loc.def);
330
331 format!(
332 "macro call [file: {:#?}] : {}\nhas rules: {}",
333 db.file_relative_path(original),
334 syntax_str,
335 macro_rules.is_some()
336 )
320 } 337 }
321} 338}