Skip to content

Commit c4f5960

Browse files
committed
Refactor remaining diagnostics
1 parent b731bfa commit c4f5960

File tree

2 files changed

+93
-103
lines changed

2 files changed

+93
-103
lines changed

compiler/rustc_attr/src/builtin.rs

Lines changed: 63 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use rustc_ast as ast;
44
use rustc_ast::{Attribute, Lit, LitKind, MetaItem, MetaItemKind, NestedMetaItem, NodeId};
55
use rustc_ast_pretty::pprust;
6-
use rustc_errors::{struct_span_err, Applicability};
76
use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg};
87
use rustc_macros::HashStable_Generic;
98
use rustc_session::lint::builtin::UNEXPECTED_CFGS;
@@ -14,7 +13,7 @@ use rustc_span::hygiene::Transparency;
1413
use rustc_span::{symbol::sym, symbol::Symbol, Span};
1514
use std::num::NonZeroU32;
1615

17-
use crate::session_diagnostics;
16+
use crate::session_diagnostics::{self, IncorrectReprFormatGenericCause};
1817

1918
pub fn is_builtin_attr(attr: &Attribute) -> bool {
2019
attr.is_doc_comment() || attr.ident().filter(|ident| is_builtin_attr_name(ident.name)).is_some()
@@ -276,7 +275,7 @@ where
276275
*item = Some(v);
277276
true
278277
} else {
279-
sess.emit_err(session_diagnostics::InvalidMetaItem { span: meta.span });
278+
sess.emit_err(session_diagnostics::IncorrectMetaItem { span: meta.span });
280279
false
281280
}
282281
};
@@ -788,7 +787,6 @@ where
788787
I: Iterator<Item = &'a Attribute>,
789788
{
790789
let mut depr: Option<(Deprecation, Span)> = None;
791-
let diagnostic = &sess.parse_sess.span_diagnostic;
792790
let is_rustc = sess.features_untracked().staged_api;
793791

794792
'outer: for attr in attrs_iter {
@@ -829,8 +827,12 @@ where
829827
),
830828
);
831829
} else {
832-
struct_span_err!(diagnostic, meta.span, E0551, "incorrect meta item")
833-
.emit();
830+
// FIXME: This diagnostic is identical to `IncorrectMetaItem`, barring
831+
// the error code. Consider changing this to `IncorrectMetaItem`. See
832+
// #51489.
833+
sess.emit_err(session_diagnostics::IncorrectMetaItem2 {
834+
span: meta.span,
835+
});
834836
}
835837

836838
false
@@ -971,19 +973,9 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
971973
sym::simd => Some(ReprSimd),
972974
sym::transparent => Some(ReprTransparent),
973975
sym::align => {
974-
let mut err = struct_span_err!(
975-
diagnostic,
976-
item.span(),
977-
E0589,
978-
"invalid `repr(align)` attribute: `align` needs an argument"
979-
);
980-
err.span_suggestion(
981-
item.span(),
982-
"supply an argument here",
983-
"align(...)",
984-
Applicability::HasPlaceholders,
985-
);
986-
err.emit();
976+
sess.emit_err(session_diagnostics::InvalidReprAlignNeedArg {
977+
span: item.span(),
978+
});
987979
recognised = true;
988980
None
989981
}
@@ -1012,109 +1004,78 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
10121004
|| int_type_of_word(name).is_some()
10131005
{
10141006
recognised = true;
1015-
struct_span_err!(
1016-
diagnostic,
1017-
item.span(),
1018-
E0552,
1019-
"invalid representation hint: `{}` does not take a parenthesized argument list",
1020-
name.to_ident_string(),
1021-
).emit();
1007+
sess.emit_err(session_diagnostics::InvalidReprHintNoParen {
1008+
span: item.span(),
1009+
name: name.to_ident_string(),
1010+
});
10221011
}
10231012
if let Some(literal_error) = literal_error {
1024-
struct_span_err!(
1025-
diagnostic,
1026-
item.span(),
1027-
E0589,
1028-
"invalid `repr({})` attribute: {}",
1029-
name.to_ident_string(),
1030-
literal_error
1031-
)
1032-
.emit();
1013+
sess.emit_err(session_diagnostics::InvalidReprGeneric {
1014+
span: item.span(),
1015+
repr_arg: name.to_ident_string(),
1016+
error_part: literal_error,
1017+
});
10331018
}
10341019
} else if let Some(meta_item) = item.meta_item() {
10351020
if let MetaItemKind::NameValue(ref value) = meta_item.kind {
10361021
if meta_item.has_name(sym::align) || meta_item.has_name(sym::packed) {
10371022
let name = meta_item.name_or_empty().to_ident_string();
10381023
recognised = true;
1039-
let mut err = struct_span_err!(
1040-
diagnostic,
1041-
item.span(),
1042-
E0693,
1043-
"incorrect `repr({})` attribute format",
1044-
name,
1045-
);
1046-
match value.kind {
1047-
ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
1048-
err.span_suggestion(
1049-
item.span(),
1050-
"use parentheses instead",
1051-
format!("{}({})", name, int),
1052-
Applicability::MachineApplicable,
1053-
);
1054-
}
1055-
ast::LitKind::Str(s, _) => {
1056-
err.span_suggestion(
1057-
item.span(),
1058-
"use parentheses instead",
1059-
format!("{}({})", name, s),
1060-
Applicability::MachineApplicable,
1061-
);
1062-
}
1063-
_ => {}
1064-
}
1065-
err.emit();
1024+
sess.emit_err(session_diagnostics::IncorrectReprFormatGeneric {
1025+
span: item.span(),
1026+
repr_arg: &name,
1027+
cause: match value.kind {
1028+
ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
1029+
Some(IncorrectReprFormatGenericCause::Int {
1030+
span: item.span(),
1031+
name: &name,
1032+
int,
1033+
})
1034+
}
1035+
ast::LitKind::Str(symbol, _) => {
1036+
Some(IncorrectReprFormatGenericCause::Symbol {
1037+
span: item.span(),
1038+
name: &name,
1039+
symbol,
1040+
})
1041+
}
1042+
_ => None,
1043+
},
1044+
});
10661045
} else {
10671046
if matches!(
10681047
meta_item.name_or_empty(),
10691048
sym::C | sym::simd | sym::transparent
10701049
) || int_type_of_word(meta_item.name_or_empty()).is_some()
10711050
{
10721051
recognised = true;
1073-
struct_span_err!(
1074-
diagnostic,
1075-
meta_item.span,
1076-
E0552,
1077-
"invalid representation hint: `{}` does not take a value",
1078-
meta_item.name_or_empty().to_ident_string(),
1079-
)
1080-
.emit();
1052+
sess.emit_err(session_diagnostics::InvalidReprHintNoValue {
1053+
span: meta_item.span,
1054+
name: meta_item.name_or_empty().to_ident_string(),
1055+
});
10811056
}
10821057
}
10831058
} else if let MetaItemKind::List(_) = meta_item.kind {
10841059
if meta_item.has_name(sym::align) {
10851060
recognised = true;
1086-
struct_span_err!(
1087-
diagnostic,
1088-
meta_item.span,
1089-
E0693,
1090-
"incorrect `repr(align)` attribute format: \
1091-
`align` takes exactly one argument in parentheses"
1092-
)
1093-
.emit();
1061+
sess.emit_err(session_diagnostics::IncorrectReprFormatAlignOneArg {
1062+
span: meta_item.span,
1063+
});
10941064
} else if meta_item.has_name(sym::packed) {
10951065
recognised = true;
1096-
struct_span_err!(
1097-
diagnostic,
1098-
meta_item.span,
1099-
E0552,
1100-
"incorrect `repr(packed)` attribute format: \
1101-
`packed` takes exactly one parenthesized argument, \
1102-
or no parentheses at all"
1103-
)
1104-
.emit();
1066+
sess.emit_err(session_diagnostics::IncorrectReprFormatPackedOneOrZeroArg {
1067+
span: meta_item.span,
1068+
});
11051069
} else if matches!(
11061070
meta_item.name_or_empty(),
11071071
sym::C | sym::simd | sym::transparent
11081072
) || int_type_of_word(meta_item.name_or_empty()).is_some()
11091073
{
11101074
recognised = true;
1111-
struct_span_err!(
1112-
diagnostic,
1113-
meta_item.span,
1114-
E0552,
1115-
"invalid representation hint: `{}` does not take a parenthesized argument list",
1116-
meta_item.name_or_empty().to_ident_string(),
1117-
).emit();
1075+
sess.emit_err(session_diagnostics::InvalidReprHintNoParen {
1076+
span: meta_item.span,
1077+
name: meta_item.name_or_empty().to_ident_string(),
1078+
});
11181079
}
11191080
}
11201081
}
@@ -1211,10 +1172,10 @@ fn allow_unstable<'a>(
12111172
let list = attrs
12121173
.filter_map(move |attr| {
12131174
attr.meta_item_list().or_else(|| {
1214-
sess.diagnostic().span_err(
1215-
attr.span,
1216-
&format!("`{}` expects a list of feature names", symbol.to_ident_string()),
1217-
);
1175+
sess.emit_err(session_diagnostics::ExpectsFeatureList {
1176+
span: attr.span,
1177+
name: symbol.to_ident_string(),
1178+
});
12181179
None
12191180
})
12201181
})
@@ -1223,10 +1184,10 @@ fn allow_unstable<'a>(
12231184
list.into_iter().filter_map(move |it| {
12241185
let name = it.ident().map(|ident| ident.name);
12251186
if name.is_none() {
1226-
sess.diagnostic().span_err(
1227-
it.span(),
1228-
&format!("`{}` expects feature names", symbol.to_ident_string()),
1229-
);
1187+
sess.emit_err(session_diagnostics::ExpectsFeatures {
1188+
span: it.span(),
1189+
name: symbol.to_ident_string(),
1190+
});
12301191
}
12311192
name
12321193
})

compiler/rustc_error_messages/locales/en-US/attr.ftl

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ attr_unsupported_literal_deprecated_kv_pair =
2828
attr_unsupported_literal_suggestion =
2929
consider removing the prefix
3030
31-
attr_invalid_meta_item =
31+
attr_incorrect_meta_item =
3232
incorrect meta item
3333
3434
attr_invalid_issue_string =
@@ -76,3 +76,32 @@ attr_deprecated_item_suggestion =
7676
7777
attr_missing_note =
7878
missing 'note'
79+
80+
attr_invalid_repr_align_need_arg =
81+
invalid `repr(align)` attribute: `align` needs an argument
82+
.suggestion = supply an argument here
83+
84+
attr_invalid_repr_generic =
85+
invalid `repr({$repr_arg})` attribute: {$error_part}
86+
87+
attr_invalid_repr_hint_no_paren =
88+
invalid representation hint: `{$name}` does not take a parenthesized argument list
89+
90+
attr_invalid_repr_hint_no_value =
91+
invalid representation hint: `{$name}` does not take a value
92+
93+
attr_incorrect_repr_format_generic =
94+
incorrect `repr({$repr_arg})` attribute format
95+
.suggestion = use parentheses instead
96+
97+
attr_incorrect_repr_format_align_one_arg =
98+
incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses
99+
100+
attr_incorrect_repr_format_packed_one_or_zero_arg =
101+
incorrect `repr(packed)` attribute format: `packed` takes exactly one parenthesized argument, or no parentheses at all
102+
103+
attr_expects_feature_list =
104+
`{$name}` expects a list of feature names
105+
106+
attr_expects_features =
107+
`{$name}` expects feature names

0 commit comments

Comments
 (0)