rust-lang / rust-lang/rust

Warn for `cfg!(target_* = "whatever")` usage in build scripts

Open
#125,441 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-cargo T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
if cfg!(target_feature = "sse2") {
    // …
}
Current output
No warning
Desired output
A warning pointing at `cfg!(target_feature = "sse2")` stating that this will be likely wrong for the target system if you cross compile
Rationale and extra context

I would expect a warning that explains that cfg!(target_*) will be evaluated in the context of the host system that compiles the build script and not in the context of the target system. It seems to be a common mistake for crate authors writing build scripts to use cfg! and not std::env in combination with the environment variables set by cargo. The warning should suggest that the later variant is the correct solution there.

Other cases

No response

Rust Version
rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the cfg!(target_*) example in a Rust build script and compare it with the std::env approach described in the report. Trace where compiler warnings for cfg! expressions are produced, then add coverage showing a warning that points to the expression and explains the host-versus-target context; done when the warning appears for this build-script case without affecting valid target checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
build-system, compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.