Skip to content

Commit 80df25e

Browse files
committed
migrate: levels.rs
1 parent ab66ea6 commit 80df25e

File tree

4 files changed

+128
-107
lines changed

4 files changed

+128
-107
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ lint_deprecated_lint_name =
2020
lint name `{$name}` is deprecated and may not have an effect in the future.
2121
.suggestion = change it to
2222
23+
lint_renamed_or_removed_lint_suggestion = use the new name
24+
25+
lint_unknown_lint =
26+
unknown lint: `{$name}`
27+
.suggestion = did you mean
28+
29+
lint_ignored_unless_crate_specified = {$level}({$name}) is ignored unless specified at crate level
30+
31+
lint_unknown_gated_lint =
32+
unknown lint: `{$name}`
33+
.note = the `{$name}` lint is unstable
34+
2335
lint_hidden_unicode_codepoints = unicode codepoint changing visible direction of text present in {$label}
2436
.label = this {$label} contains {$count ->
2537
[one] an invisible

compiler/rustc_lint/src/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ use rustc_span::{Span, Symbol};
1010

1111
#[derive(Diagnostic)]
1212
#[diag(lint_overruled_attribute, code = "E0453")]
13-
pub struct OverruledAttribute {
13+
pub struct OverruledAttribute<'a> {
1414
#[primary_span]
1515
pub span: Span,
1616
#[label]
1717
pub overruled: Span,
18-
pub lint_level: String,
18+
pub lint_level: &'a str,
1919
pub lint_source: Symbol,
2020
#[subdiagnostic]
2121
pub sub: OverruledAttributeSub,

compiler/rustc_lint/src/levels.rs

Lines changed: 39 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
13
use crate::context::{CheckLintNameResult, LintStore};
24
use crate::late::unerased_lint_store;
3-
use crate::lints::DeprecatedLintName;
5+
use crate::lints::{
6+
DeprecatedLintName, IgnoredUnlessCrateSpecified, OverruledAtributeLint, RenamedOrRemovedLint,
7+
UnknownLint,
8+
};
49
use rustc_ast as ast;
510
use rustc_ast_pretty::pprust;
611
use rustc_data_structures::fx::FxHashMap;
7-
use rustc_errors::{
8-
Applicability, DecorateLint, Diagnostic, DiagnosticBuilder, DiagnosticMessage, MultiSpan,
9-
};
12+
use rustc_errors::{fluent, DecorateLint, DiagnosticBuilder, DiagnosticMessage, MultiSpan};
1013
use rustc_hir as hir;
1114
use rustc_hir::intravisit::{self, Visitor};
1215
use rustc_hir::HirId;
@@ -18,6 +21,7 @@ use rustc_middle::lint::{
1821
};
1922
use rustc_middle::ty::query::Providers;
2023
use rustc_middle::ty::{RegisteredTools, TyCtxt};
24+
use rustc_session::lint::builtin::{RENAMED_AND_REMOVED_LINTS, UNKNOWN_LINTS, UNUSED_ATTRIBUTES};
2125
use rustc_session::lint::{
2226
builtin::{self, FORBIDDEN_LINT_GROUPS, SINGLE_USE_LIFETIMES, UNFULFILLED_LINT_EXPECTATIONS},
2327
Level, Lint, LintExpectationId, LintId,
@@ -586,57 +590,32 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
586590
old_src,
587591
id_name
588592
);
589-
590-
let decorate_diag = |diag: &mut Diagnostic| {
591-
diag.span_label(src.span(), "overruled by previous forbid");
592-
match old_src {
593-
LintLevelSource::Default => {
594-
diag.note(&format!(
595-
"`forbid` lint level is the default for {}",
596-
id.to_string()
597-
));
598-
}
599-
LintLevelSource::Node { span, reason, .. } => {
600-
diag.span_label(span, "`forbid` level set here");
601-
if let Some(rationale) = reason {
602-
diag.note(rationale.as_str());
603-
}
604-
}
605-
LintLevelSource::CommandLine(_, _) => {
606-
diag.note("`forbid` lint level was set on command line");
607-
}
593+
let sub = match old_src {
594+
LintLevelSource::Default => {
595+
OverruledAttributeSub::DefaultSource { id: id.to_string() }
596+
}
597+
LintLevelSource::Node { span, reason, .. } => {
598+
OverruledAttributeSub::NodeSource { span, reason }
608599
}
600+
LintLevelSource::CommandLine(_, _) => OverruledAttributeSub::CommandLineSource,
609601
};
610602
if !fcw_warning {
611603
self.sess.emit_err(OverruledAttribute {
612604
span: src.span(),
613605
overruled: src.span(),
614-
lint_level: level.as_str().to_string(),
606+
lint_level: level.as_str(),
615607
lint_source: src.name(),
616-
sub: match old_src {
617-
LintLevelSource::Default => {
618-
OverruledAttributeSub::DefaultSource { id: id.to_string() }
619-
}
620-
LintLevelSource::Node { span, reason, .. } => {
621-
OverruledAttributeSub::NodeSource { span, reason }
622-
}
623-
LintLevelSource::CommandLine(_, _) => {
624-
OverruledAttributeSub::CommandLineSource
625-
}
626-
},
608+
sub,
627609
});
628610
} else {
629-
self.struct_lint(
611+
self.emit_spanned_lint(
630612
FORBIDDEN_LINT_GROUPS,
631-
Some(src.span().into()),
632-
format!(
633-
"{}({}) incompatible with previous forbid",
634-
level.as_str(),
635-
src.name(),
636-
),
637-
|lint| {
638-
decorate_diag(lint);
639-
lint
613+
src.span().into(),
614+
OverruledAtributeLint {
615+
overruled: src.span(),
616+
lint_level: level.as_str(),
617+
lint_source: src.name(),
618+
sub,
640619
},
641620
);
642621
}
@@ -908,54 +887,22 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
908887
_ if !self.warn_about_weird_lints => {}
909888

910889
CheckLintNameResult::Warning(msg, renamed) => {
911-
let lint = builtin::RENAMED_AND_REMOVED_LINTS;
912-
let (renamed_lint_level, src) = self.provider.get_lint_level(lint, &sess);
913-
struct_lint_level(
914-
self.sess,
915-
lint,
916-
renamed_lint_level,
917-
src,
918-
Some(sp.into()),
919-
msg,
920-
|lint| {
921-
if let Some(new_name) = &renamed {
922-
lint.span_suggestion(
923-
sp,
924-
"use the new name",
925-
new_name,
926-
Applicability::MachineApplicable,
927-
);
928-
}
929-
lint
930-
},
890+
self.emit_spanned_lint(
891+
RENAMED_AND_REMOVED_LINTS,
892+
sp.into(),
893+
RenamedOrRemovedLint { msg, suggestion: sp, renamed },
931894
);
932895
}
933896
CheckLintNameResult::NoLint(suggestion) => {
934-
let lint = builtin::UNKNOWN_LINTS;
935-
let (level, src) = self.provider.get_lint_level(lint, self.sess);
936897
let name = if let Some(tool_ident) = tool_ident {
937898
format!("{}::{}", tool_ident.name, name)
938899
} else {
939900
name.to_string()
940901
};
941-
struct_lint_level(
942-
self.sess,
943-
lint,
944-
level,
945-
src,
946-
Some(sp.into()),
947-
format!("unknown lint: `{}`", name),
948-
|lint| {
949-
if let Some(suggestion) = suggestion {
950-
lint.span_suggestion(
951-
sp,
952-
"did you mean",
953-
suggestion,
954-
Applicability::MaybeIncorrect,
955-
);
956-
}
957-
lint
958-
},
902+
self.emit_spanned_lint(
903+
UNKNOWN_LINTS,
904+
sp.into(),
905+
UnknownLint { name, suggestion: sp, replace: suggestion },
959906
);
960907
}
961908
}
@@ -1001,20 +948,10 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
1001948
continue
1002949
};
1003950

1004-
let lint = builtin::UNUSED_ATTRIBUTES;
1005-
let (lint_level, lint_src) = self.provider.get_lint_level(lint, &self.sess);
1006-
struct_lint_level(
1007-
self.sess,
1008-
lint,
1009-
lint_level,
1010-
lint_src,
1011-
Some(lint_attr_span.into()),
1012-
format!(
1013-
"{}({}) is ignored unless specified at crate level",
1014-
level.as_str(),
1015-
lint_attr_name
1016-
),
1017-
|lint| lint,
951+
self.emit_spanned_lint(
952+
UNUSED_ATTRIBUTES,
953+
lint_attr_span.into(),
954+
IgnoredUnlessCrateSpecified { level: level.as_str(), name: lint_attr_name },
1018955
);
1019956
// don't set a separate error for every lint in the group
1020957
break;
@@ -1038,11 +975,10 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
1038975
level,
1039976
src,
1040977
Some(span.into()),
1041-
format!("unknown lint: `{}`", lint_id.lint.name_lower()),
978+
fluent::lint_unknown_gated_lint,
1042979
|lint| {
1043-
lint.note(
1044-
&format!("the `{}` lint is unstable", lint_id.lint.name_lower(),),
1045-
);
980+
lint.set_arg("name", lint_id.lint.name_lower());
981+
lint.note(fluent::note);
1046982
add_feature_diagnostics(lint, &self.sess.parse_sess, feature);
1047983
lint
1048984
},

compiler/rustc_lint/src/lints.rs

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_macros::{LintDiagnostic, Subdiagnostic};
44
use rustc_middle::ty::{Predicate, Ty, TyCtxt};
55
use rustc_span::{symbol::Ident, Span, Symbol};
66

7-
use crate::LateContext;
7+
use crate::{errors::OverruledAttributeSub, LateContext};
88

99
// array_into_iter.rs
1010
#[derive(LintDiagnostic)]
@@ -51,14 +51,87 @@ pub struct EnumIntrinsicsMemVariant<'a> {
5151

5252
// levels.rs
5353
#[derive(LintDiagnostic)]
54-
#[diag(lint::deprecated_lint_name)]
54+
#[diag(lint_overruled_attribute)]
55+
pub struct OverruledAtributeLint<'a> {
56+
#[label]
57+
pub overruled: Span,
58+
pub lint_level: &'a str,
59+
pub lint_source: Symbol,
60+
#[subdiagnostic]
61+
pub sub: OverruledAttributeSub,
62+
}
63+
64+
#[derive(LintDiagnostic)]
65+
#[diag(lint_deprecated_lint_name)]
5566
pub struct DeprecatedLintName<'a> {
5667
pub name: String,
5768
#[suggestion(code = "{replace}", applicability = "machine-applicable")]
5869
pub suggestion: Span,
5970
pub replace: &'a str,
6071
}
6172

73+
pub struct RenamedOrRemovedLint<'a> {
74+
pub msg: &'a str,
75+
pub suggestion: Span,
76+
pub renamed: &'a Option<String>,
77+
}
78+
79+
impl<'a> DecorateLint<'a, ()> for RenamedOrRemovedLint<'_> {
80+
fn decorate_lint<'b>(
81+
self,
82+
diag: &'b mut rustc_errors::DiagnosticBuilder<'a, ()>,
83+
) -> &'b mut rustc_errors::DiagnosticBuilder<'a, ()> {
84+
if let Some(new_name) = self.renamed {
85+
diag.span_suggestion(
86+
self.suggestion,
87+
fluent::lint_renamed_or_removed_lint_suggestion,
88+
new_name,
89+
Applicability::MachineApplicable,
90+
);
91+
};
92+
diag
93+
}
94+
95+
fn msg(&self) -> rustc_errors::DiagnosticMessage {
96+
rustc_errors::DiagnosticMessage::Str(self.msg.to_string())
97+
}
98+
}
99+
100+
pub struct UnknownLint<'a> {
101+
pub name: String,
102+
pub suggestion: Span,
103+
pub replace: &'a Option<Symbol>,
104+
}
105+
106+
impl<'a> DecorateLint<'a, ()> for UnknownLint<'_> {
107+
fn decorate_lint<'b>(
108+
self,
109+
diag: &'b mut rustc_errors::DiagnosticBuilder<'a, ()>,
110+
) -> &'b mut rustc_errors::DiagnosticBuilder<'a, ()> {
111+
diag.set_arg("name", self.name);
112+
if let Some(replace) = self.replace {
113+
diag.span_suggestion(
114+
self.suggestion,
115+
fluent::suggestion,
116+
replace,
117+
Applicability::MaybeIncorrect,
118+
);
119+
};
120+
diag
121+
}
122+
123+
fn msg(&self) -> rustc_errors::DiagnosticMessage {
124+
fluent::lint_unknown_lint
125+
}
126+
}
127+
128+
#[derive(LintDiagnostic)]
129+
#[diag(lint_ignored_unless_crate_specified)]
130+
pub struct IgnoredUnlessCrateSpecified<'a> {
131+
pub level: &'a str,
132+
pub name: Symbol,
133+
}
134+
62135
// methods.rs
63136
#[derive(LintDiagnostic)]
64137
#[diag(lint_cstring_ptr)]

0 commit comments

Comments
 (0)