]> git.lizzy.rs Git - rust.git/commitdiff
Refactor more diagnostics in `rustc_attr`
authorHampus Lidin <hampuslidin@gmail.com>
Sun, 21 Aug 2022 06:48:14 +0000 (08:48 +0200)
committerHampus Lidin <hampuslidin@gmail.com>
Mon, 22 Aug 2022 18:19:19 +0000 (20:19 +0200)
compiler/rustc_attr/src/builtin.rs
compiler/rustc_error_messages/locales/en-US/attr.ftl

index b43551db43d67b43cd1a56622e5e6c6743c7b8e0..98a171488caba34fcc27a6075f086ebc08e4bb73 100644 (file)
@@ -237,8 +237,6 @@ fn find_stability_generic<'a, I>(
     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 @@ fn find_stability_generic<'a, I>(
                     *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 @@ fn find_stability_generic<'a, I>(
                                 // 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 @@ fn find_stability_generic<'a, I>(
                             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 @@ fn find_stability_generic<'a, I>(
         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 @@ fn find_stability_generic<'a, I>(
         {
             *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 });
         }
     }
 
index a8207b1f7bc37a635b830eaa9d5e52c863d3d738..45dda0ea7c45843489c23f67378630c8e7fb3d8b 100644 (file)
@@ -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