Skip to content

Commit

Permalink
Migrate rustc_passes to translatable diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed Apr 26, 2023
1 parent 20d90b1 commit 6a41cfe
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 180 deletions.
43 changes: 42 additions & 1 deletion compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ passes_doc_attr_not_crate_level =
passes_attr_crate_level =
this attribute can only be applied at the crate level
.suggestion = to apply to the crate, use an inner attribute
.help = to apply to the crate, use an inner attribute
.note = read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#at-the-crate-level> for more information
passes_doc_test_unknown =
Expand Down Expand Up @@ -724,3 +723,45 @@ passes_skipping_const_checks = skipping const checks
passes_invalid_macro_export_arguments = `{$name}` isn't a valid `#[macro_export]` argument
passes_invalid_macro_export_arguments_too_many_items = `#[macro_export]` can only take 1 or 0 arguments
passes_unreachable_due_to_uninhabited = unreachable {$descr}
.label = unreachable {$descr}
.label_orig = any code following this expression is unreachable
.note = this expression has type `{$ty}`, which is uninhabited
passes_unused_var_maybe_capture_ref = unused variable: `{$name}`
.help = did you mean to capture by reference instead?
passes_unused_capture_maybe_capture_ref = value captured by `{$name}` is never read
.help = did you mean to capture by reference instead?
passes_unused_var_remove_field = unused variable: `{$name}`
passes_unused_var_remove_field_suggestion = try removing the field
passes_unused_var_assigned_only = variable `{$name}` is assigned to, but never used
.note = consider using `_{$name}` instead
passes_unnecessary_stable_feature = the feature `{$feature}` has been stable since {$since} and no longer requires an attribute to enable
passes_unnecessary_partial_stable_feature = the feature `{$feature}` has been partially stabilized since {$since} and is succeeded by the feature `{$implies}`
.suggestion = if you are using features which are still unstable, change to using `{$implies}`
.suggestion_remove = if you are using features which are now stable, remove this line
passes_ineffective_unstable_impl = an `#[unstable]` annotation here has no effect
.note = see issue #55436 <https://github.com/rust-lang/rust/issues/55436> for more information
passes_unused_assign = value assigned to `{$name}` is never read
.help = maybe it is overwritten before being read?
passes_unused_assign_passed = value passed to `{$name}` is never read
.help = maybe it is overwritten before being read?
passes_maybe_string_interpolation = you might have meant to use string interpolation in this string literal
passes_string_interpolation_only_works = string interpolation only works in `format!` invocations
passes_unused_variable_try_prefix = unused variable: `{$name}`
.label = unused variable
.suggestion = if this is intentional, prefix it with an underscore
passes_unused_variable_try_ignore = unused variable: `{$name}`
.suggestion = try ignoring the field
32 changes: 10 additions & 22 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use rustc_session::lint::builtin::{
};
use rustc_session::parse::feature_err;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::{Span, DUMMY_SP};
use rustc_span::{BytePos, Span, DUMMY_SP};
use rustc_target::spec::abi::Abi;
use rustc_trait_selection::infer::{TyCtxtInferExt, ValuePairs};
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
Expand Down Expand Up @@ -927,30 +927,18 @@ impl CheckAttrVisitor<'_> {
hir_id: HirId,
) -> bool {
if hir_id != CRATE_HIR_ID {
self.tcx.struct_span_lint_hir(
// insert a bang between `#` and `[...`
let bang_span = attr.span.lo() + BytePos(1);
let sugg = (attr.style == AttrStyle::Outer
&& self.tcx.hir().get_parent_item(hir_id) == CRATE_OWNER_ID)
.then_some(errors::AttrCrateLevelOnlySugg {
attr: attr.span.with_lo(bang_span).with_hi(bang_span),
});
self.tcx.emit_spanned_lint(
INVALID_DOC_ATTRIBUTES,
hir_id,
meta.span(),
fluent::passes_attr_crate_level,
|err| {
if attr.style == AttrStyle::Outer
&& self.tcx.hir().get_parent_item(hir_id) == CRATE_OWNER_ID
{
if let Ok(mut src) = self.tcx.sess.source_map().span_to_snippet(attr.span) {
src.insert(1, '!');
err.span_suggestion_verbose(
attr.span,
fluent::passes_suggestion,
src,
Applicability::MaybeIncorrect,
);
} else {
err.span_help(attr.span, fluent::passes_help);
}
}
err.note(fluent::passes_note);
err
},
errors::AttrCrateLevelOnly { sugg },
);
return false;
}
Expand Down
160 changes: 159 additions & 1 deletion compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use std::{
use crate::fluent_generated as fluent;
use rustc_ast::Label;
use rustc_errors::{
error_code, Applicability, DiagnosticSymbolList, ErrorGuaranteed, IntoDiagnostic, MultiSpan,
error_code, AddToDiagnostic, Applicability, Diagnostic, DiagnosticSymbolList, ErrorGuaranteed,
IntoDiagnostic, MultiSpan,
};
use rustc_hir::{self as hir, ExprKind, Target};
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
Expand Down Expand Up @@ -1555,3 +1556,160 @@ pub struct SkippingConstChecks {
#[primary_span]
pub span: Span,
}

#[derive(LintDiagnostic)]
#[diag(passes_unreachable_due_to_uninhabited)]
pub struct UnreachableDueToUninhabited<'desc, 'tcx> {
pub descr: &'desc str,
#[label]
pub expr: Span,
#[label(passes_label_orig)]
#[note]
pub orig: Span,
pub ty: Ty<'tcx>,
}

#[derive(LintDiagnostic)]
#[diag(passes_unused_var_maybe_capture_ref)]
#[help]
pub struct UnusedVarMaybeCaptureRef {
pub name: String,
}

#[derive(LintDiagnostic)]
#[diag(passes_unused_capture_maybe_capture_ref)]
#[help]
pub struct UnusedCaptureMaybeCaptureRef {
pub name: String,
}

#[derive(LintDiagnostic)]
#[diag(passes_unused_var_remove_field)]
pub struct UnusedVarRemoveField {
pub name: String,
#[subdiagnostic]
pub sugg: UnusedVarRemoveFieldSugg,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(
passes_unused_var_remove_field_suggestion,
applicability = "machine-applicable"
)]
pub struct UnusedVarRemoveFieldSugg {
#[suggestion_part(code = "")]
pub spans: Vec<Span>,
}

#[derive(LintDiagnostic)]
#[diag(passes_unused_var_assigned_only)]
#[note]
pub struct UnusedVarAssignedOnly {
pub name: String,
}

#[derive(LintDiagnostic)]
#[diag(passes_unnecessary_stable_feature)]
pub struct UnnecessaryStableFeature {
pub feature: Symbol,
pub since: Symbol,
}

#[derive(LintDiagnostic)]
#[diag(passes_unnecessary_partial_stable_feature)]
pub struct UnnecessaryPartialStableFeature {
#[suggestion(code = "{implies}", applicability = "maybe-incorrect")]
pub span: Span,
#[suggestion(passes_suggestion_remove, code = "", applicability = "maybe-incorrect")]
pub line: Span,
pub feature: Symbol,
pub since: Symbol,
pub implies: Symbol,
}

#[derive(LintDiagnostic)]
#[diag(passes_ineffective_unstable_impl)]
#[note]
pub struct IneffectiveUnstableImpl;

#[derive(LintDiagnostic)]
#[diag(passes_unused_assign)]
#[help]
pub struct UnusedAssign {
pub name: String,
}

#[derive(LintDiagnostic)]
#[diag(passes_unused_assign_passed)]
#[help]
pub struct UnusedAssignPassed {
pub name: String,
}

#[derive(LintDiagnostic)]
#[diag(passes_unused_variable_try_prefix)]
pub struct UnusedVariableTryPrefix {
#[label]
pub label: Option<Span>,
#[subdiagnostic]
pub string_interp: Vec<UnusedVariableStringInterp>,
#[subdiagnostic]
pub sugg: UnusedVariableTryPrefixSugg,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(passes_suggestion, applicability = "machine-applicable")]
pub struct UnusedVariableTryPrefixSugg {
#[suggestion_part(code = "_{name}")]
pub spans: Vec<Span>,
pub name: String,
}

pub struct UnusedVariableStringInterp {
pub lit: Span,
pub lo: Span,
pub hi: Span,
}

impl AddToDiagnostic for UnusedVariableStringInterp {
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F) {
diag.span_label(self.lit, crate::fluent_generated::passes_maybe_string_interpolation);
diag.multipart_suggestion(
crate::fluent_generated::passes_string_interpolation_only_works,
vec![(self.lo, String::from("format!(")), (self.hi, String::from(")"))],
Applicability::MachineApplicable,
);
}
}

#[derive(LintDiagnostic)]
#[diag(passes_unused_variable_try_ignore)]
pub struct UnusedVarTryIgnore {
#[subdiagnostic]
pub sugg: UnusedVarTryIgnoreSugg,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(passes_suggestion, applicability = "machine-applicable")]
pub struct UnusedVarTryIgnoreSugg {
#[suggestion_part(code = "{name}: _")]
pub shorthands: Vec<Span>,
#[suggestion_part(code = "_")]
pub non_shorthands: Vec<Span>,
pub name: String,
}

#[derive(LintDiagnostic)]
#[diag(passes_attr_crate_level)]
#[note]
pub struct AttrCrateLevelOnly {
#[subdiagnostic]
pub sugg: Option<AttrCrateLevelOnlySugg>,
}

#[derive(Subdiagnostic)]
#[suggestion(passes_suggestion, applicability = "maybe-incorrect", code = "!", style = "verbose")]
pub struct AttrCrateLevelOnlySugg {
#[primary_span]
pub attr: Span,
}
2 changes: 2 additions & 0 deletions compiler/rustc_passes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#![feature(min_specialization)]
#![feature(try_blocks)]
#![recursion_limit = "256"]
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]

#[macro_use]
extern crate rustc_middle;
Expand Down
Loading

0 comments on commit 6a41cfe

Please sign in to comment.