Skip to content

Commit 113e8df

Browse files
committed
Port dead_code lints to be translatable.
1 parent 56f1325 commit 113e8df

11 files changed

+236
-99
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,37 @@ passes_missing_const_err =
671671
attributes `#[rustc_const_unstable]` and `#[rustc_const_stable]` require the function or method to be `const`
672672
.help = make the function or method const
673673
.label = attribute specified here
674+
675+
passes_dead_codes =
676+
{ $multiple ->
677+
*[true] multiple {$descr}s are
678+
[false] { $num ->
679+
[one] {$descr} {$name_list} is
680+
*[other] {$descr}s {$name_list} are
681+
}
682+
} never {$participle}
683+
684+
passes_change_fields_to_be_of_unit_type =
685+
consider changing the { $num ->
686+
[one] field
687+
*[other] fields
688+
} to be of unit type to suppress this warning
689+
while preserving the field numbering, or remove the { $num ->
690+
[one] field
691+
*[other] fields
692+
}
693+
694+
passes_parent_info =
695+
{$num ->
696+
[one] {$descr}
697+
*[other] {$descr}s
698+
} in this {$parent_descr}
699+
700+
passes_ignored_derived_impls =
701+
`{$name}` has {$trait_list_len ->
702+
[one] a derived impl
703+
*[other] derived impls
704+
} for the {$trait_list_len ->
705+
[one] trait {$trait_list}, but this is
706+
*[other] traits {$trait_list}, but these are
707+
} intentionally ignored during dead code analysis

compiler/rustc_errors/src/diagnostic_impls.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_target::abi::TargetDataLayoutErrors;
1111
use rustc_target::spec::{PanicStrategy, SplitDebuginfo, StackProtector, TargetTriple};
1212
use std::borrow::Cow;
1313
use std::fmt;
14+
use std::fmt::Write;
1415
use std::num::ParseIntError;
1516
use std::path::{Path, PathBuf};
1617

@@ -170,6 +171,37 @@ impl IntoDiagnosticArg for Level {
170171
}
171172
}
172173

174+
#[derive(Clone)]
175+
pub struct DiagnosticSymbolList(Vec<Symbol>);
176+
177+
impl From<Vec<Symbol>> for DiagnosticSymbolList {
178+
fn from(v: Vec<Symbol>) -> Self {
179+
DiagnosticSymbolList(v)
180+
}
181+
}
182+
183+
impl IntoDiagnosticArg for DiagnosticSymbolList {
184+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
185+
// FIXME: replace the logic here with a real list formatter
186+
let symbols = match &self.0[..] {
187+
[symbol] => format!("`{symbol}`"),
188+
[symbol, last] => {
189+
format!("`{symbol}` and `{last}`",)
190+
}
191+
[symbols @ .., last] => {
192+
let mut result = String::new();
193+
for symbol in symbols {
194+
write!(result, "`{symbol}`, ").unwrap();
195+
}
196+
write!(result, "and `{last}`").unwrap();
197+
result
198+
}
199+
[] => unreachable!(),
200+
};
201+
DiagnosticArgValue::Str(Cow::Owned(symbols))
202+
}
203+
}
204+
173205
impl IntoDiagnostic<'_, !> for TargetDataLayoutErrors<'_> {
174206
fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, !> {
175207
let mut diag;

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ pub use diagnostic::{
376376
DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
377377
};
378378
pub use diagnostic_builder::{DiagnosticBuilder, EmissionGuarantee, Noted};
379-
pub use diagnostic_impls::DiagnosticArgFromDisplay;
379+
pub use diagnostic_impls::{DiagnosticArgFromDisplay, DiagnosticSymbolList};
380380
use std::backtrace::Backtrace;
381381

382382
/// A handler deals with errors and other compiler output.

compiler/rustc_passes/src/dead.rs

Lines changed: 84 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use itertools::Itertools;
66
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
7-
use rustc_errors::{pluralize, Applicability, MultiSpan};
7+
use rustc_errors::MultiSpan;
88
use rustc_hir as hir;
99
use rustc_hir::def::{CtorOf, DefKind, Res};
1010
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -18,7 +18,10 @@ use rustc_session::lint;
1818
use rustc_span::symbol::{sym, Symbol};
1919
use std::mem;
2020

21-
use crate::errors::UselessAssignment;
21+
use crate::errors::{
22+
ChangeFieldsToBeOfUnitType, IgnoredDerivedImpls, MultipleDeadCodes, ParentInfo,
23+
UselessAssignment,
24+
};
2225

2326
// Any local node that may call something in its body block should be
2427
// explored. For example, if it's a live Node::Item that is a
@@ -698,99 +701,89 @@ impl<'tcx> DeadVisitor<'tcx> {
698701
parent_item: Option<LocalDefId>,
699702
is_positional: bool,
700703
) {
701-
if let Some(&first_id) = dead_codes.first() {
702-
let tcx = self.tcx;
703-
let names: Vec<_> = dead_codes
704-
.iter()
705-
.map(|&def_id| tcx.item_name(def_id.to_def_id()).to_string())
706-
.collect();
707-
let spans: Vec<_> = dead_codes
708-
.iter()
709-
.map(|&def_id| match tcx.def_ident_span(def_id) {
710-
Some(s) => s.with_ctxt(tcx.def_span(def_id).ctxt()),
711-
None => tcx.def_span(def_id),
704+
let Some(&first_id) = dead_codes.first() else {
705+
return;
706+
};
707+
let tcx = self.tcx;
708+
let names: Vec<_> =
709+
dead_codes.iter().map(|&def_id| tcx.item_name(def_id.to_def_id())).collect();
710+
let spans: Vec<_> = dead_codes
711+
.iter()
712+
.map(|&def_id| match tcx.def_ident_span(def_id) {
713+
Some(s) => s.with_ctxt(tcx.def_span(def_id).ctxt()),
714+
None => tcx.def_span(def_id),
715+
})
716+
.collect();
717+
718+
let descr = tcx.def_kind(first_id).descr(first_id.to_def_id());
719+
let num = dead_codes.len();
720+
let multiple = num > 6;
721+
let name_list = names.into();
722+
723+
let lint = if is_positional {
724+
lint::builtin::UNUSED_TUPLE_STRUCT_FIELDS
725+
} else {
726+
lint::builtin::DEAD_CODE
727+
};
728+
729+
let parent_info = if let Some(parent_item) = parent_item {
730+
let parent_descr = tcx.def_kind(parent_item).descr(parent_item.to_def_id());
731+
Some(ParentInfo {
732+
num,
733+
descr,
734+
parent_descr,
735+
span: tcx.def_ident_span(parent_item).unwrap(),
736+
})
737+
} else {
738+
None
739+
};
740+
741+
let encl_def_id = parent_item.unwrap_or(first_id);
742+
let ignored_derived_impls =
743+
if let Some(ign_traits) = self.ignored_derived_traits.get(&encl_def_id) {
744+
let trait_list = ign_traits
745+
.iter()
746+
.map(|(trait_id, _)| self.tcx.item_name(*trait_id))
747+
.collect::<Vec<_>>();
748+
let trait_list_len = trait_list.len();
749+
Some(IgnoredDerivedImpls {
750+
name: self.tcx.item_name(encl_def_id.to_def_id()),
751+
trait_list: trait_list.into(),
752+
trait_list_len,
712753
})
713-
.collect();
714-
715-
let descr = tcx.def_kind(first_id).descr(first_id.to_def_id());
716-
let span_len = dead_codes.len();
717-
let names = match &names[..] {
718-
_ if span_len > 6 => String::new(),
719-
[name] => format!("`{name}` "),
720-
[names @ .., last] => {
721-
format!(
722-
"{} and `{last}` ",
723-
names.iter().map(|name| format!("`{name}`")).join(", ")
724-
)
725-
}
726-
[] => unreachable!(),
754+
} else {
755+
None
727756
};
728-
let msg = format!(
729-
"{these}{descr}{s} {names}{are} never {participle}",
730-
these = if span_len > 6 { "multiple " } else { "" },
731-
s = pluralize!(span_len),
732-
are = pluralize!("is", span_len),
733-
);
734-
735-
tcx.struct_span_lint_hir(
736-
if is_positional {
737-
lint::builtin::UNUSED_TUPLE_STRUCT_FIELDS
738-
} else {
739-
lint::builtin::DEAD_CODE
740-
},
741-
tcx.hir().local_def_id_to_hir_id(first_id),
742-
MultiSpan::from_spans(spans.clone()),
743-
msg,
744-
|err| {
745-
if is_positional {
746-
err.multipart_suggestion(
747-
&format!(
748-
"consider changing the field{s} to be of unit type to \
749-
suppress this warning while preserving the field \
750-
numbering, or remove the field{s}",
751-
s = pluralize!(span_len)
752-
),
753-
spans.iter().map(|sp| (*sp, "()".to_string())).collect(),
754-
// "HasPlaceholders" because applying this fix by itself isn't
755-
// enough: All constructor calls have to be adjusted as well
756-
Applicability::HasPlaceholders,
757-
);
758-
}
759757

760-
if let Some(parent_item) = parent_item {
761-
let parent_descr = tcx.def_kind(parent_item).descr(parent_item.to_def_id());
762-
err.span_label(
763-
tcx.def_ident_span(parent_item).unwrap(),
764-
format!("{descr}{s} in this {parent_descr}", s = pluralize!(span_len)),
765-
);
766-
}
758+
let diag = if is_positional {
759+
MultipleDeadCodes::UnusedTupleStructFields {
760+
multiple,
761+
num,
762+
descr,
763+
participle,
764+
name_list,
765+
change_fields_suggestion: ChangeFieldsToBeOfUnitType { num, spans: spans.clone() },
766+
parent_info,
767+
ignored_derived_impls,
768+
}
769+
} else {
770+
MultipleDeadCodes::DeadCodes {
771+
multiple,
772+
num,
773+
descr,
774+
participle,
775+
name_list,
776+
parent_info,
777+
ignored_derived_impls,
778+
}
779+
};
767780

768-
let encl_def_id = parent_item.unwrap_or(first_id);
769-
if let Some(ign_traits) = self.ignored_derived_traits.get(&encl_def_id) {
770-
let traits_str = ign_traits
771-
.iter()
772-
.map(|(trait_id, _)| format!("`{}`", self.tcx.item_name(*trait_id)))
773-
.collect::<Vec<_>>()
774-
.join(" and ");
775-
let plural_s = pluralize!(ign_traits.len());
776-
let article = if ign_traits.len() > 1 { "" } else { "a " };
777-
let is_are = if ign_traits.len() > 1 { "these are" } else { "this is" };
778-
let msg = format!(
779-
"`{}` has {}derived impl{} for the trait{} {}, but {} \
780-
intentionally ignored during dead code analysis",
781-
self.tcx.item_name(encl_def_id.to_def_id()),
782-
article,
783-
plural_s,
784-
plural_s,
785-
traits_str,
786-
is_are
787-
);
788-
err.note(&msg);
789-
}
790-
err
791-
},
792-
);
793-
}
781+
self.tcx.emit_spanned_lint(
782+
lint,
783+
tcx.hir().local_def_id_to_hir_id(first_id),
784+
MultiSpan::from_spans(spans.clone()),
785+
diag,
786+
);
794787
}
795788

796789
fn warn_dead_fields_and_variants(

compiler/rustc_passes/src/errors.rs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ use std::{
44
};
55

66
use rustc_ast::Label;
7-
use rustc_errors::{error_code, Applicability, ErrorGuaranteed, IntoDiagnostic, MultiSpan};
7+
use rustc_errors::{
8+
error_code, Applicability, DiagnosticSymbolList, ErrorGuaranteed, IntoDiagnostic, MultiSpan,
9+
};
810
use rustc_hir::{self as hir, ExprKind, Target};
911
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
1012
use rustc_middle::ty::{MainDefinition, Ty};
1113
use rustc_span::{Span, Symbol, DUMMY_SP};
1214

15+
use rustc_errors::{pluralize, AddToDiagnostic, Diagnostic, SubdiagnosticMessage};
16+
1317
use crate::lang_items::Duplicate;
1418

1519
#[derive(LintDiagnostic)]
@@ -1449,3 +1453,77 @@ pub struct MissingConstErr {
14491453
#[label]
14501454
pub const_span: Span,
14511455
}
1456+
1457+
#[derive(LintDiagnostic)]
1458+
pub enum MultipleDeadCodes<'tcx> {
1459+
#[diag(passes_dead_codes)]
1460+
DeadCodes {
1461+
multiple: bool,
1462+
num: usize,
1463+
descr: &'tcx str,
1464+
participle: &'tcx str,
1465+
name_list: DiagnosticSymbolList,
1466+
#[subdiagnostic]
1467+
parent_info: Option<ParentInfo<'tcx>>,
1468+
#[subdiagnostic]
1469+
ignored_derived_impls: Option<IgnoredDerivedImpls>,
1470+
},
1471+
#[diag(passes_dead_codes)]
1472+
UnusedTupleStructFields {
1473+
multiple: bool,
1474+
num: usize,
1475+
descr: &'tcx str,
1476+
participle: &'tcx str,
1477+
name_list: DiagnosticSymbolList,
1478+
#[subdiagnostic]
1479+
change_fields_suggestion: ChangeFieldsToBeOfUnitType,
1480+
#[subdiagnostic]
1481+
parent_info: Option<ParentInfo<'tcx>>,
1482+
#[subdiagnostic]
1483+
ignored_derived_impls: Option<IgnoredDerivedImpls>,
1484+
},
1485+
}
1486+
1487+
#[derive(Subdiagnostic)]
1488+
#[label(passes_parent_info)]
1489+
pub struct ParentInfo<'tcx> {
1490+
pub num: usize,
1491+
pub descr: &'tcx str,
1492+
pub parent_descr: &'tcx str,
1493+
#[primary_span]
1494+
pub span: Span,
1495+
}
1496+
1497+
#[derive(Subdiagnostic)]
1498+
#[note(passes_ignored_derived_impls)]
1499+
pub struct IgnoredDerivedImpls {
1500+
pub name: Symbol,
1501+
pub trait_list: DiagnosticSymbolList,
1502+
pub trait_list_len: usize,
1503+
}
1504+
1505+
pub struct ChangeFieldsToBeOfUnitType {
1506+
pub num: usize,
1507+
pub spans: Vec<Span>,
1508+
}
1509+
1510+
// FIXME: Replace this impl with a derive.
1511+
impl AddToDiagnostic for ChangeFieldsToBeOfUnitType {
1512+
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
1513+
where
1514+
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
1515+
{
1516+
diag.multipart_suggestion(
1517+
&format!(
1518+
"consider changing the field{s} to be of unit type to \
1519+
suppress this warning while preserving the field \
1520+
numbering, or remove the field{s}",
1521+
s = pluralize!(self.num)
1522+
),
1523+
self.spans.iter().map(|sp| (*sp, "()".to_string())).collect(),
1524+
// "HasPlaceholders" because applying this fix by itself isn't
1525+
// enough: All constructor calls have to be adjusted as well
1526+
Applicability::HasPlaceholders,
1527+
);
1528+
}
1529+
}

src/test/ui/derives/clone-debug-dead-code-in-the-same-struct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#[derive(Debug)]
44
pub struct Whatever {
55
pub field0: (),
6-
field1: (), //~ ERROR fields `field1`, `field2`, `field3` and `field4` are never read
6+
field1: (), //~ ERROR fields `field1`, `field2`, `field3`, and `field4` are never read
77
field2: (),
88
field3: (),
99
field4: (),

0 commit comments

Comments
 (0)