@@ -76,11 +76,16 @@ impl Step for ToolBuild {
7676 /// This will build the specified tool with the specified `host` compiler in
7777 /// `stage` into the normal cargo output directory.
7878 fn run ( self , builder : & Builder < ' _ > ) -> PathBuf {
79- let compiler = self . compiler ;
8079 let target = self . target ;
8180 let mut tool = self . tool ;
8281 let path = self . path ;
8382
83+ let compiler = if self . mode == Mode :: ToolRustc {
84+ get_tool_rustc_compiler ( builder, self . compiler )
85+ } else {
86+ self . compiler
87+ } ;
88+
8489 match self . mode {
8590 Mode :: ToolRustc => {
8691 builder. ensure ( compile:: Std :: new ( compiler, compiler. host ) ) ;
@@ -147,6 +152,9 @@ pub fn prepare_tool_cargo(
147152 source_type : SourceType ,
148153 extra_features : & [ String ] ,
149154) -> CargoCommand {
155+ let compiler =
156+ if mode == Mode :: ToolRustc { get_tool_rustc_compiler ( builder, compiler) } else { compiler } ;
157+
150158 let mut cargo = builder:: Cargo :: new ( builder, compiler, mode, source_type, target, cmd_kind) ;
151159
152160 let dir = builder. src . join ( path) ;
@@ -240,6 +248,19 @@ pub fn prepare_tool_cargo(
240248 cargo
241249}
242250
251+ fn get_tool_rustc_compiler ( builder : & Builder < ' _ > , target_compiler : Compiler ) -> Compiler {
252+ if builder. download_rustc ( ) && target_compiler. stage == 1 {
253+ // We already have the stage 1 compiler, we don't need to cut the stage.
254+ builder. compiler ( target_compiler. stage , builder. config . build )
255+ } else {
256+ // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
257+ // we'd have stageN/bin/rustc and stageN/bin/$rustc_tool be effectively different stage
258+ // compilers, which isn't what we want. Rustc tools should be linked in the same way as the
259+ // compiler it's paired with, so it must be built with the previous stage compiler.
260+ builder. compiler ( target_compiler. stage . saturating_sub ( 1 ) , builder. config . build )
261+ }
262+ }
263+
243264/// Links a built tool binary with the given `name` from the build directory to the
244265/// tools directory.
245266fn copy_link_tool_bin (
0 commit comments