Skip to content

Commit 87b7797

Browse files
committed
make impl and trait inactive diagnostics work
1 parent c1eae3d commit 87b7797

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

crates/hir-def/src/data.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ pub struct TraitData {
210210

211211
impl TraitData {
212212
pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> {
213+
db.trait_data_with_diagnostics(tr).0
214+
}
215+
216+
pub(crate) fn trait_data_with_diagnostics_query(db: &dyn DefDatabase, tr: TraitId) -> (Arc<TraitData>, Arc<Vec<DefDiagnostic>>) {
213217
let tr_loc @ ItemLoc { container: module_id, id: tree_id } = tr.lookup(db);
214218
let item_tree = tree_id.item_tree(db);
215219
let tr_def = &item_tree[tree_id.value];
@@ -229,17 +233,20 @@ impl TraitData {
229233
let mut collector =
230234
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::TraitId(tr));
231235
collector.collect(&item_tree, tree_id.tree_id(), &tr_def.items);
232-
let (items, attribute_calls) = collector.finish();
233-
234-
Arc::new(TraitData {
235-
name,
236-
attribute_calls,
237-
items,
238-
is_auto,
239-
is_unsafe,
240-
visibility,
241-
skip_array_during_method_dispatch,
242-
})
236+
let (items, attribute_calls, diagnostics) = collector.finish();
237+
238+
(
239+
Arc::new(TraitData {
240+
name,
241+
attribute_calls,
242+
items,
243+
is_auto,
244+
is_unsafe,
245+
visibility,
246+
skip_array_during_method_dispatch,
247+
}),
248+
Arc::new(diagnostics)
249+
)
243250
}
244251

245252
pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
@@ -280,7 +287,11 @@ pub struct ImplData {
280287

281288
impl ImplData {
282289
pub(crate) fn impl_data_query(db: &dyn DefDatabase, id: ImplId) -> Arc<ImplData> {
283-
let _p = profile::span("impl_data_query");
290+
db.impl_data_with_diagnostics(id).0
291+
}
292+
293+
pub(crate) fn impl_data_with_diagnostics_query(db: &dyn DefDatabase, id: ImplId) -> (Arc<ImplData>, Arc<Vec<DefDiagnostic>>) {
294+
let _p = profile::span("impl_data_with_diagnostics_query");
284295
let ItemLoc { container: module_id, id: tree_id } = id.lookup(db);
285296

286297
let item_tree = tree_id.item_tree(db);
@@ -293,10 +304,10 @@ impl ImplData {
293304
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::ImplId(id));
294305
collector.collect(&item_tree, tree_id.tree_id(), &impl_def.items);
295306

296-
let (items, attribute_calls) = collector.finish();
307+
let (items, attribute_calls, diagnostics) = collector.finish();
297308
let items = items.into_iter().map(|(_, item)| item).collect();
298309

299-
Arc::new(ImplData { target_trait, self_ty, items, is_negative, attribute_calls })
310+
(Arc::new(ImplData { target_trait, self_ty, items, is_negative, attribute_calls }), Arc::new(diagnostics))
300311
}
301312

302313
pub fn attribute_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
@@ -437,6 +448,7 @@ struct AssocItemCollector<'a> {
437448
db: &'a dyn DefDatabase,
438449
module_id: ModuleId,
439450
def_map: Arc<DefMap>,
451+
inactive_diagnostics: Vec<DefDiagnostic>,
440452
container: ItemContainerId,
441453
expander: Expander,
442454

@@ -459,15 +471,17 @@ impl<'a> AssocItemCollector<'a> {
459471
expander: Expander::new(db, file_id, module_id),
460472
items: Vec::new(),
461473
attr_calls: Vec::new(),
474+
inactive_diagnostics: Vec::new(),
462475
}
463476
}
464477

465478
fn finish(
466479
self,
467-
) -> (Vec<(Name, AssocItemId)>, Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>) {
480+
) -> (Vec<(Name, AssocItemId)>, Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>, Vec<DefDiagnostic>) {
468481
(
469482
self.items,
470483
if self.attr_calls.is_empty() { None } else { Some(Box::new(self.attr_calls)) },
484+
self.inactive_diagnostics
471485
)
472486
}
473487

@@ -479,13 +493,12 @@ impl<'a> AssocItemCollector<'a> {
479493
'items: for &item in assoc_items {
480494
let attrs = item_tree.attrs(self.db, self.module_id.krate, ModItem::from(item).into());
481495
if !attrs.is_cfg_enabled(self.expander.cfg_options()) {
482-
self.def_map.push_diagnostic(DefDiagnostic::unconfigured_code(
496+
self.inactive_diagnostics.push(DefDiagnostic::unconfigured_code(
483497
self.module_id.local_id,
484-
InFile::new(tree_id.file_id(), item.ast_id(&item_tree).upcast()),
498+
InFile::new(self.expander.current_file_id(), item.ast_id(&item_tree).upcast()),
485499
attrs.cfg().unwrap(),
486500
self.expander.cfg_options().clone()
487501
));
488-
dbg!("Ignoring assoc item!");
489502
continue;
490503
}
491504

crates/hir-def/src/db.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
intern::Interned,
2121
item_tree::{AttrOwner, ItemTree},
2222
lang_item::{LangItemTarget, LangItems},
23-
nameres::DefMap,
23+
nameres::{DefMap, diagnostics::DefDiagnostic},
2424
visibility::{self, Visibility},
2525
AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, ExternBlockId,
2626
ExternBlockLoc, FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalEnumVariantId,
@@ -106,9 +106,15 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
106106
#[salsa::invoke(ImplData::impl_data_query)]
107107
fn impl_data(&self, e: ImplId) -> Arc<ImplData>;
108108

109+
#[salsa::invoke(ImplData::impl_data_with_diagnostics_query)]
110+
fn impl_data_with_diagnostics(&self, e: ImplId) -> (Arc<ImplData>, Arc<Vec<DefDiagnostic>>);
111+
109112
#[salsa::invoke(TraitData::trait_data_query)]
110113
fn trait_data(&self, e: TraitId) -> Arc<TraitData>;
111114

115+
#[salsa::invoke(TraitData::trait_data_with_diagnostics_query)]
116+
fn trait_data_with_diagnostics(&self, tr: TraitId) -> (Arc<TraitData>, Arc<Vec<DefDiagnostic>>);
117+
112118
#[salsa::invoke(TypeAliasData::type_alias_data_query)]
113119
fn type_alias_data(&self, e: TypeAliasId) -> Arc<TypeAliasData>;
114120

crates/hir/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ impl Module {
511511
.collect()
512512
}
513513

514+
/// Fills `acc` with the module's diagnostics.
514515
pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
515516
let _p = profile::span("Module::diagnostics").detail(|| {
516517
format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string()))
@@ -530,12 +531,22 @@ impl Module {
530531
if def_map[m.id.local_id].origin.is_inline() {
531532
m.diagnostics(db, acc)
532533
}
534+
},
535+
ModuleDef::Trait(t) => {
536+
for diag in db.trait_data_with_diagnostics(t.id).1.iter() {
537+
emit_def_diagnostic(db, acc, diag);
538+
}
539+
acc.extend(decl.diagnostics(db))
533540
}
534541
_ => acc.extend(decl.diagnostics(db)),
535542
}
536543
}
537544

538545
for impl_def in self.impl_defs(db) {
546+
for diag in db.impl_data_with_diagnostics(impl_def.id).1.iter() {
547+
emit_def_diagnostic(db, acc, diag);
548+
}
549+
539550
for item in impl_def.items(db) {
540551
let def: DefWithBody = match item {
541552
AssocItem::Function(it) => it.into(),

crates/ide-diagnostics/src/handlers/inactive_code.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ fn f() {
106106

107107
#[test]
108108
fn inactive_assoc_item() {
109-
// FIXME these currently don't work, hence the *
110109
check(
111110
r#"
112111
struct Foo;

0 commit comments

Comments
 (0)