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

Overhaul RUSTFLAGS handling for compiler/ crates #138035

Closed

Conversation

nnethercote
Copy link
Contributor

Currently some flags are put into RUSTFLAGS for all compiler/ crates, but RUSTFLAGS is ignored for proc macro crates.

Also, most compiler crates have #![warn(unreachable_pub)] in them, but a few are missing, and it's easy to overlook if a new crate is added.

This PR introduces a neat way to apply a flag to all compiler/ crates, and uses it to replace all the #![warn(unreachable_pub)] attributes with a single -Wunreachable-pub mention.

r? @jieyouxu

Currently they are passed to the ones satisfying `Mode::Rustc` via
RUSTFLAGS. But RUSTFLAGS is ignored for proc macro crates. This commit
makes those flags get added directly to the command for proc macro
crates.
And fix the new errors in the handful of crates that didn't have a
`#![warn(unreachable_pub)]`.
It's no longer necessary now that `-Wunreachable_pub` is being passed.
@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Mar 5, 2025
@rustbot
Copy link
Collaborator

rustbot commented Mar 5, 2025

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in exhaustiveness checking

cc @Nadrieril

rustc_error_messages was changed

cc @davidtwco, @compiler-errors, @TaKO8Ki

Some changes occurred in diagnostic error codes

cc @GuillaumeGomez

Some changes occurred in compiler/rustc_attr_data_structures

cc @jdonszelmann

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

Some changes occurred in compiler/rustc_sanitizers

cc @rust-lang/project-exploit-mitigations, @rcvalle

@nnethercote
Copy link
Contributor Author

Best reviewed one commit at a time.

@nnethercote
Copy link
Contributor Author

#137930 is another PR where this new functionality will immediately be useful.

@rust-log-analyzer
Copy link
Collaborator

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
    Checking termcolor v1.4.1
    Checking home v0.5.9
    Checking build_helper v0.1.0 (/checkout/src/build_helper)
    Checking xz2 v0.1.7
error: constants have by default a `'static` lifetime
    |
    |
118 | pub const FLAGS_FOR_RUSTC: &[&'static str] = &[
    |                              -^^^^^^^---- help: consider removing `'static`: `&str`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes
    = note: `-D clippy::redundant-static-lifetimes` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::redundant_static_lifetimes)]`

@jieyouxu
Copy link
Member

jieyouxu commented Mar 5, 2025

r? @onur-ozkan (for the rustc wrapper bit, I'd appreciate a second pair of eyes, or reroll

@rustbot rustbot assigned onur-ozkan and unassigned jieyouxu Mar 5, 2025
@jieyouxu jieyouxu self-assigned this Mar 5, 2025
@@ -145,6 +145,35 @@ fn main() {
cmd.arg("-Z").arg("on-broken-pipe=kill");
}

// In `cargo.rs` we add the `FLAGS_FOR_RUSTC` flags for all `compiler/`
// crates to RUSTFLAGS. However, RUSTFLAGS is ignored for `compiler/` proc
// macro crates, because RUSTFLAGS is ignored when `--target` is given. So,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RUSTFLAGS is ignored when --target is given

That's not quite true, it is not ignored. But it only applies to target crates, not to host crates.

let crate_type = parse_value_from_args(&orig_args, "--crate-type");
if crate_type == Some("proc-macro") {
if let Ok(rustflags) = env::var("RUSTFLAGS") {
// Count how many of flags from `FLAGS_FOR_RUSTC` are in RUSTFLAGS.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have to use a fragile hacky heuristic like that? That shouldn't be needed. Bootstrap should clearly tell the wrapper what to add where, we should never do guesswork like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know very little about bootstrap and this was something I managed to get working. I'm happy to hear other ideas and implement them but I'll need a more detailed explanation than "bootstrap should clearly tell the wrapper" - how and where would this be done? :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was imagining something like this, which already exists:

if let Ok(lint_flags) = env::var("RUSTC_LINT_FLAGS") {
cmd.args(lint_flags.split_whitespace());
}

From some of the other discussion it sounds like what you dislike about this approach is that it will pass the flags to all invocations, not just the ones for workspace crates. However, non-workspace crates run with --cap-lints so that should be entirely non-consequential?

The discussion about this seems to be spread across multiple Zulip topics, issues, and PRs, so I have no idea which options have been considered. Has RUSTC_LINT_FLAGS been discarded for some reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This thread has most of the discussion.

I didn't even know RUSTC_LINT_FLAGS was a thing until just now. Like I said above (and in the thread), I know very little about bootstrap and I've tried several different approaches based on suggestions people have made at different times.

Copy link
Member

@RalfJung RalfJung Mar 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair -- I just thought bootstrap people were involved in the discussion / review so I'm a bit surprised it took so long for someone to suggest this. Let's make sure to include bootstrap people in the next PR.

I didn't know this env var existed either, but adding something like it was literally the first thing that came to my mind ("bootstrap should tell the wrapper to add some flags"), and then going over the rustc.rs file it took no time to see that flag. I guess I am underestimating how fragmented bootstrap knowledge is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jieyouxu, @onur-ozkan, and @Kobzol have all been involved in the various discussions and PRs. That's half the bootstrap reviewers listed in triagebot.toml. @jieyouxu also started a thread last week on t-infra/bootstrap to ask for additional ideas.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I am indeed underestimating how fragmented bootstrap knowledge is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also had no idea about the flag :) bootstrap's build logic is distributed amongst several places, it's full of hacks and edge cases, and hidden invariants that can break when you modify completely unrelated code.

Copy link
Member

@RalfJung RalfJung Mar 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said, I didn't know about the flag either. I was about to suggest to add something like this flag (it seemed a lot cleaner than what this PR does), went looking to the sources for inspiration on how to call it, and then saw that it already existed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same 😆

@jieyouxu
Copy link
Member

jieyouxu commented Mar 5, 2025

The rustc wrapper approach seems okay to me, but would appreicate a double-check since last time I thought it was okay to do some RUSTFLAGS adjustments in the wrapper I broke things :D

@petrochenkov
Copy link
Contributor

This PR introduces a neat way to apply a flag to all compiler/ crates,

IIRC, this was done several times already (I personally did it at least once), but it always gets broken after some time by bootstrap changes - for proc macros, for build scripts, for rtstartup, for bootstrap itself.
I wish there was a way to test this somehow, to ensure that it doesn't regress.

@onur-ozkan
Copy link
Member

This PR introduces a neat way to apply a flag to all compiler/ crates,

IIRC, this was done several times already (I personally did it at least once), but it always gets broken after some time by bootstrap changes - for proc macros, for build scripts, for rtstartup, for bootstrap itself. I wish there was a way to test this somehow, to ensure that it doesn't regress.

One way to do this is by unconditionally passing an arbitrary flag and asserting its presence in each compiler crate. Ugly, but it could work I think.

@jieyouxu jieyouxu added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 6, 2025
@bors
Copy link
Contributor

bors commented Mar 9, 2025

☔ The latest upstream changes (presumably #138267) made this pull request unmergeable. Please resolve the merge conflicts.

@nnethercote
Copy link
Contributor Author

After the failure of the workspace lints approach (merged in #138084, reverted in #138304 for breaking miri and other external tools) I have implemented a RUSTC_LINT_FLAGS-based approach in #138331 which has the light footprint of this PR (i.e. doesn't touch many lines of code) while being much less hacky.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants