Skip to content

Commit f0afb88

Browse files
committed
migrate lang_items.rs to translateable diagnostics
1 parent b17ec43 commit f0afb88

File tree

3 files changed

+180
-94
lines changed

3 files changed

+180
-94
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,6 @@ passes_unknown_lang_item =
383383
definition of an unknown language item: `{$name}`
384384
.label = definition of unknown language item `{$name}`
385385
386-
passes_local_duplicate_lang_item =
387-
found duplicate lang item `{$name}`
388-
389386
passes_invalid_attr_at_crate_level =
390387
`{$name}` attribute cannot be used at crate level
391388
.suggestion = perhaps you meant to use an outer attribute
@@ -534,3 +531,30 @@ passes_no_main_function =
534531
.consider_adding_main_at_crate = consider adding a `main` function at the crate level
535532
.teach_note = If you don't know the basics of Rust, you can go look to the Rust Book to get started: https://doc.rust-lang.org/book/
536533
.non_function_main = non-function item at `crate::main` is found
534+
535+
passes_duplicate_lang_item =
536+
{$message ->
537+
*[duplicate] found duplicate lang item `{$lang_item_name}`
538+
[duplicate_in_crate] duplicate lang item in crate `{$crate_name}`: `{$lang_item_name}`.
539+
[duplicate_in_crate_depends] duplicate lang item in crate `{$crate_name}` (which `{$dependency_of}` depends on): `{$lang_item_name}`.
540+
}
541+
.first_defined_span = the lang item is first defined here
542+
.first_defined_crate_depends = the lang item is first defined in crate `{$orig_crate_name}` (which `{$orig_dependency_of}` depends on)
543+
.first_defined_crate = the lang item is first defined in crate `{$orig_crate_name}`.
544+
.first_definition_local = first definition in the local crate (`{$orig_crate_name}`)
545+
.second_definition_local = second definition in the local crate (`{$crate_name}`)
546+
.first_definition_path = first definition in `{$orig_crate_name}` loaded from {$orig_path}
547+
.second_definition_path = second definition in `{$crate_name}` loaded from {$path}
548+
549+
passes_incorrect_target =
550+
`{$name}` language item must be applied to a {$kind} with {$at_least ->
551+
[true] at least {$num}
552+
*[false] {$num}
553+
} generic {$num ->
554+
[one] argument
555+
*[other] arguments
556+
}
557+
.label = this {$kind} has {$actual_num} generic {$actual_num ->
558+
[one] argument
559+
*[other] arguments
560+
}

compiler/rustc_passes/src/errors.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,3 +1191,77 @@ impl<'a> IntoDiagnostic<'a> for NoMainErr {
11911191
diag
11921192
}
11931193
}
1194+
1195+
pub struct DuplicateLangItem<'a> {
1196+
pub local_span: Option<Span>,
1197+
pub lang_item_name: Symbol,
1198+
pub crate_name: Symbol,
1199+
pub dependency_of: Symbol,
1200+
pub is_local: bool,
1201+
pub path: String,
1202+
pub first_defined_span: Option<Span>,
1203+
pub orig_crate_name: Symbol,
1204+
pub orig_dependency_of: Symbol,
1205+
pub orig_is_local: bool,
1206+
pub orig_path: String,
1207+
pub message: &'a str,
1208+
}
1209+
1210+
impl<'a, 'b> IntoDiagnostic<'a> for DuplicateLangItem<'b> {
1211+
fn into_diagnostic(
1212+
self,
1213+
handler: &'a rustc_errors::Handler,
1214+
) -> rustc_errors::DiagnosticBuilder<'a, ErrorGuaranteed> {
1215+
let mut diag = handler.struct_err_with_code(
1216+
rustc_errors::fluent::passes::duplicate_lang_item,
1217+
error_code!(E0152),
1218+
);
1219+
diag.set_arg("lang_item_name", self.lang_item_name);
1220+
diag.set_arg("crate_name", self.crate_name);
1221+
diag.set_arg("dependency_of", self.dependency_of);
1222+
diag.set_arg("path", self.path);
1223+
diag.set_arg("orig_crate_name", self.orig_crate_name);
1224+
diag.set_arg("orig_dependency_of", self.orig_dependency_of);
1225+
diag.set_arg("orig_path", self.orig_path);
1226+
diag.set_arg("message", self.message);
1227+
if let Some(span) = self.local_span {
1228+
diag.set_span(span);
1229+
}
1230+
if let Some(span) = self.first_defined_span {
1231+
diag.span_note(span, rustc_errors::fluent::passes::first_defined_span);
1232+
} else {
1233+
if self.orig_dependency_of.is_empty() {
1234+
diag.note(rustc_errors::fluent::passes::first_defined_crate);
1235+
} else {
1236+
diag.note(rustc_errors::fluent::passes::first_defined_crate_depends);
1237+
}
1238+
1239+
if self.orig_is_local {
1240+
diag.note(rustc_errors::fluent::passes::first_definition_local);
1241+
} else {
1242+
diag.note(rustc_errors::fluent::passes::first_definition_path);
1243+
}
1244+
1245+
if self.is_local {
1246+
diag.note(rustc_errors::fluent::passes::second_definition_local);
1247+
} else {
1248+
diag.note(rustc_errors::fluent::passes::second_definition_path);
1249+
}
1250+
}
1251+
diag
1252+
}
1253+
}
1254+
1255+
#[derive(Diagnostic)]
1256+
#[diag(passes::incorrect_target, code = "E0718")]
1257+
pub struct IncorrectTarget<'a> {
1258+
#[primary_span]
1259+
pub span: Span,
1260+
#[label]
1261+
pub generics_span: Span,
1262+
pub name: &'a str,
1263+
pub kind: &'static str,
1264+
pub num: usize,
1265+
pub actual_num: usize,
1266+
pub at_least: bool,
1267+
}

compiler/rustc_passes/src/lang_items.rs

Lines changed: 79 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88
//! * Functions called by the compiler itself.
99
1010
use crate::check_attr::target_from_impl_item;
11-
use crate::errors::{LangItemOnIncorrectTarget, UnknownLangItem};
11+
use crate::errors::{
12+
DuplicateLangItem, IncorrectTarget, LangItemOnIncorrectTarget, UnknownLangItem,
13+
};
1214
use crate::weak_lang_items;
1315

14-
use rustc_errors::{pluralize, struct_span_err};
1516
use rustc_hir as hir;
1617
use rustc_hir::def::DefKind;
1718
use rustc_hir::def_id::DefId;
1819
use rustc_hir::lang_items::{extract, GenericRequirement, ITEM_REFS};
1920
use rustc_hir::{HirId, LangItem, LanguageItems, Target};
2021
use rustc_middle::ty::TyCtxt;
2122
use rustc_session::cstore::ExternCrate;
22-
use rustc_span::Span;
23+
use rustc_span::{Span, Symbol};
2324

2425
use rustc_middle::ty::query::Providers;
2526

@@ -62,74 +63,72 @@ impl<'tcx> LanguageItemCollector<'tcx> {
6263
// Check for duplicates.
6364
if let Some(original_def_id) = self.items.items[item_index] {
6465
if original_def_id != item_def_id {
65-
let lang_item = LangItem::from_u32(item_index as u32).unwrap();
66-
let name = lang_item.name();
67-
let mut err = match self.tcx.hir().span_if_local(item_def_id) {
68-
Some(span) => struct_span_err!(
69-
self.tcx.sess,
70-
span,
71-
E0152,
72-
"found duplicate lang item `{}`",
73-
name
74-
),
75-
None => match self.tcx.extern_crate(item_def_id) {
76-
Some(ExternCrate { dependency_of, .. }) => {
77-
self.tcx.sess.struct_err(&format!(
78-
"duplicate lang item in crate `{}` (which `{}` depends on): `{}`.",
79-
self.tcx.crate_name(item_def_id.krate),
80-
self.tcx.crate_name(*dependency_of),
81-
name
82-
))
83-
}
84-
_ => self.tcx.sess.struct_err(&format!(
85-
"duplicate lang item in crate `{}`: `{}`.",
86-
self.tcx.crate_name(item_def_id.krate),
87-
name
88-
)),
89-
},
66+
let local_span = self.tcx.hir().span_if_local(item_def_id);
67+
let lang_item_name = LangItem::from_u32(item_index as u32).unwrap().name();
68+
let crate_name = self.tcx.crate_name(item_def_id.krate);
69+
let mut dependency_of = Symbol::intern("");
70+
let is_local = item_def_id.is_local();
71+
let path = if is_local {
72+
String::new()
73+
} else {
74+
self.tcx
75+
.crate_extern_paths(item_def_id.krate)
76+
.iter()
77+
.map(|p| p.display().to_string())
78+
.collect::<Vec<_>>()
79+
.join(", ")
80+
.into()
9081
};
91-
if let Some(span) = self.tcx.hir().span_if_local(original_def_id) {
92-
err.span_note(span, "the lang item is first defined here");
82+
let first_defined_span = self.tcx.hir().span_if_local(original_def_id);
83+
let mut orig_crate_name = Symbol::intern("");
84+
let mut orig_dependency_of = Symbol::intern("");
85+
let orig_is_local = original_def_id.is_local();
86+
let orig_path = if orig_is_local {
87+
String::new()
9388
} else {
94-
match self.tcx.extern_crate(original_def_id) {
95-
Some(ExternCrate { dependency_of, .. }) => {
96-
err.note(&format!(
97-
"the lang item is first defined in crate `{}` (which `{}` depends on)",
98-
self.tcx.crate_name(original_def_id.krate),
99-
self.tcx.crate_name(*dependency_of)
100-
));
101-
}
102-
_ => {
103-
err.note(&format!(
104-
"the lang item is first defined in crate `{}`.",
105-
self.tcx.crate_name(original_def_id.krate)
106-
));
107-
}
89+
self.tcx
90+
.crate_extern_paths(original_def_id.krate)
91+
.iter()
92+
.map(|p| p.display().to_string())
93+
.collect::<Vec<_>>()
94+
.join(", ")
95+
.into()
96+
};
97+
if first_defined_span.is_none() {
98+
orig_crate_name = self.tcx.crate_name(original_def_id.krate);
99+
if let Some(ExternCrate { dependency_of: inner_dependency_of, .. }) =
100+
self.tcx.extern_crate(original_def_id)
101+
{
102+
orig_dependency_of = self.tcx.crate_name(*inner_dependency_of);
108103
}
109-
let mut note_def = |which, def_id: DefId| {
110-
let crate_name = self.tcx.crate_name(def_id.krate);
111-
let note = if def_id.is_local() {
112-
format!("{} definition in the local crate (`{}`)", which, crate_name)
113-
} else {
114-
let paths: Vec<_> = self
115-
.tcx
116-
.crate_extern_paths(def_id.krate)
117-
.iter()
118-
.map(|p| p.display().to_string())
119-
.collect();
120-
format!(
121-
"{} definition in `{}` loaded from {}",
122-
which,
123-
crate_name,
124-
paths.join(", ")
125-
)
126-
};
127-
err.note(&note);
128-
};
129-
note_def("first", original_def_id);
130-
note_def("second", item_def_id);
131104
}
132-
err.emit();
105+
106+
let message = if local_span.is_some() {
107+
"duplicate"
108+
} else {
109+
match self.tcx.extern_crate(item_def_id) {
110+
Some(ExternCrate { dependency_of: inner_dependency_of, .. }) => {
111+
dependency_of = self.tcx.crate_name(*inner_dependency_of);
112+
"duplicate_in_crate_depends"
113+
}
114+
_ => "duplicate_in_crate",
115+
}
116+
};
117+
118+
self.tcx.sess.emit_err(DuplicateLangItem {
119+
local_span,
120+
lang_item_name,
121+
crate_name,
122+
dependency_of,
123+
is_local,
124+
path,
125+
first_defined_span,
126+
orig_crate_name,
127+
orig_dependency_of,
128+
orig_is_local,
129+
orig_path,
130+
message,
131+
});
133132
}
134133
}
135134

@@ -162,41 +161,30 @@ impl<'tcx> LanguageItemCollector<'tcx> {
162161
None => (0, *item_span),
163162
};
164163

164+
let mut at_least = false;
165165
let required = match lang_item.required_generics() {
166-
GenericRequirement::Exact(num) if num != actual_num => {
167-
Some((format!("{}", num), pluralize!(num)))
168-
}
166+
GenericRequirement::Exact(num) if num != actual_num => Some(num),
169167
GenericRequirement::Minimum(num) if actual_num < num => {
170-
Some((format!("at least {}", num), pluralize!(num)))
171-
}
168+
at_least = true;
169+
Some(num)}
170+
,
172171
// If the number matches, or there is no requirement, handle it normally
173172
_ => None,
174173
};
175174

176-
if let Some((range_str, pluralized)) = required {
175+
if let Some(num) = required {
177176
// We are issuing E0718 "incorrect target" here, because while the
178177
// item kind of the target is correct, the target is still wrong
179178
// because of the wrong number of generic arguments.
180-
struct_span_err!(
181-
self.tcx.sess,
179+
self.tcx.sess.emit_err(IncorrectTarget {
182180
span,
183-
E0718,
184-
"`{}` language item must be applied to a {} with {} generic argument{}",
185-
name,
186-
kind.descr(),
187-
range_str,
188-
pluralized,
189-
)
190-
.span_label(
191181
generics_span,
192-
format!(
193-
"this {} has {} generic argument{}",
194-
kind.descr(),
195-
actual_num,
196-
pluralize!(actual_num),
197-
),
198-
)
199-
.emit();
182+
name: name.as_str(),
183+
kind: kind.descr(),
184+
num,
185+
actual_num,
186+
at_least,
187+
});
200188

201189
// return early to not collect the lang item
202190
return;

0 commit comments

Comments
 (0)