Skip to content

bootstrap: Move all non-module items out of the crate root - #161277

Open
Zalathar wants to merge 5 commits into
rust-lang:mainfrom
Zalathar:session
Open

bootstrap: Move all non-module items out of the crate root#161277
Zalathar wants to merge 5 commits into
rust-lang:mainfrom
Zalathar:session

Conversation

@Zalathar

Copy link
Copy Markdown
Member

After several rounds of preparation, this PR moves all non-module items out of bootstrap's crate root.

Having non-trivial code in the crate root is generally a bad idea. Anything defined or imported there is unconditionally visible throughout the entire crate, leading to messy imports and unclear abstraction boundaries, and causing friction when code needs to be moved elsewhere.

The commits have been structured to preserve as much line history as possible, and to minimize the number of other changes in the commit that performs the actual move, while remaining functional at every intermediate step.

@rustbot

rustbot commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

This PR modifies src/bootstrap/src/core/config.

If appropriate, please update CONFIG_CHANGE_HISTORY in src/bootstrap/src/utils/change_tracker.rs.

This PR changes how LLVM is built. Consider updating src/bootstrap/download-ci-llvm-stamp.

@rustbot rustbot added A-bootstrap-stamp Area: bootstrap stamp logic A-testsuite Area: The testsuite used to check the correctness of rustc 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) labels Aug 18, 2026
@rustbot

rustbot commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

r? @clubby789

rustbot has assigned @clubby789.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: bootstrap
  • bootstrap expanded to 6 candidates
  • Random selection from Mark-Simulacrum, clubby789

@Kobzol

Kobzol commented Aug 18, 2026

Copy link
Copy Markdown
Member

Does this have to be combined with turning everything into pub(crate)? I don't personally like that visibility modifier, because it makes it harder for me to read function signatures and fields, it's just too long. It doesn't seem like it really allowed the removal of any interesting unused code in bootstrap so far?

@Zalathar

Zalathar commented Aug 19, 2026

Copy link
Copy Markdown
Member Author

The deliberate misuse of pub has been causing so many headaches that I think it’s unwise to want to keep it around as a cute shorthand for pub(crate).

It’s a huge pain to have to constantly struggle against a minefield full of lying visibility specifications, especially when those lies have real consequences in the language. Tracking down false visibility-conflict errors really sucks.

@rust-bors

This comment has been minimized.

Items with implicit `pub(self)` visibility in the crate root are effectively
`pub(crate)`, which causes friction when trying to move them elsewhere.
This intermediate commit helps to preserve line history.
@rustbot

rustbot commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Kobzol Kobzol left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess I agree in general, I just haven't personally experienced such issues in bootstrap 🤷 And since this pretty huge refactoring across several PRs didn't even enable us remove any non-trivial code, I wonder what improvements does it bring to you (since you drive it), because I don't see them (I'm fine with doing the refactoring, I just want to understand the motivation better).

If you feel strongly about this and want to ensure that we use pub(crate) in bootstrap, is there a lint we could enable to enforce it? Otherwise I'm pretty sure we will just continue using pub.

View changes since this review

@@ -0,0 +1,1878 @@
use std::cell::Cell;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't really have any notion of a "session" in bootstrap. This file looks to me like we just took a bunch of commonly used stuff and moved it from lib.rs to a different file. What's the benefit of that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

After previous PRs, this file mostly consists of Build, its methods, and a few pub(crate) types that are used by those methods and don't have a more obvious home elsewhere.

Moving Build out of the crate root requires me to come up with a module name, but I specifically didn't want to go with build, for two reasons:

  • It invites confusion with build.rs.
  • GitHub disables some search/navigation features for directories named build/, with no opt-out. This module currently doesn't have submodules, but I didn't want arbitrary barriers to adding submodules in the future.

Since I had to choose something, I tentatively arrived at session, with the idea that later we could maybe rename Build to Session to help improve the very murky distinction between Build and Builder.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The Build and Builder naming is indeed confusing, so I'm 👍 for trying to figure out something better there.

@Zalathar Zalathar Aug 19, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

As for why we should move this code at all, one of the big reasons is that the crate root has very unusual and awkward interactions with item visibility:

  • Every item in the crate root effectively has at least pub(crate) visibility, even if is declared with no visibility or with pub(self). Private items cannot exist in the crate root, because every other module is a descendant of it. And if you try, you'll silently get something else instead.
  • Because of those visibility problems, every top-level use in the crate root is effectively a crate-wide re-export. This is undesirable, and causes a lot of inconsistency and confusion, especially when IDEs end up auto-importing from the wrong place by mistake.
  • Any pub item in the crate root is a publicly-exported item. The compiler has no way to know some of those items aren't supposed to be exported, because we're literally telling it that they should be exported.

Furthermore, none of these items can really make a compelling argument that they should be in the crate root. Everything else in the crate is split into submodules (mainly utils and core), and having an arbitrary subset of items sitting around in the crate root creates a weird inconsistency with no useful significance that I can see. It's literally just stuff that was thrown into lib.rs and that nobody ever got around to moving before.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Actually one of my motivations for doing the move first is that I didn’t want to do the renaming under all the additional friction of trying to modify things in the crate root.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok! Thanks for the context.

@Kobzol

Kobzol commented Aug 19, 2026

Copy link
Copy Markdown
Member

You can r=me, unless you want to make more changes.

@Zalathar

Copy link
Copy Markdown
Member Author

If you feel strongly about this and want to ensure that we use pub(crate) in bootstrap, is there a lint we could enable to enforce it? Otherwise I'm pretty sure we will just continue using pub.

With everything moved out of the crate root, this can be enforced by #[warn(unreachable_pub)], though we would need to bulk-change all the remaining uses of pub before turning that on.

(There are several hundred of those, but some experiments suggest that the migration should be a relatively simple regex-replace.)

@Zalathar

Copy link
Copy Markdown
Member Author

Thanks for your patience on this one. I wasn't expecting to have to explain so much because it seemed so self-evident to me, but it's good to have the motivation stated more clearly.

@bors r=Kobzol

@rust-bors

rust-bors Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

📌 Commit effcd4f has been approved by Kobzol

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 19, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 19, 2026
bootstrap: Move all non-module items out of the crate root

- Previously: rust-lang#161219
---

After several rounds of preparation, this PR moves all non-module items out of bootstrap's crate root.

Having non-trivial code in the crate root is generally a bad idea. Anything defined or imported there is unconditionally visible throughout the entire crate, leading to messy imports and unclear abstraction boundaries, and causing friction when code needs to be moved elsewhere.

The commits have been structured to preserve as much line history as possible, and to minimize the number of other changes in the commit that performs the actual move, while remaining functional at every intermediate step.
rust-bors Bot pushed a commit that referenced this pull request Aug 19, 2026
…uwer

Rollup of 16 pull requests

Successful merges:

 - #159071 ([PAC] Encoder and hash (1/8))
 - #161277 (bootstrap: Move all non-module items out of the crate root)
 - #161332 (Some `GlobalCtxt`/`Session` cleanups)
 - #161344 (Update the `rustc-perf` submodule)
 - #150931 (rustdoc: Always document `#[repr(transparent)]` if `#[rustc_pub_transparent]` is applied)
 - #160582 (Add `remove hidden_glob_reexports item breaks downstream` test)
 - #160876 (remove unwrap from write_mir_fn_graphviz)
 - #160927 (Enhance EII UI tests)
 - #161070 (fix arm homogeneous aggregate ABI)
 - #161236 (Download auto jobs in citool in parallel)
 - #161254 (Reserve capacity for 3% anon nodes)
 - #161283 (Tighten the language used for documenting `TargetOptions::llvm_abiname`)
 - #161291 (Rename `ProjectionPredicate` and `TraitPredicate`)
 - #161299 (Remove a bunch of unnecessary explicit lifetimes)
 - #161307 (make ARM maintainers pingable)
 - #161308 (Add regression test for rustc diagnostic to recognize variables in match guards)
@JonathanBrouwer

Copy link
Copy Markdown
Member

💔 I suspect this PR failed tests as part of a rollup
@bors r-

After fixing the problem, consider running a try job for the failed job before re-approving.

Link to failure: #161356 (comment)

@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 19, 2026
@rust-bors

rust-bors Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

This PR was contained in a rollup (#161356), which was unapproved.

View changes since this unapproval

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-bootstrap-stamp Area: bootstrap stamp logic A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants