From: Hampus Lidin Date: Sun, 21 Aug 2022 06:48:14 +0000 (+0200) Subject: Refactor more diagnostics in `rustc_attr` X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=83a724eab5e7f6c7b03374efd625b71ff9cf92a3;p=rust.git Refactor more diagnostics in `rustc_attr` --- 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 @@ 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::() { + 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 }); } } 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