Skip to content

Commit 17a4a68

Browse files
naritanaraCleanCut
authored andcommitted
Migrate derivable diagnostics in lang_items.rs
1 parent 2f74d1d commit 17a4a68

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
@@ -280,3 +280,11 @@ passes_missing_alloc_error_handler = `#[alloc_error_handler]` function required,
280280
passes_missing_lang_item = language item required, but not found: `{$name}`
281281
.note = this can occur when a binary crate with `#![no_std]` is compiled for a target where `{$name}` is defined in the standard library
282282
.help = you may be able to compile for a target that doesn't need `{$name}`, specify a target with `--target` or in `.cargo/config`
283+
284+
passes_lang_item_on_incorrect_target = `{$name}` language item must be applied to a {$expected_target}
285+
.label = attribute should be applied to a {$expected_target}, not a {$actual_target}
286+
287+
passes_unknown_lang_item = definition of an unknown language item: `{$name}`
288+
.label = definition of unknown language item `{$name}`
289+
290+
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::{Diagnostic, LintDiagnostic, Subdiagnostic};
34
use rustc_span::{Span, Symbol};
45

@@ -682,3 +683,23 @@ pub struct MissingAllocErrorHandler;
682683
pub struct MissingLangItem {
683684
pub name: Symbol,
684685
}
686+
687+
#[derive(Diagnostic)]
688+
#[diag(passes::lang_item_on_incorrect_target, code = "E0718")]
689+
pub struct LangItemOnIncorrectTarget {
690+
#[primary_span]
691+
#[label]
692+
pub span: Span,
693+
pub name: Symbol,
694+
pub expected_target: Target,
695+
pub actual_target: Target,
696+
}
697+
698+
#[derive(Diagnostic)]
699+
#[diag(passes::unknown_lang_item, code = "E0522")]
700+
pub struct UnknownLangItem {
701+
#[primary_span]
702+
#[label]
703+
pub span: Span,
704+
pub name: Symbol,
705+
}

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)