When descending tokens don't bail on failed macro call expansions

This commit is contained in:
Lukas Wirth 2021-09-02 19:10:40 +02:00
parent 2aee17e556
commit 0fee14bfdd

View File

@ -467,65 +467,73 @@ impl<'db> SemanticsImpl<'db> {
let mut queue = vec![InFile::new(sa.file_id, token)]; let mut queue = vec![InFile::new(sa.file_id, token)];
let mut cache = self.expansion_info_cache.borrow_mut(); let mut cache = self.expansion_info_cache.borrow_mut();
let mut res = smallvec![]; let mut res = smallvec![];
// Remap the next token in the queue into a macro call its in, if it is not being remapped
// either due to not being in a macro-call or because its unused push it into the result vec,
// otherwise push the remapped tokens back into the queue as they can potentially be remapped again.
while let Some(token) = queue.pop() { while let Some(token) = queue.pop() {
self.db.unwind_if_cancelled(); self.db.unwind_if_cancelled();
let was_not_remapped = (|| { let was_not_remapped = (|| {
for node in token.value.ancestors() { for node in token.value.ancestors() {
match_ast! { if let Some(macro_call) = ast::MacroCall::cast(node.clone()) {
match node { let tt = match macro_call.token_tree() {
ast::MacroCall(macro_call) => { Some(tt) => tt,
let tt = macro_call.token_tree()?; None => continue,
let l_delim = match tt.left_delimiter_token() { };
Some(it) => it.text_range().end(), let l_delim = match tt.left_delimiter_token() {
None => tt.syntax().text_range().start() Some(it) => it.text_range().end(),
}; None => tt.syntax().text_range().start(),
let r_delim = match tt.right_delimiter_token() { };
Some(it) => it.text_range().start(), let r_delim = match tt.right_delimiter_token() {
None => tt.syntax().text_range().end() Some(it) => it.text_range().start(),
}; None => tt.syntax().text_range().end(),
if !TextRange::new(l_delim, r_delim).contains_range(token.value.text_range()) { };
return None; if !TextRange::new(l_delim, r_delim)
} .contains_range(token.value.text_range())
let file_id = sa.expand(self.db, token.with_value(&macro_call))?; {
let tokens = cache continue;
.entry(file_id) }
.or_insert_with(|| file_id.expansion_info(self.db.upcast())) let file_id = match sa.expand(self.db, token.with_value(&macro_call)) {
.as_ref()? Some(file_id) => file_id,
.map_token_down(self.db.upcast(), None, token.as_ref())?; None => continue,
};
let tokens = cache
.entry(file_id)
.or_insert_with(|| file_id.expansion_info(self.db.upcast()))
.as_ref()?
.map_token_down(self.db.upcast(), None, token.as_ref())?;
let len = queue.len(); let len = queue.len();
queue.extend(tokens.inspect(|token| { queue.extend(tokens.inspect(|token| {
if let Some(parent) = token.value.parent() { if let Some(parent) = token.value.parent() {
self.cache(find_root(&parent), token.file_id); self.cache(find_root(&parent), token.file_id);
} }
})); }));
return (queue.len() != len).then(|| ()); return (queue.len() != len).then(|| ());
}, } else if let Some(item) = ast::Item::cast(node.clone()) {
ast::Item(item) => { if let Some(call_id) = self
if let Some(call_id) = self.with_ctx(|ctx| ctx.item_to_macro_call(token.with_value(item.clone()))) { .with_ctx(|ctx| ctx.item_to_macro_call(token.with_value(item.clone())))
let file_id = call_id.as_file(); {
let tokens = cache let file_id = call_id.as_file();
.entry(file_id) let tokens = cache
.or_insert_with(|| file_id.expansion_info(self.db.upcast())) .entry(file_id)
.as_ref()? .or_insert_with(|| file_id.expansion_info(self.db.upcast()))
.map_token_down(self.db.upcast(), Some(item), token.as_ref())?; .as_ref()?
.map_token_down(self.db.upcast(), Some(item), token.as_ref())?;
let len = queue.len(); let len = queue.len();
queue.extend(tokens.inspect(|token| { queue.extend(tokens.inspect(|token| {
if let Some(parent) = token.value.parent() { if let Some(parent) = token.value.parent() {
self.cache(find_root(&parent), token.file_id); self.cache(find_root(&parent), token.file_id);
}
}));
return (queue.len() != len).then(|| ());
} }
}, }));
_ => {} return (queue.len() != len).then(|| ());
} }
} }
} }
None None
})().is_none(); })()
.is_none();
if was_not_remapped { if was_not_remapped {
res.push(token.value) res.push(token.value)
} }