Skip to content

Commit 4f79ac3

Browse files
committed
Change get_tool to use an enum to constrain which values it accepts.
1 parent 42ef94f commit 4f79ac3

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

src/cargo/util/config/mod.rs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl Config {
388388
/// Gets the path to the `rustdoc` executable.
389389
pub fn rustdoc(&self) -> CargoResult<&Path> {
390390
self.rustdoc
391-
.try_borrow_with(|| Ok(self.get_tool("rustdoc", &self.build_config()?.rustdoc)))
391+
.try_borrow_with(|| Ok(self.get_tool(Tool::Rustdoc, &self.build_config()?.rustdoc)))
392392
.map(AsRef::as_ref)
393393
}
394394

@@ -406,7 +406,7 @@ impl Config {
406406
);
407407

408408
Rustc::new(
409-
self.get_tool("rustc", &self.build_config()?.rustc),
409+
self.get_tool(Tool::Rustc, &self.build_config()?.rustc),
410410
wrapper,
411411
rustc_workspace_wrapper,
412412
&self
@@ -1640,15 +1640,19 @@ impl Config {
16401640
}
16411641
}
16421642

1643-
/// Looks for a path for `tool` in an environment variable or config path, defaulting to `tool`
1644-
/// as a path.
1645-
fn get_tool(&self, tool: &str, from_config: &Option<ConfigRelativePath>) -> PathBuf {
1646-
// This function is designed to only work with rustup proxies. This
1647-
// assert is to ensure that if it is ever used for something else in
1648-
// the future that you must ensure that it is a proxy-able tool, or if
1649-
// not then you need to use `maybe_get_tool` instead.
1650-
assert!(matches!(tool, "rustc" | "rustdoc"));
1651-
self.maybe_get_tool(tool, from_config)
1643+
/// Returns the path for the given tool.
1644+
///
1645+
/// This will look for the tool in the following order:
1646+
///
1647+
/// 1. From an environment variable matching the tool name (such as `RUSTC`).
1648+
/// 2. From the given config value (which is usually something like `build.rustc`).
1649+
/// 3. Finds the tool in the PATH environment variable.
1650+
///
1651+
/// This is intended for tools that are rustup proxies. If you need to get
1652+
/// a tool that is not a rustup proxy, use `maybe_get_tool` instead.
1653+
fn get_tool(&self, tool: Tool, from_config: &Option<ConfigRelativePath>) -> PathBuf {
1654+
let tool_str = tool.as_str();
1655+
self.maybe_get_tool(tool_str, from_config)
16521656
.or_else(|| {
16531657
// This is an optimization to circumvent the rustup proxies
16541658
// which can have a significant performance hit. The goal here
@@ -1671,7 +1675,7 @@ impl Config {
16711675
}
16721676
// If the tool on PATH is the same as `rustup` on path, then
16731677
// there is pretty good evidence that it will be a proxy.
1674-
let tool_resolved = paths::resolve_executable(Path::new(tool)).ok()?;
1678+
let tool_resolved = paths::resolve_executable(Path::new(tool_str)).ok()?;
16751679
let rustup_resolved = paths::resolve_executable(Path::new("rustup")).ok()?;
16761680
let tool_meta = tool_resolved.metadata().ok()?;
16771681
let rustup_meta = rustup_resolved.metadata().ok()?;
@@ -1683,7 +1687,7 @@ impl Config {
16831687
return None;
16841688
}
16851689
// Try to find the tool in rustup's toolchain directory.
1686-
let tool_exe = Path::new(tool).with_extension(env::consts::EXE_EXTENSION);
1690+
let tool_exe = Path::new(tool_str).with_extension(env::consts::EXE_EXTENSION);
16871691
let toolchain_exe = home::rustup_home()
16881692
.ok()?
16891693
.join("toolchains")
@@ -1696,7 +1700,7 @@ impl Config {
16961700
None
16971701
}
16981702
})
1699-
.unwrap_or_else(|| PathBuf::from(tool))
1703+
.unwrap_or_else(|| PathBuf::from(tool_str))
17001704
}
17011705

17021706
pub fn jobserver_from_env(&self) -> Option<&jobserver::Client> {
@@ -2697,3 +2701,17 @@ macro_rules! drop_eprint {
26972701
$crate::__shell_print!($config, err, false, $($arg)*)
26982702
);
26992703
}
2704+
2705+
enum Tool {
2706+
Rustc,
2707+
Rustdoc,
2708+
}
2709+
2710+
impl Tool {
2711+
fn as_str(&self) -> &str {
2712+
match self {
2713+
Tool::Rustc => "rustc",
2714+
Tool::Rustdoc => "rustdoc",
2715+
}
2716+
}
2717+
}

0 commit comments

Comments
 (0)