-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Stabilize #[cfg(version(...))]
#141137
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
Stabilize #[cfg(version(...))]
#141137
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//@ run-pass | ||
//@ rustc-env:RUSTC_OVERRIDE_VERSION_STRING=1.50.3 | ||
|
||
#[cfg_attr(has_cfg_version, cfg(version("1.49.0")))] | ||
const ON_1_49_0: bool = true; | ||
#[cfg(version("1.50"))] | ||
const ON_1_50_0: bool = true; | ||
#[cfg(not(version("1.51")))] | ||
const ON_1_51_0: bool = false; | ||
|
||
// This one uses the wrong syntax, so doesn't eval to true | ||
#[warn(unexpected_cfgs)] | ||
#[cfg(not(version = "1.48.0"))] //~ WARN unexpected `cfg` condition name: `version` | ||
const ON_1_48_0: bool = false; | ||
|
||
fn main() { | ||
assert!(!ON_1_48_0); | ||
assert!(ON_1_49_0); | ||
assert!(ON_1_50_0); | ||
assert!(!ON_1_51_0); | ||
assert!(cfg!(version("1.1"))); | ||
assert!(cfg!(version("1.49"))); | ||
assert!(cfg!(version("1.50.0"))); | ||
assert!(cfg!(version("1.50.3"))); | ||
assert!(!cfg!(version("1.50.4"))); | ||
assert!(!cfg!(version("1.51"))); | ||
assert!(!cfg!(version("1.100"))); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
warning: unexpected `cfg` condition name: `version` | ||
--> $DIR/cfg-version-expand.rs:13:11 | ||
| | ||
LL | #[cfg(not(version = "1.48.0"))] | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: expected names are: `FALSE` and `test` and 32 more | ||
= help: to expect this configuration use `--check-cfg=cfg(version, values("1.48.0"))` | ||
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration | ||
= note: `#[warn(unexpected_cfgs)]` on by default | ||
|
||
warning: 1 warning emitted | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,37 @@ | ||
#[cfg(version(42))] //~ ERROR: expected a version literal | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn foo() {} | ||
#[cfg(version(1.20))] //~ ERROR: expected a version literal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reasoning we don't use this syntax again? From reading #71314, it seems like it was punted on for later because of implementation difficulty. Maybe that's not true any more? I'd prefer writing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes a certain sense to me to quote it as it is a "version string". In attributes, we generally still try to match the general grammar of Rust, and we wouldn't accept an unquoted There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I can see the argument, though I don't see why that should necessarily be an absolute? Syntactically, you can write Idk., I just think it's unfortunate that we limit ourselves here, since I suspect the vast majority of cases where If we wanted this syntax, an option could be to disallow patch versions to begin with? A difficulty with
Regardless, I think what I'm saying with this is that while this doesn't have an immediately clean solution, the solution space isn't empty, and I think it's worthwhile it to choose one of these options to get the cleaner syntax. But I guess I'm also motivated here by future attributes with versions in them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CC @petrochenkov who reviewed the implementation PR that did There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From a tokenization perspective, a string fits best. A major/minor version could look like a float which we don't have now but I do want to explore bools, ints, and floats for |
||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn foo() -> bool { true } | ||
#[cfg(version = "1.20")] //~ WARN: unexpected `cfg` condition name: `version` | ||
fn foo() -> bool { true } | ||
#[cfg(version("1.44"))] | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn foo() -> bool { true } | ||
#[cfg(not(version("1.44")))] | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn foo() -> bool { false } | ||
|
||
#[cfg(version("1.43", "1.44", "1.45"))] //~ ERROR: expected single version literal | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() -> bool { false } | ||
#[cfg(version(false))] //~ ERROR: expected a version literal | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() -> bool { false } | ||
#[cfg(version("foo"))] //~ WARNING: unknown version literal format | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() -> bool { false } | ||
#[cfg(version("999"))] //~ WARNING: unknown version literal format | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() -> bool { false } | ||
#[cfg(version("-1"))] //~ WARNING: unknown version literal format | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() -> bool { false } | ||
#[cfg(version("65536"))] //~ WARNING: unknown version literal format | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() -> bool { false } | ||
#[cfg(version("0"))] //~ WARNING: unknown version literal format | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() -> bool { true } | ||
#[cfg(version("1.0"))] | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() -> bool { true } | ||
#[cfg(version("1.65536.2"))] //~ WARNING: unknown version literal format | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() -> bool { false } | ||
#[cfg(version("1.20.0-stable"))] //~ WARNING: unknown version literal format | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() {} | ||
|
||
fn main() { | ||
assert!(foo()); | ||
assert!(bar()); | ||
assert!(cfg!(version("1.42"))); //~ ERROR `cfg(version)` is experimental and subject to change | ||
assert!(cfg!(version("1.42"))); | ||
} |
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.