3
3
use rustc_ast as ast;
4
4
use rustc_ast:: { Attribute , Lit , LitKind , MetaItem , MetaItemKind , NestedMetaItem , NodeId } ;
5
5
use rustc_ast_pretty:: pprust;
6
- use rustc_errors:: { struct_span_err, Applicability } ;
7
6
use rustc_feature:: { find_gated_cfg, is_builtin_attr_name, Features , GatedCfg } ;
8
7
use rustc_macros:: HashStable_Generic ;
9
8
use rustc_session:: lint:: builtin:: UNEXPECTED_CFGS ;
@@ -14,7 +13,7 @@ use rustc_span::hygiene::Transparency;
14
13
use rustc_span:: { symbol:: sym, symbol:: Symbol , Span } ;
15
14
use std:: num:: NonZeroU32 ;
16
15
17
- use crate :: session_diagnostics;
16
+ use crate :: session_diagnostics:: { self , IncorrectReprFormatGenericCause } ;
18
17
19
18
pub fn is_builtin_attr ( attr : & Attribute ) -> bool {
20
19
attr. is_doc_comment ( ) || attr. ident ( ) . filter ( |ident| is_builtin_attr_name ( ident. name ) ) . is_some ( )
@@ -276,7 +275,7 @@ where
276
275
* item = Some ( v) ;
277
276
true
278
277
} else {
279
- sess. emit_err ( session_diagnostics:: InvalidMetaItem { span : meta. span } ) ;
278
+ sess. emit_err ( session_diagnostics:: IncorrectMetaItem { span : meta. span } ) ;
280
279
false
281
280
}
282
281
} ;
@@ -788,7 +787,6 @@ where
788
787
I : Iterator < Item = & ' a Attribute > ,
789
788
{
790
789
let mut depr: Option < ( Deprecation , Span ) > = None ;
791
- let diagnostic = & sess. parse_sess . span_diagnostic ;
792
790
let is_rustc = sess. features_untracked ( ) . staged_api ;
793
791
794
792
' outer: for attr in attrs_iter {
@@ -829,8 +827,12 @@ where
829
827
) ,
830
828
) ;
831
829
} 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
+ } ) ;
834
836
}
835
837
836
838
false
@@ -971,19 +973,9 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
971
973
sym:: simd => Some ( ReprSimd ) ,
972
974
sym:: transparent => Some ( ReprTransparent ) ,
973
975
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
+ } ) ;
987
979
recognised = true ;
988
980
None
989
981
}
@@ -1012,109 +1004,78 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
1012
1004
|| int_type_of_word ( name) . is_some ( )
1013
1005
{
1014
1006
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
+ } ) ;
1022
1011
}
1023
1012
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
+ } ) ;
1033
1018
}
1034
1019
} else if let Some ( meta_item) = item. meta_item ( ) {
1035
1020
if let MetaItemKind :: NameValue ( ref value) = meta_item. kind {
1036
1021
if meta_item. has_name ( sym:: align) || meta_item. has_name ( sym:: packed) {
1037
1022
let name = meta_item. name_or_empty ( ) . to_ident_string ( ) ;
1038
1023
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
+ } ) ;
1066
1045
} else {
1067
1046
if matches ! (
1068
1047
meta_item. name_or_empty( ) ,
1069
1048
sym:: C | sym:: simd | sym:: transparent
1070
1049
) || int_type_of_word ( meta_item. name_or_empty ( ) ) . is_some ( )
1071
1050
{
1072
1051
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
+ } ) ;
1081
1056
}
1082
1057
}
1083
1058
} else if let MetaItemKind :: List ( _) = meta_item. kind {
1084
1059
if meta_item. has_name ( sym:: align) {
1085
1060
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
+ } ) ;
1094
1064
} else if meta_item. has_name ( sym:: packed) {
1095
1065
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
+ } ) ;
1105
1069
} else if matches ! (
1106
1070
meta_item. name_or_empty( ) ,
1107
1071
sym:: C | sym:: simd | sym:: transparent
1108
1072
) || int_type_of_word ( meta_item. name_or_empty ( ) ) . is_some ( )
1109
1073
{
1110
1074
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
+ } ) ;
1118
1079
}
1119
1080
}
1120
1081
}
@@ -1211,10 +1172,10 @@ fn allow_unstable<'a>(
1211
1172
let list = attrs
1212
1173
. filter_map ( move |attr| {
1213
1174
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
+ } ) ;
1218
1179
None
1219
1180
} )
1220
1181
} )
@@ -1223,10 +1184,10 @@ fn allow_unstable<'a>(
1223
1184
list. into_iter ( ) . filter_map ( move |it| {
1224
1185
let name = it. ident ( ) . map ( |ident| ident. name ) ;
1225
1186
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
+ } ) ;
1230
1191
}
1231
1192
name
1232
1193
} )
0 commit comments