mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-29 10:13:54 +00:00
Rollup merge of #108486 - cjgillot:owner-ditem, r=Nilstrieb
Merge diagnostic_items duplicate diagnostics To deduplicate how we diagnose duplication.
This commit is contained in:
commit
abc82484d4
@ -64,13 +64,17 @@ impl ModuleItems {
|
|||||||
self.foreign_items.iter().copied()
|
self.foreign_items.iter().copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn definitions(&self) -> impl Iterator<Item = LocalDefId> + '_ {
|
pub fn owners(&self) -> impl Iterator<Item = OwnerId> + '_ {
|
||||||
self.items
|
self.items
|
||||||
.iter()
|
.iter()
|
||||||
.map(|id| id.owner_id.def_id)
|
.map(|id| id.owner_id)
|
||||||
.chain(self.trait_items.iter().map(|id| id.owner_id.def_id))
|
.chain(self.trait_items.iter().map(|id| id.owner_id))
|
||||||
.chain(self.impl_items.iter().map(|id| id.owner_id.def_id))
|
.chain(self.impl_items.iter().map(|id| id.owner_id))
|
||||||
.chain(self.foreign_items.iter().map(|id| id.owner_id.def_id))
|
.chain(self.foreign_items.iter().map(|id| id.owner_id))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn definitions(&self) -> impl Iterator<Item = LocalDefId> + '_ {
|
||||||
|
self.owners().map(|id| id.def_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn par_items(&self, f: impl Fn(ItemId) + Send + Sync) {
|
pub fn par_items(&self, f: impl Fn(ItemId) + Send + Sync) {
|
||||||
|
@ -402,9 +402,6 @@ passes_invalid_attr_at_crate_level =
|
|||||||
`{$name}` attribute cannot be used at crate level
|
`{$name}` attribute cannot be used at crate level
|
||||||
.suggestion = perhaps you meant to use an outer attribute
|
.suggestion = perhaps you meant to use an outer attribute
|
||||||
|
|
||||||
passes_duplicate_diagnostic_item =
|
|
||||||
duplicate diagnostic item found: `{$name}`.
|
|
||||||
|
|
||||||
passes_duplicate_diagnostic_item_in_crate =
|
passes_duplicate_diagnostic_item_in_crate =
|
||||||
duplicate diagnostic item in crate `{$crate_name}`: `{$name}`.
|
duplicate diagnostic item in crate `{$crate_name}`: `{$name}`.
|
||||||
.note = the diagnostic item is first defined in crate `{$orig_crate_name}`.
|
.note = the diagnostic item is first defined in crate `{$orig_crate_name}`.
|
||||||
|
@ -11,19 +11,19 @@
|
|||||||
|
|
||||||
use rustc_ast as ast;
|
use rustc_ast as ast;
|
||||||
use rustc_hir::diagnostic_items::DiagnosticItems;
|
use rustc_hir::diagnostic_items::DiagnosticItems;
|
||||||
|
use rustc_hir::OwnerId;
|
||||||
use rustc_middle::ty::query::Providers;
|
use rustc_middle::ty::query::Providers;
|
||||||
use rustc_middle::ty::TyCtxt;
|
use rustc_middle::ty::TyCtxt;
|
||||||
use rustc_span::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
|
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
|
||||||
use rustc_span::symbol::{kw::Empty, sym, Symbol};
|
use rustc_span::symbol::{sym, Symbol};
|
||||||
|
|
||||||
use crate::errors::{DuplicateDiagnosticItem, DuplicateDiagnosticItemInCrate};
|
use crate::errors::DuplicateDiagnosticItemInCrate;
|
||||||
|
|
||||||
fn observe_item(tcx: TyCtxt<'_>, diagnostic_items: &mut DiagnosticItems, def_id: LocalDefId) {
|
fn observe_item<'tcx>(tcx: TyCtxt<'tcx>, diagnostic_items: &mut DiagnosticItems, owner: OwnerId) {
|
||||||
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
|
let attrs = tcx.hir().attrs(owner.into());
|
||||||
let attrs = tcx.hir().attrs(hir_id);
|
|
||||||
if let Some(name) = extract(attrs) {
|
if let Some(name) = extract(attrs) {
|
||||||
// insert into our table
|
// insert into our table
|
||||||
collect_item(tcx, diagnostic_items, name, def_id.to_def_id());
|
collect_item(tcx, diagnostic_items, name, owner.to_def_id());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,23 +31,29 @@ fn collect_item(tcx: TyCtxt<'_>, items: &mut DiagnosticItems, name: Symbol, item
|
|||||||
items.id_to_name.insert(item_def_id, name);
|
items.id_to_name.insert(item_def_id, name);
|
||||||
if let Some(original_def_id) = items.name_to_id.insert(name, item_def_id) {
|
if let Some(original_def_id) = items.name_to_id.insert(name, item_def_id) {
|
||||||
if original_def_id != item_def_id {
|
if original_def_id != item_def_id {
|
||||||
let orig_span = tcx.hir().span_if_local(original_def_id);
|
report_duplicate_item(tcx, name, original_def_id, item_def_id);
|
||||||
let orig_crate_name =
|
|
||||||
orig_span.is_none().then(|| tcx.crate_name(original_def_id.krate));
|
|
||||||
match tcx.hir().span_if_local(item_def_id) {
|
|
||||||
Some(span) => tcx.sess.emit_err(DuplicateDiagnosticItem { span, name }),
|
|
||||||
None => tcx.sess.emit_err(DuplicateDiagnosticItemInCrate {
|
|
||||||
span: orig_span,
|
|
||||||
orig_crate_name: orig_crate_name.unwrap_or(Empty),
|
|
||||||
have_orig_crate_name: orig_crate_name.map(|_| ()),
|
|
||||||
crate_name: tcx.crate_name(item_def_id.krate),
|
|
||||||
name,
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn report_duplicate_item(
|
||||||
|
tcx: TyCtxt<'_>,
|
||||||
|
name: Symbol,
|
||||||
|
original_def_id: DefId,
|
||||||
|
item_def_id: DefId,
|
||||||
|
) {
|
||||||
|
let orig_span = tcx.hir().span_if_local(original_def_id);
|
||||||
|
let duplicate_span = tcx.hir().span_if_local(item_def_id);
|
||||||
|
tcx.sess.emit_err(DuplicateDiagnosticItemInCrate {
|
||||||
|
duplicate_span,
|
||||||
|
orig_span,
|
||||||
|
crate_name: tcx.crate_name(item_def_id.krate),
|
||||||
|
orig_crate_name: tcx.crate_name(original_def_id.krate),
|
||||||
|
different_crates: (item_def_id.krate != original_def_id.krate).then_some(()),
|
||||||
|
name,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes.
|
/// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes.
|
||||||
fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
|
fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
|
||||||
attrs.iter().find_map(|attr| {
|
attrs.iter().find_map(|attr| {
|
||||||
@ -64,21 +70,8 @@ fn diagnostic_items(tcx: TyCtxt<'_>, cnum: CrateNum) -> DiagnosticItems {
|
|||||||
|
|
||||||
// Collect diagnostic items in this crate.
|
// Collect diagnostic items in this crate.
|
||||||
let crate_items = tcx.hir_crate_items(());
|
let crate_items = tcx.hir_crate_items(());
|
||||||
|
for id in crate_items.owners() {
|
||||||
for id in crate_items.items() {
|
observe_item(tcx, &mut diagnostic_items, id);
|
||||||
observe_item(tcx, &mut diagnostic_items, id.owner_id.def_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
for id in crate_items.trait_items() {
|
|
||||||
observe_item(tcx, &mut diagnostic_items, id.owner_id.def_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
for id in crate_items.impl_items() {
|
|
||||||
observe_item(tcx, &mut diagnostic_items, id.owner_id.def_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
for id in crate_items.foreign_items() {
|
|
||||||
observe_item(tcx, &mut diagnostic_items, id.owner_id.def_id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
diagnostic_items
|
diagnostic_items
|
||||||
|
@ -809,23 +809,17 @@ impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Diagnostic)]
|
|
||||||
#[diag(passes_duplicate_diagnostic_item)]
|
|
||||||
pub struct DuplicateDiagnosticItem {
|
|
||||||
#[primary_span]
|
|
||||||
pub span: Span,
|
|
||||||
pub name: Symbol,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Diagnostic)]
|
#[derive(Diagnostic)]
|
||||||
#[diag(passes_duplicate_diagnostic_item_in_crate)]
|
#[diag(passes_duplicate_diagnostic_item_in_crate)]
|
||||||
pub struct DuplicateDiagnosticItemInCrate {
|
pub struct DuplicateDiagnosticItemInCrate {
|
||||||
|
#[primary_span]
|
||||||
|
pub duplicate_span: Option<Span>,
|
||||||
#[note(passes_diagnostic_item_first_defined)]
|
#[note(passes_diagnostic_item_first_defined)]
|
||||||
pub span: Option<Span>,
|
pub orig_span: Option<Span>,
|
||||||
pub orig_crate_name: Symbol,
|
|
||||||
#[note]
|
#[note]
|
||||||
pub have_orig_crate_name: Option<()>,
|
pub different_crates: Option<()>,
|
||||||
pub crate_name: Symbol,
|
pub crate_name: Symbol,
|
||||||
|
pub orig_crate_name: Symbol,
|
||||||
pub name: Symbol,
|
pub name: Symbol,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,5 +9,5 @@ extern crate p1;
|
|||||||
extern crate p2;
|
extern crate p2;
|
||||||
|
|
||||||
#[rustc_diagnostic_item = "Foo"]
|
#[rustc_diagnostic_item = "Foo"]
|
||||||
pub struct Foo {} //~ ERROR duplicate diagnostic item found
|
pub struct Foo {} //~ ERROR duplicate diagnostic item in crate `duplicate_diagnostic`: `Foo`
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
@ -2,11 +2,13 @@ error: duplicate diagnostic item in crate `p2`: `Foo`.
|
|||||||
|
|
|
|
||||||
= note: the diagnostic item is first defined in crate `p1`.
|
= note: the diagnostic item is first defined in crate `p1`.
|
||||||
|
|
||||||
error: duplicate diagnostic item found: `Foo`.
|
error: duplicate diagnostic item in crate `duplicate_diagnostic`: `Foo`.
|
||||||
--> $DIR/duplicate-diagnostic.rs:12:1
|
--> $DIR/duplicate-diagnostic.rs:12:1
|
||||||
|
|
|
|
||||||
LL | pub struct Foo {}
|
LL | pub struct Foo {}
|
||||||
| ^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: the diagnostic item is first defined in crate `p2`.
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user