Skip to content

Commit 7a6ae23

Browse files
committed
migrate: OverruledAttribute
1 parent 32e445a commit 7a6ae23

File tree

3 files changed

+75
-12
lines changed

3 files changed

+75
-12
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,16 @@ lint_builtin_deref_nullptr = dereferencing a null pointer
394394
395395
lint_builtin_asm_labels = avoid using named labels in inline assembly
396396
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+
397407
lint_malformed_attribute = malformed lint attribute input
398408
399409
lint_bad_attribute_argument = bad attribute argument

compiler/rustc_lint/src/errors.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,46 @@
1+
use rustc_errors::{fluent, AddSubdiagnostic};
12
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
23
use rustc_span::{Span, Symbol};
34

5+
#[derive(SessionDiagnostic)]
6+
#[error(lint::overruled_attribute, code = "E0453")]
7+
pub struct OverruledAttribute {
8+
#[primary_span]
9+
pub span: Span,
10+
#[label]
11+
pub overruled: Span,
12+
pub lint_level: String,
13+
pub lint_source: Symbol,
14+
#[subdiagnostic]
15+
pub sub: OverruledAttributeSub,
16+
}
17+
//
18+
pub enum OverruledAttributeSub {
19+
DefaultSource { id: String },
20+
NodeSource { span: Span, reason: Option<Symbol> },
21+
CommandLineSource,
22+
}
23+
24+
impl AddSubdiagnostic for OverruledAttributeSub {
25+
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
26+
match self {
27+
OverruledAttributeSub::DefaultSource { id } => {
28+
diag.note(fluent::lint::default_source);
29+
diag.set_arg("id", id);
30+
}
31+
OverruledAttributeSub::NodeSource { span, reason } => {
32+
diag.span_label(span, fluent::lint::node_source);
33+
if let Some(rationale) = reason {
34+
diag.note(rationale.as_str());
35+
}
36+
}
37+
OverruledAttributeSub::CommandLineSource => {
38+
diag.note(fluent::lint::command_line_source);
39+
}
40+
}
41+
}
42+
}
43+
444
#[derive(SessionDiagnostic)]
545
#[error(lint::malformed_attribute, code = "E0452")]
646
pub struct MalformedAttribute {

compiler/rustc_lint/src/levels.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::late::unerased_lint_store;
66
use rustc_ast as ast;
77
use rustc_ast_pretty::pprust;
88
use rustc_data_structures::fx::FxHashMap;
9-
use rustc_errors::{struct_span_err, Applicability, Diagnostic, LintDiagnosticBuilder, MultiSpan};
9+
use rustc_errors::{Applicability, Diagnostic, LintDiagnosticBuilder, MultiSpan};
1010
use rustc_hir as hir;
1111
use rustc_hir::{intravisit, HirId};
1212
use rustc_middle::hir::nested_filter;
@@ -26,7 +26,10 @@ use rustc_span::symbol::{sym, Symbol};
2626
use rustc_span::{Span, DUMMY_SP};
2727
use tracing::debug;
2828

29-
use crate::errors::{MalformedAttribute, MalformedAttributeSub, UnknownTool};
29+
use crate::errors::{
30+
MalformedAttribute, MalformedAttributeSub, OverruledAttribute, OverruledAttributeSub,
31+
UnknownTool,
32+
};
3033

3134
fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap {
3235
let store = unerased_lint_store(tcx);
@@ -191,16 +194,26 @@ impl<'s> LintLevelsBuilder<'s> {
191194
}
192195
};
193196
if !fcw_warning {
194-
let mut diag_builder = struct_span_err!(
195-
self.sess,
196-
src.span(),
197-
E0453,
198-
"{}({}) incompatible with previous forbid",
199-
level.as_str(),
200-
src.name(),
201-
);
202-
decorate_diag(&mut diag_builder);
203-
diag_builder.emit();
197+
self.sess.emit_err(OverruledAttribute {
198+
span: src.span(),
199+
overruled: src.span(),
200+
lint_level: level.as_str().to_string(),
201+
lint_source: src.name(),
202+
sub: match old_src {
203+
LintLevelSource::Default => {
204+
OverruledAttributeSub::DefaultSource { id: id.to_string() }
205+
}
206+
LintLevelSource::Node(_, forbid_source_span, reason) => {
207+
OverruledAttributeSub::NodeSource {
208+
span: forbid_source_span,
209+
reason,
210+
}
211+
}
212+
LintLevelSource::CommandLine(_, _) => {
213+
OverruledAttributeSub::CommandLineSource
214+
}
215+
},
216+
});
204217
} else {
205218
self.struct_lint(
206219
FORBIDDEN_LINT_GROUPS,

0 commit comments

Comments
 (0)