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