Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upis `#[cfg]` applicable to *some* non-stmt expressions? #32796
Comments
pnkfelix
added
I-nominated
T-lang
labels
Apr 7, 2016
This comment has been minimized.
This comment has been minimized.
|
cc @Kimundi |
This comment has been minimized.
This comment has been minimized.
|
Lang team discussed this. Opinions were mixed about whether this should be allowed or not. But the more fundamental conclusion is that the decision here should go through the RFC process (merely as an amendment to RFC 16), rather than just having the lang team make a unilateral decision; we want to get input from the community about what is best here. Note: It would be good to keep in mind that some use cases, such as function calls of the form bar(1, #[cfg(no)] 2, 3)are at the very least difficult to try to use, because one cannot write fn bar(a: u32, #[cfg(no)] b: u32, c: u32) { ... } |
pnkfelix
removed
the
I-nominated
label
Apr 7, 2016
This comment has been minimized.
This comment has been minimized.
|
I'm against such fine-grained
Compare that to
|
This comment has been minimized.
This comment has been minimized.
|
cc #15701 ... this is effectively an unresolved question that needs to be added to that tracking issue before we stabilize this feature. |
This comment has been minimized.
This comment has been minimized.
|
So, obviously unreadable messes like what @tbu- showed should be considered unidiomatic in any case, but I can see the use cases where just plain item-level cfgs won't cut it. One of the motivating examples that lead to me implementing RFC #16 was the use case of a huge struct with many While what I implemented did not fully reach that state due to things like struct constructors like #![feature(stmt_expr_attributes)]
struct Foo(
u8,
#[cfg(foo)] u16
);
fn main() {
let x = Foo(1, #[cfg(foo)] 2);
}As well as compile-time-cfg switching code like let x = (#[cfg(a)] foo(), #[cfg(not(a))] bar()).0;(Though I guess the latter might be replicable with blocks and assignment statements) In my opinion, the fact that we already support attributes on struct fields and match arms might indicate that there is a desire to have them at more specific locations than just the "top level" items, types and statements, so maybe it would make sense to investigate whether it can be extend to places like patterns or argument lists so that things like Also, the way I see it all more complicated/unreadable constructions you can build with this would end up behind a macro anyway. For example , have a macro_rules! cfg_match {
(_ => $b:block) => {
$b
};
($cfg:meta => $b:block $($t:tt)*) => {
(
#[cfg($cfg)] $b,
#[cfg(not($cfg))] (cfg_match!($($t)*)),
).0
};
() => {
{
panic!("Conditional code not supported under this configuration");
}
}
}
let x = cfg_match! {
unix => { foo() }
windows => { bar() }
};
|
This comment has been minimized.
This comment has been minimized.
|
That should be possible today, if I understand it correctly:
or even
|
This comment has been minimized.
This comment has been minimized.
|
@tbu- That needs the same feature ( |
This comment has been minimized.
This comment has been minimized.
|
There's a usecase for let backends = [
#[cfg(feature = "foo-backend")] foo_backend,
#[cfg(feature = "bar-backend")] bar_backend,
];allowing multiple backends to be enabled at compile time using cargo features, and choosing one at runtime. I guess you could consider this as a I'm more skeptical about other uses of it, especially on function calls. |
This comment has been minimized.
This comment has been minimized.
|
This looks like the only thing blocking |
This comment has been minimized.
This comment has been minimized.
|
Perhaps we can turn this behavior off and move forward with |
This comment has been minimized.
This comment has been minimized.
|
Turn off in the sense of removal, or in the sense of adding a feature for it specifically? |
This comment has been minimized.
This comment has been minimized.
|
I'm in favour of stabilising statement attributes and leaving expression ones under the existing feature flag (probably the easiest solution for users and the compiler, I'd also be happy with a new feature flag or removing expression attributes entirely, but either option seems less friendly). |
comex
referenced this issue
Aug 17, 2016
Open
Tracking issue for stmt_expr_attributes: Add attributes to expressions, etc. #15701
This comment has been minimized.
This comment has been minimized.
|
So I'm not really clear what this issue is addressing. I think it's about attributes working on some expression and not others... but I don't see representative examples. Could someone summarize the current situation (@nrc?)? |
This comment has been minimized.
This comment has been minimized.
|
Right now |
This comment has been minimized.
This comment has been minimized.
|
It's so clearly useful... I think I'd be against trying to remove it. But I'd like to know the current status too. |
Mark-Simulacrum
added
the
C-feature-request
label
Jul 24, 2017
This comment has been minimized.
This comment has been minimized.
|
yes it appears that #36995 may have (accidentally?) stabilized it for list elements, which was the main case this issue was discussing... at this point I'm not sure we can afford to remove it... |
This comment has been minimized.
This comment has been minimized.
|
So, as noted above, it appears that this (play): fn foo(_x: &[u32]) { }
fn main() {
foo(&[1, #[cfg(not(now))] 2, 3])
}is now accepted on stable. So regardless of whatever resoluton I was hoping to reach via discussion, it appears the matter has been resolved independently of the discussion here. (Note to self: Maybe in the future when I discover oddities like this, I should immediately add compile-fail tests that are lacking the feature flag to ensure they don't get accidentally stabilized. At least, my interpretation of @nrc's stated position leads me to think that they did not intend to stabilize this aspect of attributes)
|
pnkfelix commentedApr 7, 2016
Consider the following:
http://is.gd/28Q5H5
Currently this is accepted (see #29850) but RFC 16 as written does not clearly say whether this is acceptable or not (see https://github.com/rust-lang/rfcs/blob/master/text/0016-more-attributes.md#cfg )