@@ -1068,8 +1068,10 @@ fn show_hide_show_conflict_error(
1068
1068
diag. emit ( ) ;
1069
1069
}
1070
1070
1071
- /// This function checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1072
- /// `auto_cfg(show(...))` on the same item. If so, it emits an error.
1071
+ /// This functions updates the `hidden_cfg` field of the provided `cfg_info` argument.
1072
+ ///
1073
+ /// It also checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1074
+ /// `auto_cfg(show(...))` on the same item and emits an error if it's the case.
1073
1075
///
1074
1076
/// Because we go through a list of `cfg`s, we keep track of the `cfg`s we saw in `new_show_attrs`
1075
1077
/// and in `new_hide_attrs` arguments.
@@ -1121,6 +1123,31 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
1121
1123
Some ( item)
1122
1124
}
1123
1125
1126
+ fn check_changed_auto_active_status (
1127
+ changed_auto_active_status : & mut Option < rustc_span:: Span > ,
1128
+ attr : & ast:: MetaItem ,
1129
+ cfg_info : & mut CfgInfo ,
1130
+ tcx : TyCtxt < ' _ > ,
1131
+ new_value : bool ,
1132
+ ) -> bool {
1133
+ if let Some ( first_change) = changed_auto_active_status {
1134
+ if cfg_info. auto_cfg_active != new_value {
1135
+ tcx. sess
1136
+ . dcx ( )
1137
+ . struct_span_err (
1138
+ vec ! [ * first_change, attr. span] ,
1139
+ "`auto_cfg` was disabled and enabled more than once on the same item" ,
1140
+ )
1141
+ . emit ( ) ;
1142
+ return true ;
1143
+ }
1144
+ } else {
1145
+ * changed_auto_active_status = Some ( attr. span ) ;
1146
+ }
1147
+ cfg_info. auto_cfg_active = new_value;
1148
+ false
1149
+ }
1150
+
1124
1151
let mut new_show_attrs = FxHashMap :: default ( ) ;
1125
1152
let mut new_hide_attrs = FxHashMap :: default ( ) ;
1126
1153
@@ -1168,49 +1195,39 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
1168
1195
} ;
1169
1196
match & attr. kind {
1170
1197
MetaItemKind :: Word => {
1171
- if let Some ( first_change) = changed_auto_active_status {
1172
- if !cfg_info. auto_cfg_active {
1173
- tcx. sess . dcx ( ) . struct_span_err (
1174
- vec ! [ first_change, attr. span] ,
1175
- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1176
- ) . emit ( ) ;
1177
- return None ;
1178
- }
1179
- } else {
1180
- changed_auto_active_status = Some ( attr. span ) ;
1198
+ if check_changed_auto_active_status (
1199
+ & mut changed_auto_active_status,
1200
+ attr,
1201
+ cfg_info,
1202
+ tcx,
1203
+ true ,
1204
+ ) {
1205
+ return None ;
1181
1206
}
1182
- cfg_info. auto_cfg_active = true ;
1183
1207
}
1184
1208
MetaItemKind :: NameValue ( lit) => {
1185
1209
if let LitKind :: Bool ( value) = lit. kind {
1186
- if let Some ( first_change) = changed_auto_active_status {
1187
- if cfg_info. auto_cfg_active != value {
1188
- tcx. sess . dcx ( ) . struct_span_err (
1189
- vec ! [ first_change, attr. span] ,
1190
- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1191
- ) . emit ( ) ;
1192
- return None ;
1193
- }
1194
- } else {
1195
- changed_auto_active_status = Some ( attr. span ) ;
1210
+ if check_changed_auto_active_status (
1211
+ & mut changed_auto_active_status,
1212
+ attr,
1213
+ cfg_info,
1214
+ tcx,
1215
+ value,
1216
+ ) {
1217
+ return None ;
1196
1218
}
1197
- cfg_info. auto_cfg_active = value;
1198
1219
}
1199
1220
}
1200
1221
MetaItemKind :: List ( sub_attrs) => {
1201
- if let Some ( first_change) = changed_auto_active_status {
1202
- if !cfg_info. auto_cfg_active {
1203
- tcx. sess . dcx ( ) . struct_span_err (
1204
- vec ! [ first_change, attr. span] ,
1205
- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1206
- ) . emit ( ) ;
1207
- return None ;
1208
- }
1209
- } else {
1210
- changed_auto_active_status = Some ( attr. span ) ;
1222
+ if check_changed_auto_active_status (
1223
+ & mut changed_auto_active_status,
1224
+ attr,
1225
+ cfg_info,
1226
+ tcx,
1227
+ true ,
1228
+ ) {
1229
+ return None ;
1211
1230
}
1212
- // Whatever happens next, the feature is enabled again.
1213
- cfg_info. auto_cfg_active = true ;
1214
1231
for sub_attr in sub_attrs. iter ( ) {
1215
1232
if let Some ( ident) = sub_attr. ident ( )
1216
1233
&& ( ident. name == sym:: show || ident. name == sym:: hide)
0 commit comments