Skip to content

Commit

Permalink
Add more tests for cfg(version)
Browse files Browse the repository at this point in the history
  • Loading branch information
mibac138 committed May 3, 2020
1 parent 96f27c7 commit a3ee283
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/librustc_attr/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,7 @@ pub fn eval_condition(
return false;
}
};
let version = option_env!("CFG_VERSION").unwrap_or("unknown rustc version");
let version = Version::parse(version).unwrap();
let version = Version::parse(env!("CFG_VERSION")).unwrap();

version >= min_version
}
Expand Down
12 changes: 11 additions & 1 deletion src/test/ui/feature-gates/feature-gate-cfg-version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ fn bar() -> bool { false }
#[cfg(version("999"))]
//~^ ERROR `cfg(version)` is experimental and subject to change
fn bar() -> bool { false }
#[cfg(version("999"))]
#[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
Expand Down
43 changes: 41 additions & 2 deletions src/test/ui/feature-gates/feature-gate-cfg-version.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,60 @@ LL | #[cfg(version("999"))]
error[E0658]: `cfg(version)` is experimental and subject to change
--> $DIR/feature-gate-cfg-version.rs:20:7
|
LL | #[cfg(version("-1"))]
| ^^^^^^^^^^^^^
|
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
= help: add `#![feature(cfg_version)]` to the crate attributes to enable

error: invalid version literal
--> $DIR/feature-gate-cfg-version.rs:20:15
|
LL | #[cfg(version("-1"))]
| ^^^^

error[E0658]: `cfg(version)` is experimental and subject to change
--> $DIR/feature-gate-cfg-version.rs:23:7
|
LL | #[cfg(version("65536"))]
| ^^^^^^^^^^^^^^^^
|
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
= help: add `#![feature(cfg_version)]` to the crate attributes to enable

error: invalid version literal
--> $DIR/feature-gate-cfg-version.rs:23:15
|
LL | #[cfg(version("65536"))]
| ^^^^^^^

error[E0658]: `cfg(version)` is experimental and subject to change
--> $DIR/feature-gate-cfg-version.rs:26:7
|
LL | #[cfg(version("0"))]
| ^^^^^^^^^^^^
|
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
= help: add `#![feature(cfg_version)]` to the crate attributes to enable

error[E0658]: `cfg(version)` is experimental and subject to change
--> $DIR/feature-gate-cfg-version.rs:27:18
--> $DIR/feature-gate-cfg-version.rs:30:7
|
LL | #[cfg(version("1.65536.2"))]
| ^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
= help: add `#![feature(cfg_version)]` to the crate attributes to enable

error[E0658]: `cfg(version)` is experimental and subject to change
--> $DIR/feature-gate-cfg-version.rs:40:18
|
LL | assert!(cfg!(version("1.42")));
| ^^^^^^^^^^^^^^^
|
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
= help: add `#![feature(cfg_version)]` to the crate attributes to enable

error: aborting due to 11 previous errors
error: aborting due to 16 previous errors

For more information about this error, try `rustc --explain E0658`.

0 comments on commit a3ee283

Please sign in to comment.