Skip to content

Commit

Permalink
Merge the two diagnostics.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Feb 26, 2023
1 parent 11fbb57 commit 752ddd0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 32 deletions.
3 changes: 0 additions & 3 deletions compiler/rustc_passes/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,6 @@ passes_invalid_attr_at_crate_level =
`{$name}` attribute cannot be used at crate level
.suggestion = perhaps you meant to use an outer attribute
passes_duplicate_diagnostic_item =
duplicate diagnostic item found: `{$name}`.
passes_duplicate_diagnostic_item_in_crate =
duplicate diagnostic item in crate `{$crate_name}`: `{$name}`.
.note = the diagnostic item is first defined in crate `{$orig_crate_name}`.
Expand Down
28 changes: 12 additions & 16 deletions compiler/rustc_passes/src/diagnostic_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use rustc_hir::OwnerId;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::TyCtxt;
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>(tcx: TyCtxt<'tcx>, diagnostic_items: &mut DiagnosticItems, owner: OwnerId) {
let attrs = tcx.hir().attrs(owner.into());
Expand All @@ -42,20 +42,16 @@ fn report_duplicate_item(
original_def_id: DefId,
item_def_id: DefId,
) {
let (orig_span, orig_crate_name, have_orig_crate_name) = match original_def_id.as_local() {
Some(local_original) => (Some(tcx.def_span(local_original)), Empty, None),
None => (None, tcx.crate_name(original_def_id.krate), Some(())),
};
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,
have_orig_crate_name,
crate_name: tcx.crate_name(item_def_id.krate),
name,
}),
};
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.
Expand Down
16 changes: 5 additions & 11 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
#[diag(passes_duplicate_diagnostic_item_in_crate)]
pub struct DuplicateDiagnosticItemInCrate {
#[primary_span]
pub duplicate_span: Option<Span>,
#[note(passes_diagnostic_item_first_defined)]
pub span: Option<Span>,
pub orig_crate_name: Symbol,
pub orig_span: Option<Span>,
#[note]
pub have_orig_crate_name: Option<()>,
pub different_crates: Option<()>,
pub crate_name: Symbol,
pub orig_crate_name: Symbol,
pub name: Symbol,
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/tool-attributes/duplicate-diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ extern crate p1;
extern crate p2;

#[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() {}
4 changes: 3 additions & 1 deletion tests/ui/tool-attributes/duplicate-diagnostic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ error: duplicate diagnostic item in crate `p2`: `Foo`.
|
= 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
|
LL | pub struct Foo {}
| ^^^^^^^^^^^^^^
|
= note: the diagnostic item is first defined in crate `p2`.

error: aborting due to 2 previous errors

0 comments on commit 752ddd0

Please sign in to comment.