mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-06 12:18:33 +00:00
Auto merge of #12965 - DesmondWillowbrook:assoc-method-dimming, r=Veykril
feat: make trait assoc items become inactive due to cfg fixes #12394
This commit is contained in:
commit
dea163970a
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use hir_expand::{name::Name, AstId, ExpandResult, HirFileId, MacroCallId, MacroDefKind};
|
use hir_expand::{name::Name, AstId, ExpandResult, HirFileId, InFile, MacroCallId, MacroDefKind};
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
|
|
||||||
@ -12,7 +12,10 @@ use crate::{
|
|||||||
db::DefDatabase,
|
db::DefDatabase,
|
||||||
intern::Interned,
|
intern::Interned,
|
||||||
item_tree::{self, AssocItem, FnFlags, ItemTree, ItemTreeId, ModItem, Param, TreeId},
|
item_tree::{self, AssocItem, FnFlags, ItemTree, ItemTreeId, ModItem, Param, TreeId},
|
||||||
nameres::{attr_resolution::ResolvedAttr, proc_macro::ProcMacroKind, DefMap},
|
nameres::{
|
||||||
|
attr_resolution::ResolvedAttr, diagnostics::DefDiagnostic, proc_macro::ProcMacroKind,
|
||||||
|
DefMap,
|
||||||
|
},
|
||||||
type_ref::{TraitRef, TypeBound, TypeRef},
|
type_ref::{TraitRef, TypeBound, TypeRef},
|
||||||
visibility::RawVisibility,
|
visibility::RawVisibility,
|
||||||
AssocItemId, AstIdWithPath, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId,
|
AssocItemId, AstIdWithPath, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId,
|
||||||
@ -210,6 +213,13 @@ pub struct TraitData {
|
|||||||
|
|
||||||
impl TraitData {
|
impl TraitData {
|
||||||
pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> {
|
pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> {
|
||||||
|
db.trait_data_with_diagnostics(tr).0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn trait_data_with_diagnostics_query(
|
||||||
|
db: &dyn DefDatabase,
|
||||||
|
tr: TraitId,
|
||||||
|
) -> (Arc<TraitData>, Arc<Vec<DefDiagnostic>>) {
|
||||||
let tr_loc @ ItemLoc { container: module_id, id: tree_id } = tr.lookup(db);
|
let tr_loc @ ItemLoc { container: module_id, id: tree_id } = tr.lookup(db);
|
||||||
let item_tree = tree_id.item_tree(db);
|
let item_tree = tree_id.item_tree(db);
|
||||||
let tr_def = &item_tree[tree_id.value];
|
let tr_def = &item_tree[tree_id.value];
|
||||||
@ -229,17 +239,20 @@ impl TraitData {
|
|||||||
let mut collector =
|
let mut collector =
|
||||||
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::TraitId(tr));
|
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::TraitId(tr));
|
||||||
collector.collect(&item_tree, tree_id.tree_id(), &tr_def.items);
|
collector.collect(&item_tree, tree_id.tree_id(), &tr_def.items);
|
||||||
let (items, attribute_calls) = collector.finish();
|
let (items, attribute_calls, diagnostics) = collector.finish();
|
||||||
|
|
||||||
Arc::new(TraitData {
|
(
|
||||||
name,
|
Arc::new(TraitData {
|
||||||
attribute_calls,
|
name,
|
||||||
items,
|
attribute_calls,
|
||||||
is_auto,
|
items,
|
||||||
is_unsafe,
|
is_auto,
|
||||||
visibility,
|
is_unsafe,
|
||||||
skip_array_during_method_dispatch,
|
visibility,
|
||||||
})
|
skip_array_during_method_dispatch,
|
||||||
|
}),
|
||||||
|
Arc::new(diagnostics),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
|
pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
|
||||||
@ -280,7 +293,14 @@ pub struct ImplData {
|
|||||||
|
|
||||||
impl ImplData {
|
impl ImplData {
|
||||||
pub(crate) fn impl_data_query(db: &dyn DefDatabase, id: ImplId) -> Arc<ImplData> {
|
pub(crate) fn impl_data_query(db: &dyn DefDatabase, id: ImplId) -> Arc<ImplData> {
|
||||||
let _p = profile::span("impl_data_query");
|
db.impl_data_with_diagnostics(id).0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn impl_data_with_diagnostics_query(
|
||||||
|
db: &dyn DefDatabase,
|
||||||
|
id: ImplId,
|
||||||
|
) -> (Arc<ImplData>, Arc<Vec<DefDiagnostic>>) {
|
||||||
|
let _p = profile::span("impl_data_with_diagnostics_query");
|
||||||
let ItemLoc { container: module_id, id: tree_id } = id.lookup(db);
|
let ItemLoc { container: module_id, id: tree_id } = id.lookup(db);
|
||||||
|
|
||||||
let item_tree = tree_id.item_tree(db);
|
let item_tree = tree_id.item_tree(db);
|
||||||
@ -293,10 +313,13 @@ impl ImplData {
|
|||||||
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::ImplId(id));
|
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::ImplId(id));
|
||||||
collector.collect(&item_tree, tree_id.tree_id(), &impl_def.items);
|
collector.collect(&item_tree, tree_id.tree_id(), &impl_def.items);
|
||||||
|
|
||||||
let (items, attribute_calls) = collector.finish();
|
let (items, attribute_calls, diagnostics) = collector.finish();
|
||||||
let items = items.into_iter().map(|(_, item)| item).collect();
|
let items = items.into_iter().map(|(_, item)| item).collect();
|
||||||
|
|
||||||
Arc::new(ImplData { target_trait, self_ty, items, is_negative, attribute_calls })
|
(
|
||||||
|
Arc::new(ImplData { target_trait, self_ty, items, is_negative, attribute_calls }),
|
||||||
|
Arc::new(diagnostics),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn attribute_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
|
pub fn attribute_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
|
||||||
@ -437,6 +460,7 @@ struct AssocItemCollector<'a> {
|
|||||||
db: &'a dyn DefDatabase,
|
db: &'a dyn DefDatabase,
|
||||||
module_id: ModuleId,
|
module_id: ModuleId,
|
||||||
def_map: Arc<DefMap>,
|
def_map: Arc<DefMap>,
|
||||||
|
inactive_diagnostics: Vec<DefDiagnostic>,
|
||||||
container: ItemContainerId,
|
container: ItemContainerId,
|
||||||
expander: Expander,
|
expander: Expander,
|
||||||
|
|
||||||
@ -459,15 +483,21 @@ impl<'a> AssocItemCollector<'a> {
|
|||||||
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(),
|
||||||
|
inactive_diagnostics: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finish(
|
fn finish(
|
||||||
self,
|
self,
|
||||||
) -> (Vec<(Name, AssocItemId)>, Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>) {
|
) -> (
|
||||||
|
Vec<(Name, AssocItemId)>,
|
||||||
|
Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>,
|
||||||
|
Vec<DefDiagnostic>,
|
||||||
|
) {
|
||||||
(
|
(
|
||||||
self.items,
|
self.items,
|
||||||
if self.attr_calls.is_empty() { None } else { Some(Box::new(self.attr_calls)) },
|
if self.attr_calls.is_empty() { None } else { Some(Box::new(self.attr_calls)) },
|
||||||
|
self.inactive_diagnostics,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -479,6 +509,12 @@ impl<'a> AssocItemCollector<'a> {
|
|||||||
'items: for &item in assoc_items {
|
'items: for &item in assoc_items {
|
||||||
let attrs = item_tree.attrs(self.db, self.module_id.krate, ModItem::from(item).into());
|
let attrs = item_tree.attrs(self.db, self.module_id.krate, ModItem::from(item).into());
|
||||||
if !attrs.is_cfg_enabled(self.expander.cfg_options()) {
|
if !attrs.is_cfg_enabled(self.expander.cfg_options()) {
|
||||||
|
self.inactive_diagnostics.push(DefDiagnostic::unconfigured_code(
|
||||||
|
self.module_id.local_id,
|
||||||
|
InFile::new(self.expander.current_file_id(), item.ast_id(&item_tree).upcast()),
|
||||||
|
attrs.cfg().unwrap(),
|
||||||
|
self.expander.cfg_options().clone(),
|
||||||
|
));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ use crate::{
|
|||||||
intern::Interned,
|
intern::Interned,
|
||||||
item_tree::{AttrOwner, ItemTree},
|
item_tree::{AttrOwner, ItemTree},
|
||||||
lang_item::{LangItemTarget, LangItems},
|
lang_item::{LangItemTarget, LangItems},
|
||||||
nameres::DefMap,
|
nameres::{diagnostics::DefDiagnostic, DefMap},
|
||||||
visibility::{self, Visibility},
|
visibility::{self, Visibility},
|
||||||
AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, ExternBlockId,
|
AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, ExternBlockId,
|
||||||
ExternBlockLoc, FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalEnumVariantId,
|
ExternBlockLoc, FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalEnumVariantId,
|
||||||
@ -106,9 +106,16 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
|
|||||||
#[salsa::invoke(ImplData::impl_data_query)]
|
#[salsa::invoke(ImplData::impl_data_query)]
|
||||||
fn impl_data(&self, e: ImplId) -> Arc<ImplData>;
|
fn impl_data(&self, e: ImplId) -> Arc<ImplData>;
|
||||||
|
|
||||||
|
#[salsa::invoke(ImplData::impl_data_with_diagnostics_query)]
|
||||||
|
fn impl_data_with_diagnostics(&self, e: ImplId) -> (Arc<ImplData>, Arc<Vec<DefDiagnostic>>);
|
||||||
|
|
||||||
#[salsa::invoke(TraitData::trait_data_query)]
|
#[salsa::invoke(TraitData::trait_data_query)]
|
||||||
fn trait_data(&self, e: TraitId) -> Arc<TraitData>;
|
fn trait_data(&self, e: TraitId) -> Arc<TraitData>;
|
||||||
|
|
||||||
|
#[salsa::invoke(TraitData::trait_data_with_diagnostics_query)]
|
||||||
|
fn trait_data_with_diagnostics(&self, tr: TraitId)
|
||||||
|
-> (Arc<TraitData>, Arc<Vec<DefDiagnostic>>);
|
||||||
|
|
||||||
#[salsa::invoke(TypeAliasData::type_alias_data_query)]
|
#[salsa::invoke(TypeAliasData::type_alias_data_query)]
|
||||||
fn type_alias_data(&self, e: TypeAliasId) -> Arc<TypeAliasData>;
|
fn type_alias_data(&self, e: TypeAliasId) -> Arc<TypeAliasData>;
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ impl DefDiagnostic {
|
|||||||
Self { in_module: container, kind: DefDiagnosticKind::UnresolvedImport { id, index } }
|
Self { in_module: container, kind: DefDiagnosticKind::UnresolvedImport { id, index } }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn unconfigured_code(
|
pub fn unconfigured_code(
|
||||||
container: LocalModuleId,
|
container: LocalModuleId,
|
||||||
ast: AstId<ast::Item>,
|
ast: AstId<ast::Item>,
|
||||||
cfg: CfgExpr,
|
cfg: CfgExpr,
|
||||||
|
@ -511,6 +511,7 @@ impl Module {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fills `acc` with the module's diagnostics.
|
||||||
pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
|
pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
|
||||||
let _p = profile::span("Module::diagnostics").detail(|| {
|
let _p = profile::span("Module::diagnostics").detail(|| {
|
||||||
format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string()))
|
format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string()))
|
||||||
@ -531,11 +532,21 @@ impl Module {
|
|||||||
m.diagnostics(db, acc)
|
m.diagnostics(db, acc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ModuleDef::Trait(t) => {
|
||||||
|
for diag in db.trait_data_with_diagnostics(t.id).1.iter() {
|
||||||
|
emit_def_diagnostic(db, acc, diag);
|
||||||
|
}
|
||||||
|
acc.extend(decl.diagnostics(db))
|
||||||
|
}
|
||||||
_ => acc.extend(decl.diagnostics(db)),
|
_ => acc.extend(decl.diagnostics(db)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for impl_def in self.impl_defs(db) {
|
for impl_def in self.impl_defs(db) {
|
||||||
|
for diag in db.impl_data_with_diagnostics(impl_def.id).1.iter() {
|
||||||
|
emit_def_diagnostic(db, acc, diag);
|
||||||
|
}
|
||||||
|
|
||||||
for item in impl_def.items(db) {
|
for item in impl_def.items(db) {
|
||||||
let def: DefWithBody = match item {
|
let def: DefWithBody = match item {
|
||||||
AssocItem::Function(it) => it.into(),
|
AssocItem::Function(it) => it.into(),
|
||||||
|
@ -106,18 +106,17 @@ fn f() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn inactive_assoc_item() {
|
fn inactive_assoc_item() {
|
||||||
// FIXME these currently don't work, hence the *
|
|
||||||
check(
|
check(
|
||||||
r#"
|
r#"
|
||||||
struct Foo;
|
struct Foo;
|
||||||
impl Foo {
|
impl Foo {
|
||||||
#[cfg(any())] pub fn f() {}
|
#[cfg(any())] pub fn f() {}
|
||||||
//*************************** weak: code is inactive due to #[cfg] directives
|
//^^^^^^^^^^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Bar {
|
trait Bar {
|
||||||
#[cfg(any())] pub fn f() {}
|
#[cfg(any())] pub fn f() {}
|
||||||
//*************************** weak: code is inactive due to #[cfg] directives
|
//^^^^^^^^^^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user