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