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