@@ -1055,8 +1055,10 @@ fn show_hide_show_conflict_error(
10551055 diag. emit ( ) ;
10561056}
10571057
1058- /// This function checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1059- /// `auto_cfg(show(...))` on the same item. If so, it emits an error.
1058+ /// This functions updates the `hidden_cfg` field of the provided `cfg_info` argument.
1059+ ///
1060+ /// It also checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1061+ /// `auto_cfg(show(...))` on the same item and emits an error if it's the case.
10601062///
10611063/// Because we go through a list of `cfg`s, we keep track of the `cfg`s we saw in `new_show_attrs`
10621064/// and in `new_hide_attrs` arguments.
@@ -1108,6 +1110,31 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
11081110 Some ( item)
11091111 }
11101112
1113+ fn check_changed_auto_active_status (
1114+ changed_auto_active_status : & mut Option < rustc_span:: Span > ,
1115+ attr : & ast:: MetaItem ,
1116+ cfg_info : & mut CfgInfo ,
1117+ tcx : TyCtxt < ' _ > ,
1118+ new_value : bool ,
1119+ ) -> bool {
1120+ if let Some ( first_change) = changed_auto_active_status {
1121+ if cfg_info. auto_cfg_active != new_value {
1122+ tcx. sess
1123+ . dcx ( )
1124+ . struct_span_err (
1125+ vec ! [ * first_change, attr. span] ,
1126+ "`auto_cfg` was disabled and enabled more than once on the same item" ,
1127+ )
1128+ . emit ( ) ;
1129+ return true ;
1130+ }
1131+ } else {
1132+ * changed_auto_active_status = Some ( attr. span ) ;
1133+ }
1134+ cfg_info. auto_cfg_active = new_value;
1135+ false
1136+ }
1137+
11111138 let mut new_show_attrs = FxHashMap :: default ( ) ;
11121139 let mut new_hide_attrs = FxHashMap :: default ( ) ;
11131140
@@ -1155,49 +1182,39 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
11551182 } ;
11561183 match & attr. kind {
11571184 MetaItemKind :: Word => {
1158- if let Some ( first_change) = changed_auto_active_status {
1159- if !cfg_info. auto_cfg_active {
1160- tcx. sess . dcx ( ) . struct_span_err (
1161- vec ! [ first_change, attr. span] ,
1162- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1163- ) . emit ( ) ;
1164- return None ;
1165- }
1166- } else {
1167- changed_auto_active_status = Some ( attr. span ) ;
1185+ if check_changed_auto_active_status (
1186+ & mut changed_auto_active_status,
1187+ attr,
1188+ cfg_info,
1189+ tcx,
1190+ true ,
1191+ ) {
1192+ return None ;
11681193 }
1169- cfg_info. auto_cfg_active = true ;
11701194 }
11711195 MetaItemKind :: NameValue ( lit) => {
11721196 if let LitKind :: Bool ( value) = lit. kind {
1173- if let Some ( first_change) = changed_auto_active_status {
1174- if cfg_info. auto_cfg_active != value {
1175- tcx. sess . dcx ( ) . struct_span_err (
1176- vec ! [ first_change, attr. span] ,
1177- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1178- ) . emit ( ) ;
1179- return None ;
1180- }
1181- } else {
1182- changed_auto_active_status = Some ( attr. span ) ;
1197+ if check_changed_auto_active_status (
1198+ & mut changed_auto_active_status,
1199+ attr,
1200+ cfg_info,
1201+ tcx,
1202+ value,
1203+ ) {
1204+ return None ;
11831205 }
1184- cfg_info. auto_cfg_active = value;
11851206 }
11861207 }
11871208 MetaItemKind :: List ( sub_attrs) => {
1188- if let Some ( first_change) = changed_auto_active_status {
1189- if !cfg_info. auto_cfg_active {
1190- tcx. sess . dcx ( ) . struct_span_err (
1191- vec ! [ first_change, attr. span] ,
1192- "`auto_cfg` was disabled and enabled more than once on the same item" ,
1193- ) . emit ( ) ;
1194- return None ;
1195- }
1196- } else {
1197- changed_auto_active_status = Some ( attr. span ) ;
1209+ if check_changed_auto_active_status (
1210+ & mut changed_auto_active_status,
1211+ attr,
1212+ cfg_info,
1213+ tcx,
1214+ true ,
1215+ ) {
1216+ return None ;
11981217 }
1199- // Whatever happens next, the feature is enabled again.
1200- cfg_info. auto_cfg_active = true ;
12011218 for sub_attr in sub_attrs. iter ( ) {
12021219 if let Some ( ident) = sub_attr. ident ( )
12031220 && ( ident. name == sym:: show || ident. name == sym:: hide)
0 commit comments