-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/test/ui/cfg/assume-incomplete-release/assume-incomplete.rs
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,38 @@ | ||
// run-pass | ||
// aux-build:ver-cfg-rel.rs | ||
// revisions: assume no_assume | ||
// [assume]compile-flags: -Z assume-incomplete-release | ||
|
||
#![feature(cfg_version)] | ||
|
||
extern crate ver_cfg_rel; | ||
|
||
use ver_cfg_rel::ver_cfg_rel; | ||
|
||
#[ver_cfg_rel("-2")] | ||
fn foo_2() { } | ||
|
||
#[ver_cfg_rel("-1")] | ||
fn foo_1() { } | ||
|
||
#[cfg(assume)] | ||
#[ver_cfg_rel("0")] | ||
fn foo() { compile_error!("wrong+0") } | ||
|
||
#[cfg(no_assume)] | ||
#[ver_cfg_rel("0")] | ||
fn foo() { } | ||
|
||
#[ver_cfg_rel("1")] | ||
fn bar() { compile_error!("wrong+1") } | ||
|
||
#[ver_cfg_rel("2")] | ||
fn bar() { compile_error!("wrong+2") } | ||
|
||
fn main() { | ||
foo_2(); | ||
foo_1(); | ||
|
||
#[cfg(no_assume)] | ||
foo(); | ||
} |
56 changes: 56 additions & 0 deletions
56
src/test/ui/cfg/assume-incomplete-release/auxiliary/ver-cfg-rel.rs
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,56 @@ | ||
// force-host | ||
// no-prefer-dynamic | ||
|
||
#![crate_type = "proc-macro"] | ||
|
||
extern crate proc_macro; | ||
use proc_macro::{TokenStream, TokenTree as Tt}; | ||
use std::str::FromStr; | ||
|
||
// String containing the current version number of the tip, i.e. "1.41.2" | ||
static VERSION_NUMBER: &str = include_str!("../../../../../version"); | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] | ||
struct Version { | ||
major: i16, | ||
minor: i16, | ||
patch: i16, | ||
} | ||
|
||
fn parse_version(s: &str) -> Option<Version> { | ||
let mut digits = s.splitn(3, '.'); | ||
let major = digits.next()?.parse().ok()?; | ||
let minor = digits.next()?.parse().ok()?; | ||
let patch = digits.next().unwrap_or("0").trim().parse().ok()?; | ||
Some(Version { major, minor, patch }) | ||
} | ||
|
||
#[proc_macro_attribute] | ||
/// Emits a #[cfg(version)] relative to the current one, so passing | ||
/// -1 as argument on compiler 1.50 will emit #[cfg(version("1.49.0"))], | ||
/// while 1 will emit #[cfg(version("1.51.0"))] | ||
pub fn ver_cfg_rel(attr: TokenStream, input: TokenStream) -> TokenStream { | ||
let mut v_rel = None; | ||
for a in attr.into_iter() { | ||
match a { | ||
Tt::Literal(l) => { | ||
let mut s = l.to_string(); | ||
let s = s.trim_matches('"'); | ||
let v: i16 = s.parse().unwrap(); | ||
v_rel = Some(v); | ||
break; | ||
}, | ||
_ => panic!("{:?}", a), | ||
} | ||
} | ||
let v_rel = v_rel.unwrap(); | ||
|
||
let mut v = parse_version(VERSION_NUMBER).unwrap(); | ||
v.minor += v_rel; | ||
|
||
let attr_str = format!("#[cfg(version(\"{}.{}.{}\"))]", v.major, v.minor, v.patch); | ||
let mut res = Vec::<Tt>::new(); | ||
res.extend(TokenStream::from_str(&attr_str).unwrap().into_iter()); | ||
res.extend(input.into_iter()); | ||
res.into_iter().collect() | ||
} |