@@ -192,38 +192,32 @@ fn get_features(sess: &Session, krate_attrs: &[ast::Attribute]) -> Features {
192
192
}
193
193
194
194
/// `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 {
200
196
let mut strip_unconfigured =
201
197
StripUnconfigured { sess, features : None , config_tokens : false , lint_node_id } ;
202
198
203
- let unconfigured_attrs = krate. attrs . clone ( ) ;
199
+ let mut unconfigured_attrs = krate. attrs . clone ( ) ;
204
200
let diag = & sess. parse_sess . span_diagnostic ;
205
201
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) ;
224
218
}
225
- } ;
226
- ( krate , features )
219
+ features
220
+ }
227
221
}
228
222
229
223
#[ macro_export]
@@ -254,11 +248,6 @@ impl<'a> StripUnconfigured<'a> {
254
248
}
255
249
}
256
250
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
-
262
251
/// Performs cfg-expansion on `stream`, producing a new `AttrTokenStream`.
263
252
/// This is only used during the invocation of `derive` proc-macros,
264
253
/// which require that we cfg-expand their entire input.
@@ -281,7 +270,7 @@ impl<'a> StripUnconfigured<'a> {
281
270
. iter ( )
282
271
. flat_map ( |tree| match tree. clone ( ) {
283
272
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) ) ;
285
274
286
275
if self . in_cfg ( & data. attrs ) {
287
276
data. tokens = LazyAttrTokenStream :: new (
@@ -319,12 +308,16 @@ impl<'a> StripUnconfigured<'a> {
319
308
/// the syntax of any `cfg_attr` is incorrect.
320
309
fn process_cfg_attrs < T : HasAttrs > ( & self , node : & mut T ) {
321
310
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) ) ;
323
312
} ) ;
324
313
}
325
314
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
+ }
328
321
}
329
322
330
323
/// Parse and expand a single `cfg_attr` attribute into a list of attributes
@@ -334,9 +327,9 @@ impl<'a> StripUnconfigured<'a> {
334
327
/// Gives a compiler warning when the `cfg_attr` contains no attributes and
335
328
/// is in the original source file. Gives a compiler error if the syntax of
336
329
/// 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 > {
338
331
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 {
340
333
return vec ! [ ] ;
341
334
} ;
342
335
@@ -365,10 +358,10 @@ impl<'a> StripUnconfigured<'a> {
365
358
// `#[cfg_attr(false, cfg_attr(true, some_attr))]`.
366
359
expanded_attrs
367
360
. 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) ) )
369
362
. collect ( )
370
363
} 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 ( )
372
365
}
373
366
}
374
367
0 commit comments