aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ids.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-05-04 16:01:43 +0100
committerAleksey Kladov <[email protected]>2019-05-04 16:01:43 +0100
commitbcf45371ff19882e67300cc483b481450ee129fb (patch)
tree616d3e4dd5fa15228ac2ea85a93d76df0c3b8581 /crates/ra_hir/src/ids.rs
parent87a1e276d56a3cb5f9d9c59f8c52c5573e19982b (diff)
make macro expansion into a proper query
Diffstat (limited to 'crates/ra_hir/src/ids.rs')
-rw-r--r--crates/ra_hir/src/ids.rs43
1 files changed, 22 insertions, 21 deletions
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index cf0e934a9..4102951c9 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -63,19 +63,22 @@ 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(|err| { 66 match db.macro_expand(macro_call_id) {
67 // Note: 67 Ok(tt) => mbe::token_tree_to_ast_item_list(&tt),
68 // The final goal we would like to make all parse_macro success, 68 Err(err) => {
69 // such that the following log will not call anyway. 69 // Note:
70 log::warn!( 70 // The final goal we would like to make all parse_macro success,
71 "fail on macro_parse: (reason: {}) {}", 71 // such that the following log will not call anyway.
72 err, 72 log::warn!(
73 macro_call_id.debug_dump(db) 73 "fail on macro_parse: (reason: {}) {}",
74 ); 74 err,
75 75 macro_call_id.debug_dump(db)
76 // returning an empty string looks fishy... 76 );
77 SourceFile::parse("") 77
78 }) 78 // returning an empty string looks fishy...
79 SourceFile::parse("")
80 }
81 }
79 } 82 }
80 } 83 }
81 } 84 }
@@ -124,23 +127,21 @@ pub(crate) fn macro_arg_query(db: &impl DefDatabase, id: MacroCallId) -> Option<
124 Some(Arc::new(tt)) 127 Some(Arc::new(tt))
125} 128}
126 129
127fn parse_macro( 130pub(crate) fn macro_expand_query(
128 db: &impl DefDatabase, 131 db: &impl DefDatabase,
129 macro_call_id: MacroCallId, 132 id: MacroCallId,
130) -> Result<TreeArc<SourceFile>, String> { 133) -> Result<Arc<tt::Subtree>, String> {
131 let loc = macro_call_id.loc(db); 134 let loc = id.loc(db);
132 let macro_arg = db.macro_arg(macro_call_id).ok_or("Fail to args in to tt::TokenTree")?; 135 let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?;
133 136
134 let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?; 137 let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?;
135 let tt = macro_rules.expand(&macro_arg).map_err(|err| format!("{:?}", err))?; 138 let tt = macro_rules.expand(&macro_arg).map_err(|err| format!("{:?}", err))?;
136
137 // Set a hard limit for the expanded tt 139 // Set a hard limit for the expanded tt
138 let count = tt.count(); 140 let count = tt.count();
139 if count > 65536 { 141 if count > 65536 {
140 return Err(format!("Total tokens count exceed limit : count = {}", count)); 142 return Err(format!("Total tokens count exceed limit : count = {}", count));
141 } 143 }
142 144 Ok(Arc::new(tt))
143 Ok(mbe::token_tree_to_ast_item_list(&tt))
144} 145}
145 146
146macro_rules! impl_intern_key { 147macro_rules! impl_intern_key {