Rust std compilation process question

Hi all!

I want to know what is the process of building the Rust standard library. I remember reading that std has to be compiled with nightly, but then, which one?

Say Rust 1.80.0 needs to be released. It needs a compiled std to be shipped alongside the compiler. Which nightly version is used to build it? Would it be any nightly available when the compilation happens? In that case, that would be Rust 1.82.0, no? Or is Rust 1.80.0 used but with the magic flag RUSTC_BOOTSTRAP? Or maybe something completely different?

Thanks for the clarification!

There's several stages in bootstrapping the compiler; per the docs on bootstrapping, std for any compiler version is built by the same compiler version that it's going to be released with, using the stage1 compiler to build it.

We start with the old version, which we call stage0.

We use stage0 to build the new version's compiler and std, and we call the output of doing this stage1.

We use stage1 to build the new version's compiler and std again, and we call the outputs created by stage1 the stage2 compiler and std.

The release uses the stage2 compiler and std; there's an optional stage3 where we use the stage2 compiler to rebuild std and confirm that stage1 and stage2 compilers both output the same std (bit-for-bit exact).

1 Like

I don't think it answers the main question I have though (please correct me if I'm wrong). I do understand the different stages usage though (your description was spot on).

Since there are nightly features required by std, it cannot be built using stable.

Thanks for the details and link!

Let me put numbers for me to better understand. Let's say the team wants to release Rust 1.80.0. They first need a Rust compiler. The team thus uses 1.79.0 as stage0 to build rustc+std 1.80.0. A stage1 1.80.0 is thus obtained. Then 1.80.0 is rebuilt using stage1, resulting in stage2. I'll ignore stage3 here since it's mostly a verification stage.

What I still wonder is that the stage 1 and 2 in the above example are both "1.80.0 stable". Since stable cannot be used to compile std (due to its nightly features requirement) then I suppose RUSTC_BOOTSTRAP is being used at every step?

From a bootstrapping point of view I understand this, but from a "stability" point of view I find this weird. Nightly features are by definition unstable, but they also could be "immature". I'm saying "immature" to distinguish between "unstable" as in "can potentially change API/ABI/wtv" and "unstable" as in "can crash". Since the code in the compiler can evolve between a stable and nightly compiler release (ie "stable" is forked from "beta", which gets forked from "nightly" every 6 weeks), the stable compiler used to compile the different stages match older versions of the nightly compiler... Am I understanding this properly? Is the rust code in a stable release of the compiler sufficiently mature to properly compile itself and std? I guess it does if that's the process :smiley:

I am asking all this because I am interesting in building for Windows 7, which is a new target x86_64-win7-windows-msvc since Rust 1.78. See for reference Updated baseline standards for Windows targets | Rust Blog.
Since that target is Tier 3 for now, it means I need to compile std myself first before being able to compile my project. Will I have to build a stage1 and stage2 version of std with the x86_64-win7-windows-msvc target or will building it once (ie only stage1) will be enough? In addition, will I need a nightly compiler to do so or using the stable release be sufficient?

Thanks again!

Stable compilers can use nightly features under some circumstances. One of them is when you compile std.

1 Like

Yes, RUSTC_BOOTSTRAP is being used at every step. RUSTC_BOOTSTRAP is a way of telling a stable compiler that you're giving it permission to enable unstable features; you still need the attributes to tell the compiler to turn on features, but you're allowed many more features than without RUSTC_BOOTSTRAP, and it's a requirement that RUSTC_BOOTSTRAP enables all the features you need to build std.

Thus, a stage1 or stage2 compiler with RUSTC_BOOTSTRAP set can compile its matching version of std, because RUSTC_BOOTSTRAP ensures that all the unstable features that are needed for std are available.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.