-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Implement RFC 2523, #[cfg(version(..))]
#71314
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3593,6 +3593,7 @@ dependencies = [ | |
"rustc_session", | ||
"rustc_span", | ||
"serialize", | ||
"version_check", | ||
] | ||
|
||
[[package]] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# `cfg_version` | ||
|
||
The tracking issue for this feature is: [#64796] | ||
|
||
[#64796]: https://github.com/rust-lang/rust/issues/64796 | ||
|
||
------------------------ | ||
|
||
The `cfg_version` feature makes it possible to execute different code | ||
depending on the compiler version. | ||
|
||
## Examples | ||
|
||
```rust | ||
#![feature(cfg_version)] | ||
|
||
#[cfg(version("1.42"))] | ||
fn a() { | ||
// ... | ||
} | ||
|
||
#[cfg(not(version("1.42")))] | ||
fn a() { | ||
// ... | ||
} | ||
|
||
fn b() { | ||
if cfg!(version("1.42")) { | ||
// ... | ||
} else { | ||
// ... | ||
} | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fn main() { | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
println!("cargo:rerun-if-env-changed=CFG_VERSION"); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#[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"))] //~ ERROR: invalid version literal | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() -> bool { false } | ||
#[cfg(version("999"))] | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() -> bool { false } | ||
#[cfg(version("-1"))] //~ ERROR: invalid version literal | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() -> bool { false } | ||
#[cfg(version("65536"))] //~ ERROR: invalid version literal | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() -> bool { false } | ||
#[cfg(version("0"))] | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn bar() -> bool { true } | ||
mibac138 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#[cfg(version("1.65536.2"))] | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn version_check_bug() {} | ||
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. Wow, do we need this? Or maybe we can try another crate 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 think this it's unlikely for anyone to run into this, but I wanted to explicitly document this problem. I have thought about using |
||
|
||
fn main() { | ||
// This should fail but due to a bug in version_check `1.65536.2` is interpreted as `1.2`. | ||
// See https://github.com/SergioBenitez/version_check/issues/11 | ||
version_check_bug(); | ||
assert!(foo()); | ||
assert!(bar()); | ||
assert!(cfg!(version("1.42"))); //~ ERROR `cfg(version)` is experimental and subject to change | ||
} |
Uh oh!
There was an error while loading. Please reload this page.