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