-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Allow &&
, ||
, and !
in cfg
#3796
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
base: master
Are you sure you want to change the base?
Conversation
I think that a |
The problem I see with |
That interpretation feels like a stretch to me, but it's also understandable. Perhaps a |
Updated the following:
|
The problem here is that
|
I read As mentioned above, It isn't even commutative: Using If we do want to make cfg() take something that looks like an expression, we should also change the |
Let's look at a real world example: #[cfg(any(target_os = "android", target_os = "linux", target_os = "emscripten", target_os = "freebsd"))] This proposal would allow rewriting that as: #[cfg(target_os = "android" || target_os = "linux" || target_os = "emscripten" || target_os = "freebsd"))] Which looks a bit nicer. But instead of just replacing #[cfg(target_os = "android" | "linux" | "emscripten" | "freebsd")] I don't want to derail this thread with alternative proposals, but I do think that |
How would they not be the same? The RFC, I think, intents for both of these to mean the same thing: the |
|
Ah. That seems entirely consistent to me, it's I like |
My point is that we need tot look at real world examples of code we're trying to simplify. For example, quite often, Large cfg() attributes can be very confusing. But replacing |
Rust parses One might argue that
I would assume it interprets that as two So even in an attribute, I'd argue that |
maybe embrace that they are sets and allow using |
I would go so far as to argue that #[cfg(any(
feature = "foo",
feature = "bar",
feature = "baz",
feature = "quux",
feature = "magic",
test
))] Whereas a similar boolean expression is less regular and longer: something(cfg(
feature == "foo"
|| feature == "bar"
|| feature == "baz"
|| feature == "quux"
|| feature == "magic"
|| test,
)); (And there are probably even messier cases to present with On the other hand, supporting |
GitHub really needs a 😢 emoji. To be compatible with this existing syntax, it means all #[cfg((target_os="linux") && (target_arch="x86_64") && (target_env="gnu") && !debug_assertions)] |
Yes the formatting is strange, but I'd argue that the binary operator makes the
|
|
|
@m-ou-se wrote:
Agreed completely. I don't think it would be ambiguous here to reuse "feature" without adding "has_": |
Technically this change of syntax will conflict with the cfg_version feature but since it is unstable it is fine...? (plus can't use #![feature(cfg_version)]
fn main() {
dbg!(cfg!(version("1.69.0")));
dbg!(cfg!(version("2.0.0")));
dbg!(cfg!(version = "1.69.0"));
dbg!(cfg!(version = "2.0.0"));
} $ rustc +nightly --cfg 'version="2.0.0"' 1.rs
$ ./1
[1.rs:3:2] cfg!(version("1.69.0")) = true
[1.rs:4:2] cfg!(version("2.0.0")) = false
[1.rs:5:2] cfg!(version = "1.69.0") = false
[1.rs:6:2] cfg!(version = "2.0.0") = true |
@camsteffen Is churn the primary (or even sole) concern? I think the 👍s on this PR show that there's unquestionably the desire for logical operators — it feels obvious imo. |
I actually like @camsteffen's proposal a lot. It makes many scenarios simpler right now without changing ther original feature = name syntax. And the existing situation isn't so bad. And, if we feel like adding boolean operators later that still works! |
@jhpratt Not just churn, but the fact of having two ways to express something and a split in the ecosystem of which one is used. The motivation may be to make things simpler by matching other syntax, but I think the reality is that it's increasing complexity with another thing to learn. I would be more open to it if I could snap my fingers and change all the code in the world to the new syntax. But I also agree with @kpreid that any/all is better with regard to formatting. |
A little more indentation makes this look much better: #[cfg(any(
feature = "foo"
| "bar"
| "baz"
| "quux"
| "magic",
test,
))] #[cfg(all(
feature = "foo"
& "bar"
& "baz"
& "quux"
& "magic",
test,
))] |
@Jules-Bertholet the default rustfmt will not use vertical alignment. @camsteffen Rust isn't afraid of having multiple ways to express the same concept, we have This proposed syntax
|
Oddly, my first reaction to those examples that I'd like to use "any" and "&&"... I can't really explain why. But I do feel like the use of infix operators makes it less clear that we are looking at a list of cases.
|
Can we just have #![cfg(!windows)] |
@SOF3 Realistically you wouldn't because there isn't much of a use case for that specifically. |
For those that are curious about the "nearly" part: the large family of ML-based languages including OCaml use |
I really like this proposal. Not because it shortens some expressions, but because it's a step towards reducing the amount of unique microsyntaxes inside the Rust language. |
As for the |
what happens when values aren't a simple number/identifier? e.g. |
A feature name may consist of Furthermore a custom config #[cfg(feature = "c++20")]
#[cfg(feature("c++20"))]
#[cfg(feature."c++20")] |
we could do something like TOML paths where you can omit the quotation marks if it's a valid identifier or number (equivalent to the corresponding quoted version) or you can have a string literal with any random text at all (except for commas since I think it's split into separate values using commas...). so, with
as well as all the existing |
Without inventing new syntax (like thing["#$%@!"] For convenience, the
|
|
| `!accessible(std::mem::forget)` | `not(accessible(std::mem::forget))` | `!`, `&&` etc. can be used on `cfg_accessible` | | ||
| `accessible(std::a \|\| std::b)` | _syntax error_ | … but not inside | | ||
| `!version("1.42.0")` | `not(version("1.42.0"))` | `!`, `&&` etc. can be used on `cfg_version` | | ||
| `version(!"1.42.0")` | _syntax error_ | … but not inside | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would it interact with the target(os = "linux", ...)
shorthand from RFC 3239?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well it's being removed rust-lang/rust#130780
#[cfg(all(target_os = "linux", target_arch = "arm"))]
#[cfg(target_os("linux") && target_arch("arm"))]
#[cfg(target(os = "linux", arch = "arm"))]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last comments from T-lang members tend to the opposite, so probably going to stay.
Things I'm quite sure I like:
Things I'm less sure of but still open to:
|
When deciding upon a pattern here, it's probably worth thinking about the standard library's close-to-stable |
The problem adapting to |
Attempting to summarize the current state: I think the current RFC proposing
I agree with the many comments saying that
The combination of those two (function-style predicates and I think I also don't think there's been much support for |
@rust-lang/lang, shall we accept the RFC as currently proposed, for adding As currently written, the RFC defers all other potential additions to future proposals. @rfcbot merge |
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. |
That is my understanding of the current state as well. If accepted, I will almost certainly follow up with a function-like Thanks for moving this forward, Josh! |
About as simple as it gets.
Rendered