-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #71314 - mibac138:cfg-version, r=petrochenkov
Implement RFC 2523, `#[cfg(version(..))]` Hi! This is my first contribution to rust, I hope I didn't miss anything. I tried to implement this feature so that `#[cfg(version(1.44.0))]` works but the parser was printing an error that I wasn't sure how to fix so I just opted for implementing `#[cfg(version("1.44.0"))]` (note the quotes). Tracking issue: #64796
- Loading branch information
Showing
12 changed files
with
283 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3593,6 +3593,7 @@ dependencies = [ | |
"rustc_session", | ||
"rustc_span", | ||
"serialize", | ||
"version_check", | ||
] | ||
|
||
[[package]] | ||
|
34 changes: 34 additions & 0 deletions
34
src/doc/unstable-book/src/language-features/cfg-version.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
// ... | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
|
||
#[cfg(version("1.65536.2"))] | ||
//~^ ERROR `cfg(version)` is experimental and subject to change | ||
fn version_check_bug() {} | ||
|
||
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 | ||
} |
Oops, something went wrong.