Skip to content

Commit 49ce855

Browse files
committed
Migrate derivable diagnostics in lang_items.rs
1 parent 3422b40 commit 49ce855

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

compiler/rustc_error_messages/locales/en-US/passes.ftl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,11 @@ passes_missing_alloc_error_handler = `#[alloc_error_handler]` function required,
274274
passes_missing_lang_item = language item required, but not found: `{$name}`
275275
.note = this can occur when a binary crate with `#![no_std]` is compiled for a target where `{$name}` is defined in the standard library
276276
.help = you may be able to compile for a target that doesn't need `{$name}`, specify a target with `--target` or in `.cargo/config`
277+
278+
passes_lang_item_on_incorrect_target = `{$name}` language item must be applied to a {$expected_target}
279+
.label = attribute should be applied to a {$expected_target}, not a {$actual_target}
280+
281+
passes_unknown_lang_item = definition of an unknown language item: `{$name}`
282+
.label = definition of unknown language item `{$name}`
283+
284+
passes_local_duplicate_lang_item = found duplicate lang item `{$name}`

compiler/rustc_passes/src/errors.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_errors::{Applicability, MultiSpan};
2+
use rustc_hir::Target;
23
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
34
use rustc_span::{Span, Symbol};
45

@@ -666,3 +667,23 @@ pub struct MissingAllocErrorHandler;
666667
pub struct MissingLangItem {
667668
pub name: Symbol,
668669
}
670+
671+
#[derive(SessionDiagnostic)]
672+
#[diag(passes::lang_item_on_incorrect_target, code = "E0718")]
673+
pub struct LangItemOnIncorrectTarget {
674+
#[primary_span]
675+
#[label]
676+
pub span: Span,
677+
pub name: Symbol,
678+
pub expected_target: Target,
679+
pub actual_target: Target,
680+
}
681+
682+
#[derive(SessionDiagnostic)]
683+
#[diag(passes::unknown_lang_item, code = "E0522")]
684+
pub struct UnknownLangItem {
685+
#[primary_span]
686+
#[label]
687+
pub span: Span,
688+
pub name: Symbol,
689+
}

compiler/rustc_passes/src/lang_items.rs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//! * Traits that represent operators; e.g., `Add`, `Sub`, `Index`.
88
//! * Functions called by the compiler itself.
99
10+
use crate::errors::{LangItemOnIncorrectTarget, UnknownLangItem};
1011
use crate::check_attr::target_from_impl_item;
1112
use crate::weak_lang_items;
1213

@@ -42,34 +43,19 @@ impl<'tcx> LanguageItemCollector<'tcx> {
4243
}
4344
// Known lang item with attribute on incorrect target.
4445
Some((_, expected_target)) => {
45-
struct_span_err!(
46-
self.tcx.sess,
46+
self.tcx.sess.emit_err(LangItemOnIncorrectTarget {
4747
span,
48-
E0718,
49-
"`{}` language item must be applied to a {}",
50-
value,
48+
name: value,
5149
expected_target,
52-
)
53-
.span_label(
54-
span,
55-
format!(
56-
"attribute should be applied to a {}, not a {}",
57-
expected_target, actual_target,
58-
),
59-
)
60-
.emit();
50+
actual_target,
51+
});
6152
}
6253
// Unknown lang item.
6354
_ => {
64-
struct_span_err!(
65-
self.tcx.sess,
55+
self.tcx.sess.emit_err(UnknownLangItem {
6656
span,
67-
E0522,
68-
"definition of an unknown language item: `{}`",
69-
value
70-
)
71-
.span_label(span, format!("definition of unknown language item `{}`", value))
72-
.emit();
57+
name: value,
58+
});
7359
}
7460
}
7561
}

0 commit comments

Comments
 (0)