Skip to content

Commit 389dda1

Browse files
Rollup merge of #100776 - Rejyr:diagnostic-migration-rustc-lint, r=davidtwco
Migrate `rustc_lint` errors to `SessionDiagnostic` Draft PR for migrating `rustc_lint` to `SessionDiagnostic`, as part of the [recent blog post](https://blog.rust-lang.org/inside-rust/2022/08/16/diagnostic-effort.html)
2 parents 93b2acd + 1693993 commit 389dda1

File tree

6 files changed

+297
-124
lines changed

6 files changed

+297
-124
lines changed

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

+34
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,37 @@ lint_builtin_deref_nullptr = dereferencing a null pointer
393393
.label = this code causes undefined behavior when executed
394394
395395
lint_builtin_asm_labels = avoid using named labels in inline assembly
396+
397+
lint_overruled_attribute = {$lint_level}({$lint_source}) incompatible with previous forbid
398+
.label = overruled by previous forbid
399+
400+
lint_default_source = `forbid` lint level is the default for {$id}
401+
402+
lint_node_source = `forbid` level set here
403+
.note = {$reason}
404+
405+
lint_command_line_source = `forbid` lint level was set on command line
406+
407+
lint_malformed_attribute = malformed lint attribute input
408+
409+
lint_bad_attribute_argument = bad attribute argument
410+
411+
lint_reason_must_be_string_literal = reason must be a string literal
412+
413+
lint_reason_must_come_last = reason in lint attribute must come last
414+
415+
lint_unknown_tool_in_scoped_lint = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}`
416+
.help = add `#![register_tool({$tool_name})]` to the crate root
417+
418+
lint_unsupported_group = `{$lint_group}` lint group is not supported with ´--force-warn´
419+
420+
lint_requested_level = requested on the command line with `{$level} {$lint_name}`
421+
422+
lint_check_name_unknown = unknown lint: `{$lint_name}`
423+
.help = did you mean: `{$suggestion}`
424+
425+
lint_check_name_unknown_tool = unknown lint tool: `{$tool_name}`
426+
427+
lint_check_name_warning = {$msg}
428+
429+
lint_check_name_deprecated = lint name `{$lint_name}` is deprecated and does not have an effect anymore. Use: {$new_name}

compiler/rustc_lint/src/builtin.rs

+10-23
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//! `late_lint_methods!` invocation in `lib.rs`.
2222
2323
use crate::{
24+
errors::BuiltinEllpisisInclusiveRangePatterns,
2425
types::{transparent_newtype_field, CItemKind},
2526
EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext,
2627
};
@@ -1760,18 +1761,11 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
17601761
None => format!("&(..={})", end),
17611762
};
17621763
if join.edition() >= Edition::Edition2021 {
1763-
let mut err = cx.sess().struct_span_err_with_code(
1764-
pat.span,
1765-
msg,
1766-
rustc_errors::error_code!(E0783),
1767-
);
1768-
err.span_suggestion(
1769-
pat.span,
1770-
suggestion,
1764+
cx.sess().emit_err(BuiltinEllpisisInclusiveRangePatterns {
1765+
span: pat.span,
1766+
suggestion: pat.span,
17711767
replace,
1772-
Applicability::MachineApplicable,
1773-
)
1774-
.emit();
1768+
});
17751769
} else {
17761770
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, pat.span, |lint| {
17771771
lint.build(msg)
@@ -1787,18 +1781,11 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
17871781
} else {
17881782
let replace = "..=";
17891783
if join.edition() >= Edition::Edition2021 {
1790-
let mut err = cx.sess().struct_span_err_with_code(
1791-
pat.span,
1792-
msg,
1793-
rustc_errors::error_code!(E0783),
1794-
);
1795-
err.span_suggestion_short(
1796-
join,
1797-
suggestion,
1798-
replace,
1799-
Applicability::MachineApplicable,
1800-
)
1801-
.emit();
1784+
cx.sess().emit_err(BuiltinEllpisisInclusiveRangePatterns {
1785+
span: pat.span,
1786+
suggestion: join,
1787+
replace: replace.to_string(),
1788+
});
18021789
} else {
18031790
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, join, |lint| {
18041791
lint.build(msg)

compiler/rustc_lint/src/context.rs

+35-58
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
1717
use self::TargetLint::*;
1818

19+
use crate::errors::{
20+
CheckNameDeprecated, CheckNameUnknown, CheckNameUnknownTool, CheckNameWarning, RequestedLevel,
21+
UnsupportedGroup,
22+
};
1923
use crate::levels::LintLevelsBuilder;
2024
use crate::passes::{EarlyLintPassObject, LateLintPassObject};
2125
use rustc_ast::util::unicode::TEXT_FLOW_CONTROL_CHARS;
2226
use rustc_data_structures::fx::FxHashMap;
2327
use rustc_data_structures::sync;
24-
use rustc_errors::{add_elided_lifetime_in_path_suggestion, struct_span_err};
28+
use rustc_errors::add_elided_lifetime_in_path_suggestion;
2529
use rustc_errors::{
2630
Applicability, DecorateLint, LintDiagnosticBuilder, MultiSpan, SuggestionStyle,
2731
};
@@ -39,7 +43,7 @@ use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintI
3943
use rustc_session::Session;
4044
use rustc_span::lev_distance::find_best_match_for_name;
4145
use rustc_span::symbol::{sym, Ident, Symbol};
42-
use rustc_span::{BytePos, Span, DUMMY_SP};
46+
use rustc_span::{BytePos, Span};
4347
use rustc_target::abi;
4448
use tracing::debug;
4549

@@ -326,68 +330,41 @@ impl LintStore {
326330
) {
327331
let (tool_name, lint_name_only) = parse_lint_and_tool_name(lint_name);
328332
if lint_name_only == crate::WARNINGS.name_lower() && matches!(level, Level::ForceWarn(_)) {
329-
struct_span_err!(
330-
sess,
331-
DUMMY_SP,
332-
E0602,
333-
"`{}` lint group is not supported with ´--force-warn´",
334-
crate::WARNINGS.name_lower()
335-
)
336-
.emit();
333+
sess.emit_err(UnsupportedGroup { lint_group: crate::WARNINGS.name_lower() });
337334
return;
338335
}
339-
let db = match self.check_lint_name(lint_name_only, tool_name, registered_tools) {
340-
CheckLintNameResult::Ok(_) => None,
341-
CheckLintNameResult::Warning(ref msg, _) => Some(sess.struct_warn(msg)),
336+
let lint_name = lint_name.to_string();
337+
match self.check_lint_name(lint_name_only, tool_name, registered_tools) {
338+
CheckLintNameResult::Warning(msg, _) => {
339+
sess.emit_warning(CheckNameWarning {
340+
msg,
341+
sub: RequestedLevel { level, lint_name },
342+
});
343+
}
342344
CheckLintNameResult::NoLint(suggestion) => {
343-
let mut err =
344-
struct_span_err!(sess, DUMMY_SP, E0602, "unknown lint: `{}`", lint_name);
345-
346-
if let Some(suggestion) = suggestion {
347-
err.help(&format!("did you mean: `{}`", suggestion));
345+
sess.emit_err(CheckNameUnknown {
346+
lint_name: lint_name.clone(),
347+
suggestion,
348+
sub: RequestedLevel { level, lint_name },
349+
});
350+
}
351+
CheckLintNameResult::Tool(result) => {
352+
if let Err((Some(_), new_name)) = result {
353+
sess.emit_warning(CheckNameDeprecated {
354+
lint_name: lint_name.clone(),
355+
new_name,
356+
sub: RequestedLevel { level, lint_name },
357+
});
348358
}
349-
350-
Some(err.forget_guarantee())
351359
}
352-
CheckLintNameResult::Tool(result) => match result {
353-
Err((Some(_), new_name)) => Some(sess.struct_warn(&format!(
354-
"lint name `{}` is deprecated \
355-
and does not have an effect anymore. \
356-
Use: {}",
357-
lint_name, new_name
358-
))),
359-
_ => None,
360-
},
361-
CheckLintNameResult::NoTool => Some(
362-
struct_span_err!(
363-
sess,
364-
DUMMY_SP,
365-
E0602,
366-
"unknown lint tool: `{}`",
367-
tool_name.unwrap()
368-
)
369-
.forget_guarantee(),
370-
),
360+
CheckLintNameResult::NoTool => {
361+
sess.emit_err(CheckNameUnknownTool {
362+
tool_name: tool_name.unwrap(),
363+
sub: RequestedLevel { level, lint_name },
364+
});
365+
}
366+
_ => {}
371367
};
372-
373-
if let Some(mut db) = db {
374-
let msg = format!(
375-
"requested on the command line with `{} {}`",
376-
match level {
377-
Level::Allow => "-A",
378-
Level::Warn => "-W",
379-
Level::ForceWarn(_) => "--force-warn",
380-
Level::Deny => "-D",
381-
Level::Forbid => "-F",
382-
Level::Expect(_) => {
383-
unreachable!("lints with the level of `expect` should not run this code");
384-
}
385-
},
386-
lint_name
387-
);
388-
db.note(&msg);
389-
db.emit();
390-
}
391368
}
392369

393370
/// True if this symbol represents a lint group name.

compiler/rustc_lint/src/errors.rs

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
use rustc_errors::{fluent, AddSubdiagnostic, ErrorGuaranteed};
2+
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
3+
use rustc_session::{lint::Level, parse::ParseSess, SessionDiagnostic};
4+
use rustc_span::{Span, Symbol};
5+
6+
#[derive(SessionDiagnostic)]
7+
#[diag(lint::overruled_attribute, code = "E0453")]
8+
pub struct OverruledAttribute {
9+
#[primary_span]
10+
pub span: Span,
11+
#[label]
12+
pub overruled: Span,
13+
pub lint_level: String,
14+
pub lint_source: Symbol,
15+
#[subdiagnostic]
16+
pub sub: OverruledAttributeSub,
17+
}
18+
//
19+
pub enum OverruledAttributeSub {
20+
DefaultSource { id: String },
21+
NodeSource { span: Span, reason: Option<Symbol> },
22+
CommandLineSource,
23+
}
24+
25+
impl AddSubdiagnostic for OverruledAttributeSub {
26+
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
27+
match self {
28+
OverruledAttributeSub::DefaultSource { id } => {
29+
diag.note(fluent::lint::default_source);
30+
diag.set_arg("id", id);
31+
}
32+
OverruledAttributeSub::NodeSource { span, reason } => {
33+
diag.span_label(span, fluent::lint::node_source);
34+
if let Some(rationale) = reason {
35+
diag.note(rationale.as_str());
36+
}
37+
}
38+
OverruledAttributeSub::CommandLineSource => {
39+
diag.note(fluent::lint::command_line_source);
40+
}
41+
}
42+
}
43+
}
44+
45+
#[derive(SessionDiagnostic)]
46+
#[diag(lint::malformed_attribute, code = "E0452")]
47+
pub struct MalformedAttribute {
48+
#[primary_span]
49+
pub span: Span,
50+
#[subdiagnostic]
51+
pub sub: MalformedAttributeSub,
52+
}
53+
54+
#[derive(SessionSubdiagnostic)]
55+
pub enum MalformedAttributeSub {
56+
#[label(lint::bad_attribute_argument)]
57+
BadAttributeArgument(#[primary_span] Span),
58+
#[label(lint::reason_must_be_string_literal)]
59+
ReasonMustBeStringLiteral(#[primary_span] Span),
60+
#[label(lint::reason_must_come_last)]
61+
ReasonMustComeLast(#[primary_span] Span),
62+
}
63+
64+
#[derive(SessionDiagnostic)]
65+
#[diag(lint::unknown_tool_in_scoped_lint, code = "E0710")]
66+
pub struct UnknownToolInScopedLint {
67+
#[primary_span]
68+
pub span: Option<Span>,
69+
pub tool_name: Symbol,
70+
pub lint_name: String,
71+
#[help]
72+
pub is_nightly_build: Option<()>,
73+
}
74+
75+
#[derive(SessionDiagnostic)]
76+
#[diag(lint::builtin_ellipsis_inclusive_range_patterns, code = "E0783")]
77+
pub struct BuiltinEllpisisInclusiveRangePatterns {
78+
#[primary_span]
79+
pub span: Span,
80+
#[suggestion_short(code = "{replace}", applicability = "machine-applicable")]
81+
pub suggestion: Span,
82+
pub replace: String,
83+
}
84+
85+
pub struct RequestedLevel {
86+
pub level: Level,
87+
pub lint_name: String,
88+
}
89+
90+
impl AddSubdiagnostic for RequestedLevel {
91+
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
92+
diag.note(fluent::lint::requested_level);
93+
diag.set_arg(
94+
"level",
95+
match self.level {
96+
Level::Allow => "-A",
97+
Level::Warn => "-W",
98+
Level::ForceWarn(_) => "--force-warn",
99+
Level::Deny => "-D",
100+
Level::Forbid => "-F",
101+
Level::Expect(_) => {
102+
unreachable!("lints with the level of `expect` should not run this code");
103+
}
104+
},
105+
);
106+
diag.set_arg("lint_name", self.lint_name);
107+
}
108+
}
109+
110+
#[derive(SessionDiagnostic)]
111+
#[diag(lint::unsupported_group, code = "E0602")]
112+
pub struct UnsupportedGroup {
113+
pub lint_group: String,
114+
}
115+
116+
pub struct CheckNameUnknown {
117+
pub lint_name: String,
118+
pub suggestion: Option<Symbol>,
119+
pub sub: RequestedLevel,
120+
}
121+
122+
impl SessionDiagnostic<'_> for CheckNameUnknown {
123+
fn into_diagnostic(
124+
self,
125+
sess: &ParseSess,
126+
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
127+
let mut diag = sess.struct_err(fluent::lint::check_name_unknown);
128+
diag.code(rustc_errors::error_code!(E0602));
129+
if let Some(suggestion) = self.suggestion {
130+
diag.help(fluent::lint::help);
131+
diag.set_arg("suggestion", suggestion);
132+
}
133+
diag.set_arg("lint_name", self.lint_name);
134+
diag.subdiagnostic(self.sub);
135+
diag
136+
}
137+
}
138+
139+
#[derive(SessionDiagnostic)]
140+
#[diag(lint::check_name_unknown_tool, code = "E0602")]
141+
pub struct CheckNameUnknownTool {
142+
pub tool_name: Symbol,
143+
#[subdiagnostic]
144+
pub sub: RequestedLevel,
145+
}
146+
147+
#[derive(SessionDiagnostic)]
148+
#[diag(lint::check_name_warning)]
149+
pub struct CheckNameWarning {
150+
pub msg: String,
151+
#[subdiagnostic]
152+
pub sub: RequestedLevel,
153+
}
154+
155+
#[derive(SessionDiagnostic)]
156+
#[diag(lint::check_name_deprecated)]
157+
pub struct CheckNameDeprecated {
158+
pub lint_name: String,
159+
pub new_name: String,
160+
#[subdiagnostic]
161+
pub sub: RequestedLevel,
162+
}

0 commit comments

Comments
 (0)