Skip to content

Commit 0de803c

Browse files
authored
Rollup merge of #138632 - clubby789:stabilize-cfg-boolean-lit, r=davidtwco,Urgau,traviscross
Stabilize `cfg_boolean_literals` Closes #131204 `@rustbot` labels +T-lang +I-lang-nominated This will end up conflicting with the test in #138293 so whichever doesn't land first will need updating -- # Stabilization Report ## General design ### What is the RFC for this feature and what changes have occurred to the user-facing design since the RFC was finalized? [RFC 3695](rust-lang/rfcs#3695), none. ### What behavior are we committing to that has been controversial? Summarize the major arguments pro/con. None ### Are there extensions to this feature that remain unstable? How do we know that we are not accidentally committing to those? None ## Has a call-for-testing period been conducted? If so, what feedback was received? Yes; only positive feedback was received. ## Implementation quality ### Summarize the major parts of the implementation and provide links into the code (or to PRs) Implemented in [#131034](#131034). ### Summarize existing test coverage of this feature - [Basic usage, including `#[cfg()]`, `cfg!()` and `#[cfg_attr()]`](https://github.com/rust-lang/rust/blob/6d71251cf9e40326461f90f8ff9a7024706aea87/tests/ui/cfg/true-false.rs) - [`--cfg=true/false` on the command line being accessible via `r#true/r#false`](https://github.com/rust-lang/rust/blob/6d71251cf9e40326461f90f8ff9a7024706aea87/tests/ui/cfg/raw-true-false.rs) - [Interaction with the unstable `#[doc(cfg(..))]` feature](https://github.com/rust-lang/rust/tree/6d71251/tests/rustdoc-ui/cfg-boolean-literal.rs) - [Denying `--check-cfg=cfg(true/false)`](https://github.com/rust-lang/rust/tree/6d71251/tests/ui/check-cfg/invalid-arguments.rs) - Ensuring `--cfg false` on the command line doesn't change the meaning of `cfg(false)`: `tests/ui/cfg/cmdline-false.rs` - Ensuring both `cfg(true)` and `cfg(false)` on the same item result in it being disabled: `tests/ui/cfg/both-true-false.rs` ### What outstanding bugs in the issue tracker involve this feature? Are they stabilization-blocking? The above mentioned issue; it should not block as it interacts with another unstable feature. ### What FIXMEs are still in the code for that feature and why is it ok to leave them there? None ### Summarize contributors to the feature by name for recognition and assuredness that people involved in the feature agree with stabilization - `@clubby789` (RFC) - `@Urgau` (Implementation in rustc) ### Which tools need to be adjusted to support this feature. Has this work been done? `rustdoc`'s unstable`#[doc(cfg(..)]` has been updated to respect it. `cargo` has been updated with a forward compatibility lint to enable supporting it in cargo once stabilized. ## Type system and execution rules ### What updates are needed to the reference/specification? (link to PRs when they exist) A few lines to be added to the reference for configuration predicates, specified in the RFC.
2 parents 3920514 + 303c1b4 commit 0de803c

File tree

160 files changed

+537
-633
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+537
-633
lines changed

compiler/rustc_attr_parsing/src/attributes/cfg.rs

-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_session::config::ExpectedValues;
77
use rustc_session::lint::BuiltinLintDiag;
88
use rustc_session::lint::builtin::UNEXPECTED_CFGS;
99
use rustc_session::parse::feature_err;
10-
use rustc_span::symbol::kw;
1110
use rustc_span::{Span, Symbol, sym};
1211

1312
use crate::session_diagnostics::{self, UnsupportedLiteralReason};
@@ -89,20 +88,6 @@ pub fn eval_condition(
8988
let cfg = match cfg {
9089
MetaItemInner::MetaItem(meta_item) => meta_item,
9190
MetaItemInner::Lit(MetaItemLit { kind: LitKind::Bool(b), .. }) => {
92-
if let Some(features) = features {
93-
// we can't use `try_gate_cfg` as symbols don't differentiate between `r#true`
94-
// and `true`, and we want to keep the former working without feature gate
95-
gate_cfg(
96-
&(
97-
if *b { kw::True } else { kw::False },
98-
sym::cfg_boolean_literals,
99-
|features: &Features| features.cfg_boolean_literals(),
100-
),
101-
cfg.span(),
102-
sess,
103-
features,
104-
);
105-
}
10691
return *b;
10792
}
10893
_ => {

compiler/rustc_feature/src/accepted.rs

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

compiler/rustc_feature/src/unstable.rs

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

src/doc/unstable-book/src/language-features/cfg-boolean-literals.md

-22
This file was deleted.

src/tools/rust-analyzer/crates/ide-db/src/generated/lints.rs

-29
Original file line numberDiff line numberDiff line change
@@ -3789,35 +3789,6 @@ The tracking issue for this feature is: [#64797]
37893789
[#64797]: https://github.com/rust-lang/rust/issues/64797
37903790

37913791
------------------------
3792-
"##,
3793-
default_severity: Severity::Allow,
3794-
warn_since: None,
3795-
deny_since: None,
3796-
},
3797-
Lint {
3798-
label: "cfg_boolean_literals",
3799-
description: r##"# `cfg_boolean_literals`
3800-
3801-
The tracking issue for this feature is: [#131204]
3802-
3803-
[#131204]: https://github.com/rust-lang/rust/issues/131204
3804-
3805-
------------------------
3806-
3807-
The `cfg_boolean_literals` feature makes it possible to use the `true`/`false`
3808-
literal as cfg predicate. They always evaluate to true/false respectively.
3809-
3810-
## Examples
3811-
3812-
```rust
3813-
#![feature(cfg_boolean_literals)]
3814-
3815-
#[cfg(true)]
3816-
const A: i32 = 5;
3817-
3818-
#[cfg(all(false))]
3819-
const A: i32 = 58 * 89;
3820-
```
38213792
"##,
38223793
default_severity: Severity::Allow,
38233794
warn_since: None,

tests/pretty/ast-stmt-expr-attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
fn main() {}
44

5-
#[cfg(FALSE)]
5+
#[cfg(false)]
66
fn syntax() {
77
let _ = #[attr] [];
88
let _ = #[attr] [0];

tests/pretty/enum-variant-vis.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
fn main() {}
66

7-
#[cfg(FALSE)]
7+
#[cfg(false)]
88
enum Foo { pub V, }

tests/pretty/if-attr.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ pp-exact
22

3-
#[cfg(FALSE)]
3+
#[cfg(false)]
44
fn simple_attr() {
55

66
#[attr]
@@ -10,21 +10,21 @@ fn simple_attr() {
1010
if true {}
1111
}
1212

13-
#[cfg(FALSE)]
13+
#[cfg(false)]
1414
fn if_else_chain() {
1515

1616
#[first_attr]
1717
if true {} else if false {} else {}
1818
}
1919

20-
#[cfg(FALSE)]
20+
#[cfg(false)]
2121
fn if_let() {
2222

2323
#[attr]
2424
if let Some(_) = Some(true) {}
2525
}
2626

27-
#[cfg(FALSE)]
27+
#[cfg(false)]
2828
fn let_attr_if() {
2929
let _ = #[attr] if let _ = 0 {};
3030
let _ = #[attr] if true {};

tests/pretty/nested-item-vis-defaultness.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
fn main() {}
66

7-
#[cfg(FALSE)]
7+
#[cfg(false)]
88
extern "C" {
99
static X: u8;
1010
type X;
@@ -14,7 +14,7 @@ extern "C" {
1414
pub fn foo();
1515
}
1616

17-
#[cfg(FALSE)]
17+
#[cfg(false)]
1818
trait T {
1919
const X: u8;
2020
type X;
@@ -30,7 +30,7 @@ trait T {
3030
pub default fn foo();
3131
}
3232

33-
#[cfg(FALSE)]
33+
#[cfg(false)]
3434
impl T for S {
3535
const X: u8;
3636
type X;

tests/rustdoc-ui/cfg-boolean-literal.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//@ check-pass
22

3-
#![feature(cfg_boolean_literals)]
43
#![feature(doc_cfg)]
54

65
#[doc(cfg(false))]

tests/rustdoc-ui/doc-cfg-unstable.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// #138113: rustdoc didn't gate unstable predicates inside `doc(cfg(..))`
22
#![feature(doc_cfg)]
33

4-
// `cfg_boolean_literals`
5-
#[doc(cfg(false))] //~ ERROR `cfg(false)` is experimental and subject to change
6-
pub fn cfg_boolean_literals() {}
7-
84
// `cfg_version`
95
#[doc(cfg(sanitize = "thread"))] //~ ERROR `cfg(sanitize)` is experimental and subject to change
106
pub fn cfg_sanitize() {}
+2-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
error[E0658]: `cfg(false)` is experimental and subject to change
2-
--> $DIR/doc-cfg-unstable.rs:5:11
3-
|
4-
LL | #[doc(cfg(false))]
5-
| ^^^^^
6-
|
7-
= note: see issue #131204 <https://github.com/rust-lang/rust/issues/131204> for more information
8-
= help: add `#![feature(cfg_boolean_literals)]` 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-
111
error[E0658]: `cfg(sanitize)` is experimental and subject to change
12-
--> $DIR/doc-cfg-unstable.rs:9:11
2+
--> $DIR/doc-cfg-unstable.rs:5:11
133
|
144
LL | #[doc(cfg(sanitize = "thread"))]
155
| ^^^^^^^^^^^^^^^^^^^
@@ -18,6 +8,6 @@ LL | #[doc(cfg(sanitize = "thread"))]
188
= help: add `#![feature(cfg_sanitize)]` to the crate attributes to enable
199
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2010

21-
error: aborting due to 2 previous errors
11+
error: aborting due to 1 previous error
2212

2313
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn main() {
2-
#[cfg(FALSE)]
2+
#[cfg(false)]
33
<() as module>::mac!(); //~ ERROR macros cannot use qualified paths
44
}

tests/ui/async-await/feature-async-for-loop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn f() {
1111
};
1212
}
1313

14-
#[cfg(FALSE)]
14+
#[cfg(false)]
1515
fn g() {
1616
let _ = async {
1717
for await _i in core::async_iter::from_iter(0..3) {

tests/ui/async-await/no-unsafe-async.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
struct S;
44

55
impl S {
6-
#[cfg(FALSE)]
6+
#[cfg(false)]
77
unsafe async fn g() {} //~ ERROR expected one of `extern` or `fn`, found keyword `async`
88
}
99

10-
#[cfg(FALSE)]
10+
#[cfg(false)]
1111
unsafe async fn f() {} //~ ERROR expected one of `extern` or `fn`, found keyword `async`
1212

1313
fn main() {}

tests/ui/attributes/z-crate-attr/cfg-false.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Ensure that `-Z crate-attr=cfg(FALSE)` can comment out the whole crate
2-
//@ compile-flags: --crate-type=lib -Zcrate-attr=cfg(FALSE)
1+
// Ensure that `-Z crate-attr=cfg(false)` can comment out the whole crate
2+
//@ compile-flags: --crate-type=lib -Zcrate-attr=cfg(false)
33
//@ check-pass
44

55
// NOTE: duplicate items are load-bearing

tests/ui/auto-traits/pre-cfg.rs

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

3-
#[cfg(FALSE)]
3+
#[cfg(false)]
44
auto trait Foo {}
55
//~^ WARN `auto` traits are unstable
66
//~| WARN unstable syntax can change at any point in the future, causing a hard error!
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// `#![no_std]` on a fully unconfigured crate is respected if it's placed before `cfg(FALSE)`.
1+
// `#![no_std]` on a fully unconfigured crate is respected if it's placed before `cfg(false)`.
22
// This crate has no such attribute, therefore this crate does link to libstd.
33

4-
#![cfg(FALSE)]
4+
#![cfg(false)]
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// `#![no_std]` on a fully unconfigured crate is respected if it's placed before `cfg(FALSE)`.
1+
// `#![no_std]` on a fully unconfigured crate is respected if it's placed before `cfg(false)`.
22
// Therefore this crate does link to libstd.
33

4-
#![cfg(FALSE)]
4+
#![cfg(false)]
55
#![no_std]
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// `#![no_std]` on a fully unconfigured crate is respected if it's placed before `cfg(FALSE)`.
1+
// `#![no_std]` on a fully unconfigured crate is respected if it's placed before `cfg(false)`.
22
// Therefore this crate doesn't link to libstd.
33

44
//@ no-prefer-dynamic
55

66
#![no_std]
77
#![crate_type = "lib"]
8-
#![cfg(FALSE)]
8+
#![cfg(false)]

tests/ui/cfg/auxiliary/cfged_out.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
pub mod inner {
2-
#[cfg(FALSE)]
2+
#[cfg(false)]
33
pub fn uwu() {}
44

5-
#[cfg(FALSE)]
5+
#[cfg(false)]
66
pub mod doesnt_exist {
77
pub fn hello() {}
88
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// Test that placing a `cfg(true)` and `cfg(false)` on the same item result in
2+
//. it being disabled.`
3+
4+
#[cfg(false)]
5+
#[cfg(true)]
6+
fn foo() {}
7+
8+
#[cfg(true)]
9+
#[cfg(false)]
10+
fn foo() {}
11+
12+
fn main() {
13+
foo(); //~ ERROR cannot find function `foo` in this scope
14+
}

tests/ui/cfg/both-true-false.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0425]: cannot find function `foo` in this scope
2+
--> $DIR/both-true-false.rs:13:5
3+
|
4+
LL | foo();
5+
| ^^^ not found in this scope
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0425`.

tests/ui/cfg/cfg-false-feature.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// Features above `cfg(FALSE)` are in effect in a fully unconfigured crate (issue #104633).
1+
// Features above `cfg(false)` are in effect in a fully unconfigured crate (issue #104633).
22

33
//@ check-pass
44
//@ compile-flags: --crate-type lib
55

66
#![feature(decl_macro)]
7-
#![cfg(FALSE)]
7+
#![cfg(false)]
88
#![feature(box_patterns)]
99

1010
macro mac() {} // OK

tests/ui/cfg/cfg-macros-notfoo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// check that cfg correctly chooses between the macro impls (see also
44
// cfg-macros-foo.rs)
55

6-
#[cfg(FALSE)]
6+
#[cfg(false)]
77
#[macro_use]
88
mod foo {
99
macro_rules! bar {

tests/ui/cfg/cfg-match-arm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn foo(f: Foo) {
1111
Foo::Bar => {},
1212
#[cfg(not(FALSE))]
1313
Foo::Baz => {},
14-
#[cfg(FALSE)]
14+
#[cfg(false)]
1515
Basdfwe => {}
1616
}
1717
}

tests/ui/cfg/cfg-stmt-recovery.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#[cfg_eval]
77
fn main() {
88
#[cfg_eval]
9-
let _ = #[cfg(FALSE)] 0;
9+
let _ = #[cfg(false)] 0;
1010
//~^ ERROR removing an expression is not supported in this position
1111
//~| ERROR expected expression, found `;`
1212
//~| ERROR removing an expression is not supported in this position

tests/ui/cfg/cfg-stmt-recovery.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
error: removing an expression is not supported in this position
22
--> $DIR/cfg-stmt-recovery.rs:9:13
33
|
4-
LL | let _ = #[cfg(FALSE)] 0;
4+
LL | let _ = #[cfg(false)] 0;
55
| ^^^^^^^^^^^^^
66

77
error: expected expression, found `;`
88
--> $DIR/cfg-stmt-recovery.rs:9:28
99
|
10-
LL | let _ = #[cfg(FALSE)] 0;
10+
LL | let _ = #[cfg(false)] 0;
1111
| ^ expected expression
1212

1313
error: removing an expression is not supported in this position
1414
--> $DIR/cfg-stmt-recovery.rs:9:13
1515
|
16-
LL | let _ = #[cfg(FALSE)] 0;
16+
LL | let _ = #[cfg(false)] 0;
1717
| ^^^^^^^^^^^^^
1818

1919
error: aborting due to 3 previous errors

0 commit comments

Comments
 (0)