Skip to content

Commit 5794918

Browse files
committed
Feature gate boolean lit support in cfg predicates
1 parent 6b45e4f commit 5794918

File tree

8 files changed

+116
-2
lines changed

8 files changed

+116
-2
lines changed

compiler/rustc_attr/src/builtin.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_session::parse::feature_err;
1818
use rustc_session::{RustcVersion, Session};
1919
use rustc_span::Span;
2020
use rustc_span::hygiene::Transparency;
21-
use rustc_span::symbol::{Symbol, sym};
21+
use rustc_span::symbol::{Symbol, kw, sym};
2222

2323
use crate::fluent_generated;
2424
use crate::session_diagnostics::{self, IncorrectReprFormatGenericCause};
@@ -603,7 +603,23 @@ pub fn eval_condition(
603603

604604
let cfg = match cfg {
605605
ast::NestedMetaItem::MetaItem(meta_item) => meta_item,
606-
ast::NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Bool(b), .. }) => return *b,
606+
ast::NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Bool(b), .. }) => {
607+
if let Some(features) = features {
608+
// we can't use `try_gate_cfg` as symbols don't differentiate between `r#true`
609+
// and `true`, and we want to keep the former working without feature gate
610+
gate_cfg(
611+
&((
612+
if *b { kw::True } else { kw::False },
613+
sym::cfg_boolean_literal,
614+
|features: &Features| features.cfg_boolean_literal,
615+
)),
616+
cfg.span(),
617+
sess,
618+
features,
619+
);
620+
}
621+
return *b;
622+
}
607623
_ => {
608624
dcx.emit_err(session_diagnostics::UnsupportedLiteral {
609625
span: cfg.span(),

compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ declare_features! (
371371
(unstable, async_for_loop, "1.77.0", Some(118898)),
372372
/// Allows using C-variadics.
373373
(unstable, c_variadic, "1.34.0", Some(44930)),
374+
/// Allows the use of `#[cfg(<true/false>)]`.
375+
(unstable, cfg_boolean_literal, "CURRENT_RUSTC_VERSION", Some(999999)),
374376
/// Allows the use of `#[cfg(overflow_checks)` to check if integer overflow behaviour.
375377
(unstable, cfg_overflow_checks, "1.71.0", Some(111466)),
376378
/// Provides the relocation model information as cfg entry

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ symbols! {
543543
cfg_accessible,
544544
cfg_attr,
545545
cfg_attr_multi,
546+
cfg_boolean_literal,
546547
cfg_doctest,
547548
cfg_eval,
548549
cfg_fmt_debug,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# `cfg_boolean_literal`
2+
3+
The tracking issue for this feature is: [#00000]
4+
5+
[#00000]: https://github.com/rust-lang/rust/issues/00000
6+
7+
------------------------
8+
9+
The `cfg_boolean_literal` feature makes it possible to use the `true`/`false`
10+
literal as cfg predicate. They always evaluate to true/false respectively.
11+
12+
## Examples
13+
14+
```rust
15+
#![feature(cfg_boolean_literal)]
16+
17+
#[cfg(true)]
18+
const A: i32 = 5;
19+
20+
#[cfg(all(false))]
21+
const A: i32 = 58 * 89;
22+
```

tests/ui/cfg/raw-true-false.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ check-pass
2+
//@ compile-flags: --cfg false --check-cfg=cfg(r#false)
3+
4+
#![deny(warnings)]
5+
6+
#[expect(unexpected_cfgs)]
7+
mod a {
8+
#[cfg(r#true)]
9+
pub fn foo() {}
10+
}
11+
12+
mod b {
13+
#[cfg(r#false)]
14+
pub fn bar() {}
15+
}
16+
17+
fn main() {
18+
b::bar()
19+
}

tests/ui/cfg/true-false.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ run-pass
22

33
#![feature(link_cfg)]
4+
#![feature(cfg_boolean_literal)]
45

56
#[cfg(true)]
67
fn foo() -> bool {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[cfg(true)] //~ ERROR `cfg(true)` is experimental
2+
fn foo() {}
3+
4+
#[cfg_attr(true, cfg(false))] //~ ERROR `cfg(true)` is experimental
5+
//~^ ERROR `cfg(false)` is experimental
6+
fn foo() {}
7+
8+
fn main() {
9+
cfg!(false); //~ ERROR `cfg(false)` is experimental
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0658]: `cfg(true)` is experimental and subject to change
2+
--> $DIR/feature-gate-cfg-boolean-literal.rs:1:7
3+
|
4+
LL | #[cfg(true)]
5+
| ^^^^
6+
|
7+
= note: see issue #999999 <https://github.com/rust-lang/rust/issues/999999> for more information
8+
= help: add `#![feature(cfg_boolean_literal)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: `cfg(true)` is experimental and subject to change
12+
--> $DIR/feature-gate-cfg-boolean-literal.rs:4:12
13+
|
14+
LL | #[cfg_attr(true, cfg(false))]
15+
| ^^^^
16+
|
17+
= note: see issue #999999 <https://github.com/rust-lang/rust/issues/999999> for more information
18+
= help: add `#![feature(cfg_boolean_literal)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error[E0658]: `cfg(false)` is experimental and subject to change
22+
--> $DIR/feature-gate-cfg-boolean-literal.rs:4:22
23+
|
24+
LL | #[cfg_attr(true, cfg(false))]
25+
| ^^^^^
26+
|
27+
= note: see issue #999999 <https://github.com/rust-lang/rust/issues/999999> for more information
28+
= help: add `#![feature(cfg_boolean_literal)]` to the crate attributes to enable
29+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
30+
31+
error[E0658]: `cfg(false)` is experimental and subject to change
32+
--> $DIR/feature-gate-cfg-boolean-literal.rs:9:10
33+
|
34+
LL | cfg!(false);
35+
| ^^^^^
36+
|
37+
= note: see issue #999999 <https://github.com/rust-lang/rust/issues/999999> for more information
38+
= help: add `#![feature(cfg_boolean_literal)]` to the crate attributes to enable
39+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
40+
41+
error: aborting due to 4 previous errors
42+
43+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)