Skip to content

Commit

Permalink
Stabilize checking of cfgs at compile-time: --check-cfg option
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Apr 15, 2024
1 parent 99d0186 commit 909fcfc
Show file tree
Hide file tree
Showing 55 changed files with 60 additions and 95 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
opt::flag_s("h", "help", "Display this message"),
opt::multi_s("", "cfg", "Configure the compilation environment.
SPEC supports the syntax `NAME[=\"VALUE\"]`.", "SPEC"),
opt::multi("", "check-cfg", "Provide list of valid cfg options for checking", "SPEC"),
opt::multi_s("", "check-cfg", "Provide list of expected cfgs for checking", "SPEC"),
opt::multi_s(
"L",
"",
Expand Down
7 changes: 2 additions & 5 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,8 @@ pub(crate) fn generate_args_file(file_path: &Path, options: &RustdocOptions) ->
for cfg in &options.cfgs {
content.push(format!("--cfg={cfg}"));
}
if !options.check_cfgs.is_empty() {
content.push("-Zunstable-options".to_string());
for check_cfg in &options.check_cfgs {
content.push(format!("--check-cfg={check_cfg}"));
}
for check_cfg in &options.check_cfgs {
content.push(format!("--check-cfg={check_cfg}"));
}

for lib_str in &options.lib_strs {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ fn opts() -> Vec<RustcOptGroup> {
o.optmulti("L", "library-path", "directory to add to crate search path", "DIR")
}),
stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
unstable("check-cfg", |o| o.optmulti("", "check-cfg", "pass a --check-cfg to rustc", "")),
stable("check-cfg", |o| o.optmulti("", "check-cfg", "pass a --check-cfg to rustc", "")),
stable("extern", |o| o.optmulti("", "extern", "pass an --extern to rustc", "NAME[=PATH]")),
unstable("extern-html-root-url", |o| {
o.optmulti(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ check-pass
//@ compile-flags: --check-cfg=cfg() -Z unstable-options
//@ compile-flags: --check-cfg=cfg()

/// uniz is nor a builtin nor pass as arguments so is unexpected
#[cfg(uniz)]
Expand Down
File renamed without changes.
11 changes: 0 additions & 11 deletions tests/rustdoc-ui/check-cfg/check-cfg-test.stderr

This file was deleted.

2 changes: 0 additions & 2 deletions tests/rustdoc-ui/check-cfg/check-cfg-unstable.rs

This file was deleted.

2 changes: 0 additions & 2 deletions tests/rustdoc-ui/check-cfg/check-cfg-unstable.stderr

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ui/check-cfg/allow-at-crate-level.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This test check that #![allow(unexpected_cfgs)] works with --cfg
//
//@ check-pass
//@ compile-flags: --cfg=unexpected --check-cfg=cfg() -Z unstable-options
//@ compile-flags: --cfg=unexpected --check-cfg=cfg()

#![allow(unexpected_cfgs)]

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/allow-macro-cfg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This test check that local #[allow(unexpected_cfgs)] works
//
//@ check-pass
//@ compile-flags: --check-cfg=cfg() -Z unstable-options
//@ compile-flags: --check-cfg=cfg()

#[allow(unexpected_cfgs)]
fn foo() {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/allow-same-level.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This test check that #[allow(unexpected_cfgs)] doesn't work if put on the same level
//
//@ check-pass
//@ compile-flags: --check-cfg=cfg() -Z unstable-options
//@ compile-flags: --check-cfg=cfg()

#[allow(unexpected_cfgs)]
#[cfg(FALSE)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/allow-top-level.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This test check that a top-level #![allow(unexpected_cfgs)] works
//
//@ check-pass
//@ compile-flags: --check-cfg=cfg() -Z unstable-options
//@ compile-flags: --check-cfg=cfg()

#![allow(unexpected_cfgs)]

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/allow-upper-level.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This test check that #[allow(unexpected_cfgs)] work if put on an upper level
//
//@ check-pass
//@ compile-flags: --check-cfg=cfg() -Z unstable-options
//@ compile-flags: --check-cfg=cfg()

#[allow(unexpected_cfgs)]
mod aa {
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/check-cfg/cargo-feature.none.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: unexpected `cfg` condition value: `serde`
--> $DIR/cargo-feature.rs:14:7
--> $DIR/cargo-feature.rs:13:7
|
LL | #[cfg(feature = "serde")]
| ^^^^^^^^^^^^^^^^^ help: remove the condition
Expand All @@ -10,7 +10,7 @@ LL | #[cfg(feature = "serde")]
= note: `#[warn(unexpected_cfgs)]` on by default

warning: unexpected `cfg` condition value: (none)
--> $DIR/cargo-feature.rs:18:7
--> $DIR/cargo-feature.rs:17:7
|
LL | #[cfg(feature)]
| ^^^^^^^ help: remove the condition
Expand All @@ -20,7 +20,7 @@ LL | #[cfg(feature)]
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration

warning: unexpected `cfg` condition name: `tokio_unstable`
--> $DIR/cargo-feature.rs:22:7
--> $DIR/cargo-feature.rs:21:7
|
LL | #[cfg(tokio_unstable)]
| ^^^^^^^^^^^^^^
Expand All @@ -30,7 +30,7 @@ LL | #[cfg(tokio_unstable)]
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration

warning: unexpected `cfg` condition name: `CONFIG_NVME`
--> $DIR/cargo-feature.rs:26:7
--> $DIR/cargo-feature.rs:25:7
|
LL | #[cfg(CONFIG_NVME = "m")]
| ^^^^^^^^^^^^^^^^^
Expand Down
1 change: 0 additions & 1 deletion tests/ui/check-cfg/cargo-feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//@ check-pass
//@ revisions: some none
//@ rustc-env:CARGO_CRATE_NAME=foo
//@ compile-flags: -Z unstable-options
//@ [none]compile-flags: --check-cfg=cfg(feature,values())
//@ [some]compile-flags: --check-cfg=cfg(feature,values("bitcode"))
//@ [some]compile-flags: --check-cfg=cfg(CONFIG_NVME,values("y"))
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/check-cfg/cargo-feature.some.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: unexpected `cfg` condition value: `serde`
--> $DIR/cargo-feature.rs:14:7
--> $DIR/cargo-feature.rs:13:7
|
LL | #[cfg(feature = "serde")]
| ^^^^^^^^^^^^^^^^^
Expand All @@ -10,7 +10,7 @@ LL | #[cfg(feature = "serde")]
= note: `#[warn(unexpected_cfgs)]` on by default

warning: unexpected `cfg` condition value: (none)
--> $DIR/cargo-feature.rs:18:7
--> $DIR/cargo-feature.rs:17:7
|
LL | #[cfg(feature)]
| ^^^^^^^- help: specify a config value: `= "bitcode"`
Expand All @@ -20,7 +20,7 @@ LL | #[cfg(feature)]
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration

warning: unexpected `cfg` condition name: `tokio_unstable`
--> $DIR/cargo-feature.rs:22:7
--> $DIR/cargo-feature.rs:21:7
|
LL | #[cfg(tokio_unstable)]
| ^^^^^^^^^^^^^^
Expand All @@ -30,7 +30,7 @@ LL | #[cfg(tokio_unstable)]
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration

warning: unexpected `cfg` condition value: `m`
--> $DIR/cargo-feature.rs:26:7
--> $DIR/cargo-feature.rs:25:7
|
LL | #[cfg(CONFIG_NVME = "m")]
| ^^^^^^^^^^^^^^---
Expand Down
1 change: 0 additions & 1 deletion tests/ui/check-cfg/cfg-value-for-cfg-name-duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// This test checks we won't suggest more than 3 span suggestions for cfg names
//
//@ check-pass
//@ compile-flags: -Z unstable-options
//@ compile-flags: --check-cfg=cfg(foo,values("value")) --check-cfg=cfg(bar,values("value")) --check-cfg=cfg(bee,values("value")) --check-cfg=cfg(cow,values("value"))

#[cfg(value)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/cfg-value-for-cfg-name-duplicate.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: unexpected `cfg` condition name: `value`
--> $DIR/cfg-value-for-cfg-name-duplicate.rs:8:7
--> $DIR/cfg-value-for-cfg-name-duplicate.rs:7:7
|
LL | #[cfg(value)]
| ^^^^^
Expand Down
1 change: 0 additions & 1 deletion tests/ui/check-cfg/cfg-value-for-cfg-name-multiple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// This test checks that when a single cfg has a value for user's specified name
//
//@ check-pass
//@ compile-flags: -Z unstable-options
//@ compile-flags: --check-cfg=cfg(foo,values("my_value")) --check-cfg=cfg(bar,values("my_value"))

#[cfg(my_value)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/cfg-value-for-cfg-name-multiple.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: unexpected `cfg` condition name: `my_value`
--> $DIR/cfg-value-for-cfg-name-multiple.rs:8:7
--> $DIR/cfg-value-for-cfg-name-multiple.rs:7:7
|
LL | #[cfg(my_value)]
| ^^^^^^^^
Expand Down
1 change: 0 additions & 1 deletion tests/ui/check-cfg/cfg-value-for-cfg-name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// suggest to use `#[cfg(target_os = "linux")]` instead of `#[cfg(linux)]`
//
//@ check-pass
//@ compile-flags: -Z unstable-options
//@ compile-flags: --check-cfg=cfg()

#[cfg(linux)]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/check-cfg/cfg-value-for-cfg-name.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: unexpected `cfg` condition name: `linux`
--> $DIR/cfg-value-for-cfg-name.rs:9:7
--> $DIR/cfg-value-for-cfg-name.rs:8:7
|
LL | #[cfg(linux)]
| ^^^^^ help: found config with similar value: `target_os = "linux"`
Expand All @@ -10,7 +10,7 @@ LL | #[cfg(linux)]
= note: `#[warn(unexpected_cfgs)]` on by default

warning: unexpected `cfg` condition name: `linux`
--> $DIR/cfg-value-for-cfg-name.rs:14:7
--> $DIR/cfg-value-for-cfg-name.rs:13:7
|
LL | #[cfg(linux = "os-name")]
| ^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/compact-names.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This test check that we correctly emit an warning for compact cfg
//
//@ check-pass
//@ compile-flags: --check-cfg=cfg() -Z unstable-options
//@ compile-flags: --check-cfg=cfg()

#![feature(cfg_target_compact)]

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/compact-values.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This test check that we correctly emit an warning for compact cfg
//
//@ check-pass
//@ compile-flags: --check-cfg=cfg() -Z unstable-options
//@ compile-flags: --check-cfg=cfg()

#![feature(cfg_target_compact)]

Expand Down
1 change: 0 additions & 1 deletion tests/ui/check-cfg/concat-values.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@ check-pass
//@ compile-flags: -Z unstable-options
//@ compile-flags: --check-cfg=cfg(my_cfg,values("foo")) --check-cfg=cfg(my_cfg,values("bar"))
//@ compile-flags: --check-cfg=cfg(my_cfg,values())

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/check-cfg/concat-values.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: unexpected `cfg` condition value: (none)
--> $DIR/concat-values.rs:6:7
--> $DIR/concat-values.rs:5:7
|
LL | #[cfg(my_cfg)]
| ^^^^^^
Expand All @@ -10,7 +10,7 @@ LL | #[cfg(my_cfg)]
= note: `#[warn(unexpected_cfgs)]` on by default

warning: unexpected `cfg` condition value: `unk`
--> $DIR/concat-values.rs:10:7
--> $DIR/concat-values.rs:9:7
|
LL | #[cfg(my_cfg = "unk")]
| ^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/diagnotics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@ revisions: cargo rustc
//@ [rustc]unset-rustc-env:CARGO_CRATE_NAME
//@ [cargo]rustc-env:CARGO_CRATE_NAME=foo
//@ compile-flags: --check-cfg=cfg(feature,values("foo")) --check-cfg=cfg(no_values) -Z unstable-options
//@ compile-flags: --check-cfg=cfg(feature,values("foo")) --check-cfg=cfg(no_values)

#[cfg(featur)]
//~^ WARNING unexpected `cfg` condition name
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/empty-values.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Check that we detect unexpected value when none are allowed
//
//@ check-pass
//@ compile-flags: --check-cfg=cfg(foo,values()) -Zunstable-options
//@ compile-flags: --check-cfg=cfg(foo,values())

#[cfg(foo = "foo")]
//~^ WARNING unexpected `cfg` condition value
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/check-cfg/exhaustive-names-values.empty_cfg.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: unexpected `cfg` condition name: `unknown_key`
--> $DIR/exhaustive-names-values.rs:10:7
--> $DIR/exhaustive-names-values.rs:9:7
|
LL | #[cfg(unknown_key = "value")]
| ^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -10,7 +10,7 @@ LL | #[cfg(unknown_key = "value")]
= note: `#[warn(unexpected_cfgs)]` on by default

warning: unexpected `cfg` condition value: `value`
--> $DIR/exhaustive-names-values.rs:14:7
--> $DIR/exhaustive-names-values.rs:13:7
|
LL | #[cfg(test = "value")]
| ^^^^----------
Expand All @@ -21,7 +21,7 @@ LL | #[cfg(test = "value")]
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration

warning: unexpected `cfg` condition name: `feature`
--> $DIR/exhaustive-names-values.rs:18:7
--> $DIR/exhaustive-names-values.rs:17:7
|
LL | #[cfg(feature = "unk")]
| ^^^^^^^^^^^^^^^
Expand All @@ -30,7 +30,7 @@ LL | #[cfg(feature = "unk")]
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration

warning: unexpected `cfg` condition name: `feature`
--> $DIR/exhaustive-names-values.rs:25:7
--> $DIR/exhaustive-names-values.rs:24:7
|
LL | #[cfg(feature = "std")]
| ^^^^^^^^^^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/check-cfg/exhaustive-names-values.feature.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: unexpected `cfg` condition name: `unknown_key`
--> $DIR/exhaustive-names-values.rs:10:7
--> $DIR/exhaustive-names-values.rs:9:7
|
LL | #[cfg(unknown_key = "value")]
| ^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -10,7 +10,7 @@ LL | #[cfg(unknown_key = "value")]
= note: `#[warn(unexpected_cfgs)]` on by default

warning: unexpected `cfg` condition value: `value`
--> $DIR/exhaustive-names-values.rs:14:7
--> $DIR/exhaustive-names-values.rs:13:7
|
LL | #[cfg(test = "value")]
| ^^^^----------
Expand All @@ -21,7 +21,7 @@ LL | #[cfg(test = "value")]
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration

warning: unexpected `cfg` condition value: `unk`
--> $DIR/exhaustive-names-values.rs:18:7
--> $DIR/exhaustive-names-values.rs:17:7
|
LL | #[cfg(feature = "unk")]
| ^^^^^^^^^^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/check-cfg/exhaustive-names-values.full.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: unexpected `cfg` condition name: `unknown_key`
--> $DIR/exhaustive-names-values.rs:10:7
--> $DIR/exhaustive-names-values.rs:9:7
|
LL | #[cfg(unknown_key = "value")]
| ^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -10,7 +10,7 @@ LL | #[cfg(unknown_key = "value")]
= note: `#[warn(unexpected_cfgs)]` on by default

warning: unexpected `cfg` condition value: `value`
--> $DIR/exhaustive-names-values.rs:14:7
--> $DIR/exhaustive-names-values.rs:13:7
|
LL | #[cfg(test = "value")]
| ^^^^----------
Expand All @@ -21,7 +21,7 @@ LL | #[cfg(test = "value")]
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration

warning: unexpected `cfg` condition value: `unk`
--> $DIR/exhaustive-names-values.rs:18:7
--> $DIR/exhaustive-names-values.rs:17:7
|
LL | #[cfg(feature = "unk")]
| ^^^^^^^^^^^^^^^
Expand Down
1 change: 0 additions & 1 deletion tests/ui/check-cfg/exhaustive-names-values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
//@ check-pass
//@ revisions: empty_cfg feature full
//@ compile-flags: -Z unstable-options
//@ [empty_cfg]compile-flags: --check-cfg=cfg()
//@ [feature]compile-flags: --check-cfg=cfg(feature,values("std"))
//@ [full]compile-flags: --check-cfg=cfg(feature,values("std")) --check-cfg=cfg()
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/exhaustive-names.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Check warning for unexpected cfg
//
//@ check-pass
//@ compile-flags: --check-cfg=cfg() -Z unstable-options
//@ compile-flags: --check-cfg=cfg()

#[cfg(unknown_key = "value")]
//~^ WARNING unexpected `cfg` condition name
Expand Down
Loading

0 comments on commit 909fcfc

Please sign in to comment.