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