@@ -1082,8 +1082,10 @@ fn show_hide_show_conflict_error(
10821082 diag. emit ( ) ;
10831083}
10841084
1085- /// This function checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1086- /// `auto_cfg(show(...))` on the same item. If so, it emits an error.
1085+ /// This functions updates the `hidden_cfg` field of the provided `cfg_info` argument.
1086+ ///
1087+ /// It also checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1088+ /// `auto_cfg(show(...))` on the same item and emits an error if it's the case.
10871089///
10881090/// Because we go through a list of `cfg`s, we keep track of the `cfg`s we saw in `new_show_attrs`
10891091/// and in `new_hide_attrs` arguments.
@@ -1135,6 +1137,31 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
11351137 Some ( item)
11361138 }
11371139
1140+ fn check_changed_auto_active_status (
1141+ changed_auto_active_status : & mut Option < rustc_span:: Span > ,
1142+ attr : & ast:: MetaItem ,
1143+ cfg_info : & mut CfgInfo ,
1144+ tcx : TyCtxt < ' _ > ,
1145+ new_value : bool ,
1146+ ) -> bool {
1147+ if let Some ( first_change) = changed_auto_active_status {
1148+ if cfg_info. auto_cfg_active != new_value {
1149+ tcx. sess
1150+ . dcx ( )
1151+ . struct_span_err (
1152+ vec ! [ * first_change, attr. span] ,
1153+ "`auto_cfg` was disabled and enabled more than once on the same item" ,
1154+ )
1155+ . emit ( ) ;
1156+ return true ;
1157+ }
1158+ } else {
1159+ * changed_auto_active_status = Some ( attr. span ) ;
1160+ }
1161+ cfg_info. auto_cfg_active = new_value;
1162+ false
1163+ }
1164+
11381165 let mut new_show_attrs = FxHashMap :: default ( ) ;
11391166 let mut new_hide_attrs = FxHashMap :: default ( ) ;
11401167
@@ -1182,49 +1209,39 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
11821209 } ;
11831210 match & attr. kind {
11841211 MetaItemKind :: Word => {
1185- if let Some ( first_change) = changed_auto_active_status {
1186- if !cfg_info. auto_cfg_active {
1187- tcx. sess . dcx ( ) . struct_span_err (
1188- vec ! [ first_change, attr. span] ,
1189- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1190- ) . emit ( ) ;
1191- return None ;
1192- }
1193- } else {
1194- changed_auto_active_status = Some ( attr. span ) ;
1212+ if check_changed_auto_active_status (
1213+ & mut changed_auto_active_status,
1214+ attr,
1215+ cfg_info,
1216+ tcx,
1217+ true ,
1218+ ) {
1219+ return None ;
11951220 }
1196- cfg_info. auto_cfg_active = true ;
11971221 }
11981222 MetaItemKind :: NameValue ( lit) => {
11991223 if let LitKind :: Bool ( value) = lit. kind {
1200- if let Some ( first_change) = changed_auto_active_status {
1201- if cfg_info. auto_cfg_active != value {
1202- tcx. sess . dcx ( ) . struct_span_err (
1203- vec ! [ first_change, attr. span] ,
1204- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1205- ) . emit ( ) ;
1206- return None ;
1207- }
1208- } else {
1209- changed_auto_active_status = Some ( attr. span ) ;
1224+ if check_changed_auto_active_status (
1225+ & mut changed_auto_active_status,
1226+ attr,
1227+ cfg_info,
1228+ tcx,
1229+ value,
1230+ ) {
1231+ return None ;
12101232 }
1211- cfg_info. auto_cfg_active = value;
12121233 }
12131234 }
12141235 MetaItemKind :: List ( sub_attrs) => {
1215- if let Some ( first_change) = changed_auto_active_status {
1216- if !cfg_info. auto_cfg_active {
1217- tcx. sess . dcx ( ) . struct_span_err (
1218- vec ! [ first_change, attr. span] ,
1219- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1220- ) . emit ( ) ;
1221- return None ;
1222- }
1223- } else {
1224- changed_auto_active_status = Some ( attr. span ) ;
1236+ if check_changed_auto_active_status (
1237+ & mut changed_auto_active_status,
1238+ attr,
1239+ cfg_info,
1240+ tcx,
1241+ true ,
1242+ ) {
1243+ return None ;
12251244 }
1226- // Whatever happens next, the feature is enabled again.
1227- cfg_info. auto_cfg_active = true ;
12281245 for sub_attr in sub_attrs. iter ( ) {
12291246 if let Some ( ident) = sub_attr. ident ( )
12301247 && ( ident. name == sym:: show || ident. name == sym:: hide)
0 commit comments