Stabilize cfg_boolean_literals

This commit is contained in:
clubby789 2025-03-18 01:01:15 +00:00
parent 946aea0b3d
commit 984c51f6a1
15 changed files with 26 additions and 163 deletions

View file

@ -7,7 +7,6 @@ use rustc_session::config::ExpectedValues;
use rustc_session::lint::BuiltinLintDiag;
use rustc_session::lint::builtin::UNEXPECTED_CFGS;
use rustc_session::parse::feature_err;
use rustc_span::symbol::kw;
use rustc_span::{Span, Symbol, sym};
use crate::session_diagnostics::{self, UnsupportedLiteralReason};
@ -89,20 +88,6 @@ pub fn eval_condition(
let cfg = match cfg {
MetaItemInner::MetaItem(meta_item) => meta_item,
MetaItemInner::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;
}
_ => {

View file

@ -95,6 +95,8 @@ declare_features! (
(accepted, c_unwind, "1.81.0", Some(74990)),
/// Allows `#[cfg_attr(predicate, multiple, attributes, here)]`.
(accepted, cfg_attr_multi, "1.33.0", Some(54881)),
/// Allows the use of `#[cfg(<true/false>)]`.
(accepted, cfg_boolean_literals, "CURRENT_RUSTC_VERSION", Some(131204)),
/// Allows the use of `#[cfg(doctest)]`, set when rustdoc is collecting doctests.
(accepted, cfg_doctest, "1.40.0", Some(62210)),
/// Enables `#[cfg(panic = "...")]` config key.

View file

@ -391,8 +391,6 @@ declare_features! (
(unstable, async_trait_bounds, "1.85.0", Some(62290)),
/// Allows using C-variadics.
(unstable, c_variadic, "1.34.0", Some(44930)),
/// Allows the use of `#[cfg(<true/false>)]`.
(unstable, cfg_boolean_literals, "1.83.0", Some(131204)),
/// Allows the use of `#[cfg(contract_checks)` to check if contract checks are enabled.
(unstable, cfg_contract_checks, "1.86.0", Some(128044)),
/// Allows the use of `#[cfg(overflow_checks)` to check if integer overflow behaviour.

View file

@ -1,22 +0,0 @@
# `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;
```

View file

@ -3789,35 +3789,6 @@ The tracking issue for this feature is: [#64797]
[#64797]: https://github.com/rust-lang/rust/issues/64797
------------------------
"##,
default_severity: Severity::Allow,
warn_since: None,
deny_since: None,
},
Lint {
label: "cfg_boolean_literals",
description: r##"# `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;
```
"##,
default_severity: Severity::Allow,
warn_since: None,

View file

@ -1,6 +1,5 @@
//@ check-pass
#![feature(cfg_boolean_literals)]
#![feature(doc_cfg)]
#[doc(cfg(false))]

View file

@ -1,10 +1,6 @@
// #138113: rustdoc didn't gate unstable predicates inside `doc(cfg(..))`
#![feature(doc_cfg)]
// `cfg_boolean_literals`
#[doc(cfg(false))] //~ ERROR `cfg(false)` is experimental and subject to change
pub fn cfg_boolean_literals() {}
// `cfg_version`
#[doc(cfg(sanitize = "thread"))] //~ ERROR `cfg(sanitize)` is experimental and subject to change
pub fn cfg_sanitize() {}

View file

@ -1,15 +1,5 @@
error[E0658]: `cfg(false)` is experimental and subject to change
--> $DIR/doc-cfg-unstable.rs:5:11
|
LL | #[doc(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(sanitize)` is experimental and subject to change
--> $DIR/doc-cfg-unstable.rs:9:11
--> $DIR/doc-cfg-unstable.rs:5:11
|
LL | #[doc(cfg(sanitize = "thread"))]
| ^^^^^^^^^^^^^^^^^^^
@ -18,6 +8,6 @@ LL | #[doc(cfg(sanitize = "thread"))]
= help: add `#![feature(cfg_sanitize)]` 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 2 previous errors
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0658`.

View file

@ -1,7 +1,6 @@
//@ run-pass
#![feature(link_cfg)]
#![feature(cfg_boolean_literals)]
#[cfg(true)]
fn foo() -> bool {

View file

@ -1,10 +0,0 @@
#[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
}

View file

@ -1,43 +0,0 @@
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`.

View file

@ -1,6 +1,5 @@
//@ check-pass
#![feature(cfg_boolean_literals)]
#![warn(unused)]
macro_rules! foo {

View file

@ -1,41 +1,41 @@
warning: unused attribute `inline`
--> $DIR/inert-attr-macro.rs:11:5
--> $DIR/inert-attr-macro.rs:10:5
|
LL | #[inline] foo!();
| ^^^^^^^^^
|
note: the built-in attribute `inline` will be ignored, since it's applied to the macro invocation `foo`
--> $DIR/inert-attr-macro.rs:11:15
--> $DIR/inert-attr-macro.rs:10:15
|
LL | #[inline] foo!();
| ^^^
note: the lint level is defined here
--> $DIR/inert-attr-macro.rs:4:9
--> $DIR/inert-attr-macro.rs:3:9
|
LL | #![warn(unused)]
| ^^^^^^
= note: `#[warn(unused_attributes)]` implied by `#[warn(unused)]`
warning: unused attribute `allow`
--> $DIR/inert-attr-macro.rs:15:5
--> $DIR/inert-attr-macro.rs:14:5
|
LL | #[allow(warnings)] #[inline] foo!();
| ^^^^^^^^^^^^^^^^^^
|
note: the built-in attribute `allow` will be ignored, since it's applied to the macro invocation `foo`
--> $DIR/inert-attr-macro.rs:15:34
--> $DIR/inert-attr-macro.rs:14:34
|
LL | #[allow(warnings)] #[inline] foo!();
| ^^^
warning: unused attribute `inline`
--> $DIR/inert-attr-macro.rs:15:24
--> $DIR/inert-attr-macro.rs:14:24
|
LL | #[allow(warnings)] #[inline] foo!();
| ^^^^^^^^^
|
note: the built-in attribute `inline` will be ignored, since it's applied to the macro invocation `foo`
--> $DIR/inert-attr-macro.rs:15:34
--> $DIR/inert-attr-macro.rs:14:34
|
LL | #[allow(warnings)] #[inline] foo!();
| ^^^

View file

@ -3,7 +3,6 @@
//@ check-pass
//@ proc-macro: test-macros.rs
#![feature(cfg_boolean_literals)]
#![feature(cfg_eval)]
#[macro_use]

View file

@ -4,75 +4,75 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
spacing: Alone,
span: #0 bytes(305..306),
span: #0 bytes(271..272),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "test_macros",
span: #0 bytes(322..333),
span: #0 bytes(288..299),
},
Punct {
ch: ':',
spacing: Joint,
span: #0 bytes(333..334),
span: #0 bytes(299..300),
},
Punct {
ch: ':',
spacing: Alone,
span: #0 bytes(334..335),
span: #0 bytes(300..301),
},
Ident {
ident: "print_attr",
span: #0 bytes(335..345),
span: #0 bytes(301..311),
},
],
span: #0 bytes(306..347),
span: #0 bytes(272..313),
},
Ident {
ident: "struct",
span: #0 bytes(348..354),
span: #0 bytes(314..320),
},
Ident {
ident: "S",
span: #0 bytes(355..356),
span: #0 bytes(321..322),
},
Punct {
ch: ';',
spacing: Alone,
span: #0 bytes(356..357),
span: #0 bytes(322..323),
},
]
PRINT-ATTR INPUT (DISPLAY): struct S;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
span: #0 bytes(348..354),
span: #0 bytes(314..320),
},
Ident {
ident: "S",
span: #0 bytes(355..356),
span: #0 bytes(321..322),
},
Punct {
ch: ';',
spacing: Alone,
span: #0 bytes(356..357),
span: #0 bytes(322..323),
},
]
PRINT-ATTR INPUT (DISPLAY): struct Z;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
span: #0 bytes(411..417),
span: #0 bytes(377..383),
},
Ident {
ident: "Z",
span: #0 bytes(418..419),
span: #0 bytes(384..385),
},
Punct {
ch: ';',
spacing: Alone,
span: #0 bytes(419..420),
span: #0 bytes(385..386),
},
]