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 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 });
}
}

View 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