Skip to content

Commit 874a79f

Browse files
committed
migrate: bad_attr to SessionDiagnostic
1 parent d197c1e commit 874a79f

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,5 +394,13 @@ lint_builtin_deref_nullptr = dereferencing a null pointer
394394
395395
lint_builtin_asm_labels = avoid using named labels in inline assembly
396396
397+
lint_malformed_attribute = malformed lint attribute input
398+
399+
lint_bad_attribute_argument = bad attribute argument
400+
401+
lint_reason_must_be_string_literal = reason must be a string literal
402+
403+
lint_reason_must_come_last = reason in lint attribute must come last
404+
397405
lint_unknown_tool = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}`
398406
.help = add `#![register_tool({$tool_name})]` to the crate root

compiler/rustc_lint/src/errors.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1-
use rustc_macros::SessionDiagnostic;
1+
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
22
use rustc_span::Span;
33

4+
#[derive(SessionDiagnostic)]
5+
#[error(lint::malformed_attribute, code = "E0452")]
6+
pub struct MalformedAttribute {
7+
#[primary_span]
8+
pub span: Span,
9+
#[subdiagnostic]
10+
pub sub: MalformedAttributeSub,
11+
}
12+
13+
#[derive(SessionSubdiagnostic)]
14+
pub enum MalformedAttributeSub {
15+
#[label(lint::bad_attribute_argument)]
16+
BadAttributeArgument(#[primary_span] Span),
17+
#[label(lint::reason_must_be_string_literal)]
18+
ReasonMustBeStringLiteral(#[primary_span] Span),
19+
#[label(lint::reason_must_come_last)]
20+
ReasonMustComeLast(#[primary_span] Span),
21+
}
22+
423
#[derive(SessionDiagnostic)]
524
#[error(lint::unknown_tool, code = "E0710")]
625
pub struct UnknownTool {

compiler/rustc_lint/src/levels.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_span::symbol::{sym, Symbol};
2626
use rustc_span::{Span, DUMMY_SP};
2727
use tracing::debug;
2828

29-
use crate::errors::UnknownTool;
29+
use crate::errors::{MalformedAttribute, MalformedAttributeSub, UnknownTool};
3030

3131
fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap {
3232
let store = unerased_lint_store(tcx);
@@ -271,7 +271,7 @@ impl<'s> LintLevelsBuilder<'s> {
271271
self.cur = self.sets.list.push(LintSet { specs: FxHashMap::default(), parent: prev });
272272

273273
let sess = self.sess;
274-
let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input");
274+
// let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input");
275275
for (attr_index, attr) in attrs.iter().enumerate() {
276276
if attr.has_name(sym::automatically_derived) {
277277
self.current_specs_mut().insert(
@@ -322,20 +322,27 @@ impl<'s> LintLevelsBuilder<'s> {
322322
}
323323
reason = Some(rationale);
324324
} else {
325-
bad_attr(name_value.span)
326-
.span_label(name_value.span, "reason must be a string literal")
327-
.emit();
325+
sess.emit_err(MalformedAttribute {
326+
span: name_value.span,
327+
sub: MalformedAttributeSub::ReasonMustBeStringLiteral(
328+
name_value.span,
329+
),
330+
});
328331
}
329332
// found reason, reslice meta list to exclude it
330333
metas.pop().unwrap();
331334
} else {
332-
bad_attr(item.span)
333-
.span_label(item.span, "bad attribute argument")
334-
.emit();
335+
sess.emit_err(MalformedAttribute {
336+
span: item.span,
337+
sub: MalformedAttributeSub::BadAttributeArgument(item.span),
338+
});
335339
}
336340
}
337341
ast::MetaItemKind::List(_) => {
338-
bad_attr(item.span).span_label(item.span, "bad attribute argument").emit();
342+
sess.emit_err(MalformedAttribute {
343+
span: item.span,
344+
sub: MalformedAttributeSub::BadAttributeArgument(item.span),
345+
});
339346
}
340347
}
341348
}
@@ -353,20 +360,21 @@ impl<'s> LintLevelsBuilder<'s> {
353360
let meta_item = match li {
354361
ast::NestedMetaItem::MetaItem(meta_item) if meta_item.is_word() => meta_item,
355362
_ => {
356-
let mut err = bad_attr(sp);
357-
let mut add_label = true;
358363
if let Some(item) = li.meta_item() {
359364
if let ast::MetaItemKind::NameValue(_) = item.kind {
360365
if item.path == sym::reason {
361-
err.span_label(sp, "reason in lint attribute must come last");
362-
add_label = false;
366+
sess.emit_err(MalformedAttribute {
367+
span: sp,
368+
sub: MalformedAttributeSub::ReasonMustComeLast(sp),
369+
});
370+
continue;
363371
}
364372
}
365373
}
366-
if add_label {
367-
err.span_label(sp, "bad attribute argument");
368-
}
369-
err.emit();
374+
sess.emit_err(MalformedAttribute {
375+
span: sp,
376+
sub: MalformedAttributeSub::BadAttributeArgument(sp),
377+
});
370378
continue;
371379
}
372380
};

0 commit comments

Comments
 (0)