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