Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable "Checking conditional compilation value at compile time" when it is stable? #2293

Open
SteveLauC opened this issue Jan 15, 2024 · 1 comment

Comments

@SteveLauC
Copy link
Member

This will check invalid conditions, which would be quite useful for us, see the following demo:

#[cfg(target_os = "linxu")]  // NOTE this typo!
fn foo_for_linux() { }

fn main() { }
$ cargo +nightly check -Z unstable-options -Z check-cfg
    Checking rust v0.1.0 (/home/steve/Documents/workspace/playground/rust)

warning: unexpected `cfg` condition value: `linxu`
 --> src/main.rs:1:7
  |
1 | #[cfg(target_os = "linxu")]
  |       ^^^^^^^^^^^^-------
  |                   |
  |                   help: there is a expected value with a similar name: `"linux"`
  |
  = note: expected values for `target_os` are: `aix`, `android`, `cuda`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `hurd`, `illumos`, `ios`, `l4re`, `linux`, `macos`, `netbsd`, `none`, `nto`, `openbsd`, `psp`, `redox`, `solaris`, `solid_asp3`, `teeos`, `tvos`, `uefi`, `unknown`, `vita`, `vxworks`, `wasi`, `watchos`, `windows`, `xous`
  = note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
  = note: `#[warn(unexpected_cfgs)]` on by default

Our aliases defined in build.rs are also invalid, to allow them, we can:

// build.rs

fn main() {
    println!("cargo:rustc-check-cfg=cfg(apple_targets)");
    println!("cargo:rustc-check-cfg=cfg(bsd)");
    println!("cargo:rustc-check-cfg=cfg(linux_android)");
    println!("cargo:rustc-check-cfg=cfg(freebsdlike)");
    println!("cargo:rustc-check-cfg=cfg(netbsdlike)");
    println!("cargo:rustc-check-cfg=cfg(solarish)");
    
    ...

}

It is still unstable and only available in nightly, maybe we can enable this check when it is in stable.

@SteveLauC
Copy link
Member Author

Oops, I just noticed this is a duplicate of #1734

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant