File tree Expand file tree Collapse file tree 11 files changed +259
-0
lines changed Expand file tree Collapse file tree 11 files changed +259
-0
lines changed Original file line number Diff line number Diff line change
1
+ use crate :: util:: check_builtin_macro_attribute;
2
+
3
+ use rustc_ast:: { self as ast, AstLike } ;
4
+ use rustc_expand:: base:: { Annotatable , ExtCtxt } ;
5
+ use rustc_expand:: config:: StripUnconfigured ;
6
+ use rustc_span:: symbol:: sym;
7
+ use rustc_span:: Span ;
8
+
9
+ pub fn expand (
10
+ ecx : & mut ExtCtxt < ' _ > ,
11
+ _span : Span ,
12
+ meta_item : & ast:: MetaItem ,
13
+ item : Annotatable ,
14
+ ) -> Vec < Annotatable > {
15
+ check_builtin_macro_attribute ( ecx, meta_item, sym:: cfg_eval) ;
16
+
17
+ let mut visitor =
18
+ StripUnconfigured { sess : ecx. sess , features : ecx. ecfg . features , modified : false } ;
19
+ let mut item = visitor. fully_configure ( item) ;
20
+ if visitor. modified {
21
+ // Erase the tokens if cfg-stripping modified the item
22
+ // This will cause us to synthesize fake tokens
23
+ // when `nt_to_tokenstream` is called on this item.
24
+ if let Some ( tokens) = item. tokens_mut ( ) {
25
+ * tokens = None ;
26
+ }
27
+ }
28
+ vec ! [ item]
29
+ }
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ mod asm;
24
24
mod assert;
25
25
mod cfg;
26
26
mod cfg_accessible;
27
+ mod cfg_eval;
27
28
mod compile_error;
28
29
mod concat;
29
30
mod concat_idents;
@@ -89,6 +90,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
89
90
register_attr! {
90
91
bench: test:: expand_bench,
91
92
cfg_accessible: cfg_accessible:: Expander ,
93
+ cfg_eval: cfg_eval:: expand,
92
94
derive: derive:: Expander ,
93
95
global_allocator: global_allocator:: expand,
94
96
test: test:: expand_test,
Original file line number Diff line number Diff line change @@ -344,6 +344,7 @@ symbols! {
344
344
cfg_attr,
345
345
cfg_attr_multi,
346
346
cfg_doctest,
347
+ cfg_eval,
347
348
cfg_panic,
348
349
cfg_sanitize,
349
350
cfg_target_feature,
Original file line number Diff line number Diff line change @@ -1452,6 +1452,18 @@ pub(crate) mod builtin {
1452
1452
/* compiler built-in */
1453
1453
}
1454
1454
1455
+ /// Expands all `#[cfg]` and `#[cfg_attr]` attributes in the code fragment it's applied to.
1456
+ #[ cfg( not( bootstrap) ) ]
1457
+ #[ unstable(
1458
+ feature = "cfg_eval" ,
1459
+ issue = "82679" ,
1460
+ reason = "`cfg_eval` is a recently implemented feature"
1461
+ ) ]
1462
+ #[ rustc_builtin_macro]
1463
+ pub macro cfg_eval( $( $tt: tt) * ) {
1464
+ /* compiler built-in */
1465
+ }
1466
+
1455
1467
/// Unstable implementation detail of the `rustc` compiler, do not use.
1456
1468
#[ rustc_builtin_macro]
1457
1469
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
Original file line number Diff line number Diff line change @@ -81,3 +81,12 @@ pub use crate::macros::builtin::derive;
81
81
) ]
82
82
#[ doc( no_inline) ]
83
83
pub use crate :: macros:: builtin:: cfg_accessible;
84
+
85
+ #[ cfg( not( bootstrap) ) ]
86
+ #[ unstable(
87
+ feature = "cfg_eval" ,
88
+ issue = "82679" ,
89
+ reason = "`cfg_eval` is a recently implemented feature"
90
+ ) ]
91
+ #[ doc( no_inline) ]
92
+ pub use crate :: macros:: builtin:: cfg_eval;
Original file line number Diff line number Diff line change 234
234
#![ feature( box_syntax) ]
235
235
#![ feature( c_variadic) ]
236
236
#![ feature( cfg_accessible) ]
237
+ #![ cfg_attr( not( bootstrap) , feature( cfg_eval) ) ]
237
238
#![ feature( cfg_target_has_atomic) ]
238
239
#![ feature( cfg_target_thread_local) ]
239
240
#![ feature( char_error_internals) ]
Original file line number Diff line number Diff line change @@ -67,6 +67,15 @@ pub use core::prelude::v1::derive;
67
67
#[ doc( hidden) ]
68
68
pub use core:: prelude:: v1:: cfg_accessible;
69
69
70
+ #[ cfg( not( bootstrap) ) ]
71
+ #[ unstable(
72
+ feature = "cfg_eval" ,
73
+ issue = "82679" ,
74
+ reason = "`cfg_eval` is a recently implemented feature"
75
+ ) ]
76
+ #[ doc( hidden) ]
77
+ pub use core:: prelude:: v1:: cfg_eval;
78
+
70
79
// The file so far is equivalent to src/libcore/prelude/v1.rs,
71
80
// and below to src/liballoc/prelude.rs.
72
81
// Those files are duplicated rather than using glob imports
Original file line number Diff line number Diff line change
1
+ #![ feature( cfg_eval) ]
2
+ #![ feature( stmt_expr_attributes) ]
3
+
4
+ fn main ( ) {
5
+ let _ = #[ cfg_eval] #[ cfg( FALSE ) ] 0 ;
6
+ //~^ ERROR removing an expression is not supported in this position
7
+ //~| ERROR removing an expression is not supported in this position
8
+ //~| ERROR removing an expression is not supported in this position
9
+ }
Original file line number Diff line number Diff line change
1
+ error: removing an expression is not supported in this position
2
+ --> $DIR/cfg-eval-fail.rs:5:25
3
+ |
4
+ LL | let _ = #[cfg_eval] #[cfg(FALSE)] 0;
5
+ | ^^^^^^^^^^^^^
6
+
7
+ error: removing an expression is not supported in this position
8
+ --> $DIR/cfg-eval-fail.rs:5:25
9
+ |
10
+ LL | let _ = #[cfg_eval] #[cfg(FALSE)] 0;
11
+ | ^^^^^^^^^^^^^
12
+
13
+ error: removing an expression is not supported in this position
14
+ --> $DIR/cfg-eval-fail.rs:5:25
15
+ |
16
+ LL | let _ = #[cfg_eval] #[cfg(FALSE)] 0;
17
+ | ^^^^^^^^^^^^^
18
+
19
+ error: aborting due to 3 previous errors
20
+
Original file line number Diff line number Diff line change
1
+ // check-pass
2
+ // compile-flags: -Z span-debug
3
+ // aux-build:test-macros.rs
4
+
5
+ #![ feature( cfg_eval) ]
6
+ #![ feature( proc_macro_hygiene) ]
7
+ #![ feature( stmt_expr_attributes) ]
8
+
9
+ #![ no_std] // Don't load unnecessary hygiene information from std
10
+ extern crate std;
11
+
12
+ #[ macro_use]
13
+ extern crate test_macros;
14
+
15
+ #[ cfg_eval]
16
+ #[ print_attr]
17
+ struct S1 {
18
+ #[ cfg( FALSE ) ]
19
+ field_false : u8 ,
20
+ #[ cfg( all( /*true*/ ) ) ]
21
+ #[ cfg_attr( FALSE , unknown_attr) ]
22
+ #[ cfg_attr( all( /*true*/ ) , allow( ) ) ]
23
+ field_true : u8 ,
24
+ }
25
+
26
+ #[ cfg_eval]
27
+ #[ cfg( FALSE ) ]
28
+ struct S2 { }
29
+
30
+ fn main ( ) {
31
+ let _ = #[ cfg_eval] #[ print_attr] ( #[ cfg( FALSE ) ] 0 , #[ cfg ( all ( /*true*/ ) ) ] 1 ) ;
32
+ }
You can’t perform that action at this time.
0 commit comments