fix: Fix attribute macros on assoc items being discarded with disabled proc macros

This commit is contained in:
Lukas Wirth 2022-07-01 16:21:21 +02:00
parent f8c416e1b9
commit 8e764a8bb1

View file

@ -458,7 +458,6 @@ impl<'a> AssocItemCollector<'a> {
def_map: module_id.def_map(db), def_map: module_id.def_map(db),
container, container,
expander: Expander::new(db, file_id, module_id), expander: Expander::new(db, file_id, module_id),
items: Vec::new(), items: Vec::new(),
attr_calls: Vec::new(), attr_calls: Vec::new(),
} }
@ -473,6 +472,7 @@ impl<'a> AssocItemCollector<'a> {
} }
} }
// FIXME: proc-macro diagnostics
fn collect(&mut self, tree_id: TreeId, assoc_items: &[AssocItem]) { fn collect(&mut self, tree_id: TreeId, assoc_items: &[AssocItem]) {
let item_tree = tree_id.item_tree(self.db); let item_tree = tree_id.item_tree(self.db);
@ -482,7 +482,7 @@ impl<'a> AssocItemCollector<'a> {
continue; continue;
} }
for attr in &*attrs { 'attrs: for attr in &*attrs {
let ast_id = let ast_id =
AstId::new(self.expander.current_file_id(), item.ast_id(&item_tree).upcast()); AstId::new(self.expander.current_file_id(), item.ast_id(&item_tree).upcast());
let ast_id_with_path = AstIdWithPath { path: (*attr.path).clone(), ast_id }; let ast_id_with_path = AstIdWithPath { path: (*attr.path).clone(), ast_id };
@ -494,10 +494,18 @@ impl<'a> AssocItemCollector<'a> {
attr, attr,
) { ) {
self.attr_calls.push((ast_id, call_id)); self.attr_calls.push((ast_id, call_id));
let res = self.expander.enter_expand_id(self.db, call_id); // If proc attribute macro expansion is disabled, skip expanding it here
self.collect_macro_items(res); if !self.db.enable_proc_attr_macros() {
continue 'attrs;
}
match self.expander.enter_expand_id(self.db, call_id) {
ExpandResult { value: Some((mark, mac)), .. } => {
self.collect_macro_items(mark, mac);
continue 'items; continue 'items;
} }
ExpandResult { .. } => {}
}
}
} }
match item { match item {
@ -537,16 +545,15 @@ impl<'a> AssocItemCollector<'a> {
stdx::panic_context::enter(format!("collect_items MacroCall: {}", call)); stdx::panic_context::enter(format!("collect_items MacroCall: {}", call));
let res = self.expander.enter_expand(self.db, call); let res = self.expander.enter_expand(self.db, call);
if let Ok(res) = res { if let Ok(ExpandResult { value: Some((mark, mac)), .. }) = res {
self.collect_macro_items(res); self.collect_macro_items(mark, mac);
} }
} }
} }
} }
} }
fn collect_macro_items(&mut self, res: ExpandResult<Option<(Mark, ast::MacroItems)>>) { fn collect_macro_items(&mut self, mark: Mark, mac: ast::MacroItems) {
if let Some((mark, mac)) = res.value {
let src: InFile<ast::MacroItems> = self.expander.to_source(mac); let src: InFile<ast::MacroItems> = self.expander.to_source(mac);
let tree_id = item_tree::TreeId::new(src.file_id, None); let tree_id = item_tree::TreeId::new(src.file_id, None);
let item_tree = tree_id.item_tree(self.db); let item_tree = tree_id.item_tree(self.db);
@ -558,4 +565,3 @@ impl<'a> AssocItemCollector<'a> {
self.expander.exit(self.db, mark); self.expander.exit(self.db, mark);
} }
} }
}