From 602c46252ea6f438075574732bcc8bab18252dc7 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Tue, 21 Jul 2026 21:38:30 +1000 Subject: [PATCH] Remove the blanket `#![cfg_attr(test, allow(unused))]` from bootstrap This blanket allow was hiding several unused imports and items in test-only code. It has been replaced with a number of narrower ensures. --- src/bootstrap/Cargo.lock | 23 ------- src/bootstrap/Cargo.toml | 1 - src/bootstrap/src/core/build_steps/llvm.rs | 1 + src/bootstrap/src/core/build_steps/tool.rs | 1 + .../src/core/builder/cli_paths/tests.rs | 6 +- src/bootstrap/src/core/builder/tests.rs | 66 ++++--------------- src/bootstrap/src/core/config/config.rs | 1 + src/bootstrap/src/core/config/tests.rs | 18 ++--- src/bootstrap/src/core/config/toml/llvm.rs | 1 + src/bootstrap/src/core/download.rs | 7 +- src/bootstrap/src/lib.rs | 1 - src/bootstrap/src/utils/build_stamp/tests.rs | 4 +- src/bootstrap/src/utils/cache.rs | 2 +- src/bootstrap/src/utils/cache/tests.rs | 4 +- src/bootstrap/src/utils/cc_detect/tests.rs | 10 +-- src/bootstrap/src/utils/helpers.rs | 1 + src/bootstrap/src/utils/helpers/tests.rs | 1 - src/bootstrap/src/utils/tests/git.rs | 2 - src/bootstrap/src/utils/tests/mod.rs | 12 ++-- 19 files changed, 39 insertions(+), 123 deletions(-) diff --git a/src/bootstrap/Cargo.lock b/src/bootstrap/Cargo.lock index 973334a8dda45..c05eeb97829b3 100644 --- a/src/bootstrap/Cargo.lock +++ b/src/bootstrap/Cargo.lock @@ -55,7 +55,6 @@ dependencies = [ "libc", "object", "opener", - "pretty_assertions", "semver", "serde", "serde_derive", @@ -229,12 +228,6 @@ dependencies = [ "typenum", ] -[[package]] -name = "diff" -version = "0.1.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" - [[package]] name = "digest" version = "0.10.7" @@ -526,16 +519,6 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" -[[package]] -name = "pretty_assertions" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d" -dependencies = [ - "diff", - "yansi", -] - [[package]] name = "proc-macro2" version = "1.0.89" @@ -1144,9 +1127,3 @@ checksum = "388c44dc09d76f1536602ead6d325eb532f5c122f17782bd57fb47baeeb767e2" dependencies = [ "lzma-sys", ] - -[[package]] -name = "yansi" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" diff --git a/src/bootstrap/Cargo.toml b/src/bootstrap/Cargo.toml index b2e8c42adf48b..cb96af268a6aa 100644 --- a/src/bootstrap/Cargo.toml +++ b/src/bootstrap/Cargo.toml @@ -85,7 +85,6 @@ features = [ ] [dev-dependencies] -pretty_assertions = "1.4" insta = "1.43" # We care a lot about bootstrap's compile times, so don't include debuginfo for diff --git a/src/bootstrap/src/core/build_steps/llvm.rs b/src/bootstrap/src/core/build_steps/llvm.rs index 0c7806342a1e6..572df47d3a62c 100644 --- a/src/bootstrap/src/core/build_steps/llvm.rs +++ b/src/bootstrap/src/core/build_steps/llvm.rs @@ -200,6 +200,7 @@ pub const LLVM_INVALIDATION_PATHS: &[&str] = &[ ]; /// Detect whether LLVM sources have been modified locally or not. +#[cfg_attr(test, expect(dead_code))] pub(crate) fn detect_llvm_freshness(config: &Config, is_git: bool) -> PathFreshness { if is_git { config.check_path_modifications(LLVM_INVALIDATION_PATHS) diff --git a/src/bootstrap/src/core/build_steps/tool.rs b/src/bootstrap/src/core/build_steps/tool.rs index e5d24280092d9..ea8a0b581fcb0 100644 --- a/src/bootstrap/src/core/build_steps/tool.rs +++ b/src/bootstrap/src/core/build_steps/tool.rs @@ -1228,6 +1228,7 @@ impl Step for LlvmBitcodeLinker { } #[derive(Debug, Clone, Hash, PartialEq, Eq)] +#[cfg_attr(test, expect(dead_code))] pub struct LibcxxVersionTool { pub target: TargetSelection, } diff --git a/src/bootstrap/src/core/builder/cli_paths/tests.rs b/src/bootstrap/src/core/builder/cli_paths/tests.rs index 40a37e5cf5631..79137de12981b 100644 --- a/src/bootstrap/src/core/builder/cli_paths/tests.rs +++ b/src/bootstrap/src/core/builder/cli_paths/tests.rs @@ -1,10 +1,8 @@ use std::collections::{BTreeSet, HashSet}; -use std::fs; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::sync::{Arc, Mutex}; use crate::Build; -use crate::core::builder::cli_paths::match_paths_to_steps_and_run; use crate::core::builder::{Builder, StepDescription}; use crate::utils::tests::TestCtx; @@ -34,7 +32,7 @@ fn render_steps_for_cli_args(args_str: &str) -> String { let mut builder = Builder::new(&build); // Tell the builder to log steps that it would run, instead of running them. - let mut buf = Arc::new(Mutex::new(String::new())); + let buf = Arc::new(Mutex::new(String::new())); let buf2 = Arc::clone(&buf); builder.log_cli_step_for_tests = Some(Box::new(move |step_desc, pathsets, targets| { use std::fmt::Write; diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index c8730dd8fa956..02c57023dc354 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -1,13 +1,10 @@ // ignore-tidy-file-filelength -use std::env::VarError; -use std::{panic, thread}; +use std::panic; use build_helper::stage0_parser::parse_stage0_file; use llvm::prebuilt_llvm_config; use super::*; -use crate::Flags; -use crate::core::build_steps::doc::DocumentationFormat; use crate::core::builder::cli_paths::PATH_REMAP; use crate::core::config::Config; use crate::utils::cache::ExecutedStep; @@ -17,7 +14,6 @@ use crate::utils::tests::{ConfigBuilder, TestCtx}; static TEST_TRIPLE_1: &str = "i686-unknown-haiku"; static TEST_TRIPLE_2: &str = "i686-unknown-hurd-gnu"; -static TEST_TRIPLE_3: &str = "i686-unknown-netbsd"; fn configure(cmd: &str, host: &[&str], target: &[&str]) -> Config { configure_with_args(&[cmd], host, target) @@ -27,10 +23,6 @@ fn configure_with_args(cmd: &[&str], host: &[&str], target: &[&str]) -> Config { TestCtx::new().config(cmd[0]).args(&cmd[1..]).hosts(host).targets(target).create_config() } -fn first(v: Vec<(A, B)>) -> Vec { - v.into_iter().map(|(a, _)| a).collect::>() -} - fn run_build(paths: &[PathBuf], config: Config) -> Cache { let kind = config.cmd.kind(); let build = Build::new(config); @@ -46,28 +38,6 @@ fn check_cli(paths: [&str; N]) { ); } -macro_rules! std { - ($host:ident => $target:ident, stage = $stage:literal) => { - compile::Std::new( - Compiler::new($stage, TargetSelection::from_user($host)), - TargetSelection::from_user($target), - ) - }; -} - -macro_rules! doc_std { - ($host:ident => $target:ident, stage = $stage:literal) => {{ doc::Std::new($stage, TargetSelection::from_user($target), DocumentationFormat::Html) }}; -} - -macro_rules! rustc { - ($host:ident => $target:ident, stage = $stage:literal) => { - compile::Rustc::new( - Compiler::new($stage, TargetSelection::from_user($host)), - TargetSelection::from_user($target), - ) - }; -} - #[test] fn test_valid() { // make sure multi suite paths are accepted @@ -228,10 +198,7 @@ fn parse_config_download_rustc_at(path: &Path, download_rustc: &str, ci: bool) - } mod dist { - use pretty_assertions::assert_eq; - - use super::{Config, TEST_TRIPLE_1, TEST_TRIPLE_2, TEST_TRIPLE_3, first, run_build}; - use crate::Flags; + use super::{Config, TEST_TRIPLE_1, TEST_TRIPLE_2}; use crate::core::builder::tests::host_target; use crate::core::builder::*; @@ -344,7 +311,7 @@ fn test_test_coverage() { // case is the one that failed. println!("Testing case: {cmd:?}"); let config = configure_with_args(cmd, &[], &[TEST_TRIPLE_1]); - let mut cache = run_build(&config.paths.clone(), config); + let cache = run_build(&config.paths.clone(), config); let modes = cache.inspect_all_steps_of_type::(|step, ()| step.mode.as_str()); @@ -507,22 +474,15 @@ fn any_debug() { /// These tests use insta for snapshot testing. /// See bootstrap's README on how to bless the snapshots. mod snapshot { - use std::path::PathBuf; - - use crate::core::build_steps::{compile, dist, doc, test, tool}; - use crate::core::builder::tests::{ - RenderConfig, TEST_TRIPLE_1, TEST_TRIPLE_2, TEST_TRIPLE_3, configure, first, host_target, - render_steps, run_build, - }; - use crate::core::builder::{Builder, Kind, StepDescription, StepMetadata}; + use crate::Compiler; + use crate::core::build_steps::test; + use crate::core::builder::tests::{RenderConfig, TEST_TRIPLE_1, TEST_TRIPLE_2, host_target}; + use crate::core::builder::{Kind, StepMetadata}; use crate::core::config::TargetSelection; use crate::core::config::toml::target::{ DefaultLinuxLinkerOverride, with_default_linux_linker_overrides, }; - use crate::utils::cache::Cache; - use crate::utils::helpers::get_host_target; use crate::utils::tests::{ConfigBuilder, TestCtx}; - use crate::{Build, Compiler, Config, Flags, Subcommand}; #[test] fn build_default() { @@ -2426,7 +2386,6 @@ mod snapshot { #[test] fn test_library_tests_only_does_not_build_rustdoc() { let ctx = TestCtx::new(); - let host = TargetSelection::from_user(&host_target()); insta::assert_snapshot!( ctx.config("test").args(&["--tests", "library/core"]).render_steps(), @r" @@ -3257,7 +3216,7 @@ impl ExecutedSteps { } fn fuzzy_metadata_eq(executed: &StepMetadata, to_match: &StepMetadata) -> bool { - let StepMetadata { name, kind, target, built_by: _, stage: _, metadata } = executed; + let StepMetadata { name, kind, target, built_by: _, stage: _, metadata: _ } = executed; *name == to_match.name && *kind == to_match.kind && *target == to_match.target } @@ -3309,8 +3268,6 @@ fn render_steps(steps: &[ExecutedStep], config: RenderConfig) -> String { steps .iter() .filter_map(|step| { - use std::fmt::Write; - let Some(metadata) = &step.metadata else { return None; }; @@ -3324,12 +3281,13 @@ fn render_steps(steps: &[ExecutedStep], config: RenderConfig) -> String { fn render_metadata(metadata: &StepMetadata, config: &RenderConfig) -> String { let mut record = format!("[{}] ", metadata.kind.as_str()); if let Some(compiler) = metadata.built_by { - write!(record, "{} -> ", render_compiler(compiler, config)); + write!(record, "{} -> ", render_compiler(compiler, config)).unwrap(); } let stage = metadata.get_stage().map(|stage| format!("{stage} ")).unwrap_or_default(); - write!(record, "{} {stage}<{}>", metadata.name, normalize_target(metadata.target, config)); + write!(record, "{} {stage}<{}>", metadata.name, normalize_target(metadata.target, config)) + .unwrap(); if let Some(metadata) = &metadata.metadata { - write!(record, " {metadata}"); + write!(record, " {metadata}").unwrap(); } record } diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index eaa9672d2130f..8b8c595882579 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -57,6 +57,7 @@ use crate::core::download::{ }; use crate::utils::channel; use crate::utils::exec::{ExecutionContext, command}; +#[cfg_attr(test, expect(unused_imports))] use crate::utils::helpers::{exe, fail, get_host_target}; use crate::{ CodegenBackendKind, GitInfo, OnceLock, TargetSelection, check_ci_llvm, exit, helpers, t, diff --git a/src/bootstrap/src/core/config/tests.rs b/src/bootstrap/src/core/config/tests.rs index 89046c0eee779..91327057a8183 100644 --- a/src/bootstrap/src/core/config/tests.rs +++ b/src/bootstrap/src/core/config/tests.rs @@ -1,13 +1,12 @@ use std::collections::BTreeSet; -use std::fs::{File, remove_file}; +use std::fs; +use std::fs::File; use std::io::Write; -use std::path::{Path, PathBuf}; -use std::{env, fs}; +use std::path::PathBuf; use build_helper::ci::CiEnv; use build_helper::git::PathFreshness; use clap::CommandFactory; -use serde::Deserialize; use super::flags::Flags; use super::toml::change_id::ChangeIdWrapper; @@ -16,11 +15,7 @@ use super::{Config, RUSTC_IF_UNCHANGED_ALLOWED_PATHS}; use crate::ChangeId; use crate::core::build_steps::clippy::{LintConfig, get_clippy_rules_in_order}; use crate::core::build_steps::llvm::LLVM_INVALIDATION_PATHS; -use crate::core::build_steps::{llvm, test}; -use crate::core::config::toml::TomlConfig; -use crate::core::config::{ - BootstrapOverrideLld, CompilerBuiltins, StringOrBool, Target, TargetSelection, -}; +use crate::core::config::{BootstrapOverrideLld, CompilerBuiltins, Target, TargetSelection}; use crate::utils::tests::TestCtx; use crate::utils::tests::git::git_test; @@ -28,11 +23,6 @@ pub(crate) fn parse(config: &str) -> Config { TestCtx::new().config("check").with_default_toml_config(config).create_config() } -fn get_toml(file: &Path) -> Result { - let contents = std::fs::read_to_string(file).unwrap(); - toml::from_str(&contents).and_then(|table: toml::Value| TomlConfig::deserialize(table)) -} - fn modified(upstream: impl Into, changes: &[&str]) -> PathFreshness { PathFreshness::HasLocalModifications { upstream: upstream.into(), diff --git a/src/bootstrap/src/core/config/toml/llvm.rs b/src/bootstrap/src/core/config/toml/llvm.rs index 5f08884e4ef71..8bc6b4ef2491c 100644 --- a/src/bootstrap/src/core/config/toml/llvm.rs +++ b/src/bootstrap/src/core/config/toml/llvm.rs @@ -4,6 +4,7 @@ use serde::{Deserialize, Deserializer}; use crate::core::config::StringOrBool; +#[cfg_attr(test, expect(unused_imports))] use crate::core::config::toml::{Merge, ReplaceOpt, TomlConfig}; use crate::{HashMap, HashSet, PathBuf, define_config, exit}; diff --git a/src/bootstrap/src/core/download.rs b/src/bootstrap/src/core/download.rs index 078012be72c59..76fd2cb0bc14b 100644 --- a/src/bootstrap/src/core/download.rs +++ b/src/bootstrap/src/core/download.rs @@ -505,8 +505,8 @@ pub(crate) fn is_download_ci_available(target_triple: &str, llvm_assertions: boo #[cfg(test)] pub(crate) fn maybe_download_rustfmt<'a>( - dwn_ctx: impl AsRef>, - out: &Path, + _dwn_ctx: impl AsRef>, + _out: &Path, ) -> Option { Some(PathBuf::new()) } @@ -574,7 +574,7 @@ pub(crate) fn maybe_download_rustfmt<'a>( } #[cfg(test)] -pub(crate) fn download_beta_toolchain<'a>(dwn_ctx: impl AsRef>, out: &Path) {} +pub(crate) fn download_beta_toolchain<'a>(_dwn_ctx: impl AsRef>, _out: &Path) {} #[cfg(not(test))] pub(crate) fn download_beta_toolchain<'a>(dwn_ctx: impl AsRef>, out: &Path) { @@ -600,6 +600,7 @@ pub(crate) fn download_beta_toolchain<'a>(dwn_ctx: impl AsRef( dwn_ctx: impl AsRef>, out: &Path, diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index f08346789e7e8..c487941b34b87 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -15,7 +15,6 @@ //! //! More documentation can be found in each respective module below, and you can //! also check out the `src/bootstrap/README.md` file for more information. -#![cfg_attr(test, allow(unused))] use std::cell::Cell; use std::collections::{BTreeSet, HashMap, HashSet}; diff --git a/src/bootstrap/src/utils/build_stamp/tests.rs b/src/bootstrap/src/utils/build_stamp/tests.rs index f150266159a25..ae70c6085e7fb 100644 --- a/src/bootstrap/src/utils/build_stamp/tests.rs +++ b/src/bootstrap/src/utils/build_stamp/tests.rs @@ -1,8 +1,6 @@ -use std::path::PathBuf; - use tempfile::TempDir; -use crate::{BuildStamp, Config, Flags}; +use crate::BuildStamp; #[test] #[should_panic(expected = "prefix can not start or end with '.'")] diff --git a/src/bootstrap/src/utils/cache.rs b/src/bootstrap/src/utils/cache.rs index 8c5b3529979d7..1de1359abb159 100644 --- a/src/bootstrap/src/utils/cache.rs +++ b/src/bootstrap/src/utils/cache.rs @@ -289,7 +289,7 @@ impl Cache { } #[cfg(test)] - pub fn into_executed_steps(mut self) -> Vec { + pub fn into_executed_steps(self) -> Vec { mem::take(&mut self.executed_steps.borrow_mut()) } } diff --git a/src/bootstrap/src/utils/cache/tests.rs b/src/bootstrap/src/utils/cache/tests.rs index fd0a7cccd60fc..722b66d861b83 100644 --- a/src/bootstrap/src/utils/cache/tests.rs +++ b/src/bootstrap/src/utils/cache/tests.rs @@ -1,6 +1,4 @@ -use std::path::PathBuf; - -use crate::utils::cache::{INTERNER, Internable, Interner, TyIntern}; +use crate::utils::cache::{INTERNER, Interner, TyIntern}; #[test] fn test_string_interning() { diff --git a/src/bootstrap/src/utils/cc_detect/tests.rs b/src/bootstrap/src/utils/cc_detect/tests.rs index 30757a39caa1c..a2f35e6a1030d 100644 --- a/src/bootstrap/src/utils/cc_detect/tests.rs +++ b/src/bootstrap/src/utils/cc_detect/tests.rs @@ -1,10 +1,10 @@ -use std::path::{Path, PathBuf}; -use std::{env, iter}; +use std::iter; +use std::path::PathBuf; use super::*; +use crate::Build; use crate::core::config::{Target, TargetSelection}; use crate::utils::tests::TestCtx; -use crate::{Build, Config, Flags, t}; #[test] fn test_ndk_compiler_c() { @@ -85,7 +85,7 @@ fn test_default_compiler_wasi() { let wasi_sdk = PathBuf::from("/wasi-sdk"); build.wasi_sdk_path = Some(wasi_sdk.clone()); - let mut cfg = cc::Build::new(); + let cfg = cc::Build::new(); if let Some(result) = default_compiler(&cfg, Language::C, target.clone(), &build) { let expected = { let compiler = format!("{}-clang", target.triple); @@ -104,7 +104,7 @@ fn test_default_compiler_fallback() { let config = TestCtx::new().config("build").create_config(); let build = Build::new(config); let target = TargetSelection::from_user("x86_64-unknown-linux-gnu"); - let mut cfg = cc::Build::new(); + let cfg = cc::Build::new(); let result = default_compiler(&cfg, Language::C, target, &build); assert!(result.is_none(), "default_compiler should return None for generic targets"); } diff --git a/src/bootstrap/src/utils/helpers.rs b/src/bootstrap/src/utils/helpers.rs index 394e14c80a1ef..c36d43f0cb5a4 100644 --- a/src/bootstrap/src/utils/helpers.rs +++ b/src/bootstrap/src/utils/helpers.rs @@ -595,6 +595,7 @@ pub fn detail_exit(code: i32, is_test: bool) -> ! { } } +#[cfg_attr(test, expect(dead_code))] pub fn fail(s: &str) -> ! { eprintln!("\n\n{s}\n\n"); detail_exit(1, cfg!(test)); diff --git a/src/bootstrap/src/utils/helpers/tests.rs b/src/bootstrap/src/utils/helpers/tests.rs index b051a374a0866..cfb1dde2d6ea3 100644 --- a/src/bootstrap/src/utils/helpers/tests.rs +++ b/src/bootstrap/src/utils/helpers/tests.rs @@ -7,7 +7,6 @@ use crate::utils::helpers::{ symlink_dir, }; use crate::utils::tests::TestCtx; -use crate::{Config, Flags}; #[test] fn test_make() { diff --git a/src/bootstrap/src/utils/tests/git.rs b/src/bootstrap/src/utils/tests/git.rs index d9dd9ab980088..528452680d70b 100644 --- a/src/bootstrap/src/utils/tests/git.rs +++ b/src/bootstrap/src/utils/tests/git.rs @@ -8,7 +8,6 @@ use build_helper::git::{GitConfig, PathFreshness, check_path_modifications}; pub struct GitCtx { dir: tempfile::TempDir, - pub git_repo: String, pub nightly_branch: String, pub merge_bot_email: String, } @@ -18,7 +17,6 @@ impl GitCtx { let dir = tempfile::TempDir::new().unwrap(); let ctx = Self { dir, - git_repo: "rust-lang/rust".to_string(), nightly_branch: "nightly".to_string(), merge_bot_email: "Merge bot ".to_string(), }; diff --git a/src/bootstrap/src/utils/tests/mod.rs b/src/bootstrap/src/utils/tests/mod.rs index e2a689c0f0ccf..94dfa33455d80 100644 --- a/src/bootstrap/src/utils/tests/mod.rs +++ b/src/bootstrap/src/utils/tests/mod.rs @@ -1,14 +1,10 @@ //! This module contains shared utilities for bootstrap tests. use std::path::{Path, PathBuf}; -use std::thread; use tempfile::TempDir; -use crate::core::builder::Builder; -use crate::core::config::DryRun; -use crate::utils::helpers::get_host_target; -use crate::{Build, Config, Flags, t}; +use crate::{Config, Flags}; pub mod git; @@ -69,11 +65,11 @@ impl ConfigBuilder { } } - pub fn path(mut self, path: &str) -> Self { + pub fn path(self, path: &str) -> Self { self.arg(path) } - pub fn paths(mut self, paths: &[&str]) -> Self { + pub fn paths(self, paths: &[&str]) -> Self { self.args(paths) } @@ -90,7 +86,7 @@ impl ConfigBuilder { } /// Set the specified target to be treated as a no_std target. - pub fn override_target_no_std(mut self, target: &str) -> Self { + pub fn override_target_no_std(self, target: &str) -> Self { self.args(&["--set", &format!("target.{target}.no-std=true")]) }