Refactor more diagnostics in rustc_attr

This commit is contained in:
Hampus Lidin 2022-08-21 08:48:14 +02:00
parent 0005f628f0
commit 83a724eab5
2 changed files with 49 additions and 48 deletions

View File

@ -237,8 +237,6 @@ where
let mut promotable = false; let mut promotable = false;
let mut allowed_through_unstable_modules = false; let mut allowed_through_unstable_modules = false;
let diagnostic = &sess.parse_sess.span_diagnostic;
'outer: for attr in attrs_iter { 'outer: for attr in attrs_iter {
if ![ if ![
sym::rustc_const_unstable, sym::rustc_const_unstable,
@ -278,7 +276,7 @@ where
*item = Some(v); *item = Some(v);
true true
} else { } else {
struct_span_err!(diagnostic, meta.span, E0539, "incorrect meta item").emit(); sess.emit_err(session_diagnostics::InvalidMetaItem { span: meta.span });
false false
} }
}; };
@ -344,39 +342,28 @@ where
// is a name/value pair string literal. // is a name/value pair string literal.
issue_num = match issue.unwrap().as_str() { issue_num = match issue.unwrap().as_str() {
"none" => None, "none" => None,
issue => { issue => match issue.parse::<NonZeroU32>() {
let emit_diag = |msg: &str| { Ok(num) => Some(num),
struct_span_err!( Err(err) => {
diagnostic, sess.emit_err(
mi.span, session_diagnostics::InvalidIssueString {
E0545, span: mi.span,
"`issue` must be a non-zero numeric string \ cause: session_diagnostics::InvalidIssueStringCause::from_int_error_kind(
or \"none\"", mi.name_value_literal_span().unwrap(),
) err.kind(),
.span_label(mi.name_value_literal_span().unwrap(), msg) ),
.emit(); },
}; );
match issue.parse() { continue 'outer;
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;
}
} }
} },
}; };
} }
sym::soft => { sym::soft => {
if !mi.is_word() { if !mi.is_word() {
let msg = "`soft` should not have any arguments"; sess.emit_err(session_diagnostics::SoftNoArgs {
sess.parse_sess.span_diagnostic.span_err(mi.span, msg); span: mi.span,
});
} }
is_soft = true; is_soft = true;
} }
@ -434,8 +421,7 @@ where
continue; continue;
} }
_ => { _ => {
struct_span_err!(diagnostic, attr.span, E0547, "missing 'issue'") sess.emit_err(session_diagnostics::MissingIssue { span: attr.span });
.emit();
continue; continue;
} }
} }
@ -530,14 +516,7 @@ where
if let Some((ref mut stab, _)) = const_stab { if let Some((ref mut stab, _)) = const_stab {
stab.promotable = promotable; stab.promotable = promotable;
} else { } else {
struct_span_err!( sess.emit_err(session_diagnostics::RustcPromotablePairing { span: item_sp });
diagnostic,
item_sp,
E0717,
"`rustc_promotable` attribute must be paired with either a `rustc_const_unstable` \
or a `rustc_const_stable` attribute"
)
.emit();
} }
} }
@ -552,13 +531,7 @@ where
{ {
*allowed_through_unstable_modules = true; *allowed_through_unstable_modules = true;
} else { } else {
struct_span_err!( sess.emit_err(session_diagnostics::RustcAllowedUnstablePairing { span: item_sp });
diagnostic,
item_sp,
E0789,
"`rustc_allowed_through_unstable_modules` attribute must be paired with a `stable` attribute"
)
.emit();
} }
} }

View File

@ -27,3 +27,31 @@ attr_unsupported_literal_deprecated_kv_pair =
item in `deprecated` must be a key/value pair item in `deprecated` must be a key/value pair
attr_unsupported_literal_suggestion = attr_unsupported_literal_suggestion =
consider removing the prefix 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