@@ -1042,8 +1042,10 @@ fn show_hide_show_conflict_error(
1042
1042
diag. emit ( ) ;
1043
1043
}
1044
1044
1045
- /// This function checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1046
- /// `auto_cfg(show(...))` on the same item. If so, it emits an error.
1045
+ /// This functions updates the `hidden_cfg` field of the provided `cfg_info` argument.
1046
+ ///
1047
+ /// It also checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1048
+ /// `auto_cfg(show(...))` on the same item and emits an error if it's the case.
1047
1049
///
1048
1050
/// Because we go through a list of `cfg`s, we keep track of the `cfg`s we saw in `new_show_attrs`
1049
1051
/// and in `new_hide_attrs` arguments.
@@ -1095,6 +1097,31 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
1095
1097
Some ( item)
1096
1098
}
1097
1099
1100
+ fn check_changed_auto_active_status (
1101
+ changed_auto_active_status : & mut Option < rustc_span:: Span > ,
1102
+ attr : & ast:: MetaItem ,
1103
+ cfg_info : & mut CfgInfo ,
1104
+ tcx : TyCtxt < ' _ > ,
1105
+ new_value : bool ,
1106
+ ) -> bool {
1107
+ if let Some ( first_change) = changed_auto_active_status {
1108
+ if cfg_info. auto_cfg_active != new_value {
1109
+ tcx. sess
1110
+ . dcx ( )
1111
+ . struct_span_err (
1112
+ vec ! [ * first_change, attr. span] ,
1113
+ "`auto_cfg` was disabled and enabled more than once on the same item" ,
1114
+ )
1115
+ . emit ( ) ;
1116
+ return true ;
1117
+ }
1118
+ } else {
1119
+ * changed_auto_active_status = Some ( attr. span ) ;
1120
+ }
1121
+ cfg_info. auto_cfg_active = new_value;
1122
+ false
1123
+ }
1124
+
1098
1125
let mut new_show_attrs = FxHashMap :: default ( ) ;
1099
1126
let mut new_hide_attrs = FxHashMap :: default ( ) ;
1100
1127
@@ -1142,49 +1169,39 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
1142
1169
} ;
1143
1170
match & attr. kind {
1144
1171
MetaItemKind :: Word => {
1145
- if let Some ( first_change) = changed_auto_active_status {
1146
- if !cfg_info. auto_cfg_active {
1147
- tcx. sess . dcx ( ) . struct_span_err (
1148
- vec ! [ first_change, attr. span] ,
1149
- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1150
- ) . emit ( ) ;
1151
- return None ;
1152
- }
1153
- } else {
1154
- changed_auto_active_status = Some ( attr. span ) ;
1172
+ if check_changed_auto_active_status (
1173
+ & mut changed_auto_active_status,
1174
+ attr,
1175
+ cfg_info,
1176
+ tcx,
1177
+ true ,
1178
+ ) {
1179
+ return None ;
1155
1180
}
1156
- cfg_info. auto_cfg_active = true ;
1157
1181
}
1158
1182
MetaItemKind :: NameValue ( lit) => {
1159
1183
if let LitKind :: Bool ( value) = lit. kind {
1160
- if let Some ( first_change) = changed_auto_active_status {
1161
- if cfg_info. auto_cfg_active != value {
1162
- tcx. sess . dcx ( ) . struct_span_err (
1163
- vec ! [ first_change, attr. span] ,
1164
- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1165
- ) . emit ( ) ;
1166
- return None ;
1167
- }
1168
- } else {
1169
- 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
+ value,
1190
+ ) {
1191
+ return None ;
1170
1192
}
1171
- cfg_info. auto_cfg_active = value;
1172
1193
}
1173
1194
}
1174
1195
MetaItemKind :: List ( sub_attrs) => {
1175
- if let Some ( first_change) = changed_auto_active_status {
1176
- if !cfg_info. auto_cfg_active {
1177
- tcx. sess . dcx ( ) . struct_span_err (
1178
- vec ! [ first_change, attr. span] ,
1179
- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1180
- ) . emit ( ) ;
1181
- return None ;
1182
- }
1183
- } else {
1184
- 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
+ true ,
1202
+ ) {
1203
+ return None ;
1185
1204
}
1186
- // Whatever happens next, the feature is enabled again.
1187
- cfg_info. auto_cfg_active = true ;
1188
1205
for sub_attr in sub_attrs. iter ( ) {
1189
1206
if let Some ( ident) = sub_attr. ident ( )
1190
1207
&& ( ident. name == sym:: show || ident. name == sym:: hide)
0 commit comments