- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.6k
          RFC: Allow boolean literals as cfg predicates
          #3695
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            13 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      2aa0e82
              
                `cfg` boolean literals
              
              
                clubby789 599dc26
              
                Add RFC PR number
              
              
                clubby789 9466a66
              
                Fix typo
              
              
                joshtriplett 7f542ea
              
                Generalize a description
              
              
                joshtriplett ed27bd1
              
                Update filename
              
              
                clubby789 4aac81d
              
                Merge bullet points for clarity
              
              
                clubby789 e0fb5b1
              
                Add note about 'raw' true/false
              
              
                clubby789 da1a483
              
                Note that edition change would be required for alternative
              
              
                clubby789 08ac406
              
                Add note about `cfg(all())` -> `cfg(true)`
              
              
                clubby789 4defc8d
              
                Add another note about `cfg(all())`
              
              
                clubby789 f01355c
              
                Specify that `true`/`false` should be accepted for cfg_attr and cfg!
              
              
                clubby789 72d6e46
              
                Update feature name and tracking issue
              
              
                clubby789 673e6a5
              
                Prepare RFC 3695 to be merged
              
              
                traviscross File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| - Feature Name: `cfg_boolean_literal` | ||
| - 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) | ||
|  | ||
| # 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: | ||
|  | ||
| > **<sup>Syntax</sup>**\ | ||
| > _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 | ||
|         
                  clubby789 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| - Giving special meaning to a valid identifier can change the meaning of existing code (although `check-cfg` warnings should reduce the impact of this) | ||
|         
                  clubby789 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| - 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. | ||
|         
                  joshtriplett marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| - 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)`. | ||
|         
                  joshtriplett marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| The `check-cfg` lint could be with a special case for identifiers such as `FALSE` and suggest `cfg(false)` instead. | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.