about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUrgau <urgau@numericable.fr>2024-09-19 11:19:35 +0200
committerUrgau <urgau@numericable.fr>2024-10-04 09:09:20 +0200
commit62ef411631efb25134e29da76fcdb7802aa94bd4 (patch)
tree52298c664f291f37a910d8a0c2e10b1e062314f5
parentc99f29b29fa4115436c352a921f9963b173b5608 (diff)
downloadrust-62ef411631efb25134e29da76fcdb7802aa94bd4.tar.gz
rust-62ef411631efb25134e29da76fcdb7802aa94bd4.zip
Feature gate boolean lit support in cfg predicates
-rw-r--r--compiler/rustc_attr/src/builtin.rs20
-rw-r--r--compiler/rustc_feature/src/unstable.rs2
-rw-r--r--compiler/rustc_span/src/symbol.rs1
-rw-r--r--src/doc/unstable-book/src/language-features/cfg-boolean-literals.md22
-rw-r--r--tests/ui/cfg/raw-true-false.rs19
-rw-r--r--tests/ui/cfg/true-false.rs1
-rw-r--r--tests/ui/feature-gates/feature-gate-cfg-boolean-literals.rs10
-rw-r--r--tests/ui/feature-gates/feature-gate-cfg-boolean-literals.stderr43
8 files changed, 116 insertions, 2 deletions
diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs
index 930fdeb975d..a3c6132d484 100644
--- a/compiler/rustc_attr/src/builtin.rs
+++ b/compiler/rustc_attr/src/builtin.rs
@@ -18,7 +18,7 @@ use rustc_session::parse::feature_err;
 use rustc_session::{RustcVersion, Session};
 use rustc_span::Span;
 use rustc_span::hygiene::Transparency;
-use rustc_span::symbol::{Symbol, sym};
+use rustc_span::symbol::{Symbol, kw, sym};
 
 use crate::fluent_generated;
 use crate::session_diagnostics::{self, IncorrectReprFormatGenericCause};
@@ -603,7 +603,23 @@ pub fn eval_condition(
 
     let cfg = match cfg {
         ast::NestedMetaItem::MetaItem(meta_item) => meta_item,
-        ast::NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Bool(b), .. }) => return *b,
+        ast::NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Bool(b), .. }) => {
+            if let Some(features) = features {
+                // we can't use `try_gate_cfg` as symbols don't differentiate between `r#true`
+                // and `true`, and we want to keep the former working without feature gate
+                gate_cfg(
+                    &((
+                        if *b { kw::True } else { kw::False },
+                        sym::cfg_boolean_literals,
+                        |features: &Features| features.cfg_boolean_literals,
+                    )),
+                    cfg.span(),
+                    sess,
+                    features,
+                );
+            }
+            return *b;
+        }
         _ => {
             dcx.emit_err(session_diagnostics::UnsupportedLiteral {
                 span: cfg.span(),
diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs
index 1ffd35dbf91..643a95e6ea1 100644
--- a/compiler/rustc_feature/src/unstable.rs
+++ b/compiler/rustc_feature/src/unstable.rs
@@ -371,6 +371,8 @@ declare_features! (
     (unstable, async_for_loop, "1.77.0", Some(118898)),
     /// Allows using C-variadics.
     (unstable, c_variadic, "1.34.0", Some(44930)),
+    /// Allows the use of `#[cfg(<true/false>)]`.
+    (unstable, cfg_boolean_literals, "CURRENT_RUSTC_VERSION", Some(131204)),
     /// Allows the use of `#[cfg(overflow_checks)` to check if integer overflow behaviour.
     (unstable, cfg_overflow_checks, "1.71.0", Some(111466)),
     /// Provides the relocation model information as cfg entry
diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs
index 8f226b26bef..ea8c5f135ab 100644
--- a/compiler/rustc_span/src/symbol.rs
+++ b/compiler/rustc_span/src/symbol.rs
@@ -543,6 +543,7 @@ symbols! {
         cfg_accessible,
         cfg_attr,
         cfg_attr_multi,
+        cfg_boolean_literals,
         cfg_doctest,
         cfg_eval,
         cfg_fmt_debug,
diff --git a/src/doc/unstable-book/src/language-features/cfg-boolean-literals.md b/src/doc/unstable-book/src/language-features/cfg-boolean-literals.md
new file mode 100644
index 00000000000..ad795ff9d9b
--- /dev/null
+++ b/src/doc/unstable-book/src/language-features/cfg-boolean-literals.md
@@ -0,0 +1,22 @@
+# `cfg_boolean_literals`
+
+The tracking issue for this feature is: [#131204]
+
+[#131204]: https://github.com/rust-lang/rust/issues/131204
+
+------------------------
+
+The `cfg_boolean_literals` feature makes it possible to use the `true`/`false`
+literal as cfg predicate. They always evaluate to true/false respectively.
+
+## Examples
+
+```rust
+#![feature(cfg_boolean_literals)]
+
+#[cfg(true)]
+const A: i32 = 5;
+
+#[cfg(all(false))]
+const A: i32 = 58 * 89;
+```
diff --git a/tests/ui/cfg/raw-true-false.rs b/tests/ui/cfg/raw-true-false.rs
new file mode 100644
index 00000000000..4cb8bb71c92
--- /dev/null
+++ b/tests/ui/cfg/raw-true-false.rs
@@ -0,0 +1,19 @@
+//@ check-pass
+//@ compile-flags: --cfg false --check-cfg=cfg(r#false)
+
+#![deny(warnings)]
+
+#[expect(unexpected_cfgs)]
+mod a {
+  #[cfg(r#true)]
+  pub fn foo() {}
+}
+
+mod b {
+  #[cfg(r#false)]
+  pub fn bar() {}
+}
+
+fn main() {
+    b::bar()
+}
diff --git a/tests/ui/cfg/true-false.rs b/tests/ui/cfg/true-false.rs
index 0bd1cf427fa..03d96fbafec 100644
--- a/tests/ui/cfg/true-false.rs
+++ b/tests/ui/cfg/true-false.rs
@@ -1,6 +1,7 @@
 //@ run-pass
 
 #![feature(link_cfg)]
+#![feature(cfg_boolean_literals)]
 
 #[cfg(true)]
 fn foo() -> bool {
diff --git a/tests/ui/feature-gates/feature-gate-cfg-boolean-literals.rs b/tests/ui/feature-gates/feature-gate-cfg-boolean-literals.rs
new file mode 100644
index 00000000000..6784b445049
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-cfg-boolean-literals.rs
@@ -0,0 +1,10 @@
+#[cfg(true)] //~ ERROR `cfg(true)` is experimental
+fn foo() {}
+
+#[cfg_attr(true, cfg(false))] //~ ERROR `cfg(true)` is experimental
+//~^ ERROR `cfg(false)` is experimental
+fn foo() {}
+
+fn main() {
+    cfg!(false); //~ ERROR `cfg(false)` is experimental
+}
diff --git a/tests/ui/feature-gates/feature-gate-cfg-boolean-literals.stderr b/tests/ui/feature-gates/feature-gate-cfg-boolean-literals.stderr
new file mode 100644
index 00000000000..64491464f1d
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-cfg-boolean-literals.stderr
@@ -0,0 +1,43 @@
+error[E0658]: `cfg(true)` is experimental and subject to change
+  --> $DIR/feature-gate-cfg-boolean-literals.rs:1:7
+   |
+LL | #[cfg(true)]
+   |       ^^^^
+   |
+   = note: see issue #131204 <https://github.com/rust-lang/rust/issues/131204> for more information
+   = help: add `#![feature(cfg_boolean_literals)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+
+error[E0658]: `cfg(true)` is experimental and subject to change
+  --> $DIR/feature-gate-cfg-boolean-literals.rs:4:12
+   |
+LL | #[cfg_attr(true, cfg(false))]
+   |            ^^^^
+   |
+   = note: see issue #131204 <https://github.com/rust-lang/rust/issues/131204> for more information
+   = help: add `#![feature(cfg_boolean_literals)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+
+error[E0658]: `cfg(false)` is experimental and subject to change
+  --> $DIR/feature-gate-cfg-boolean-literals.rs:4:22
+   |
+LL | #[cfg_attr(true, cfg(false))]
+   |                      ^^^^^
+   |
+   = note: see issue #131204 <https://github.com/rust-lang/rust/issues/131204> for more information
+   = help: add `#![feature(cfg_boolean_literals)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+
+error[E0658]: `cfg(false)` is experimental and subject to change
+  --> $DIR/feature-gate-cfg-boolean-literals.rs:9:10
+   |
+LL |     cfg!(false);
+   |          ^^^^^
+   |
+   = note: see issue #131204 <https://github.com/rust-lang/rust/issues/131204> for more information
+   = help: add `#![feature(cfg_boolean_literals)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0658`.