Skip to content

Commit 8489cd7

Browse files
committed
Auto merge of #12670 - Veykril:assoc-attrs, r=Veykril
fix: Fix attribute macros on assoc items being discarded with disabled proc macros Fixes rust-lang/rust-analyzer#12669
2 parents f8c416e + 8e764a8 commit 8489cd7

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

crates/hir-def/src/data.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@ impl<'a> AssocItemCollector<'a> {
458458
def_map: module_id.def_map(db),
459459
container,
460460
expander: Expander::new(db, file_id, module_id),
461-
462461
items: Vec::new(),
463462
attr_calls: Vec::new(),
464463
}
@@ -473,6 +472,7 @@ impl<'a> AssocItemCollector<'a> {
473472
}
474473
}
475474

475+
// FIXME: proc-macro diagnostics
476476
fn collect(&mut self, tree_id: TreeId, assoc_items: &[AssocItem]) {
477477
let item_tree = tree_id.item_tree(self.db);
478478

@@ -482,7 +482,7 @@ impl<'a> AssocItemCollector<'a> {
482482
continue;
483483
}
484484

485-
for attr in &*attrs {
485+
'attrs: for attr in &*attrs {
486486
let ast_id =
487487
AstId::new(self.expander.current_file_id(), item.ast_id(&item_tree).upcast());
488488
let ast_id_with_path = AstIdWithPath { path: (*attr.path).clone(), ast_id };
@@ -494,9 +494,17 @@ impl<'a> AssocItemCollector<'a> {
494494
attr,
495495
) {
496496
self.attr_calls.push((ast_id, call_id));
497-
let res = self.expander.enter_expand_id(self.db, call_id);
498-
self.collect_macro_items(res);
499-
continue 'items;
497+
// If proc attribute macro expansion is disabled, skip expanding it here
498+
if !self.db.enable_proc_attr_macros() {
499+
continue 'attrs;
500+
}
501+
match self.expander.enter_expand_id(self.db, call_id) {
502+
ExpandResult { value: Some((mark, mac)), .. } => {
503+
self.collect_macro_items(mark, mac);
504+
continue 'items;
505+
}
506+
ExpandResult { .. } => {}
507+
}
500508
}
501509
}
502510

@@ -537,25 +545,23 @@ impl<'a> AssocItemCollector<'a> {
537545
stdx::panic_context::enter(format!("collect_items MacroCall: {}", call));
538546
let res = self.expander.enter_expand(self.db, call);
539547

540-
if let Ok(res) = res {
541-
self.collect_macro_items(res);
548+
if let Ok(ExpandResult { value: Some((mark, mac)), .. }) = res {
549+
self.collect_macro_items(mark, mac);
542550
}
543551
}
544552
}
545553
}
546554
}
547555

548-
fn collect_macro_items(&mut self, res: ExpandResult<Option<(Mark, ast::MacroItems)>>) {
549-
if let Some((mark, mac)) = res.value {
550-
let src: InFile<ast::MacroItems> = self.expander.to_source(mac);
551-
let tree_id = item_tree::TreeId::new(src.file_id, None);
552-
let item_tree = tree_id.item_tree(self.db);
553-
let iter: Vec<_> =
554-
item_tree.top_level_items().iter().filter_map(ModItem::as_assoc_item).collect();
556+
fn collect_macro_items(&mut self, mark: Mark, mac: ast::MacroItems) {
557+
let src: InFile<ast::MacroItems> = self.expander.to_source(mac);
558+
let tree_id = item_tree::TreeId::new(src.file_id, None);
559+
let item_tree = tree_id.item_tree(self.db);
560+
let iter: Vec<_> =
561+
item_tree.top_level_items().iter().filter_map(ModItem::as_assoc_item).collect();
555562

556-
self.collect(tree_id, &iter);
563+
self.collect(tree_id, &iter);
557564

558-
self.expander.exit(self.db, mark);
559-
}
565+
self.expander.exit(self.db, mark);
560566
}
561567
}

0 commit comments

Comments
 (0)