about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRejyr <jerrylwang123@gmail.com>2022-08-19 17:17:14 -0400
committerRejyr <jerrylwang123@gmail.com>2022-08-22 08:24:14 -0400
commit874a79fae30ec93bcd9847187741378c405bd9da (patch)
tree8baa6b2f3a93d03c468281cc11417a141e4b72f8
parentd197c1eb5bee2d4c2c27c895f5e496df935ccbd0 (diff)
downloadrust-874a79fae30ec93bcd9847187741378c405bd9da.tar.gz
rust-874a79fae30ec93bcd9847187741378c405bd9da.zip
migrate: `bad_attr` to `SessionDiagnostic`
-rw-r--r--compiler/rustc_error_messages/locales/en-US/lint.ftl8
-rw-r--r--compiler/rustc_lint/src/errors.rs21
-rw-r--r--compiler/rustc_lint/src/levels.rs42
3 files changed, 53 insertions, 18 deletions
diff --git a/compiler/rustc_error_messages/locales/en-US/lint.ftl b/compiler/rustc_error_messages/locales/en-US/lint.ftl
index e0943323ade..4569ce82e3e 100644
--- a/compiler/rustc_error_messages/locales/en-US/lint.ftl
+++ b/compiler/rustc_error_messages/locales/en-US/lint.ftl
@@ -394,5 +394,13 @@ lint_builtin_deref_nullptr = dereferencing a null pointer
 
 lint_builtin_asm_labels = avoid using named labels in inline assembly
 
+lint_malformed_attribute = malformed lint attribute input
+
+lint_bad_attribute_argument = bad attribute argument
+
+lint_reason_must_be_string_literal = reason must be a string literal
+
+lint_reason_must_come_last = reason in lint attribute must come last
+
 lint_unknown_tool = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}`
     .help = add `#![register_tool({$tool_name})]` to the crate root
diff --git a/compiler/rustc_lint/src/errors.rs b/compiler/rustc_lint/src/errors.rs
index 3f4d856a1cc..679cfad4961 100644
--- a/compiler/rustc_lint/src/errors.rs
+++ b/compiler/rustc_lint/src/errors.rs
@@ -1,7 +1,26 @@
-use rustc_macros::SessionDiagnostic;
+use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
 use rustc_span::Span;
 
 #[derive(SessionDiagnostic)]
+#[error(lint::malformed_attribute, code = "E0452")]
+pub struct MalformedAttribute {
+    #[primary_span]
+    pub span: Span,
+    #[subdiagnostic]
+    pub sub: MalformedAttributeSub,
+}
+
+#[derive(SessionSubdiagnostic)]
+pub enum MalformedAttributeSub {
+    #[label(lint::bad_attribute_argument)]
+    BadAttributeArgument(#[primary_span] Span),
+    #[label(lint::reason_must_be_string_literal)]
+    ReasonMustBeStringLiteral(#[primary_span] Span),
+    #[label(lint::reason_must_come_last)]
+    ReasonMustComeLast(#[primary_span] Span),
+}
+
+#[derive(SessionDiagnostic)]
 #[error(lint::unknown_tool, code = "E0710")]
 pub struct UnknownTool {
     #[primary_span]
diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs
index 3db88b7ace4..2dd4d2b34cc 100644
--- a/compiler/rustc_lint/src/levels.rs
+++ b/compiler/rustc_lint/src/levels.rs
@@ -26,7 +26,7 @@ use rustc_span::symbol::{sym, Symbol};
 use rustc_span::{Span, DUMMY_SP};
 use tracing::debug;
 
-use crate::errors::UnknownTool;
+use crate::errors::{MalformedAttribute, MalformedAttributeSub, UnknownTool};
 
 fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap {
     let store = unerased_lint_store(tcx);
@@ -271,7 +271,7 @@ impl<'s> LintLevelsBuilder<'s> {
         self.cur = self.sets.list.push(LintSet { specs: FxHashMap::default(), parent: prev });
 
         let sess = self.sess;
-        let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input");
+        // let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input");
         for (attr_index, attr) in attrs.iter().enumerate() {
             if attr.has_name(sym::automatically_derived) {
                 self.current_specs_mut().insert(
@@ -322,20 +322,27 @@ impl<'s> LintLevelsBuilder<'s> {
                                 }
                                 reason = Some(rationale);
                             } else {
-                                bad_attr(name_value.span)
-                                    .span_label(name_value.span, "reason must be a string literal")
-                                    .emit();
+                                sess.emit_err(MalformedAttribute {
+                                    span: name_value.span,
+                                    sub: MalformedAttributeSub::ReasonMustBeStringLiteral(
+                                        name_value.span,
+                                    ),
+                                });
                             }
                             // found reason, reslice meta list to exclude it
                             metas.pop().unwrap();
                         } else {
-                            bad_attr(item.span)
-                                .span_label(item.span, "bad attribute argument")
-                                .emit();
+                            sess.emit_err(MalformedAttribute {
+                                span: item.span,
+                                sub: MalformedAttributeSub::BadAttributeArgument(item.span),
+                            });
                         }
                     }
                     ast::MetaItemKind::List(_) => {
-                        bad_attr(item.span).span_label(item.span, "bad attribute argument").emit();
+                        sess.emit_err(MalformedAttribute {
+                            span: item.span,
+                            sub: MalformedAttributeSub::BadAttributeArgument(item.span),
+                        });
                     }
                 }
             }
@@ -353,20 +360,21 @@ impl<'s> LintLevelsBuilder<'s> {
                 let meta_item = match li {
                     ast::NestedMetaItem::MetaItem(meta_item) if meta_item.is_word() => meta_item,
                     _ => {
-                        let mut err = bad_attr(sp);
-                        let mut add_label = true;
                         if let Some(item) = li.meta_item() {
                             if let ast::MetaItemKind::NameValue(_) = item.kind {
                                 if item.path == sym::reason {
-                                    err.span_label(sp, "reason in lint attribute must come last");
-                                    add_label = false;
+                                    sess.emit_err(MalformedAttribute {
+                                        span: sp,
+                                        sub: MalformedAttributeSub::ReasonMustComeLast(sp),
+                                    });
+                                    continue;
                                 }
                             }
                         }
-                        if add_label {
-                            err.span_label(sp, "bad attribute argument");
-                        }
-                        err.emit();
+                        sess.emit_err(MalformedAttribute {
+                            span: sp,
+                            sub: MalformedAttributeSub::BadAttributeArgument(sp),
+                        });
                         continue;
                     }
                 };