Skip to content

Commit 8e7dee9

Browse files
authored
Rollup merge of rust-lang#82682 - petrochenkov:cfgeval, r=Aaron1011
Implement built-in attribute macro `#[cfg_eval]` + some refactoring This PR implements a built-in attribute macro `#[cfg_eval]` as it was suggested in rust-lang#79078 to avoid `#[derive()]` without arguments being abused as a way to configure input for other attributes. The macro is used for eagerly expanding all `#[cfg]` and `#[cfg_attr]` attributes in its input ("fully configuring" the input). The effect is identical to effect of `#[derive(Foo, Bar)]` which also fully configures its input before passing it to macros `Foo` and `Bar`, but unlike `#[derive]` `#[cfg_eval]` can be applied to any syntax nodes supporting macro attributes, not only certain items. `cfg_eval` was the first name suggested in rust-lang#79078, but other alternatives are also possible, e.g. `cfg_expand`. ```rust #[cfg_eval] #[my_attr] // Receives `struct S {}` as input, the field is configured away by `#[cfg_eval]` struct S { #[cfg(FALSE)] field: u8, } ``` Tracking issue: rust-lang#82679
2 parents 2a67a31 + 0a35464 commit 8e7dee9

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

core/src/macros/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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")]

core/src/prelude/v1.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,12 @@ pub use crate::macros::builtin::derive;
8181
)]
8282
#[doc(no_inline)]
8383
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;

std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@
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)]

std/src/prelude/v1.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ pub use core::prelude::v1::derive;
6767
#[doc(hidden)]
6868
pub 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

0 commit comments

Comments
 (0)