about summary refs log tree commit diff
diff options
context:
space:
mode:
authorHampus Lidin <hampuslidin@gmail.com>2022-08-21 08:48:14 +0200
committerHampus Lidin <hampuslidin@gmail.com>2022-08-22 20:19:19 +0200
commit83a724eab5e7f6c7b03374efd625b71ff9cf92a3 (patch)
tree4b1e93db63d66ed6e75f59ff3f8e9a41d33960d9
parent0005f628f068a766647bb0b159dac2c6cefcefa1 (diff)
downloadrust-83a724eab5e7f6c7b03374efd625b71ff9cf92a3.tar.gz
rust-83a724eab5e7f6c7b03374efd625b71ff9cf92a3.zip
Refactor more diagnostics in `rustc_attr`
-rw-r--r--compiler/rustc_attr/src/builtin.rs69
-rw-r--r--compiler/rustc_error_messages/locales/en-US/attr.ftl28
2 files changed, 49 insertions, 48 deletions
diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs
index b43551db43d..98a171488ca 100644
--- a/compiler/rustc_attr/src/builtin.rs
+++ b/compiler/rustc_attr/src/builtin.rs
@@ -237,8 +237,6 @@ where
     let mut promotable = false;
     let mut allowed_through_unstable_modules = false;
 
-    let diagnostic = &sess.parse_sess.span_diagnostic;
-
     'outer: for attr in attrs_iter {
         if ![
             sym::rustc_const_unstable,
@@ -278,7 +276,7 @@ where
                     *item = Some(v);
                     true
                 } else {
-                    struct_span_err!(diagnostic, meta.span, E0539, "incorrect meta item").emit();
+                    sess.emit_err(session_diagnostics::InvalidMetaItem { span: meta.span });
                     false
                 }
             };
@@ -344,39 +342,28 @@ where
                                 // is a name/value pair string literal.
                                 issue_num = match issue.unwrap().as_str() {
                                     "none" => None,
-                                    issue => {
-                                        let emit_diag = |msg: &str| {
-                                            struct_span_err!(
-                                                diagnostic,
-                                                mi.span,
-                                                E0545,
-                                                "`issue` must be a non-zero numeric string \
-                                                or \"none\"",
-                                            )
-                                            .span_label(mi.name_value_literal_span().unwrap(), msg)
-                                            .emit();
-                                        };
-                                        match issue.parse() {
-                                            Ok(0) => {
-                                                emit_diag(
-                                                    "`issue` must not be \"0\", \
-                                                    use \"none\" instead",
-                                                );
-                                                continue 'outer;
-                                            }
-                                            Ok(num) => NonZeroU32::new(num),
-                                            Err(err) => {
-                                                emit_diag(&err.to_string());
-                                                continue 'outer;
-                                            }
+                                    issue => match issue.parse::<NonZeroU32>() {
+                                        Ok(num) => Some(num),
+                                        Err(err) => {
+                                            sess.emit_err(
+                                                session_diagnostics::InvalidIssueString {
+                                                    span: mi.span,
+                                                    cause: session_diagnostics::InvalidIssueStringCause::from_int_error_kind(
+                                                        mi.name_value_literal_span().unwrap(),
+                                                        err.kind(),
+                                                    ),
+                                                },
+                                            );
+                                            continue 'outer;
                                         }
-                                    }
+                                    },
                                 };
                             }
                             sym::soft => {
                                 if !mi.is_word() {
-                                    let msg = "`soft` should not have any arguments";
-                                    sess.parse_sess.span_diagnostic.span_err(mi.span, msg);
+                                    sess.emit_err(session_diagnostics::SoftNoArgs {
+                                        span: mi.span,
+                                    });
                                 }
                                 is_soft = true;
                             }
@@ -434,8 +421,7 @@ where
                             continue;
                         }
                         _ => {
-                            struct_span_err!(diagnostic, attr.span, E0547, "missing 'issue'")
-                                .emit();
+                            sess.emit_err(session_diagnostics::MissingIssue { span: attr.span });
                             continue;
                         }
                     }
@@ -530,14 +516,7 @@ where
         if let Some((ref mut stab, _)) = const_stab {
             stab.promotable = promotable;
         } else {
-            struct_span_err!(
-                diagnostic,
-                item_sp,
-                E0717,
-                "`rustc_promotable` attribute must be paired with either a `rustc_const_unstable` \
-                or a `rustc_const_stable` attribute"
-            )
-            .emit();
+            sess.emit_err(session_diagnostics::RustcPromotablePairing { span: item_sp });
         }
     }
 
@@ -552,13 +531,7 @@ where
         {
             *allowed_through_unstable_modules = true;
         } else {
-            struct_span_err!(
-                diagnostic,
-                item_sp,
-                E0789,
-                "`rustc_allowed_through_unstable_modules` attribute must be paired with a `stable` attribute"
-            )
-            .emit();
+            sess.emit_err(session_diagnostics::RustcAllowedUnstablePairing { span: item_sp });
         }
     }
 
diff --git a/compiler/rustc_error_messages/locales/en-US/attr.ftl b/compiler/rustc_error_messages/locales/en-US/attr.ftl
index a8207b1f7bc..45dda0ea7c4 100644
--- a/compiler/rustc_error_messages/locales/en-US/attr.ftl
+++ b/compiler/rustc_error_messages/locales/en-US/attr.ftl
@@ -27,3 +27,31 @@ attr_unsupported_literal_deprecated_kv_pair =
     item in `deprecated` must be a key/value pair
 attr_unsupported_literal_suggestion =
     consider removing the prefix
+
+attr_invalid_meta_item =
+    incorrect meta item
+
+attr_invalid_issue_string =
+    `issue` must be a non-zero numeric string or "none"
+attr_must_not_be_zero =
+    `issue` must not be "0", use "none" instead
+attr_empty =
+    cannot parse integer from empty string
+attr_invalid_digit =
+    invalid digit found in string
+attr_pos_overflow =
+    number too large to fit in target type
+attr_neg_overflow =
+    number too small to fit in target type
+
+attr_missing_issue =
+    missing 'issue'
+
+attr_rustc_promotable_pairing =
+    `rustc_promotable` attribute must be paired with either a `rustc_const_unstable` or a `rustc_const_stable` attribute
+
+attr_rustc_allowed_unstable_pairing =
+    `rustc_allowed_through_unstable_modules` attribute must be paired with a `stable` attribute
+
+attr_soft_no_args =
+    `soft` should not have any arguments