Skip to content

Commit 9f447ad

Browse files
Merge #10865
10865: internal: Use the right `ItemTree` when re-resolving attr r=jonas-schievink a=jonas-schievink Followup to #10863, which caused a panic when analyzing diesel bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 5ea8a9c + c291e5e commit 9f447ad

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

crates/hir_def/src/nameres/collector.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ struct MacroDirective {
219219
enum MacroDirectiveKind {
220220
FnLike { ast_id: AstIdWithPath<ast::MacroCall>, expand_to: ExpandTo },
221221
Derive { ast_id: AstIdWithPath<ast::Item>, derive_attr: AttrId },
222-
Attr { ast_id: AstIdWithPath<ast::Item>, attr: Attr, mod_item: ModItem },
222+
Attr { ast_id: AstIdWithPath<ast::Item>, attr: Attr, mod_item: ModItem, tree: TreeId },
223223
}
224224

225225
/// Walks the tree of module recursively
@@ -420,17 +420,16 @@ impl DefCollector<'_> {
420420

421421
let mut unresolved_macros = std::mem::take(&mut self.unresolved_macros);
422422
let pos = unresolved_macros.iter().position(|directive| {
423-
if let MacroDirectiveKind::Attr { ast_id, mod_item, attr } = &directive.kind {
423+
if let MacroDirectiveKind::Attr { ast_id, mod_item, attr, tree } = &directive.kind {
424424
self.skip_attrs.insert(ast_id.ast_id.with_value(*mod_item), attr.id);
425425

426-
let file_id = ast_id.ast_id.file_id;
427-
let item_tree = self.db.file_item_tree(file_id);
426+
let item_tree = tree.item_tree(self.db);
428427
let mod_dir = self.mod_dirs[&directive.module_id].clone();
429428
ModCollector {
430429
def_collector: self,
431430
macro_depth: directive.depth,
432431
module_id: directive.module_id,
433-
tree_id: TreeId::new(file_id, None),
432+
tree_id: *tree,
434433
item_tree: &item_tree,
435434
mod_dir,
436435
}
@@ -1076,20 +1075,22 @@ impl DefCollector<'_> {
10761075
return false;
10771076
}
10781077
}
1079-
MacroDirectiveKind::Attr { ast_id: file_ast_id, mod_item, attr } => {
1078+
MacroDirectiveKind::Attr { ast_id: file_ast_id, mod_item, attr, tree } => {
10801079
let &AstIdWithPath { ast_id, ref path } = file_ast_id;
10811080
let file_id = ast_id.file_id;
10821081

1083-
let mut recollect_without = |collector: &mut Self, item_tree| {
1082+
let mut recollect_without = |collector: &mut Self| {
10841083
// Remove the original directive since we resolved it.
10851084
let mod_dir = collector.mod_dirs[&directive.module_id].clone();
10861085
collector.skip_attrs.insert(InFile::new(file_id, *mod_item), attr.id);
1086+
1087+
let item_tree = tree.item_tree(self.db);
10871088
ModCollector {
10881089
def_collector: collector,
10891090
macro_depth: directive.depth,
10901091
module_id: directive.module_id,
1091-
tree_id: TreeId::new(file_id, None),
1092-
item_tree,
1092+
tree_id: *tree,
1093+
item_tree: &item_tree,
10931094
mod_dir,
10941095
}
10951096
.collect(&[*mod_item]);
@@ -1103,8 +1104,7 @@ impl DefCollector<'_> {
11031104
cov_mark::hit!(resolved_derive_helper);
11041105
// Resolved to derive helper. Collect the item's attributes again,
11051106
// starting after the derive helper.
1106-
let item_tree = self.db.file_item_tree(file_id);
1107-
return recollect_without(self, &item_tree);
1107+
return recollect_without(self);
11081108
}
11091109
}
11101110
}
@@ -1116,7 +1116,6 @@ impl DefCollector<'_> {
11161116
if expander.is_derive()
11171117
) {
11181118
// Resolved to `#[derive]`
1119-
let item_tree = self.db.file_item_tree(file_id);
11201119

11211120
match mod_item {
11221121
ModItem::Struct(_) | ModItem::Union(_) | ModItem::Enum(_) => (),
@@ -1127,7 +1126,7 @@ impl DefCollector<'_> {
11271126
attr.id,
11281127
);
11291128
self.def_map.diagnostics.push(diag);
1130-
return recollect_without(self, &item_tree);
1129+
return recollect_without(self);
11311130
}
11321131
}
11331132

@@ -1155,7 +1154,7 @@ impl DefCollector<'_> {
11551154
}
11561155
}
11571156

1158-
return recollect_without(self, &item_tree);
1157+
return recollect_without(self);
11591158
}
11601159

11611160
if !self.db.enable_proc_attr_macros() {
@@ -1175,8 +1174,7 @@ impl DefCollector<'_> {
11751174
MacroDefKind::BuiltInAttr(expander, _)
11761175
if expander.is_test() || expander.is_bench()
11771176
) {
1178-
let item_tree = self.db.file_item_tree(file_id);
1179-
return recollect_without(self, &item_tree);
1177+
return recollect_without(self);
11801178
}
11811179

11821180
if let MacroDefKind::ProcMacro(exp, ..) = loc.def.kind {
@@ -1190,8 +1188,7 @@ impl DefCollector<'_> {
11901188
),
11911189
);
11921190

1193-
let item_tree = self.db.file_item_tree(file_id);
1194-
return recollect_without(self, &item_tree);
1191+
return recollect_without(self);
11951192
}
11961193
}
11971194

@@ -1771,7 +1768,12 @@ impl ModCollector<'_, '_> {
17711768
self.def_collector.unresolved_macros.push(MacroDirective {
17721769
module_id: self.module_id,
17731770
depth: self.macro_depth + 1,
1774-
kind: MacroDirectiveKind::Attr { ast_id, attr: attr.clone(), mod_item },
1771+
kind: MacroDirectiveKind::Attr {
1772+
ast_id,
1773+
attr: attr.clone(),
1774+
mod_item,
1775+
tree: self.tree_id,
1776+
},
17751777
});
17761778

17771779
return Err(());

0 commit comments

Comments
 (0)