@@ -192,38 +192,32 @@ fn get_features(sess: &Session, krate_attrs: &[ast::Attribute]) -> Features {
192192}
193193
194194/// `cfg_attr`-process the crate's attributes and compute the crate's features.
195- pub fn features (
196- sess : & Session ,
197- mut krate : ast:: Crate ,
198- lint_node_id : NodeId ,
199- ) -> ( ast:: Crate , Features ) {
195+ pub fn features ( sess : & Session , krate : & mut ast:: Crate , lint_node_id : NodeId ) -> Features {
200196 let mut strip_unconfigured =
201197 StripUnconfigured { sess, features : None , config_tokens : false , lint_node_id } ;
202198
203- let unconfigured_attrs = krate. attrs . clone ( ) ;
199+ let mut unconfigured_attrs = krate. attrs . clone ( ) ;
204200 let diag = & sess. parse_sess . span_diagnostic ;
205201 let err_count = diag. err_count ( ) ;
206- let features = match strip_unconfigured. configure_krate_attrs ( krate. attrs ) {
207- None => {
208- // The entire crate is unconfigured.
209- krate. attrs = ast:: AttrVec :: new ( ) ;
210- krate. items = ThinVec :: new ( ) ;
211- Features :: default ( )
212- }
213- Some ( attrs) => {
214- krate. attrs = attrs;
215- let features = get_features ( sess, & krate. attrs ) ;
216- if err_count == diag. err_count ( ) {
217- // Avoid reconfiguring malformed `cfg_attr`s.
218- strip_unconfigured. features = Some ( & features) ;
219- // Run configuration again, this time with features available
220- // so that we can perform feature-gating.
221- strip_unconfigured. configure_krate_attrs ( unconfigured_attrs) ;
222- }
223- features
202+
203+ krate. attrs . flat_map_in_place ( |attr| strip_unconfigured. process_cfg_attr ( & attr) ) ;
204+ if !strip_unconfigured. in_cfg ( & krate. attrs ) {
205+ // The entire crate is unconfigured.
206+ krate. attrs = ast:: AttrVec :: new ( ) ;
207+ krate. items = ThinVec :: new ( ) ;
208+ Features :: default ( )
209+ } else {
210+ let features = get_features ( sess, & krate. attrs ) ;
211+ if err_count == diag. err_count ( ) {
212+ // Avoid reconfiguring malformed `cfg_attr`s.
213+ strip_unconfigured. features = Some ( & features) ;
214+ // Run configuration again, this time with features available
215+ // so that we can perform feature-gating.
216+ unconfigured_attrs. flat_map_in_place ( |attr| strip_unconfigured. process_cfg_attr ( & attr) ) ;
217+ strip_unconfigured. in_cfg ( & unconfigured_attrs) ;
224218 }
225- } ;
226- ( krate , features )
219+ features
220+ }
227221}
228222
229223#[ macro_export]
@@ -254,11 +248,6 @@ impl<'a> StripUnconfigured<'a> {
254248 }
255249 }
256250
257- fn configure_krate_attrs ( & self , mut attrs : ast:: AttrVec ) -> Option < ast:: AttrVec > {
258- attrs. flat_map_in_place ( |attr| self . process_cfg_attr ( attr) ) ;
259- self . in_cfg ( & attrs) . then_some ( attrs)
260- }
261-
262251 /// Performs cfg-expansion on `stream`, producing a new `AttrTokenStream`.
263252 /// This is only used during the invocation of `derive` proc-macros,
264253 /// which require that we cfg-expand their entire input.
@@ -281,7 +270,7 @@ impl<'a> StripUnconfigured<'a> {
281270 . iter ( )
282271 . flat_map ( |tree| match tree. clone ( ) {
283272 AttrTokenTree :: Attributes ( mut data) => {
284- data. attrs . flat_map_in_place ( |attr| self . process_cfg_attr ( attr) ) ;
273+ data. attrs . flat_map_in_place ( |attr| self . process_cfg_attr ( & attr) ) ;
285274
286275 if self . in_cfg ( & data. attrs ) {
287276 data. tokens = LazyAttrTokenStream :: new (
@@ -319,12 +308,16 @@ impl<'a> StripUnconfigured<'a> {
319308 /// the syntax of any `cfg_attr` is incorrect.
320309 fn process_cfg_attrs < T : HasAttrs > ( & self , node : & mut T ) {
321310 node. visit_attrs ( |attrs| {
322- attrs. flat_map_in_place ( |attr| self . process_cfg_attr ( attr) ) ;
311+ attrs. flat_map_in_place ( |attr| self . process_cfg_attr ( & attr) ) ;
323312 } ) ;
324313 }
325314
326- fn process_cfg_attr ( & self , attr : Attribute ) -> Vec < Attribute > {
327- if attr. has_name ( sym:: cfg_attr) { self . expand_cfg_attr ( attr, true ) } else { vec ! [ attr] }
315+ fn process_cfg_attr ( & self , attr : & Attribute ) -> Vec < Attribute > {
316+ if attr. has_name ( sym:: cfg_attr) {
317+ self . expand_cfg_attr ( attr, true )
318+ } else {
319+ vec ! [ attr. clone( ) ]
320+ }
328321 }
329322
330323 /// Parse and expand a single `cfg_attr` attribute into a list of attributes
@@ -334,9 +327,9 @@ impl<'a> StripUnconfigured<'a> {
334327 /// Gives a compiler warning when the `cfg_attr` contains no attributes and
335328 /// is in the original source file. Gives a compiler error if the syntax of
336329 /// the attribute is incorrect.
337- pub ( crate ) fn expand_cfg_attr ( & self , attr : Attribute , recursive : bool ) -> Vec < Attribute > {
330+ pub ( crate ) fn expand_cfg_attr ( & self , attr : & Attribute , recursive : bool ) -> Vec < Attribute > {
338331 let Some ( ( cfg_predicate, expanded_attrs) ) =
339- rustc_parse:: parse_cfg_attr ( & attr, & self . sess . parse_sess ) else {
332+ rustc_parse:: parse_cfg_attr ( attr, & self . sess . parse_sess ) else {
340333 return vec ! [ ] ;
341334 } ;
342335
@@ -365,10 +358,10 @@ impl<'a> StripUnconfigured<'a> {
365358 // `#[cfg_attr(false, cfg_attr(true, some_attr))]`.
366359 expanded_attrs
367360 . into_iter ( )
368- . flat_map ( |item| self . process_cfg_attr ( self . expand_cfg_attr_item ( & attr, item) ) )
361+ . flat_map ( |item| self . process_cfg_attr ( & self . expand_cfg_attr_item ( attr, item) ) )
369362 . collect ( )
370363 } else {
371- expanded_attrs. into_iter ( ) . map ( |item| self . expand_cfg_attr_item ( & attr, item) ) . collect ( )
364+ expanded_attrs. into_iter ( ) . map ( |item| self . expand_cfg_attr_item ( attr, item) ) . collect ( )
372365 }
373366 }
374367
0 commit comments