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;
2424mod assert;
2525mod cfg;
2626mod cfg_accessible;
27+ mod cfg_eval;
2728mod compile_error;
2829mod concat;
2930mod concat_idents;
@@ -89,6 +90,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
8990 register_attr! {
9091 bench: test:: expand_bench,
9192 cfg_accessible: cfg_accessible:: Expander ,
93+ cfg_eval: cfg_eval:: expand,
9294 derive: derive:: Expander ,
9395 global_allocator: global_allocator:: expand,
9496 test: test:: expand_test,
Original file line number Diff line number Diff line change @@ -344,6 +344,7 @@ symbols! {
344344 cfg_attr,
345345 cfg_attr_multi,
346346 cfg_doctest,
347+ cfg_eval,
347348 cfg_panic,
348349 cfg_sanitize,
349350 cfg_target_feature,
Original file line number Diff line number Diff line change @@ -1452,6 +1452,18 @@ pub(crate) mod builtin {
14521452 /* compiler built-in */
14531453 }
14541454
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+
14551467 /// Unstable implementation detail of the `rustc` compiler, do not use.
14561468 #[ rustc_builtin_macro]
14571469 #[ 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;
8181) ]
8282#[ doc( no_inline) ]
8383pub 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 234234#![ feature( box_syntax) ]
235235#![ feature( c_variadic) ]
236236#![ feature( cfg_accessible) ]
237+ #![ cfg_attr( not( bootstrap) , feature( cfg_eval) ) ]
237238#![ feature( cfg_target_has_atomic) ]
238239#![ feature( cfg_target_thread_local) ]
239240#![ feature( char_error_internals) ]
Original file line number Diff line number Diff line change @@ -67,6 +67,15 @@ pub use core::prelude::v1::derive;
6767#[ doc( hidden) ]
6868pub use core:: prelude:: v1:: cfg_accessible;
6969
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+
7079// The file so far is equivalent to src/libcore/prelude/v1.rs,
7180// and below to src/liballoc/prelude.rs.
7281// 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