Skip to content
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

RFC: Allow boolean literals as cfg predicates #3695

Merged
merged 13 commits into from
Oct 4, 2024

Conversation

clubby789
Copy link
Contributor

@clubby789 clubby789 commented Sep 16, 2024

This RFC proposes allowing boolean literals as cfg predicates, e.g. cfg(true) and cfg(false).


Rendered

Tracking:

@clarfonthey
Copy link
Contributor

Also worth noting that, in addition to cfg(any()) meaning cfg(false), cfg(all()) means cfg(true) as well. So, both technically have easy versions, but I agree that true and false care clearer.

@joshtriplett joshtriplett added the T-lang Relevant to the language team, which will review and decide on the RFC. label Sep 16, 2024
@joshtriplett
Copy link
Member

This is simple, straightforward, solves an existing problem, and has no compatibility issues.

@rfcbot merge

@rfcbot
Copy link
Collaborator

rfcbot commented Sep 16, 2024

Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. disposition-merge This RFC is in PFCP or FCP with a disposition to merge it. labels Sep 16, 2024
joshtriplett and others added 2 commits September 16, 2024 14:26
Co-authored-by: lolbinarycat <dogedoge61+github@gmail.com>
@kennytm
Copy link
Member

kennytm commented Sep 16, 2024

the RFC should mention somewhere that the raw identifiers r#true and r#false continue to work:

rustc --cfg false --check-cfg 'cfg(r#false, values(none()))' 1.rs
#     ^~~~~~~~~~~
// 1.rs
#![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()
}

@nikomatsakis
Copy link
Contributor

Yes please. I'm always surprised I can't do cfg(false).

@rfcbot reviewed

@clubby789
Copy link
Contributor Author

Added a note about r#true/r#false

@clubby789
Copy link
Contributor Author

Also worth noting that, in addition to cfg(any()) meaning cfg(false), cfg(all()) means cfg(true) as well.

Mentioned this in a couple of places for completeness

@lolbinarycat
Copy link
Contributor

probably should also mention cfg_attr for completeness

@traviscross
Copy link
Contributor

@rfcbot reviewed

In particular, now that we're warning about #[cfg(False)], #[cfg(FALSE)], etc., this is somewhat more pressing.

@rfcbot rfcbot added final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. and removed proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. labels Sep 18, 2024
@rfcbot
Copy link
Collaborator

rfcbot commented Sep 18, 2024

🔔 This is now entering its final comment period, as per the review above. 🔔

@kennytm
Copy link
Member

kennytm commented Sep 18, 2024

probably should also mention cfg_attr for completeness

speaking of which i suppose these are also available in Cargo.toml's and .cargo/config.toml's [target.<cfg>] sections.

@clubby789
Copy link
Contributor Author

clubby789 commented Sep 18, 2024

Would changing Cargo need a separate RFC? Currently, Cargo seems to accept these:

warning: unused manifest key: target.cfg(false).rustflags

so changing that could maybe break code - not sure if you can pass custom cfg values to Cargo itself?

@kennytm
Copy link
Member

kennytm commented Sep 18, 2024

not sure if you can pass custom cfg values to Cargo itself?

uh oh. according to rust-lang/cargo#5313 injecting a custom cfg via RUSTFLAGS is supported.

@lolbinarycat
Copy link
Contributor

so changing that could maybe break code

I'm pretty sure adding new manifest keys is not considered a breaking change.

@CAD97
Copy link

CAD97 commented Sep 18, 2024

Also, it's worth noting that we've introduced and gated new well-known cfg (e.g. cfg(ub_checks)) before without edition gates. That doesn't itself justify doing so again, but in the case of Cargo's cfg not matching rustc's (does Cargo gate everything that rustc does?) I believe fixing the incongruity would be allowed as a bug fix even if it's technically breaking.

@workingjubilee
Copy link
Member

I agree we should just send bad news for anyone who was using cfg(false) and cfg(true) for any meaning beyond the obvious one.

@kennytm
Copy link
Member

kennytm commented Sep 18, 2024

current situation of cfg-like constructs

false r#false
Rust language
(#[cfg], cfg!, etc)
❌ `cfg` predicate key cannot be a literal
rustc --cfg
rustc --check-cfg ❌ invalid `--check-cfg` argument: `false` is a boolean literal
cargo [target.<cfg>] ❌ failed to parse `r#false` as a cfg expression: unexpected content `#false` found after cfg expression

I think cargo should be made to accept raw identifiers, but this is indeed really out-of-scope for this RFC.

@tmandry
Copy link
Member

tmandry commented Sep 18, 2024

Thanks for this RFC, I've wanted this for awhile!

@rfcbot reviewed

@T0mstone
Copy link

T0mstone commented Sep 20, 2024

Another future possibility that addresses the drawback: There could be an allow-by-default lint for using these, so you could #![deny(cfg_boolean_literal)] (or whatever it'd be called).

@lolbinarycat
Copy link
Contributor

note that lint names should be plural if they are nouns

@joshtriplett
Copy link
Member

We discussed this in today's @rust-lang/cargo meeting. We confirmed that we generally want Cargo to implement every cfg that rustc does, to avoid confusion, and that we anticipate this being easy to implement.

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this RFC. to-announce and removed final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. labels Sep 28, 2024
@rfcbot
Copy link
Collaborator

rfcbot commented Sep 28, 2024

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@clubby789
Copy link
Contributor Author

Updated the name to cfg_boolean_literals: (rust-lang/rust#131034 (comment))

@traviscross traviscross merged commit 60a6758 into rust-lang:master Oct 4, 2024
@traviscross
Copy link
Contributor

The team has accepted this RFC, and it's now been merged.

Thanks to @clubby789 for writing this up and pushing it forward.

For further updates, follow the tracking issue:

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 4, 2024
Implement RFC3695 Allow boolean literals as cfg predicates

This PR implements rust-lang/rfcs#3695: allow boolean literals as cfg predicates, i.e. `cfg(true)` and `cfg(false)`.

r? `@nnethercote` *(or anyone with parser knowledge)*
cc `@clubby789`
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Oct 4, 2024
Implement RFC3695 Allow boolean literals as cfg predicates

This PR implements rust-lang/rfcs#3695: allow boolean literals as cfg predicates, i.e. `cfg(true)` and `cfg(false)`.

r? `@nnethercote` *(or anyone with parser knowledge)*
cc `@clubby789`
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 4, 2024
Rollup merge of rust-lang#131034 - Urgau:cfg-true-false, r=nnethercote

Implement RFC3695 Allow boolean literals as cfg predicates

This PR implements rust-lang/rfcs#3695: allow boolean literals as cfg predicates, i.e. `cfg(true)` and `cfg(false)`.

r? `@nnethercote` *(or anyone with parser knowledge)*
cc `@clubby789`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This RFC is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this RFC. T-lang Relevant to the language team, which will review and decide on the RFC. to-announce
Projects
None yet
Development

Successfully merging this pull request may close these issues.