From 91f75ac4bb23d0b72a805316046513785c8c0e63 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Tue, 20 Feb 2024 12:49:19 -0500 Subject: [PATCH] refactor: enum to represent kinds of tools being run --- src/cargo/core/compiler/compilation.rs | 32 +++++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 018792d2b13..0e768bfcb11 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -13,6 +13,25 @@ use crate::core::compiler::{CompileKind, Metadata, Unit}; use crate::core::Package; use crate::util::{config, CargoResult, GlobalContext}; +/// Represents the kind of process we are creating. +#[derive(Debug)] +enum ToolKind { + /// See [`Compilation::rustc_process`]. + Rustc, + /// See [`Compilation::rustdoc_process`]. + Rustdoc, + /// See [`Compilation::host_process`]. + HostProcess, + /// See [`Compilation::target_process`]. + TargetProcess, +} + +impl ToolKind { + fn is_rustc_tool(&self) -> bool { + matches!(self, ToolKind::Rustc | ToolKind::Rustdoc) + } +} + /// Structure with enough information to run `rustdoc --test`. pub struct Doctest { /// What's being doctested @@ -176,7 +195,7 @@ impl<'gctx> Compilation<'gctx> { }; let cmd = fill_rustc_tool_env(rustc, unit); - self.fill_env(cmd, &unit.pkg, None, unit.kind, true) + self.fill_env(cmd, &unit.pkg, None, unit.kind, ToolKind::Rustc) } /// Returns a [`ProcessBuilder`] for running `rustdoc`. @@ -187,7 +206,7 @@ impl<'gctx> Compilation<'gctx> { ) -> CargoResult { let rustdoc = ProcessBuilder::new(&*self.gctx.rustdoc()?); let cmd = fill_rustc_tool_env(rustdoc, unit); - let mut cmd = self.fill_env(cmd, &unit.pkg, script_meta, unit.kind, true)?; + let mut cmd = self.fill_env(cmd, &unit.pkg, script_meta, unit.kind, ToolKind::Rustdoc)?; cmd.retry_with_argfile(true); unit.target.edition().cmd_edition_arg(&mut cmd); @@ -214,7 +233,7 @@ impl<'gctx> Compilation<'gctx> { pkg, None, CompileKind::Host, - false, + ToolKind::HostProcess, ) } @@ -249,7 +268,8 @@ impl<'gctx> Compilation<'gctx> { } else { ProcessBuilder::new(cmd) }; - let mut builder = self.fill_env(builder, pkg, script_meta, kind, false)?; + let tool_kind = ToolKind::TargetProcess; + let mut builder = self.fill_env(builder, pkg, script_meta, kind, tool_kind)?; if let Some(client) = self.gctx.jobserver_from_env() { builder.inherit_jobserver(client); @@ -269,10 +289,10 @@ impl<'gctx> Compilation<'gctx> { pkg: &Package, script_meta: Option, kind: CompileKind, - is_rustc_tool: bool, + tool_kind: ToolKind, ) -> CargoResult { let mut search_path = Vec::new(); - if is_rustc_tool { + if tool_kind.is_rustc_tool() { search_path.push(self.deps_output[&CompileKind::Host].clone()); } else { search_path.extend(super::filter_dynamic_search_path(