Skip to content

Commit dea1639

Browse files
committed
Auto merge of rust-lang#12965 - DesmondWillowbrook:assoc-method-dimming, r=Veykril
feat: make trait assoc items become inactive due to cfg fixes rust-lang#12394
2 parents f27f4a9 + 23c00ed commit dea1639

File tree

5 files changed

+75
-22
lines changed

5 files changed

+75
-22
lines changed

Diff for: crates/hir-def/src/data.rs

+53-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::sync::Arc;
44

5-
use hir_expand::{name::Name, AstId, ExpandResult, HirFileId, MacroCallId, MacroDefKind};
5+
use hir_expand::{name::Name, AstId, ExpandResult, HirFileId, InFile, MacroCallId, MacroDefKind};
66
use smallvec::SmallVec;
77
use syntax::ast;
88

@@ -12,7 +12,10 @@ use crate::{
1212
db::DefDatabase,
1313
intern::Interned,
1414
item_tree::{self, AssocItem, FnFlags, ItemTree, ItemTreeId, ModItem, Param, TreeId},
15-
nameres::{attr_resolution::ResolvedAttr, proc_macro::ProcMacroKind, DefMap},
15+
nameres::{
16+
attr_resolution::ResolvedAttr, diagnostics::DefDiagnostic, proc_macro::ProcMacroKind,
17+
DefMap,
18+
},
1619
type_ref::{TraitRef, TypeBound, TypeRef},
1720
visibility::RawVisibility,
1821
AssocItemId, AstIdWithPath, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId,
@@ -210,6 +213,13 @@ pub struct TraitData {
210213

211214
impl TraitData {
212215
pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> {
216+
db.trait_data_with_diagnostics(tr).0
217+
}
218+
219+
pub(crate) fn trait_data_with_diagnostics_query(
220+
db: &dyn DefDatabase,
221+
tr: TraitId,
222+
) -> (Arc<TraitData>, Arc<Vec<DefDiagnostic>>) {
213223
let tr_loc @ ItemLoc { container: module_id, id: tree_id } = tr.lookup(db);
214224
let item_tree = tree_id.item_tree(db);
215225
let tr_def = &item_tree[tree_id.value];
@@ -229,17 +239,20 @@ impl TraitData {
229239
let mut collector =
230240
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::TraitId(tr));
231241
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-
})
242+
let (items, attribute_calls, diagnostics) = collector.finish();
243+
244+
(
245+
Arc::new(TraitData {
246+
name,
247+
attribute_calls,
248+
items,
249+
is_auto,
250+
is_unsafe,
251+
visibility,
252+
skip_array_during_method_dispatch,
253+
}),
254+
Arc::new(diagnostics),
255+
)
243256
}
244257

245258
pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
@@ -280,7 +293,14 @@ pub struct ImplData {
280293

281294
impl ImplData {
282295
pub(crate) fn impl_data_query(db: &dyn DefDatabase, id: ImplId) -> Arc<ImplData> {
283-
let _p = profile::span("impl_data_query");
296+
db.impl_data_with_diagnostics(id).0
297+
}
298+
299+
pub(crate) fn impl_data_with_diagnostics_query(
300+
db: &dyn DefDatabase,
301+
id: ImplId,
302+
) -> (Arc<ImplData>, Arc<Vec<DefDiagnostic>>) {
303+
let _p = profile::span("impl_data_with_diagnostics_query");
284304
let ItemLoc { container: module_id, id: tree_id } = id.lookup(db);
285305

286306
let item_tree = tree_id.item_tree(db);
@@ -293,10 +313,13 @@ impl ImplData {
293313
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::ImplId(id));
294314
collector.collect(&item_tree, tree_id.tree_id(), &impl_def.items);
295315

296-
let (items, attribute_calls) = collector.finish();
316+
let (items, attribute_calls, diagnostics) = collector.finish();
297317
let items = items.into_iter().map(|(_, item)| item).collect();
298318

299-
Arc::new(ImplData { target_trait, self_ty, items, is_negative, attribute_calls })
319+
(
320+
Arc::new(ImplData { target_trait, self_ty, items, is_negative, attribute_calls }),
321+
Arc::new(diagnostics),
322+
)
300323
}
301324

302325
pub fn attribute_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
@@ -437,6 +460,7 @@ struct AssocItemCollector<'a> {
437460
db: &'a dyn DefDatabase,
438461
module_id: ModuleId,
439462
def_map: Arc<DefMap>,
463+
inactive_diagnostics: Vec<DefDiagnostic>,
440464
container: ItemContainerId,
441465
expander: Expander,
442466

@@ -459,15 +483,21 @@ impl<'a> AssocItemCollector<'a> {
459483
expander: Expander::new(db, file_id, module_id),
460484
items: Vec::new(),
461485
attr_calls: Vec::new(),
486+
inactive_diagnostics: Vec::new(),
462487
}
463488
}
464489

465490
fn finish(
466491
self,
467-
) -> (Vec<(Name, AssocItemId)>, Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>) {
492+
) -> (
493+
Vec<(Name, AssocItemId)>,
494+
Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>,
495+
Vec<DefDiagnostic>,
496+
) {
468497
(
469498
self.items,
470499
if self.attr_calls.is_empty() { None } else { Some(Box::new(self.attr_calls)) },
500+
self.inactive_diagnostics,
471501
)
472502
}
473503

@@ -479,6 +509,12 @@ impl<'a> AssocItemCollector<'a> {
479509
'items: for &item in assoc_items {
480510
let attrs = item_tree.attrs(self.db, self.module_id.krate, ModItem::from(item).into());
481511
if !attrs.is_cfg_enabled(self.expander.cfg_options()) {
512+
self.inactive_diagnostics.push(DefDiagnostic::unconfigured_code(
513+
self.module_id.local_id,
514+
InFile::new(self.expander.current_file_id(), item.ast_id(&item_tree).upcast()),
515+
attrs.cfg().unwrap(),
516+
self.expander.cfg_options().clone(),
517+
));
482518
continue;
483519
}
484520

Diff for: crates/hir-def/src/db.rs

+8-1
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::{diagnostics::DefDiagnostic, DefMap},
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,16 @@ 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)
117+
-> (Arc<TraitData>, Arc<Vec<DefDiagnostic>>);
118+
112119
#[salsa::invoke(TypeAliasData::type_alias_data_query)]
113120
fn type_alias_data(&self, e: TypeAliasId) -> Arc<TypeAliasData>;
114121

Diff for: crates/hir-def/src/nameres/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl DefDiagnostic {
7373
Self { in_module: container, kind: DefDiagnosticKind::UnresolvedImport { id, index } }
7474
}
7575

76-
pub(super) fn unconfigured_code(
76+
pub fn unconfigured_code(
7777
container: LocalModuleId,
7878
ast: AstId<ast::Item>,
7979
cfg: CfgExpr,

Diff for: crates/hir/src/lib.rs

+11
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()))
@@ -531,11 +532,21 @@ impl Module {
531532
m.diagnostics(db, acc)
532533
}
533534
}
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))
540+
}
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(),

Diff for: crates/ide-diagnostics/src/handlers/inactive_code.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,17 @@ 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;
113112
impl Foo {
114113
#[cfg(any())] pub fn f() {}
115-
//*************************** weak: code is inactive due to #[cfg] directives
114+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives
116115
}
117116
118117
trait Bar {
119118
#[cfg(any())] pub fn f() {}
120-
//*************************** weak: code is inactive due to #[cfg] directives
119+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives
121120
}
122121
"#,
123122
);

0 commit comments

Comments
 (0)