Skip to content

Commit 63fbb05

Browse files
authored
Rollup merge of #110816 - clubby789:rustc-passes-diagnostics, r=compiler-errors
Migrate `rustc_passes` to translatable diagnostics cc #100717
2 parents b1ff6e3 + 6a41cfe commit 63fbb05

11 files changed

+313
-180
lines changed

compiler/rustc_passes/messages.ftl

+42-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ passes_doc_attr_not_crate_level =
139139
passes_attr_crate_level =
140140
this attribute can only be applied at the crate level
141141
.suggestion = to apply to the crate, use an inner attribute
142-
.help = to apply to the crate, use an inner attribute
143142
.note = read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#at-the-crate-level> for more information
144143
145144
passes_doc_test_unknown =
@@ -724,3 +723,45 @@ passes_skipping_const_checks = skipping const checks
724723
passes_invalid_macro_export_arguments = `{$name}` isn't a valid `#[macro_export]` argument
725724
726725
passes_invalid_macro_export_arguments_too_many_items = `#[macro_export]` can only take 1 or 0 arguments
726+
727+
passes_unreachable_due_to_uninhabited = unreachable {$descr}
728+
.label = unreachable {$descr}
729+
.label_orig = any code following this expression is unreachable
730+
.note = this expression has type `{$ty}`, which is uninhabited
731+
732+
passes_unused_var_maybe_capture_ref = unused variable: `{$name}`
733+
.help = did you mean to capture by reference instead?
734+
735+
passes_unused_capture_maybe_capture_ref = value captured by `{$name}` is never read
736+
.help = did you mean to capture by reference instead?
737+
738+
passes_unused_var_remove_field = unused variable: `{$name}`
739+
passes_unused_var_remove_field_suggestion = try removing the field
740+
741+
passes_unused_var_assigned_only = variable `{$name}` is assigned to, but never used
742+
.note = consider using `_{$name}` instead
743+
744+
passes_unnecessary_stable_feature = the feature `{$feature}` has been stable since {$since} and no longer requires an attribute to enable
745+
746+
passes_unnecessary_partial_stable_feature = the feature `{$feature}` has been partially stabilized since {$since} and is succeeded by the feature `{$implies}`
747+
.suggestion = if you are using features which are still unstable, change to using `{$implies}`
748+
.suggestion_remove = if you are using features which are now stable, remove this line
749+
750+
passes_ineffective_unstable_impl = an `#[unstable]` annotation here has no effect
751+
.note = see issue #55436 <https://github.com/rust-lang/rust/issues/55436> for more information
752+
753+
passes_unused_assign = value assigned to `{$name}` is never read
754+
.help = maybe it is overwritten before being read?
755+
756+
passes_unused_assign_passed = value passed to `{$name}` is never read
757+
.help = maybe it is overwritten before being read?
758+
759+
passes_maybe_string_interpolation = you might have meant to use string interpolation in this string literal
760+
passes_string_interpolation_only_works = string interpolation only works in `format!` invocations
761+
762+
passes_unused_variable_try_prefix = unused variable: `{$name}`
763+
.label = unused variable
764+
.suggestion = if this is intentional, prefix it with an underscore
765+
766+
passes_unused_variable_try_ignore = unused variable: `{$name}`
767+
.suggestion = try ignoring the field

compiler/rustc_passes/src/check_attr.rs

+10-22
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_session::lint::builtin::{
2929
};
3030
use rustc_session::parse::feature_err;
3131
use rustc_span::symbol::{kw, sym, Symbol};
32-
use rustc_span::{Span, DUMMY_SP};
32+
use rustc_span::{BytePos, Span, DUMMY_SP};
3333
use rustc_target::spec::abi::Abi;
3434
use rustc_trait_selection::infer::{TyCtxtInferExt, ValuePairs};
3535
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
@@ -927,30 +927,18 @@ impl CheckAttrVisitor<'_> {
927927
hir_id: HirId,
928928
) -> bool {
929929
if hir_id != CRATE_HIR_ID {
930-
self.tcx.struct_span_lint_hir(
930+
// insert a bang between `#` and `[...`
931+
let bang_span = attr.span.lo() + BytePos(1);
932+
let sugg = (attr.style == AttrStyle::Outer
933+
&& self.tcx.hir().get_parent_item(hir_id) == CRATE_OWNER_ID)
934+
.then_some(errors::AttrCrateLevelOnlySugg {
935+
attr: attr.span.with_lo(bang_span).with_hi(bang_span),
936+
});
937+
self.tcx.emit_spanned_lint(
931938
INVALID_DOC_ATTRIBUTES,
932939
hir_id,
933940
meta.span(),
934-
fluent::passes_attr_crate_level,
935-
|err| {
936-
if attr.style == AttrStyle::Outer
937-
&& self.tcx.hir().get_parent_item(hir_id) == CRATE_OWNER_ID
938-
{
939-
if let Ok(mut src) = self.tcx.sess.source_map().span_to_snippet(attr.span) {
940-
src.insert(1, '!');
941-
err.span_suggestion_verbose(
942-
attr.span,
943-
fluent::passes_suggestion,
944-
src,
945-
Applicability::MaybeIncorrect,
946-
);
947-
} else {
948-
err.span_help(attr.span, fluent::passes_help);
949-
}
950-
}
951-
err.note(fluent::passes_note);
952-
err
953-
},
941+
errors::AttrCrateLevelOnly { sugg },
954942
);
955943
return false;
956944
}

compiler/rustc_passes/src/errors.rs

+159-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use std::{
66
use crate::fluent_generated as fluent;
77
use rustc_ast::Label;
88
use rustc_errors::{
9-
error_code, Applicability, DiagnosticSymbolList, ErrorGuaranteed, IntoDiagnostic, MultiSpan,
9+
error_code, AddToDiagnostic, Applicability, Diagnostic, DiagnosticSymbolList, ErrorGuaranteed,
10+
IntoDiagnostic, MultiSpan,
1011
};
1112
use rustc_hir::{self as hir, ExprKind, Target};
1213
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
@@ -1555,3 +1556,160 @@ pub struct SkippingConstChecks {
15551556
#[primary_span]
15561557
pub span: Span,
15571558
}
1559+
1560+
#[derive(LintDiagnostic)]
1561+
#[diag(passes_unreachable_due_to_uninhabited)]
1562+
pub struct UnreachableDueToUninhabited<'desc, 'tcx> {
1563+
pub descr: &'desc str,
1564+
#[label]
1565+
pub expr: Span,
1566+
#[label(passes_label_orig)]
1567+
#[note]
1568+
pub orig: Span,
1569+
pub ty: Ty<'tcx>,
1570+
}
1571+
1572+
#[derive(LintDiagnostic)]
1573+
#[diag(passes_unused_var_maybe_capture_ref)]
1574+
#[help]
1575+
pub struct UnusedVarMaybeCaptureRef {
1576+
pub name: String,
1577+
}
1578+
1579+
#[derive(LintDiagnostic)]
1580+
#[diag(passes_unused_capture_maybe_capture_ref)]
1581+
#[help]
1582+
pub struct UnusedCaptureMaybeCaptureRef {
1583+
pub name: String,
1584+
}
1585+
1586+
#[derive(LintDiagnostic)]
1587+
#[diag(passes_unused_var_remove_field)]
1588+
pub struct UnusedVarRemoveField {
1589+
pub name: String,
1590+
#[subdiagnostic]
1591+
pub sugg: UnusedVarRemoveFieldSugg,
1592+
}
1593+
1594+
#[derive(Subdiagnostic)]
1595+
#[multipart_suggestion(
1596+
passes_unused_var_remove_field_suggestion,
1597+
applicability = "machine-applicable"
1598+
)]
1599+
pub struct UnusedVarRemoveFieldSugg {
1600+
#[suggestion_part(code = "")]
1601+
pub spans: Vec<Span>,
1602+
}
1603+
1604+
#[derive(LintDiagnostic)]
1605+
#[diag(passes_unused_var_assigned_only)]
1606+
#[note]
1607+
pub struct UnusedVarAssignedOnly {
1608+
pub name: String,
1609+
}
1610+
1611+
#[derive(LintDiagnostic)]
1612+
#[diag(passes_unnecessary_stable_feature)]
1613+
pub struct UnnecessaryStableFeature {
1614+
pub feature: Symbol,
1615+
pub since: Symbol,
1616+
}
1617+
1618+
#[derive(LintDiagnostic)]
1619+
#[diag(passes_unnecessary_partial_stable_feature)]
1620+
pub struct UnnecessaryPartialStableFeature {
1621+
#[suggestion(code = "{implies}", applicability = "maybe-incorrect")]
1622+
pub span: Span,
1623+
#[suggestion(passes_suggestion_remove, code = "", applicability = "maybe-incorrect")]
1624+
pub line: Span,
1625+
pub feature: Symbol,
1626+
pub since: Symbol,
1627+
pub implies: Symbol,
1628+
}
1629+
1630+
#[derive(LintDiagnostic)]
1631+
#[diag(passes_ineffective_unstable_impl)]
1632+
#[note]
1633+
pub struct IneffectiveUnstableImpl;
1634+
1635+
#[derive(LintDiagnostic)]
1636+
#[diag(passes_unused_assign)]
1637+
#[help]
1638+
pub struct UnusedAssign {
1639+
pub name: String,
1640+
}
1641+
1642+
#[derive(LintDiagnostic)]
1643+
#[diag(passes_unused_assign_passed)]
1644+
#[help]
1645+
pub struct UnusedAssignPassed {
1646+
pub name: String,
1647+
}
1648+
1649+
#[derive(LintDiagnostic)]
1650+
#[diag(passes_unused_variable_try_prefix)]
1651+
pub struct UnusedVariableTryPrefix {
1652+
#[label]
1653+
pub label: Option<Span>,
1654+
#[subdiagnostic]
1655+
pub string_interp: Vec<UnusedVariableStringInterp>,
1656+
#[subdiagnostic]
1657+
pub sugg: UnusedVariableTryPrefixSugg,
1658+
}
1659+
1660+
#[derive(Subdiagnostic)]
1661+
#[multipart_suggestion(passes_suggestion, applicability = "machine-applicable")]
1662+
pub struct UnusedVariableTryPrefixSugg {
1663+
#[suggestion_part(code = "_{name}")]
1664+
pub spans: Vec<Span>,
1665+
pub name: String,
1666+
}
1667+
1668+
pub struct UnusedVariableStringInterp {
1669+
pub lit: Span,
1670+
pub lo: Span,
1671+
pub hi: Span,
1672+
}
1673+
1674+
impl AddToDiagnostic for UnusedVariableStringInterp {
1675+
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F) {
1676+
diag.span_label(self.lit, crate::fluent_generated::passes_maybe_string_interpolation);
1677+
diag.multipart_suggestion(
1678+
crate::fluent_generated::passes_string_interpolation_only_works,
1679+
vec![(self.lo, String::from("format!(")), (self.hi, String::from(")"))],
1680+
Applicability::MachineApplicable,
1681+
);
1682+
}
1683+
}
1684+
1685+
#[derive(LintDiagnostic)]
1686+
#[diag(passes_unused_variable_try_ignore)]
1687+
pub struct UnusedVarTryIgnore {
1688+
#[subdiagnostic]
1689+
pub sugg: UnusedVarTryIgnoreSugg,
1690+
}
1691+
1692+
#[derive(Subdiagnostic)]
1693+
#[multipart_suggestion(passes_suggestion, applicability = "machine-applicable")]
1694+
pub struct UnusedVarTryIgnoreSugg {
1695+
#[suggestion_part(code = "{name}: _")]
1696+
pub shorthands: Vec<Span>,
1697+
#[suggestion_part(code = "_")]
1698+
pub non_shorthands: Vec<Span>,
1699+
pub name: String,
1700+
}
1701+
1702+
#[derive(LintDiagnostic)]
1703+
#[diag(passes_attr_crate_level)]
1704+
#[note]
1705+
pub struct AttrCrateLevelOnly {
1706+
#[subdiagnostic]
1707+
pub sugg: Option<AttrCrateLevelOnlySugg>,
1708+
}
1709+
1710+
#[derive(Subdiagnostic)]
1711+
#[suggestion(passes_suggestion, applicability = "maybe-incorrect", code = "!", style = "verbose")]
1712+
pub struct AttrCrateLevelOnlySugg {
1713+
#[primary_span]
1714+
pub attr: Span,
1715+
}

compiler/rustc_passes/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#![feature(min_specialization)]
1313
#![feature(try_blocks)]
1414
#![recursion_limit = "256"]
15+
#![deny(rustc::untranslatable_diagnostic)]
16+
#![deny(rustc::diagnostic_outside_of_impl)]
1517

1618
#[macro_use]
1719
extern crate rustc_middle;

0 commit comments

Comments
 (0)