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