-
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 |
I feel like many problems raised (by various people) here can be partially mitigated by changing I didn't come up with this, it felt like a slightly nicer way of writing @m-ou-se's Having said that, let this comment be a motivation for why I think this mitigates so many of the concerns. The advantages I see are:
|
I'll defer to the team here. I'm happy to write up whatever changes are desired, which at this point sounds like it may be |
I like the proposal in #3796 (comment), but the
|
I propose that we add In a world of boolean logic, the existence of any/all/not functions is indubitably justified, and they deserve our respect. |
@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
|
|
About as simple as it gets.
Rendered