@@ -1035,8 +1035,10 @@ fn show_hide_show_conflict_error(
10351035 diag. emit ( ) ;
10361036}
10371037
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.
10401042///
10411043/// Because we go through a list of `cfg`s, we keep track of the `cfg`s we saw in `new_show_attrs`
10421044/// and in `new_hide_attrs` arguments.
@@ -1088,6 +1090,31 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
10881090 Some ( item)
10891091 }
10901092
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+
10911118 let mut new_show_attrs = FxHashMap :: default ( ) ;
10921119 let mut new_hide_attrs = FxHashMap :: default ( ) ;
10931120
@@ -1135,49 +1162,39 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
11351162 } ;
11361163 match & attr. kind {
11371164 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 ;
11481173 }
1149- cfg_info. auto_cfg_active = true ;
11501174 }
11511175 MetaItemKind :: NameValue ( lit) => {
11521176 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 ;
11631185 }
1164- cfg_info. auto_cfg_active = value;
11651186 }
11661187 }
11671188 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 ;
11781197 }
1179- // Whatever happens next, the feature is enabled again.
1180- cfg_info. auto_cfg_active = true ;
11811198 for sub_attr in sub_attrs. iter ( ) {
11821199 if let Some ( ident) = sub_attr. ident ( )
11831200 && ( ident. name == sym:: show || ident. name == sym:: hide)
0 commit comments