-
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.
Add
--print=check-cfg
to get the expected configs
- Loading branch information
Showing
8 changed files
with
184 additions
and
2 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
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
25 changes: 25 additions & 0 deletions
25
src/doc/unstable-book/src/compiler-flags/print-check-cfg.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,25 @@ | ||
# `print=check-cfg` | ||
|
||
The tracking issue for this feature is: [#XXXXXX](https://github.com/rust-lang/rust/issues/XXXXXX). | ||
|
||
------------------------ | ||
|
||
This option of the `--print` flag print the list of expected cfgs. | ||
|
||
This is related to the `--check-cfg` flag which allows specifying arbitrary expected | ||
names and values. | ||
|
||
This print option works similarly to `--print=cfg` (modulo check-cfg specifics): | ||
- *check_cfg syntax*: *output of --print=check-cfg* | ||
- `cfg(windows)`: `windows` | ||
- `cfg(feature, values("foo", "bar"))`: `feature="foo"` and `feature="bar"` | ||
- `cfg(feature, values(none(), ""))`: `feature` and `feature=""` | ||
- `cfg(feature, values(any()))`: `feature=any()` | ||
- `cfg(any())`: `any()` | ||
- *nothing*: `any()=any()` | ||
|
||
To be used like this: | ||
|
||
```bash | ||
rustc --print=check-cfg -Zunstable-options lib.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 @@ | ||
// empty crate |
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,106 @@ | ||
//! This checks the output of `--print=check-cfg` | ||
|
||
extern crate run_make_support; | ||
|
||
use std::collections::HashSet; | ||
use std::iter::FromIterator; | ||
use std::ops::Deref; | ||
|
||
use run_make_support::rustc; | ||
|
||
fn main() { | ||
check( | ||
/*args*/ &[], | ||
/*has_any*/ false, | ||
/*has_any_any*/ true, | ||
/*contains*/ &[], | ||
); | ||
check( | ||
/*args*/ &["--check-cfg=cfg()"], | ||
/*has_any*/ false, | ||
/*has_any_any*/ false, | ||
/*contains*/ &["unix", "miri"], | ||
); | ||
check( | ||
/*args*/ &["--check-cfg=cfg(any())"], | ||
/*has_any*/ true, | ||
/*has_any_any*/ false, | ||
/*contains*/ &["windows", "test"], | ||
); | ||
check( | ||
/*args*/ &["--check-cfg=cfg(feature)"], | ||
/*has_any*/ false, | ||
/*has_any_any*/ false, | ||
/*contains*/ &["unix", "miri", "feature"], | ||
); | ||
check( | ||
/*args*/ &[r#"--check-cfg=cfg(feature, values(none(), "", "test", "lol"))"#], | ||
/*has_any*/ false, | ||
/*has_any_any*/ false, | ||
/*contains*/ &["feature", "feature=\"\"", "feature=\"test\"", "feature=\"lol\""], | ||
); | ||
check( | ||
/*args*/ &[ | ||
r#"--check-cfg=cfg(feature, values(any()))"#, | ||
r#"--check-cfg=cfg(feature, values("tmp"))"# | ||
], | ||
/*has_any*/ false, | ||
/*has_any_any*/ false, | ||
/*contains*/ &["unix", "miri", "feature=any()"], | ||
); | ||
check( | ||
/*args*/ &[ | ||
r#"--check-cfg=cfg(has_foo, has_bar)"#, | ||
r#"--check-cfg=cfg(feature, values("tmp"))"#, | ||
r#"--check-cfg=cfg(feature, values("tmp"))"# | ||
], | ||
/*has_any*/ false, | ||
/*has_any_any*/ false, | ||
/*contains*/ &["has_foo", "has_bar", "feature=\"tmp\""], | ||
); | ||
} | ||
|
||
fn check(args: &[&str], has_any: bool, has_any_any: bool, contains: &[&str]) { | ||
let output = rustc() | ||
.input("lib.rs") | ||
.arg("-Zunstable-options") | ||
.arg("--print=check-cfg") | ||
.args(&*args) | ||
.run(); | ||
|
||
let stdout = String::from_utf8(output.stdout).unwrap(); | ||
|
||
let mut found_any = false; | ||
let mut found_any_any = false; | ||
let mut found = HashSet::<String>::new(); | ||
let mut recorded = HashSet::<String>::new(); | ||
|
||
for l in stdout.lines() { | ||
assert!(l == l.trim()); | ||
if l == "any()" { | ||
found_any = true; | ||
} else if l == "any()=any()" { | ||
found_any_any = true; | ||
} else if let Some((left, right)) = l.split_once('=') { | ||
if right != "any()" && right != "" { | ||
assert!(right.starts_with("\"")); | ||
assert!(right.ends_with("\"")); | ||
} | ||
assert!(!left.contains("\"")); | ||
} else { | ||
assert!(!l.contains("\"")); | ||
} | ||
assert!(recorded.insert(l.to_string()), "{}", &l); | ||
if contains.contains(&l) { | ||
assert!(found.insert(l.to_string()), "{}", &l); | ||
} | ||
} | ||
|
||
let should_found = HashSet::<String>::from_iter(contains.iter().map(|s| s.to_string())); | ||
let diff: Vec<_> = should_found.difference(&found).collect(); | ||
|
||
assert_eq!(found_any, has_any); | ||
assert_eq!(found_any_any, has_any_any); | ||
assert_eq!(found_any_any, recorded.len() == 1); | ||
assert!(diff.is_empty(), "{:?} != {:?} (~ {:?})", &should_found, &found, &diff); | ||
} |
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,3 @@ | ||
//@ compile-flags: --print=check-cfg | ||
|
||
fn main() {} |
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,2 @@ | ||
error: the `-Z unstable-options` flag must also be passed to enable the check-cfg print option | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
error: unknown print request: `yyyy` | ||
| | ||
= help: valid print requests are: `all-target-specs-json`, `calling-conventions`, `cfg`, `code-models`, `crate-name`, `deployment-target`, `file-names`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `tls-models` | ||
= help: valid print requests are: `all-target-specs-json`, `calling-conventions`, `cfg`, `check-cfg`, `code-models`, `crate-name`, `deployment-target`, `file-names`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `tls-models` | ||
|