Skip to content

Commit 3e6a286

Browse files
authored
Rollup merge of rust-lang#141027 - onur-ozkan:simpler-rustfmt-initialization, r=albertlarsan68
remove `RustfmtState` to reduce `initial_rustfmt` complexity The current use of `RustfmtState` doesn't serve its main purpose as it never does the lazy evaulation since `Build::build` forces it to be ready on the early stage. If we want rustfmt to be ready on the early stage, we don't need to have `RustfmtState` complexity at all.
2 parents 9927143 + 92116bc commit 3e6a286

File tree

5 files changed

+12
-50
lines changed

5 files changed

+12
-50
lines changed

src/bootstrap/src/core/build_steps/format.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn rustfmt(
5858
fn get_rustfmt_version(build: &Builder<'_>) -> Option<(String, BuildStamp)> {
5959
let stamp_file = BuildStamp::new(&build.out).with_prefix("rustfmt");
6060

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

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

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

246-
let rustfmt_path = build.initial_rustfmt().unwrap_or_else(|| {
246+
let rustfmt_path = build.config.initial_rustfmt.clone().unwrap_or_else(|| {
247247
eprintln!("fmt error: `x fmt` is not supported on this channel");
248248
crate::exit!(1);
249249
});

src/bootstrap/src/core/build_steps/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ impl Step for Tidy {
11021102
if builder.config.channel == "dev" || builder.config.channel == "nightly" {
11031103
if !builder.config.json_output {
11041104
builder.info("fmt check");
1105-
if builder.initial_rustfmt().is_none() {
1105+
if builder.config.initial_rustfmt.is_none() {
11061106
let inferred_rustfmt_dir = builder.initial_sysroot.join("bin");
11071107
eprintln!(
11081108
"\

src/bootstrap/src/core/config/config.rs

+4-41
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This module implements parsing `bootstrap.toml` configuration files to tweak
44
//! how the build runs.
55
6-
use std::cell::{Cell, RefCell};
6+
use std::cell::Cell;
77
use std::collections::{BTreeSet, HashMap, HashSet};
88
use std::fmt::{self, Display};
99
use std::hash::Hash;
@@ -406,11 +406,7 @@ pub struct Config {
406406
pub initial_rustc: PathBuf,
407407
pub initial_cargo_clippy: Option<PathBuf>,
408408
pub initial_sysroot: PathBuf,
409-
410-
#[cfg(not(test))]
411-
initial_rustfmt: RefCell<RustfmtState>,
412-
#[cfg(test)]
413-
pub initial_rustfmt: RefCell<RustfmtState>,
409+
pub initial_rustfmt: Option<PathBuf>,
414410

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

431-
#[derive(Clone, Debug, Default)]
432-
pub enum RustfmtState {
433-
SystemToolchain(PathBuf),
434-
Downloaded(PathBuf),
435-
Unavailable,
436-
#[default]
437-
LazyEvaluated,
438-
}
439-
440427
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
441428
pub enum LlvmLibunwind {
442429
#[default]
@@ -2448,13 +2435,8 @@ impl Config {
24482435
});
24492436
}
24502437

2451-
if let Some(r) = rustfmt {
2452-
*config.initial_rustfmt.borrow_mut() = if r.exists() {
2453-
RustfmtState::SystemToolchain(r)
2454-
} else {
2455-
RustfmtState::Unavailable
2456-
};
2457-
}
2438+
config.initial_rustfmt =
2439+
if let Some(r) = rustfmt { Some(r) } else { config.maybe_download_rustfmt() };
24582440

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

2854-
pub(crate) fn initial_rustfmt(&self) -> Option<PathBuf> {
2855-
match &mut *self.initial_rustfmt.borrow_mut() {
2856-
RustfmtState::SystemToolchain(p) | RustfmtState::Downloaded(p) => Some(p.clone()),
2857-
RustfmtState::Unavailable => None,
2858-
r @ RustfmtState::LazyEvaluated => {
2859-
if self.dry_run() {
2860-
return Some(PathBuf::new());
2861-
}
2862-
let path = self.maybe_download_rustfmt();
2863-
*r = if let Some(p) = &path {
2864-
RustfmtState::Downloaded(p.clone())
2865-
} else {
2866-
RustfmtState::Unavailable
2867-
};
2868-
path
2869-
}
2870-
}
2871-
}
2872-
28732836
/// Runs a function if verbosity is greater than 0
28742837
pub fn verbose(&self, f: impl Fn()) {
28752838
if self.is_verbose() {

src/bootstrap/src/core/download.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ impl Config {
446446

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

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

458+
if self.dry_run() {
459+
return Some(PathBuf::new());
460+
}
461+
458462
let VersionMetadata { date, version } = self.stage0_metadata.rustfmt.as_ref()?;
459463
let channel = format!("{version}-{date}");
460464

src/bootstrap/src/lib.rs

-5
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ forward! {
325325
tempdir() -> PathBuf,
326326
llvm_link_shared() -> bool,
327327
download_rustc() -> bool,
328-
initial_rustfmt() -> Option<PathBuf>,
329328
}
330329

331330
impl Build {
@@ -614,10 +613,6 @@ impl Build {
614613
crate::utils::job::setup(self);
615614
}
616615

617-
// Download rustfmt early so that it can be used in rust-analyzer configs.
618-
trace!("downloading rustfmt early");
619-
let _ = &builder::Builder::new(self).initial_rustfmt();
620-
621616
// Handle hard-coded subcommands.
622617
{
623618
#[cfg(feature = "tracing")]

0 commit comments

Comments
 (0)