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