@@ -1055,8 +1055,10 @@ fn show_hide_show_conflict_error(
1055
1055
diag. emit ( ) ;
1056
1056
}
1057
1057
1058
- /// This function checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1059
- /// `auto_cfg(show(...))` on the same item. If so, it emits an error.
1058
+ /// This functions updates the `hidden_cfg` field of the provided `cfg_info` argument.
1059
+ ///
1060
+ /// It also checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1061
+ /// `auto_cfg(show(...))` on the same item and emits an error if it's the case.
1060
1062
///
1061
1063
/// Because we go through a list of `cfg`s, we keep track of the `cfg`s we saw in `new_show_attrs`
1062
1064
/// and in `new_hide_attrs` arguments.
@@ -1108,6 +1110,31 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
1108
1110
Some ( item)
1109
1111
}
1110
1112
1113
+ fn check_changed_auto_active_status (
1114
+ changed_auto_active_status : & mut Option < rustc_span:: Span > ,
1115
+ attr : & ast:: MetaItem ,
1116
+ cfg_info : & mut CfgInfo ,
1117
+ tcx : TyCtxt < ' _ > ,
1118
+ new_value : bool ,
1119
+ ) -> bool {
1120
+ if let Some ( first_change) = changed_auto_active_status {
1121
+ if cfg_info. auto_cfg_active != new_value {
1122
+ tcx. sess
1123
+ . dcx ( )
1124
+ . struct_span_err (
1125
+ vec ! [ * first_change, attr. span] ,
1126
+ "`auto_cfg` was disabled and enabled more than once on the same item" ,
1127
+ )
1128
+ . emit ( ) ;
1129
+ return true ;
1130
+ }
1131
+ } else {
1132
+ * changed_auto_active_status = Some ( attr. span ) ;
1133
+ }
1134
+ cfg_info. auto_cfg_active = new_value;
1135
+ false
1136
+ }
1137
+
1111
1138
let mut new_show_attrs = FxHashMap :: default ( ) ;
1112
1139
let mut new_hide_attrs = FxHashMap :: default ( ) ;
1113
1140
@@ -1155,49 +1182,39 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
1155
1182
} ;
1156
1183
match & attr. kind {
1157
1184
MetaItemKind :: Word => {
1158
- if let Some ( first_change) = changed_auto_active_status {
1159
- if !cfg_info. auto_cfg_active {
1160
- tcx. sess . dcx ( ) . struct_span_err (
1161
- vec ! [ first_change, attr. span] ,
1162
- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1163
- ) . emit ( ) ;
1164
- return None ;
1165
- }
1166
- } else {
1167
- changed_auto_active_status = Some ( attr. span ) ;
1185
+ if check_changed_auto_active_status (
1186
+ & mut changed_auto_active_status,
1187
+ attr,
1188
+ cfg_info,
1189
+ tcx,
1190
+ true ,
1191
+ ) {
1192
+ return None ;
1168
1193
}
1169
- cfg_info. auto_cfg_active = true ;
1170
1194
}
1171
1195
MetaItemKind :: NameValue ( lit) => {
1172
1196
if let LitKind :: Bool ( value) = lit. kind {
1173
- if let Some ( first_change) = changed_auto_active_status {
1174
- if cfg_info. auto_cfg_active != value {
1175
- tcx. sess . dcx ( ) . struct_span_err (
1176
- vec ! [ first_change, attr. span] ,
1177
- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1178
- ) . emit ( ) ;
1179
- return None ;
1180
- }
1181
- } else {
1182
- changed_auto_active_status = Some ( attr. span ) ;
1197
+ if check_changed_auto_active_status (
1198
+ & mut changed_auto_active_status,
1199
+ attr,
1200
+ cfg_info,
1201
+ tcx,
1202
+ value,
1203
+ ) {
1204
+ return None ;
1183
1205
}
1184
- cfg_info. auto_cfg_active = value;
1185
1206
}
1186
1207
}
1187
1208
MetaItemKind :: List ( sub_attrs) => {
1188
- if let Some ( first_change) = changed_auto_active_status {
1189
- if !cfg_info. auto_cfg_active {
1190
- tcx. sess . dcx ( ) . struct_span_err (
1191
- vec ! [ first_change, attr. span] ,
1192
- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1193
- ) . emit ( ) ;
1194
- return None ;
1195
- }
1196
- } else {
1197
- changed_auto_active_status = Some ( attr. span ) ;
1209
+ if check_changed_auto_active_status (
1210
+ & mut changed_auto_active_status,
1211
+ attr,
1212
+ cfg_info,
1213
+ tcx,
1214
+ true ,
1215
+ ) {
1216
+ return None ;
1198
1217
}
1199
- // Whatever happens next, the feature is enabled again.
1200
- cfg_info. auto_cfg_active = true ;
1201
1218
for sub_attr in sub_attrs. iter ( ) {
1202
1219
if let Some ( ident) = sub_attr. ident ( )
1203
1220
&& ( ident. name == sym:: show || ident. name == sym:: hide)
0 commit comments