@@ -1082,8 +1082,10 @@ fn show_hide_show_conflict_error(
1082
1082
diag. emit ( ) ;
1083
1083
}
1084
1084
1085
- /// This function checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1086
- /// `auto_cfg(show(...))` on the same item. If so, it emits an error.
1085
+ /// This functions updates the `hidden_cfg` field of the provided `cfg_info` argument.
1086
+ ///
1087
+ /// It also checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1088
+ /// `auto_cfg(show(...))` on the same item and emits an error if it's the case.
1087
1089
///
1088
1090
/// Because we go through a list of `cfg`s, we keep track of the `cfg`s we saw in `new_show_attrs`
1089
1091
/// and in `new_hide_attrs` arguments.
@@ -1135,6 +1137,31 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
1135
1137
Some ( item)
1136
1138
}
1137
1139
1140
+ fn check_changed_auto_active_status (
1141
+ changed_auto_active_status : & mut Option < rustc_span:: Span > ,
1142
+ attr : & ast:: MetaItem ,
1143
+ cfg_info : & mut CfgInfo ,
1144
+ tcx : TyCtxt < ' _ > ,
1145
+ new_value : bool ,
1146
+ ) -> bool {
1147
+ if let Some ( first_change) = changed_auto_active_status {
1148
+ if cfg_info. auto_cfg_active != new_value {
1149
+ tcx. sess
1150
+ . dcx ( )
1151
+ . struct_span_err (
1152
+ vec ! [ * first_change, attr. span] ,
1153
+ "`auto_cfg` was disabled and enabled more than once on the same item" ,
1154
+ )
1155
+ . emit ( ) ;
1156
+ return true ;
1157
+ }
1158
+ } else {
1159
+ * changed_auto_active_status = Some ( attr. span ) ;
1160
+ }
1161
+ cfg_info. auto_cfg_active = new_value;
1162
+ false
1163
+ }
1164
+
1138
1165
let mut new_show_attrs = FxHashMap :: default ( ) ;
1139
1166
let mut new_hide_attrs = FxHashMap :: default ( ) ;
1140
1167
@@ -1182,49 +1209,39 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
1182
1209
} ;
1183
1210
match & attr. kind {
1184
1211
MetaItemKind :: Word => {
1185
- if let Some ( first_change) = changed_auto_active_status {
1186
- if !cfg_info. auto_cfg_active {
1187
- tcx. sess . dcx ( ) . struct_span_err (
1188
- vec ! [ first_change, attr. span] ,
1189
- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1190
- ) . emit ( ) ;
1191
- return None ;
1192
- }
1193
- } else {
1194
- changed_auto_active_status = Some ( attr. span ) ;
1212
+ if check_changed_auto_active_status (
1213
+ & mut changed_auto_active_status,
1214
+ attr,
1215
+ cfg_info,
1216
+ tcx,
1217
+ true ,
1218
+ ) {
1219
+ return None ;
1195
1220
}
1196
- cfg_info. auto_cfg_active = true ;
1197
1221
}
1198
1222
MetaItemKind :: NameValue ( lit) => {
1199
1223
if let LitKind :: Bool ( value) = lit. kind {
1200
- if let Some ( first_change) = changed_auto_active_status {
1201
- if cfg_info. auto_cfg_active != value {
1202
- tcx. sess . dcx ( ) . struct_span_err (
1203
- vec ! [ first_change, attr. span] ,
1204
- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1205
- ) . emit ( ) ;
1206
- return None ;
1207
- }
1208
- } else {
1209
- changed_auto_active_status = Some ( attr. span ) ;
1224
+ if check_changed_auto_active_status (
1225
+ & mut changed_auto_active_status,
1226
+ attr,
1227
+ cfg_info,
1228
+ tcx,
1229
+ value,
1230
+ ) {
1231
+ return None ;
1210
1232
}
1211
- cfg_info. auto_cfg_active = value;
1212
1233
}
1213
1234
}
1214
1235
MetaItemKind :: List ( sub_attrs) => {
1215
- if let Some ( first_change) = changed_auto_active_status {
1216
- if !cfg_info. auto_cfg_active {
1217
- tcx. sess . dcx ( ) . struct_span_err (
1218
- vec ! [ first_change, attr. span] ,
1219
- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1220
- ) . emit ( ) ;
1221
- return None ;
1222
- }
1223
- } else {
1224
- changed_auto_active_status = Some ( attr. span ) ;
1236
+ if check_changed_auto_active_status (
1237
+ & mut changed_auto_active_status,
1238
+ attr,
1239
+ cfg_info,
1240
+ tcx,
1241
+ true ,
1242
+ ) {
1243
+ return None ;
1225
1244
}
1226
- // Whatever happens next, the feature is enabled again.
1227
- cfg_info. auto_cfg_active = true ;
1228
1245
for sub_attr in sub_attrs. iter ( ) {
1229
1246
if let Some ( ident) = sub_attr. ident ( )
1230
1247
&& ( ident. name == sym:: show || ident. name == sym:: hide)
0 commit comments