Skip to content

remove RustfmtState to reduce initial_rustfmt complexity #141027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/bootstrap/src/core/build_steps/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn rustfmt(
fn get_rustfmt_version(build: &Builder<'_>) -> Option<(String, BuildStamp)> {
let stamp_file = BuildStamp::new(&build.out).with_prefix("rustfmt");

let mut cmd = command(build.initial_rustfmt()?);
let mut cmd = command(build.config.initial_rustfmt.as_ref()?);
cmd.arg("--version");

let output = cmd.allow_failure().run_capture(build);
Expand Down Expand Up @@ -243,7 +243,7 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {

let override_ = override_builder.build().unwrap(); // `override` is a reserved keyword

let rustfmt_path = build.initial_rustfmt().unwrap_or_else(|| {
let rustfmt_path = build.config.initial_rustfmt.clone().unwrap_or_else(|| {
eprintln!("fmt error: `x fmt` is not supported on this channel");
crate::exit!(1);
});
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ impl Step for Tidy {
if builder.config.channel == "dev" || builder.config.channel == "nightly" {
if !builder.config.json_output {
builder.info("fmt check");
if builder.initial_rustfmt().is_none() {
if builder.config.initial_rustfmt.is_none() {
let inferred_rustfmt_dir = builder.initial_sysroot.join("bin");
eprintln!(
"\
Expand Down
45 changes: 4 additions & 41 deletions src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! This module implements parsing `bootstrap.toml` configuration files to tweak
//! how the build runs.

use std::cell::{Cell, RefCell};
use std::cell::Cell;
use std::collections::{BTreeSet, HashMap, HashSet};
use std::fmt::{self, Display};
use std::hash::Hash;
Expand Down Expand Up @@ -406,11 +406,7 @@ pub struct Config {
pub initial_rustc: PathBuf,
pub initial_cargo_clippy: Option<PathBuf>,
pub initial_sysroot: PathBuf,

#[cfg(not(test))]
initial_rustfmt: RefCell<RustfmtState>,
#[cfg(test)]
pub initial_rustfmt: RefCell<RustfmtState>,
pub initial_rustfmt: Option<PathBuf>,

/// The paths to work with. For example: with `./x check foo bar` we get
/// `paths=["foo", "bar"]`.
Expand All @@ -428,15 +424,6 @@ pub struct Config {
pub path_modification_cache: Arc<Mutex<HashMap<Vec<&'static str>, PathFreshness>>>,
}

#[derive(Clone, Debug, Default)]
pub enum RustfmtState {
SystemToolchain(PathBuf),
Downloaded(PathBuf),
Unavailable,
#[default]
LazyEvaluated,
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum LlvmLibunwind {
#[default]
Expand Down Expand Up @@ -2448,13 +2435,8 @@ impl Config {
});
}

if let Some(r) = rustfmt {
*config.initial_rustfmt.borrow_mut() = if r.exists() {
RustfmtState::SystemToolchain(r)
} else {
RustfmtState::Unavailable
};
}
config.initial_rustfmt =
if let Some(r) = rustfmt { Some(r) } else { config.maybe_download_rustfmt() };

// Now that we've reached the end of our configuration, infer the
// default values for all options that we haven't otherwise stored yet.
Expand Down Expand Up @@ -2851,25 +2833,6 @@ impl Config {
.as_deref()
}

pub(crate) fn initial_rustfmt(&self) -> Option<PathBuf> {
match &mut *self.initial_rustfmt.borrow_mut() {
RustfmtState::SystemToolchain(p) | RustfmtState::Downloaded(p) => Some(p.clone()),
RustfmtState::Unavailable => None,
r @ RustfmtState::LazyEvaluated => {
if self.dry_run() {
return Some(PathBuf::new());
}
let path = self.maybe_download_rustfmt();
*r = if let Some(p) = &path {
RustfmtState::Downloaded(p.clone())
} else {
RustfmtState::Unavailable
};
path
}
}
}

/// Runs a function if verbosity is greater than 0
pub fn verbose(&self, f: impl Fn()) {
if self.is_verbose() {
Expand Down
6 changes: 5 additions & 1 deletion src/bootstrap/src/core/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ impl Config {

#[cfg(test)]
pub(crate) fn maybe_download_rustfmt(&self) -> Option<PathBuf> {
None
Some(PathBuf::new())
}

/// NOTE: rustfmt is a completely different toolchain than the bootstrap compiler, so it can't
Expand All @@ -455,6 +455,10 @@ impl Config {
pub(crate) fn maybe_download_rustfmt(&self) -> Option<PathBuf> {
use build_helper::stage0_parser::VersionMetadata;

if self.dry_run() {
return Some(PathBuf::new());
}

let VersionMetadata { date, version } = self.stage0_metadata.rustfmt.as_ref()?;
let channel = format!("{version}-{date}");

Expand Down
5 changes: 0 additions & 5 deletions src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ forward! {
tempdir() -> PathBuf,
llvm_link_shared() -> bool,
download_rustc() -> bool,
initial_rustfmt() -> Option<PathBuf>,
}

impl Build {
Expand Down Expand Up @@ -614,10 +613,6 @@ impl Build {
crate::utils::job::setup(self);
}

// Download rustfmt early so that it can be used in rust-analyzer configs.
trace!("downloading rustfmt early");
let _ = &builder::Builder::new(self).initial_rustfmt();

// Handle hard-coded subcommands.
{
#[cfg(feature = "tracing")]
Expand Down
Loading