1
Fork 0

Refactor remaining diagnostics

This commit is contained in:
Hampus Lidin 2022-08-21 20:56:29 +02:00
parent b731bfa2d4
commit c4f59605d2
2 changed files with 93 additions and 103 deletions

View file

@ -3,7 +3,6 @@
use rustc_ast as ast; use rustc_ast as ast;
use rustc_ast::{Attribute, Lit, LitKind, MetaItem, MetaItemKind, NestedMetaItem, NodeId}; use rustc_ast::{Attribute, Lit, LitKind, MetaItem, MetaItemKind, NestedMetaItem, NodeId};
use rustc_ast_pretty::pprust; use rustc_ast_pretty::pprust;
use rustc_errors::{struct_span_err, Applicability};
use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg}; use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg};
use rustc_macros::HashStable_Generic; use rustc_macros::HashStable_Generic;
use rustc_session::lint::builtin::UNEXPECTED_CFGS; use rustc_session::lint::builtin::UNEXPECTED_CFGS;
@ -14,7 +13,7 @@ use rustc_span::hygiene::Transparency;
use rustc_span::{symbol::sym, symbol::Symbol, Span}; use rustc_span::{symbol::sym, symbol::Symbol, Span};
use std::num::NonZeroU32; use std::num::NonZeroU32;
use crate::session_diagnostics; use crate::session_diagnostics::{self, IncorrectReprFormatGenericCause};
pub fn is_builtin_attr(attr: &Attribute) -> bool { pub fn is_builtin_attr(attr: &Attribute) -> bool {
attr.is_doc_comment() || attr.ident().filter(|ident| is_builtin_attr_name(ident.name)).is_some() attr.is_doc_comment() || attr.ident().filter(|ident| is_builtin_attr_name(ident.name)).is_some()
@ -276,7 +275,7 @@ where
*item = Some(v); *item = Some(v);
true true
} else { } else {
sess.emit_err(session_diagnostics::InvalidMetaItem { span: meta.span }); sess.emit_err(session_diagnostics::IncorrectMetaItem { span: meta.span });
false false
} }
}; };
@ -788,7 +787,6 @@ where
I: Iterator<Item = &'a Attribute>, I: Iterator<Item = &'a Attribute>,
{ {
let mut depr: Option<(Deprecation, Span)> = None; let mut depr: Option<(Deprecation, Span)> = None;
let diagnostic = &sess.parse_sess.span_diagnostic;
let is_rustc = sess.features_untracked().staged_api; let is_rustc = sess.features_untracked().staged_api;
'outer: for attr in attrs_iter { 'outer: for attr in attrs_iter {
@ -829,8 +827,12 @@ where
), ),
); );
} else { } else {
struct_span_err!(diagnostic, meta.span, E0551, "incorrect meta item") // FIXME: This diagnostic is identical to `IncorrectMetaItem`, barring
.emit(); // the error code. Consider changing this to `IncorrectMetaItem`. See
// #51489.
sess.emit_err(session_diagnostics::IncorrectMetaItem2 {
span: meta.span,
});
} }
false false
@ -971,19 +973,9 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
sym::simd => Some(ReprSimd), sym::simd => Some(ReprSimd),
sym::transparent => Some(ReprTransparent), sym::transparent => Some(ReprTransparent),
sym::align => { sym::align => {
let mut err = struct_span_err!( sess.emit_err(session_diagnostics::InvalidReprAlignNeedArg {
diagnostic, span: item.span(),
item.span(), });
E0589,
"invalid `repr(align)` attribute: `align` needs an argument"
);
err.span_suggestion(
item.span(),
"supply an argument here",
"align(...)",
Applicability::HasPlaceholders,
);
err.emit();
recognised = true; recognised = true;
None None
} }
@ -1012,57 +1004,44 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
|| int_type_of_word(name).is_some() || int_type_of_word(name).is_some()
{ {
recognised = true; recognised = true;
struct_span_err!( sess.emit_err(session_diagnostics::InvalidReprHintNoParen {
diagnostic, span: item.span(),
item.span(), name: name.to_ident_string(),
E0552, });
"invalid representation hint: `{}` does not take a parenthesized argument list",
name.to_ident_string(),
).emit();
} }
if let Some(literal_error) = literal_error { if let Some(literal_error) = literal_error {
struct_span_err!( sess.emit_err(session_diagnostics::InvalidReprGeneric {
diagnostic, span: item.span(),
item.span(), repr_arg: name.to_ident_string(),
E0589, error_part: literal_error,
"invalid `repr({})` attribute: {}", });
name.to_ident_string(),
literal_error
)
.emit();
} }
} else if let Some(meta_item) = item.meta_item() { } else if let Some(meta_item) = item.meta_item() {
if let MetaItemKind::NameValue(ref value) = meta_item.kind { if let MetaItemKind::NameValue(ref value) = meta_item.kind {
if meta_item.has_name(sym::align) || meta_item.has_name(sym::packed) { if meta_item.has_name(sym::align) || meta_item.has_name(sym::packed) {
let name = meta_item.name_or_empty().to_ident_string(); let name = meta_item.name_or_empty().to_ident_string();
recognised = true; recognised = true;
let mut err = struct_span_err!( sess.emit_err(session_diagnostics::IncorrectReprFormatGeneric {
diagnostic, span: item.span(),
item.span(), repr_arg: &name,
E0693, cause: match value.kind {
"incorrect `repr({})` attribute format",
name,
);
match value.kind {
ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => { ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
err.span_suggestion( Some(IncorrectReprFormatGenericCause::Int {
item.span(), span: item.span(),
"use parentheses instead", name: &name,
format!("{}({})", name, int), int,
Applicability::MachineApplicable, })
);
} }
ast::LitKind::Str(s, _) => { ast::LitKind::Str(symbol, _) => {
err.span_suggestion( Some(IncorrectReprFormatGenericCause::Symbol {
item.span(), span: item.span(),
"use parentheses instead", name: &name,
format!("{}({})", name, s), symbol,
Applicability::MachineApplicable, })
);
} }
_ => {} _ => None,
} },
err.emit(); });
} else { } else {
if matches!( if matches!(
meta_item.name_or_empty(), meta_item.name_or_empty(),
@ -1070,51 +1049,33 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
) || int_type_of_word(meta_item.name_or_empty()).is_some() ) || int_type_of_word(meta_item.name_or_empty()).is_some()
{ {
recognised = true; recognised = true;
struct_span_err!( sess.emit_err(session_diagnostics::InvalidReprHintNoValue {
diagnostic, span: meta_item.span,
meta_item.span, name: meta_item.name_or_empty().to_ident_string(),
E0552, });
"invalid representation hint: `{}` does not take a value",
meta_item.name_or_empty().to_ident_string(),
)
.emit();
} }
} }
} else if let MetaItemKind::List(_) = meta_item.kind { } else if let MetaItemKind::List(_) = meta_item.kind {
if meta_item.has_name(sym::align) { if meta_item.has_name(sym::align) {
recognised = true; recognised = true;
struct_span_err!( sess.emit_err(session_diagnostics::IncorrectReprFormatAlignOneArg {
diagnostic, span: meta_item.span,
meta_item.span, });
E0693,
"incorrect `repr(align)` attribute format: \
`align` takes exactly one argument in parentheses"
)
.emit();
} else if meta_item.has_name(sym::packed) { } else if meta_item.has_name(sym::packed) {
recognised = true; recognised = true;
struct_span_err!( sess.emit_err(session_diagnostics::IncorrectReprFormatPackedOneOrZeroArg {
diagnostic, span: meta_item.span,
meta_item.span, });
E0552,
"incorrect `repr(packed)` attribute format: \
`packed` takes exactly one parenthesized argument, \
or no parentheses at all"
)
.emit();
} else if matches!( } else if matches!(
meta_item.name_or_empty(), meta_item.name_or_empty(),
sym::C | sym::simd | sym::transparent sym::C | sym::simd | sym::transparent
) || int_type_of_word(meta_item.name_or_empty()).is_some() ) || int_type_of_word(meta_item.name_or_empty()).is_some()
{ {
recognised = true; recognised = true;
struct_span_err!( sess.emit_err(session_diagnostics::InvalidReprHintNoParen {
diagnostic, span: meta_item.span,
meta_item.span, name: meta_item.name_or_empty().to_ident_string(),
E0552, });
"invalid representation hint: `{}` does not take a parenthesized argument list",
meta_item.name_or_empty().to_ident_string(),
).emit();
} }
} }
} }
@ -1211,10 +1172,10 @@ fn allow_unstable<'a>(
let list = attrs let list = attrs
.filter_map(move |attr| { .filter_map(move |attr| {
attr.meta_item_list().or_else(|| { attr.meta_item_list().or_else(|| {
sess.diagnostic().span_err( sess.emit_err(session_diagnostics::ExpectsFeatureList {
attr.span, span: attr.span,
&format!("`{}` expects a list of feature names", symbol.to_ident_string()), name: symbol.to_ident_string(),
); });
None None
}) })
}) })
@ -1223,10 +1184,10 @@ fn allow_unstable<'a>(
list.into_iter().filter_map(move |it| { list.into_iter().filter_map(move |it| {
let name = it.ident().map(|ident| ident.name); let name = it.ident().map(|ident| ident.name);
if name.is_none() { if name.is_none() {
sess.diagnostic().span_err( sess.emit_err(session_diagnostics::ExpectsFeatures {
it.span(), span: it.span(),
&format!("`{}` expects feature names", symbol.to_ident_string()), name: symbol.to_ident_string(),
); });
} }
name name
}) })

View file

@ -28,7 +28,7 @@ attr_unsupported_literal_deprecated_kv_pair =
attr_unsupported_literal_suggestion = attr_unsupported_literal_suggestion =
consider removing the prefix consider removing the prefix
attr_invalid_meta_item = attr_incorrect_meta_item =
incorrect meta item incorrect meta item
attr_invalid_issue_string = attr_invalid_issue_string =
@ -76,3 +76,32 @@ attr_deprecated_item_suggestion =
attr_missing_note = attr_missing_note =
missing 'note' missing 'note'
attr_invalid_repr_align_need_arg =
invalid `repr(align)` attribute: `align` needs an argument
.suggestion = supply an argument here
attr_invalid_repr_generic =
invalid `repr({$repr_arg})` attribute: {$error_part}
attr_invalid_repr_hint_no_paren =
invalid representation hint: `{$name}` does not take a parenthesized argument list
attr_invalid_repr_hint_no_value =
invalid representation hint: `{$name}` does not take a value
attr_incorrect_repr_format_generic =
incorrect `repr({$repr_arg})` attribute format
.suggestion = use parentheses instead
attr_incorrect_repr_format_align_one_arg =
incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses
attr_incorrect_repr_format_packed_one_or_zero_arg =
incorrect `repr(packed)` attribute format: `packed` takes exactly one parenthesized argument, or no parentheses at all
attr_expects_feature_list =
`{$name}` expects a list of feature names
attr_expects_features =
`{$name}` expects feature names