From 2aa0e828302672eb1010811eebbd6aaf64c8ee4a Mon Sep 17 00:00:00 2001 From: clubby789 Date: Mon, 16 Sep 2024 10:58:25 -0300 Subject: [PATCH 01/13] `cfg` boolean literals --- text/0000-cfg_boolean_literal.md | 74 ++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 text/0000-cfg_boolean_literal.md diff --git a/text/0000-cfg_boolean_literal.md b/text/0000-cfg_boolean_literal.md new file mode 100644 index 00000000..1757e03d --- /dev/null +++ b/text/0000-cfg_boolean_literal.md @@ -0,0 +1,74 @@ +- Feature Name: `cfg_boolean_literal` +- Start Date: 2024-09-16 +- RFC PR: [rust-lang/rfcs#0000](https://github.com/rust-lang/rfcs/pull/0000) +- Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000) + +# Summary +[summary]: #summary + +Allow `true` and `false` boolean literals as `cfg` predicates, i.e. `cfg(true)`/`cfg(false)`. + +# Motivation +[motivation]: #motivation + +Often, we may want to temporarily disable a block of code while working on a project; this can be useful, for example, to disable functions which have errors while refactoring a codebase. + +Currently, the easiest ways for programmers to do this are to comment out the code block (which means syntax highlighting no longer works), or to use `cfg(any())` (which is not explicit in meaning). + +By allowing `#[cfg(false)]`, we can provide programmers with an explicit and more intuitive way to disable code, while retaining IDE functionality. + +Allowing `cfg(true)` would also make temporarily enabling `cfg`'ed out code easier; a `true` may be added to the end of a `cfg(any(..))` list. + +# Guide-level explanation +[guide-level-explanation]: #guide-level-explanation + +Boolean literals (i.e. `true` and `false`) may be used as `cfg` predicates, to evaluate as always true/false respectively. + +# Reference-level explanation +[reference-level-explanation]: #reference-level-explanation + +The syntax for configuration predicates should be extended to include boolean literals: + +> **Syntax**\ +> _ConfigurationPredicate_ :\ +>       _ConfigurationOption_\ +>    | _ConfigurationAll_\ +>    | _ConfigurationAny_\ +>    | _ConfigurationNot_ \ +>    | `true` | `false` + +And the line +> - `true` or `false` literals, which are always `true`/`false` respectively + +should be added to the explanation of the predicates. + +# Drawbacks +[drawbacks]: #drawbacks + +By making it more convenient, this may encourage unconditionally disabled blocks of code being committed, which is undesirable. + +# Rationale and alternatives +[rationale-and-alternatives]: #rationale-and-alternatives + +- This could instead be spelled as `cfg(disabled|enabled)`, or `cfg(none)` for disabling code only +- Giving special meaning to a valid identifier can change the meaning of existing code (although `check-cfg` warnings should reduce the impact of this) +- As the existing predicates evaluate to booleans, using boolean literals is the most intuitive way to spell this + +# Prior art +[prior-art]: #prior-art + +Many languages with conditional compilation constructs have a way to disable a blog entirely. + +- C: `#if 0` +- C#: `#if false` +- Dlang: `version(none)` +- Haskell: `#if 0` + +Searching for `cfg(false)` on [GitHub](https://github.com/search?q=%23%5Bcfg%28false%29%5D+language%3ARust&type=code) reveals many examples of projects (including Rust itself) using `cfg(FALSE)` as a way to get this behavior - although this raises a `check-cfg` warning. + +# Future possibilities +[future-possibilities]: #future-possibilities + +A future lint could suggest replacing constructs such as `cfg(any())` with `cfg(false)`. + +The `check-cfg` lint could be with a special case for identifiers such as `FALSE` and suggest `cfg(false)` instead. From 599dc26961c8aa2bee891bbf05630df234501c41 Mon Sep 17 00:00:00 2001 From: clubby789 Date: Mon, 16 Sep 2024 11:03:43 -0300 Subject: [PATCH 02/13] Add RFC PR number --- text/0000-cfg_boolean_literal.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/0000-cfg_boolean_literal.md b/text/0000-cfg_boolean_literal.md index 1757e03d..b791e851 100644 --- a/text/0000-cfg_boolean_literal.md +++ b/text/0000-cfg_boolean_literal.md @@ -1,6 +1,6 @@ - Feature Name: `cfg_boolean_literal` - Start Date: 2024-09-16 -- RFC PR: [rust-lang/rfcs#0000](https://github.com/rust-lang/rfcs/pull/0000) +- RFC PR: [rust-lang/rfcs#3695](https://github.com/rust-lang/rfcs/pull/3695) - Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000) # Summary From 9466a6637af5efb3d5f248b8ad20900c9988b1a4 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Mon, 16 Sep 2024 14:26:32 -0400 Subject: [PATCH 03/13] Fix typo Co-authored-by: lolbinarycat --- text/0000-cfg_boolean_literal.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/0000-cfg_boolean_literal.md b/text/0000-cfg_boolean_literal.md index b791e851..d2c4f5fe 100644 --- a/text/0000-cfg_boolean_literal.md +++ b/text/0000-cfg_boolean_literal.md @@ -57,7 +57,7 @@ By making it more convenient, this may encourage unconditionally disabled blocks # Prior art [prior-art]: #prior-art -Many languages with conditional compilation constructs have a way to disable a blog entirely. +Many languages with conditional compilation constructs have a way to disable a block entirely. - C: `#if 0` - C#: `#if false` From 7f542ea80775812cadef5bd0a738e089a19bafec Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Mon, 16 Sep 2024 14:27:03 -0400 Subject: [PATCH 04/13] Generalize a description --- text/0000-cfg_boolean_literal.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/0000-cfg_boolean_literal.md b/text/0000-cfg_boolean_literal.md index d2c4f5fe..f3683650 100644 --- a/text/0000-cfg_boolean_literal.md +++ b/text/0000-cfg_boolean_literal.md @@ -17,7 +17,7 @@ Currently, the easiest ways for programmers to do this are to comment out the co By allowing `#[cfg(false)]`, we can provide programmers with an explicit and more intuitive way to disable code, while retaining IDE functionality. -Allowing `cfg(true)` would also make temporarily enabling `cfg`'ed out code easier; a `true` may be added to the end of a `cfg(any(..))` list. +Allowing `cfg(true)` would also make temporarily enabling `cfg`'ed out code easier; a `true` may be added to a `cfg(any(..))` list. # Guide-level explanation [guide-level-explanation]: #guide-level-explanation From ed27bd16bbc891fbbcbde6a8b56fa71340bb2c9f Mon Sep 17 00:00:00 2001 From: clubby789 Date: Tue, 17 Sep 2024 06:57:42 -0300 Subject: [PATCH 05/13] Update filename --- text/{0000-cfg_boolean_literal.md => 3695-cfg_boolean_literal.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename text/{0000-cfg_boolean_literal.md => 3695-cfg_boolean_literal.md} (100%) diff --git a/text/0000-cfg_boolean_literal.md b/text/3695-cfg_boolean_literal.md similarity index 100% rename from text/0000-cfg_boolean_literal.md rename to text/3695-cfg_boolean_literal.md From 4aac81db6e7d00435035a02d5154790eb5a36a0c Mon Sep 17 00:00:00 2001 From: clubby789 Date: Tue, 17 Sep 2024 07:00:05 -0300 Subject: [PATCH 06/13] Merge bullet points for clarity --- text/3695-cfg_boolean_literal.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/text/3695-cfg_boolean_literal.md b/text/3695-cfg_boolean_literal.md index f3683650..4b0939c8 100644 --- a/text/3695-cfg_boolean_literal.md +++ b/text/3695-cfg_boolean_literal.md @@ -50,8 +50,7 @@ By making it more convenient, this may encourage unconditionally disabled blocks # Rationale and alternatives [rationale-and-alternatives]: #rationale-and-alternatives -- This could instead be spelled as `cfg(disabled|enabled)`, or `cfg(none)` for disabling code only -- Giving special meaning to a valid identifier can change the meaning of existing code (although `check-cfg` warnings should reduce the impact of this) +- This could instead be spelled as `cfg(disabled|enabled)`, or `cfg(none)` for disabling code only. However, giving special meaning to a valid identifier can change the meaning of existing code (although `check-cfg` warnings should reduce the impact of this) - As the existing predicates evaluate to booleans, using boolean literals is the most intuitive way to spell this # Prior art From e0fb5b18fa62b46eb8f223f7ec374662e9574533 Mon Sep 17 00:00:00 2001 From: clubby789 Date: Tue, 17 Sep 2024 07:03:51 -0300 Subject: [PATCH 07/13] Add note about 'raw' true/false --- text/3695-cfg_boolean_literal.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/text/3695-cfg_boolean_literal.md b/text/3695-cfg_boolean_literal.md index 4b0939c8..0b9152cf 100644 --- a/text/3695-cfg_boolean_literal.md +++ b/text/3695-cfg_boolean_literal.md @@ -42,6 +42,8 @@ And the line should be added to the explanation of the predicates. +`cfg(r#true)` and `cfg(r#false)` should continue to work as they did previously (i.e. enabled when `--cfg true`/`--cfg false` are passed). + # Drawbacks [drawbacks]: #drawbacks From da1a483d4e826d55138aaf6db98a4482513f8d13 Mon Sep 17 00:00:00 2001 From: clubby789 Date: Tue, 17 Sep 2024 07:04:42 -0300 Subject: [PATCH 08/13] Note that edition change would be required for alternative --- text/3695-cfg_boolean_literal.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/3695-cfg_boolean_literal.md b/text/3695-cfg_boolean_literal.md index 0b9152cf..f8138ec3 100644 --- a/text/3695-cfg_boolean_literal.md +++ b/text/3695-cfg_boolean_literal.md @@ -52,7 +52,7 @@ By making it more convenient, this may encourage unconditionally disabled blocks # Rationale and alternatives [rationale-and-alternatives]: #rationale-and-alternatives -- This could instead be spelled as `cfg(disabled|enabled)`, or `cfg(none)` for disabling code only. However, giving special meaning to a valid identifier can change the meaning of existing code (although `check-cfg` warnings should reduce the impact of this) +- This could instead be spelled as `cfg(disabled|enabled)`, or `cfg(none)` for disabling code only. However, giving special meaning to a valid identifier will change the meaning of existing code, requiring a new edition - As the existing predicates evaluate to booleans, using boolean literals is the most intuitive way to spell this # Prior art From 08ac4069f806c27474425c798c4452e00b2efa2b Mon Sep 17 00:00:00 2001 From: clubby789 Date: Tue, 17 Sep 2024 07:08:25 -0300 Subject: [PATCH 09/13] Add note about `cfg(all())` -> `cfg(true)` --- text/3695-cfg_boolean_literal.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/3695-cfg_boolean_literal.md b/text/3695-cfg_boolean_literal.md index f8138ec3..ffed219b 100644 --- a/text/3695-cfg_boolean_literal.md +++ b/text/3695-cfg_boolean_literal.md @@ -17,7 +17,7 @@ Currently, the easiest ways for programmers to do this are to comment out the co By allowing `#[cfg(false)]`, we can provide programmers with an explicit and more intuitive way to disable code, while retaining IDE functionality. -Allowing `cfg(true)` would also make temporarily enabling `cfg`'ed out code easier; a `true` may be added to a `cfg(any(..))` list. +Allowing `cfg(true)` would also make temporarily enabling `cfg`'ed out code easier; a `true` may be added to a `cfg(any(..))` list. Adding a `cfg(all())` is the current equivalent of this. # Guide-level explanation [guide-level-explanation]: #guide-level-explanation From 4defc8ddb6f00852b87ab0cd1311cc8d09b8a0ff Mon Sep 17 00:00:00 2001 From: clubby789 Date: Wed, 18 Sep 2024 14:42:03 +0100 Subject: [PATCH 10/13] Add another note about `cfg(all())` --- text/3695-cfg_boolean_literal.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/3695-cfg_boolean_literal.md b/text/3695-cfg_boolean_literal.md index ffed219b..799b6832 100644 --- a/text/3695-cfg_boolean_literal.md +++ b/text/3695-cfg_boolean_literal.md @@ -70,6 +70,6 @@ Searching for `cfg(false)` on [GitHub](https://github.com/search?q=%23%5Bcfg%28f # Future possibilities [future-possibilities]: #future-possibilities -A future lint could suggest replacing constructs such as `cfg(any())` with `cfg(false)`. +A future lint could suggest replacing constructs such as `cfg(any())` with `cfg(false)`, and `cfg(all())` with `cfg(true)`. The `check-cfg` lint could be with a special case for identifiers such as `FALSE` and suggest `cfg(false)` instead. From f01355c6c7c1548766463c8d89ba7865a33ed20f Mon Sep 17 00:00:00 2001 From: clubby789 Date: Wed, 18 Sep 2024 14:43:56 +0100 Subject: [PATCH 11/13] Specify that `true`/`false` should be accepted for cfg_attr and cfg! --- text/3695-cfg_boolean_literal.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/text/3695-cfg_boolean_literal.md b/text/3695-cfg_boolean_literal.md index 799b6832..c970b0b9 100644 --- a/text/3695-cfg_boolean_literal.md +++ b/text/3695-cfg_boolean_literal.md @@ -44,6 +44,11 @@ should be added to the explanation of the predicates. `cfg(r#true)` and `cfg(r#false)` should continue to work as they did previously (i.e. enabled when `--cfg true`/`--cfg false` are passed). +`true` and `false` should be expected everywhere Configuration Predicates are used, i.e. +- the `#[cfg(..)]` attribute +- the `cfg!(..)` macro +- the `#[cfg_attr(.., ..)]` attribute + # Drawbacks [drawbacks]: #drawbacks From 72d6e464033722cc7e8a1d5d39595822b230448e Mon Sep 17 00:00:00 2001 From: clubby789 Date: Thu, 3 Oct 2024 15:28:52 +0100 Subject: [PATCH 12/13] Update feature name and tracking issue --- ...95-cfg_boolean_literal.md => 3695-cfg_boolean_literals.md} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename text/{3695-cfg_boolean_literal.md => 3695-cfg_boolean_literals.md} (96%) diff --git a/text/3695-cfg_boolean_literal.md b/text/3695-cfg_boolean_literals.md similarity index 96% rename from text/3695-cfg_boolean_literal.md rename to text/3695-cfg_boolean_literals.md index c970b0b9..c0e24f69 100644 --- a/text/3695-cfg_boolean_literal.md +++ b/text/3695-cfg_boolean_literals.md @@ -1,7 +1,7 @@ -- Feature Name: `cfg_boolean_literal` +- Feature Name: `cfg_boolean_literals` - Start Date: 2024-09-16 - RFC PR: [rust-lang/rfcs#3695](https://github.com/rust-lang/rfcs/pull/3695) -- Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000) +- Rust Issue: [rust-lang/rust#131204](https://github.com/rust-lang/rust/issues/131204) # Summary [summary]: #summary From 673e6a5f52cf34c6cd4e6ee31344c1a76806755d Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Fri, 4 Oct 2024 00:47:35 +0000 Subject: [PATCH 13/13] Prepare RFC 3695 to be merged --- ...695-cfg_boolean_literals.md => 3695-cfg-boolean-literals.md} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename text/{3695-cfg_boolean_literals.md => 3695-cfg-boolean-literals.md} (97%) diff --git a/text/3695-cfg_boolean_literals.md b/text/3695-cfg-boolean-literals.md similarity index 97% rename from text/3695-cfg_boolean_literals.md rename to text/3695-cfg-boolean-literals.md index c0e24f69..03405b7a 100644 --- a/text/3695-cfg_boolean_literals.md +++ b/text/3695-cfg-boolean-literals.md @@ -1,7 +1,7 @@ - Feature Name: `cfg_boolean_literals` - Start Date: 2024-09-16 - RFC PR: [rust-lang/rfcs#3695](https://github.com/rust-lang/rfcs/pull/3695) -- Rust Issue: [rust-lang/rust#131204](https://github.com/rust-lang/rust/issues/131204) +- Tracking Issue: [rust-lang/rust#131204](https://github.com/rust-lang/rust/issues/131204) # Summary [summary]: #summary