diff options
Diffstat (limited to 'src')
93 files changed, 2207 insertions, 2027 deletions
diff --git a/src/bootstrap/src/core/build_steps/compile.rs b/src/bootstrap/src/core/build_steps/compile.rs index e1ee0773107..79174eb281f 100644 --- a/src/bootstrap/src/core/build_steps/compile.rs +++ b/src/bootstrap/src/core/build_steps/compile.rs @@ -23,8 +23,7 @@ use crate::core::build_steps::tool::{RustcPrivateCompilers, SourceType, copy_lld use crate::core::build_steps::{dist, llvm}; use crate::core::builder; use crate::core::builder::{ - Builder, Cargo, Kind, PathSet, RunConfig, ShouldRun, Step, StepMetadata, TaskPath, - crate_description, + Builder, Cargo, Kind, RunConfig, ShouldRun, Step, StepMetadata, crate_description, }; use crate::core::config::{DebuginfoLevel, LlvmLibunwind, RustcLto, TargetSelection}; use crate::utils::build_stamp; @@ -1539,99 +1538,131 @@ impl Step for RustcLink { } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct CodegenBackend { +pub struct GccCodegenBackend { compilers: RustcPrivateCompilers, - backend: CodegenBackendKind, } -fn needs_codegen_config(run: &RunConfig<'_>) -> bool { - let mut needs_codegen_cfg = false; - for path_set in &run.paths { - needs_codegen_cfg = match path_set { - PathSet::Set(set) => set.iter().any(|p| is_codegen_cfg_needed(p, run)), - PathSet::Suite(suite) => is_codegen_cfg_needed(suite, run), - } +impl Step for GccCodegenBackend { + type Output = BuildStamp; + const ONLY_HOSTS: bool = true; + + fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { + run.alias("rustc_codegen_gcc").alias("cg_gcc") } - needs_codegen_cfg -} -pub(crate) const CODEGEN_BACKEND_PREFIX: &str = "rustc_codegen_"; + fn make_run(run: RunConfig<'_>) { + run.builder.ensure(GccCodegenBackend { + compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target), + }); + } -fn is_codegen_cfg_needed(path: &TaskPath, run: &RunConfig<'_>) -> bool { - let path = path.path.to_str().unwrap(); + #[cfg_attr( + feature = "tracing", + instrument( + level = "debug", + name = "GccCodegenBackend::run", + skip_all, + fields( + compilers = ?self.compilers, + ), + ), + )] + fn run(self, builder: &Builder<'_>) -> Self::Output { + let target = self.compilers.target(); + let build_compiler = self.compilers.build_compiler(); - let is_explicitly_called = |p| -> bool { run.builder.paths.contains(p) }; - let should_enforce = run.builder.kind == Kind::Dist || run.builder.kind == Kind::Install; + let stamp = build_stamp::codegen_backend_stamp( + builder, + build_compiler, + target, + &CodegenBackendKind::Gcc, + ); - if path.contains(CODEGEN_BACKEND_PREFIX) { - let mut needs_codegen_backend_config = true; - for backend in run.builder.config.codegen_backends(run.target) { - if path.ends_with(&(CODEGEN_BACKEND_PREFIX.to_owned() + backend.name())) { - needs_codegen_backend_config = false; - } - } - if (is_explicitly_called(&PathBuf::from(path)) || should_enforce) - && needs_codegen_backend_config - { - run.builder.info( - "WARNING: no codegen-backends config matched the requested path to build a codegen backend. \ - HELP: add backend to codegen-backends in bootstrap.toml.", + if builder.config.keep_stage.contains(&build_compiler.stage) { + trace!("`keep-stage` requested"); + builder.info( + "WARNING: Using a potentially old codegen backend. \ + This may not behave well.", ); - return true; + // Codegen backends are linked separately from this step today, so we don't do + // anything here. + return stamp; } + + let mut cargo = builder::Cargo::new( + builder, + build_compiler, + Mode::Codegen, + SourceType::InTree, + target, + Kind::Build, + ); + cargo.arg("--manifest-path").arg(builder.src.join("compiler/rustc_codegen_gcc/Cargo.toml")); + rustc_cargo_env(builder, &mut cargo, target); + + let gcc = builder.ensure(Gcc { target }); + add_cg_gcc_cargo_flags(&mut cargo, &gcc); + + let _guard = builder.msg_rustc_tool( + Kind::Build, + build_compiler.stage, + "codegen backend gcc", + build_compiler.host, + target, + ); + let files = run_cargo(builder, cargo, vec![], &stamp, vec![], false, false); + write_codegen_backend_stamp(stamp, files, builder.config.dry_run()) } - false + fn metadata(&self) -> Option<StepMetadata> { + Some( + StepMetadata::build("rustc_codegen_gcc", self.compilers.target()) + .built_by(self.compilers.build_compiler()), + ) + } } -impl Step for CodegenBackend { - type Output = (); +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct CraneliftCodegenBackend { + pub compilers: RustcPrivateCompilers, +} + +impl Step for CraneliftCodegenBackend { + type Output = BuildStamp; const ONLY_HOSTS: bool = true; - /// Only the backends specified in the `codegen-backends` entry of `bootstrap.toml` are built. - const DEFAULT: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { - run.paths(&["compiler/rustc_codegen_cranelift", "compiler/rustc_codegen_gcc"]) + run.alias("rustc_codegen_cranelift").alias("cg_clif") } fn make_run(run: RunConfig<'_>) { - if needs_codegen_config(&run) { - return; - } - - for backend in run.builder.config.codegen_backends(run.target) { - if backend.is_llvm() { - continue; // Already built as part of rustc - } - - run.builder.ensure(CodegenBackend { - compilers: RustcPrivateCompilers::new( - run.builder, - run.builder.top_stage, - run.target, - ), - backend: backend.clone(), - }); - } + run.builder.ensure(CraneliftCodegenBackend { + compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target), + }); } #[cfg_attr( feature = "tracing", instrument( level = "debug", - name = "CodegenBackend::run", + name = "CraneliftCodegenBackend::run", skip_all, fields( compilers = ?self.compilers, - backend = ?self.backend, ), ), )] - fn run(self, builder: &Builder<'_>) { - let backend = self.backend; + fn run(self, builder: &Builder<'_>) -> Self::Output { let target = self.compilers.target(); let build_compiler = self.compilers.build_compiler(); + let stamp = build_stamp::codegen_backend_stamp( + builder, + build_compiler, + target, + &CodegenBackendKind::Cranelift, + ); + if builder.config.keep_stage.contains(&build_compiler.stage) { trace!("`keep-stage` requested"); builder.info( @@ -1640,11 +1671,9 @@ impl Step for CodegenBackend { ); // Codegen backends are linked separately from this step today, so we don't do // anything here. - return; + return stamp; } - let out_dir = builder.cargo_out(build_compiler, Mode::Codegen, target); - let mut cargo = builder::Cargo::new( builder, build_compiler, @@ -1655,71 +1684,67 @@ impl Step for CodegenBackend { ); cargo .arg("--manifest-path") - .arg(builder.src.join(format!("compiler/{}/Cargo.toml", backend.crate_name()))); + .arg(builder.src.join("compiler/rustc_codegen_cranelift/Cargo.toml")); rustc_cargo_env(builder, &mut cargo, target); - // Ideally, we'd have a separate step for the individual codegen backends, - // like we have in tests (test::CodegenGCC) but that would require a lot of restructuring. - // If the logic gets more complicated, it should probably be done. - if backend.is_gcc() { - let gcc = builder.ensure(Gcc { target }); - add_cg_gcc_cargo_flags(&mut cargo, &gcc); - } - - let tmp_stamp = BuildStamp::new(&out_dir).with_prefix("tmp"); - let _guard = builder.msg_rustc_tool( Kind::Build, build_compiler.stage, - format_args!("codegen backend {}", backend.name()), + "codegen backend cranelift", build_compiler.host, target, ); - let files = run_cargo(builder, cargo, vec![], &tmp_stamp, vec![], false, false); - if builder.config.dry_run() { - return; - } - let mut files = files.into_iter().filter(|f| { - let filename = f.file_name().unwrap().to_str().unwrap(); - is_dylib(f) && filename.contains("rustc_codegen_") - }); - let codegen_backend = match files.next() { - Some(f) => f, - None => panic!("no dylibs built for codegen backend?"), - }; - if let Some(f) = files.next() { - panic!( - "codegen backend built two dylibs:\n{}\n{}", - codegen_backend.display(), - f.display() - ); - } - let stamp = build_stamp::codegen_backend_stamp(builder, build_compiler, target, &backend); - let codegen_backend = codegen_backend.to_str().unwrap(); - t!(stamp.add_stamp(codegen_backend).write()); + let files = run_cargo(builder, cargo, vec![], &stamp, vec![], false, false); + write_codegen_backend_stamp(stamp, files, builder.config.dry_run()) } fn metadata(&self) -> Option<StepMetadata> { Some( - StepMetadata::build(&self.backend.crate_name(), self.compilers.target()) + StepMetadata::build("rustc_codegen_cranelift", self.compilers.target()) .built_by(self.compilers.build_compiler()), ) } } +/// Write filtered `files` into the passed build stamp and returns it. +fn write_codegen_backend_stamp( + mut stamp: BuildStamp, + files: Vec<PathBuf>, + dry_run: bool, +) -> BuildStamp { + if dry_run { + return stamp; + } + + let mut files = files.into_iter().filter(|f| { + let filename = f.file_name().unwrap().to_str().unwrap(); + is_dylib(f) && filename.contains("rustc_codegen_") + }); + let codegen_backend = match files.next() { + Some(f) => f, + None => panic!("no dylibs built for codegen backend?"), + }; + if let Some(f) = files.next() { + panic!("codegen backend built two dylibs:\n{}\n{}", codegen_backend.display(), f.display()); + } + + let codegen_backend = codegen_backend.to_str().unwrap(); + stamp = stamp.add_stamp(codegen_backend); + t!(stamp.write()); + stamp +} + /// Creates the `codegen-backends` folder for a compiler that's about to be /// assembled as a complete compiler. /// -/// This will take the codegen artifacts produced by `compiler` and link them +/// This will take the codegen artifacts recorded in the given `stamp` and link them /// into an appropriate location for `target_compiler` to be a functional /// compiler. fn copy_codegen_backends_to_sysroot( builder: &Builder<'_>, - compiler: Compiler, + stamp: BuildStamp, target_compiler: Compiler, ) { - let target = target_compiler.host; - // Note that this step is different than all the other `*Link` steps in // that it's not assembling a bunch of libraries but rather is primarily // moving the codegen backend into place. The codegen backend of rustc is @@ -1735,25 +1760,18 @@ fn copy_codegen_backends_to_sysroot( return; } - for backend in builder.config.codegen_backends(target) { - if backend.is_llvm() { - continue; // Already built as part of rustc - } - - let stamp = build_stamp::codegen_backend_stamp(builder, compiler, target, backend); - if stamp.path().exists() { - let dylib = t!(fs::read_to_string(stamp.path())); - let file = Path::new(&dylib); - let filename = file.file_name().unwrap().to_str().unwrap(); - // change `librustc_codegen_cranelift-xxxxxx.so` to - // `librustc_codegen_cranelift-release.so` - let target_filename = { - let dash = filename.find('-').unwrap(); - let dot = filename.find('.').unwrap(); - format!("{}-{}{}", &filename[..dash], builder.rust_release(), &filename[dot..]) - }; - builder.copy_link(file, &dst.join(target_filename), FileType::NativeLibrary); - } + if stamp.path().exists() { + let dylib = t!(fs::read_to_string(stamp.path())); + let file = Path::new(&dylib); + let filename = file.file_name().unwrap().to_str().unwrap(); + // change `librustc_codegen_cranelift-xxxxxx.so` to + // `librustc_codegen_cranelift-release.so` + let target_filename = { + let dash = filename.find('-').unwrap(); + let dot = filename.find('.').unwrap(); + format!("{}-{}{}", &filename[..dash], builder.rust_release(), &filename[dot..]) + }; + builder.copy_link(file, &dst.join(target_filename), FileType::NativeLibrary); } } @@ -2162,44 +2180,52 @@ impl Step for Assemble { ); build_compiler.stage = actual_stage; - #[cfg(feature = "tracing")] - let _codegen_backend_span = - span!(tracing::Level::DEBUG, "building requested codegen backends").entered(); - for backend in builder.config.codegen_backends(target_compiler.host) { - if backend.is_llvm() { - debug!("llvm codegen backend is already built as part of rustc"); - continue; // Already built as part of rustc - } + let mut codegen_backend_stamps = vec![]; + { + #[cfg(feature = "tracing")] + let _codegen_backend_span = + span!(tracing::Level::DEBUG, "building requested codegen backends").entered(); + + for backend in builder.config.enabled_codegen_backends(target_compiler.host) { + // FIXME: this is a horrible hack used to make `x check` work when other codegen + // backends are enabled. + // `x check` will check stage 1 rustc, which copies its rmetas to the stage0 sysroot. + // Then it checks codegen backends, which correctly use these rmetas. + // Then it needs to check std, but for that it needs to build stage 1 rustc. + // This copies the build rmetas into the stage0 sysroot, effectively poisoning it, + // because we then have both check and build rmetas in the same sysroot. + // That would be fine on its own. However, when another codegen backend is enabled, + // then building stage 1 rustc implies also building stage 1 codegen backend (even if + // it isn't used for anything). And since that tries to use the poisoned + // rmetas, it fails to build. + // We don't actually need to build rustc-private codegen backends for checking std, + // so instead we skip that. + // Note: this would be also an issue for other rustc-private tools, but that is "solved" + // by check::Std being last in the list of checked things (see + // `Builder::get_step_descriptions`). + if builder.kind == Kind::Check && builder.top_stage == 1 { + continue; + } - // FIXME: this is a horrible hack used to make `x check` work when other codegen - // backends are enabled. - // `x check` will check stage 1 rustc, which copies its rmetas to the stage0 sysroot. - // Then it checks codegen backends, which correctly use these rmetas. - // Then it needs to check std, but for that it needs to build stage 1 rustc. - // This copies the build rmetas into the stage0 sysroot, effectively poisoning it, - // because we then have both check and build rmetas in the same sysroot. - // That would be fine on its own. However, when another codegen backend is enabled, - // then building stage 1 rustc implies also building stage 1 codegen backend (even if - // it isn't used for anything). And since that tries to use the poisoned - // rmetas, it fails to build. - // We don't actually need to build rustc-private codegen backends for checking std, - // so instead we skip that. - // Note: this would be also an issue for other rustc-private tools, but that is "solved" - // by check::Std being last in the list of checked things (see - // `Builder::get_step_descriptions`). - if builder.kind == Kind::Check && builder.top_stage == 1 { - continue; + let prepare_compilers = || { + RustcPrivateCompilers::from_build_and_target_compiler( + build_compiler, + target_compiler, + ) + }; + + let stamp = match backend { + CodegenBackendKind::Cranelift => { + builder.ensure(CraneliftCodegenBackend { compilers: prepare_compilers() }) + } + CodegenBackendKind::Gcc => { + builder.ensure(GccCodegenBackend { compilers: prepare_compilers() }) + } + CodegenBackendKind::Llvm | CodegenBackendKind::Custom(_) => continue, + }; + codegen_backend_stamps.push(stamp); } - builder.ensure(CodegenBackend { - compilers: RustcPrivateCompilers::from_build_and_target_compiler( - build_compiler, - target_compiler, - ), - backend: backend.clone(), - }); } - #[cfg(feature = "tracing")] - drop(_codegen_backend_span); let stage = target_compiler.stage; let host = target_compiler.host; @@ -2260,7 +2286,9 @@ impl Step for Assemble { } debug!("copying codegen backends to sysroot"); - copy_codegen_backends_to_sysroot(builder, build_compiler, target_compiler); + for stamp in codegen_backend_stamps { + copy_codegen_backends_to_sysroot(builder, stamp, target_compiler); + } if builder.config.lld_enabled { let lld_wrapper = diff --git a/src/bootstrap/src/core/build_steps/dist.rs b/src/bootstrap/src/core/build_steps/dist.rs index cbbfb6b6a11..3fbc8cdbcd5 100644 --- a/src/bootstrap/src/core/build_steps/dist.rs +++ b/src/bootstrap/src/core/build_steps/dist.rs @@ -1389,38 +1389,39 @@ impl Step for Miri { } #[derive(Debug, Clone, Hash, PartialEq, Eq)] -pub struct CodegenBackend { - pub compiler: Compiler, - pub backend: CodegenBackendKind, +pub struct CraneliftCodegenBackend { + pub build_compiler: Compiler, } -impl Step for CodegenBackend { +impl Step for CraneliftCodegenBackend { type Output = Option<GeneratedTarball>; const DEFAULT: bool = true; const ONLY_HOSTS: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { - run.path("compiler/rustc_codegen_cranelift") + // We only want to build the cranelift backend in `x dist` if the backend was enabled + // in rust.codegen-backends. + // Sadly, we don't have access to the actual target for which we're disting clif here.. + // So we just use the host target. + let clif_enabled_by_default = run + .builder + .config + .enabled_codegen_backends(run.builder.host_target) + .contains(&CodegenBackendKind::Cranelift); + run.alias("rustc_codegen_cranelift").default_condition(clif_enabled_by_default) } fn make_run(run: RunConfig<'_>) { - for backend in run.builder.config.codegen_backends(run.target) { - if backend.is_llvm() { - continue; // Already built as part of rustc - } - - run.builder.ensure(CodegenBackend { - compiler: run.builder.compiler(run.builder.top_stage, run.target), - backend: backend.clone(), - }); - } + run.builder.ensure(CraneliftCodegenBackend { + build_compiler: run.builder.compiler_for( + run.builder.top_stage, + run.builder.config.host_target, + run.target, + ), + }); } fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> { - if builder.config.dry_run() { - return None; - } - // This prevents rustc_codegen_cranelift from being built for "dist" // or "install" on the stable/beta channels. It is not yet stable and // should not be included. @@ -1428,46 +1429,39 @@ impl Step for CodegenBackend { return None; } - if !builder.config.codegen_backends(self.compiler.host).contains(&self.backend) { - return None; - } - - if self.backend.is_cranelift() && !target_supports_cranelift_backend(self.compiler.host) { + let target = self.build_compiler.host; + let compilers = + RustcPrivateCompilers::from_build_compiler(builder, self.build_compiler, target); + if !target_supports_cranelift_backend(target) { builder.info("target not supported by rustc_codegen_cranelift. skipping"); return None; } - let compiler = self.compiler; - let backend = self.backend; + let mut tarball = Tarball::new(builder, "rustc-codegen-cranelift", &target.triple); + tarball.set_overlay(OverlayKind::RustcCodegenCranelift); + tarball.is_preview(true); + tarball.add_legal_and_readme_to("share/doc/rustc_codegen_cranelift"); - let mut tarball = Tarball::new( - builder, - &format!("rustc-codegen-{}", backend.name()), - &compiler.host.triple, - ); - if backend.is_cranelift() { - tarball.set_overlay(OverlayKind::RustcCodegenCranelift); - } else { - panic!("Unknown codegen backend {}", backend.name()); + builder.ensure(compile::CraneliftCodegenBackend { compilers }); + + if builder.config.dry_run() { + return None; } - tarball.is_preview(true); - tarball.add_legal_and_readme_to(format!("share/doc/{}", backend.crate_name())); - let src = builder.sysroot(compiler); - let backends_src = builder.sysroot_codegen_backends(compiler); + let src = builder.sysroot(self.build_compiler); + let backends_src = builder.sysroot_codegen_backends(self.build_compiler); let backends_rel = backends_src .strip_prefix(src) .unwrap() - .strip_prefix(builder.sysroot_libdir_relative(compiler)) + .strip_prefix(builder.sysroot_libdir_relative(self.build_compiler)) .unwrap(); // Don't use custom libdir here because ^lib/ will be resolved again with installer let backends_dst = PathBuf::from("lib").join(backends_rel); - let backend_name = backend.crate_name(); let mut found_backend = false; for backend in fs::read_dir(&backends_src).unwrap() { let file_name = backend.unwrap().file_name(); - if file_name.to_str().unwrap().contains(&backend_name) { + if file_name.to_str().unwrap().contains("rustc_codegen_cranelift") { tarball.add_file( backends_src.join(file_name), &backends_dst, @@ -1480,6 +1474,13 @@ impl Step for CodegenBackend { Some(tarball.generate()) } + + fn metadata(&self) -> Option<StepMetadata> { + Some( + StepMetadata::dist("rustc_codegen_cranelift", self.build_compiler.host) + .built_by(self.build_compiler), + ) + } } #[derive(Debug, Clone, Hash, PartialEq, Eq)] @@ -1590,9 +1591,8 @@ impl Step for Extended { add_component!("clippy" => Clippy { build_compiler: compiler, target }); add_component!("miri" => Miri { build_compiler: compiler, target }); add_component!("analysis" => Analysis { compiler, target }); - add_component!("rustc-codegen-cranelift" => CodegenBackend { - compiler: builder.compiler(stage, target), - backend: CodegenBackendKind::Cranelift, + add_component!("rustc-codegen-cranelift" => CraneliftCodegenBackend { + build_compiler: compiler, }); add_component!("llvm-bitcode-linker" => LlvmBitcodeLinker { build_compiler: compiler, diff --git a/src/bootstrap/src/core/build_steps/doc.rs b/src/bootstrap/src/core/build_steps/doc.rs index 9f3524905d7..434e9c0977d 100644 --- a/src/bootstrap/src/core/build_steps/doc.rs +++ b/src/bootstrap/src/core/build_steps/doc.rs @@ -12,7 +12,9 @@ use std::path::{Path, PathBuf}; use std::{env, fs, mem}; use crate::core::build_steps::compile; -use crate::core::build_steps::tool::{self, SourceType, Tool, prepare_tool_cargo}; +use crate::core::build_steps::tool::{ + self, RustcPrivateCompilers, SourceType, Tool, prepare_tool_cargo, +}; use crate::core::builder::{ self, Alias, Builder, Compiler, Kind, RunConfig, ShouldRun, Step, StepMetadata, crate_description, @@ -1082,9 +1084,9 @@ tool_doc!( crates = ["compiletest"] ); -#[derive(Ord, PartialOrd, Debug, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Clone, Hash, PartialEq, Eq)] pub struct ErrorIndex { - pub target: TargetSelection, + compilers: RustcPrivateCompilers, } impl Step for ErrorIndex { @@ -1098,17 +1100,29 @@ impl Step for ErrorIndex { } fn make_run(run: RunConfig<'_>) { - let target = run.target; - run.builder.ensure(ErrorIndex { target }); + run.builder.ensure(ErrorIndex { + compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target), + }); } /// Generates the HTML rendered error-index by running the /// `error_index_generator` tool. fn run(self, builder: &Builder<'_>) { - builder.info(&format!("Documenting error index ({})", self.target)); - let out = builder.doc_out(self.target); + builder.info(&format!("Documenting error index ({})", self.compilers.target())); + let out = builder.doc_out(self.compilers.target()); t!(fs::create_dir_all(&out)); - tool::ErrorIndex::command(builder).arg("html").arg(out).arg(&builder.version).run(builder); + tool::ErrorIndex::command(builder, self.compilers) + .arg("html") + .arg(out) + .arg(&builder.version) + .run(builder); + } + + fn metadata(&self) -> Option<StepMetadata> { + Some( + StepMetadata::doc("error-index", self.compilers.target()) + .built_by(self.compilers.build_compiler()), + ) } } diff --git a/src/bootstrap/src/core/build_steps/install.rs b/src/bootstrap/src/core/build_steps/install.rs index f628330e9ed..acee78dcf59 100644 --- a/src/bootstrap/src/core/build_steps/install.rs +++ b/src/bootstrap/src/core/build_steps/install.rs @@ -12,7 +12,7 @@ use crate::core::config::{Config, TargetSelection}; use crate::utils::exec::command; use crate::utils::helpers::t; use crate::utils::tarball::GeneratedTarball; -use crate::{CodegenBackendKind, Compiler, Kind}; +use crate::{Compiler, Kind}; #[cfg(target_os = "illumos")] const SHELL: &str = "bash"; @@ -274,9 +274,8 @@ install!((self, builder, _config), install_sh(builder, "rustc", self.compiler.stage, Some(self.target), &tarball); }; RustcCodegenCranelift, alias = "rustc-codegen-cranelift", Self::should_build(_config), only_hosts: true, { - if let Some(tarball) = builder.ensure(dist::CodegenBackend { - compiler: self.compiler, - backend: CodegenBackendKind::Cranelift, + if let Some(tarball) = builder.ensure(dist::CraneliftCodegenBackend { + build_compiler: self.compiler, }) { install_sh(builder, "rustc-codegen-cranelift", self.compiler.stage, Some(self.target), &tarball); } else { diff --git a/src/bootstrap/src/core/build_steps/llvm.rs b/src/bootstrap/src/core/build_steps/llvm.rs index 721ba6ca459..79244827059 100644 --- a/src/bootstrap/src/core/build_steps/llvm.rs +++ b/src/bootstrap/src/core/build_steps/llvm.rs @@ -421,6 +421,13 @@ impl Step for Llvm { ldflags.shared.push(" -latomic"); } + if target.starts_with("arm64ec") { + // MSVC linker requires the -machine:arm64ec flag to be passed to + // know it's linking as Arm64EC (vs Arm64X). + ldflags.exe.push(" -machine:arm64ec"); + ldflags.shared.push(" -machine:arm64ec"); + } + if target.is_msvc() { cfg.define("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreaded"); cfg.static_crt(true); @@ -986,6 +993,7 @@ impl Step for Enzyme { .env("LLVM_CONFIG_REAL", &llvm_config) .define("LLVM_ENABLE_ASSERTIONS", "ON") .define("ENZYME_EXTERNAL_SHARED_LIB", "ON") + .define("ENZYME_BC_LOADER", "OFF") .define("LLVM_DIR", builder.llvm_out(target)); cfg.build(); diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 34d50b1dccd..32c3ef53e12 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -2502,7 +2502,7 @@ test_book!( #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ErrorIndex { - compiler: Compiler, + compilers: RustcPrivateCompilers, } impl Step for ErrorIndex { @@ -2520,8 +2520,12 @@ impl Step for ErrorIndex { // error_index_generator depends on librustdoc. Use the compiler that // is normally used to build rustdoc for other tests (like compiletest // tests in tests/rustdoc) so that it shares the same artifacts. - let compiler = run.builder.compiler(run.builder.top_stage, run.builder.config.host_target); - run.builder.ensure(ErrorIndex { compiler }); + let compilers = RustcPrivateCompilers::new( + run.builder, + run.builder.top_stage, + run.builder.config.host_target, + ); + run.builder.ensure(ErrorIndex { compilers }); } /// Runs the error index generator tool to execute the tests located in the error @@ -2531,24 +2535,30 @@ impl Step for ErrorIndex { /// generate a markdown file from the error indexes of the code base which is /// then passed to `rustdoc --test`. fn run(self, builder: &Builder<'_>) { - let compiler = self.compiler; + // The compiler that we are testing + let target_compiler = self.compilers.target_compiler(); - let dir = testdir(builder, compiler.host); + let dir = testdir(builder, target_compiler.host); t!(fs::create_dir_all(&dir)); let output = dir.join("error-index.md"); - let mut tool = tool::ErrorIndex::command(builder); + let mut tool = tool::ErrorIndex::command(builder, self.compilers); tool.arg("markdown").arg(&output); - let guard = - builder.msg(Kind::Test, compiler.stage, "error-index", compiler.host, compiler.host); + let guard = builder.msg( + Kind::Test, + target_compiler.stage, + "error-index", + target_compiler.host, + target_compiler.host, + ); let _time = helpers::timeit(builder); tool.run_capture(builder); drop(guard); // The tests themselves need to link to std, so make sure it is // available. - builder.std(compiler, compiler.host); - markdown_test(builder, compiler, &output); + builder.std(target_compiler, target_compiler.host); + markdown_test(builder, target_compiler, &output); } } @@ -3468,7 +3478,11 @@ impl Step for CodegenCranelift { return; } - if !builder.config.codegen_backends(run.target).contains(&CodegenBackendKind::Cranelift) { + if !builder + .config + .enabled_codegen_backends(run.target) + .contains(&CodegenBackendKind::Cranelift) + { builder.info("cranelift not in rust.codegen-backends. skipping"); return; } @@ -3595,7 +3609,7 @@ impl Step for CodegenGCC { return; } - if !builder.config.codegen_backends(run.target).contains(&CodegenBackendKind::Gcc) { + if !builder.config.enabled_codegen_backends(run.target).contains(&CodegenBackendKind::Gcc) { builder.info("gcc not in rust.codegen-backends. skipping"); return; } diff --git a/src/bootstrap/src/core/build_steps/tool.rs b/src/bootstrap/src/core/build_steps/tool.rs index e3f49fa126e..aa00cd03c5b 100644 --- a/src/bootstrap/src/core/build_steps/tool.rs +++ b/src/bootstrap/src/core/build_steps/tool.rs @@ -334,7 +334,8 @@ pub fn prepare_tool_cargo( /// Determines how to build a `ToolTarget`, i.e. which compiler should be used to compile it. /// The compiler stage is automatically bumped if we need to cross-compile a stage 1 tool. pub enum ToolTargetBuildMode { - /// Build the tool using rustc that corresponds to the selected CLI stage. + /// Build the tool for the given `target` using rustc that corresponds to the top CLI + /// stage. Build(TargetSelection), /// Build the tool so that it can be attached to the sysroot of the passed compiler. /// Since we always dist stage 2+, the compiler that builds the tool in this case has to be @@ -366,7 +367,10 @@ pub(crate) fn get_tool_target_compiler( } else { // If we are cross-compiling a stage 1 tool, we cannot do that with a stage 0 compiler, // so we auto-bump the tool's stage to 2, which means we need a stage 1 compiler. - builder.compiler(build_compiler_stage.max(1), builder.host_target) + let build_compiler = builder.compiler(build_compiler_stage.max(1), builder.host_target); + // We also need the host stdlib to compile host code (proc macros/build scripts) + builder.std(build_compiler, builder.host_target); + build_compiler }; builder.std(compiler, target); compiler @@ -588,20 +592,20 @@ impl Step for RustcPerf { } } -#[derive(Debug, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)] +#[derive(Debug, Clone, Hash, PartialEq, Eq)] pub struct ErrorIndex { - pub compiler: Compiler, + compilers: RustcPrivateCompilers, } impl ErrorIndex { - pub fn command(builder: &Builder<'_>) -> BootstrapCommand { + pub fn command(builder: &Builder<'_>, compilers: RustcPrivateCompilers) -> BootstrapCommand { // Error-index-generator links with the rustdoc library, so we need to add `rustc_lib_paths` // for rustc_private and libLLVM.so, and `sysroot_lib` for libstd, etc. - let host = builder.config.host_target; - let compiler = builder.compiler_for(builder.top_stage, host, host); - let mut cmd = command(builder.ensure(ErrorIndex { compiler }).tool_path); - let mut dylib_paths = builder.rustc_lib_paths(compiler); - dylib_paths.push(builder.sysroot_target_libdir(compiler, compiler.host)); + let mut cmd = command(builder.ensure(ErrorIndex { compilers }).tool_path); + + let target_compiler = compilers.target_compiler(); + let mut dylib_paths = builder.rustc_lib_paths(target_compiler); + dylib_paths.push(builder.sysroot_target_libdir(target_compiler, target_compiler.host)); add_dylib_path(dylib_paths, &mut cmd); cmd } @@ -620,14 +624,19 @@ impl Step for ErrorIndex { // src/tools/error-index-generator` which almost nobody does. // Normally, `x.py test` or `x.py doc` will use the // `ErrorIndex::command` function instead. - let compiler = run.builder.compiler(run.builder.top_stage, run.builder.config.host_target); - run.builder.ensure(ErrorIndex { compiler }); + run.builder.ensure(ErrorIndex { + compilers: RustcPrivateCompilers::new( + run.builder, + run.builder.top_stage, + run.builder.host_target, + ), + }); } fn run(self, builder: &Builder<'_>) -> ToolBuildResult { builder.ensure(ToolBuild { - build_compiler: self.compiler, - target: self.compiler.host, + build_compiler: self.compilers.build_compiler, + target: self.compilers.target(), tool: "error_index_generator", mode: Mode::ToolRustc, path: "src/tools/error_index_generator", @@ -638,6 +647,13 @@ impl Step for ErrorIndex { artifact_kind: ToolArtifactKind::Binary, }) } + + fn metadata(&self) -> Option<StepMetadata> { + Some( + StepMetadata::build("error-index", self.compilers.target()) + .built_by(self.compilers.build_compiler), + ) + } } #[derive(Debug, Clone, Hash, PartialEq, Eq)] diff --git a/src/bootstrap/src/core/builder/cargo.rs b/src/bootstrap/src/core/builder/cargo.rs index e10af2b55f9..39f46bf43af 100644 --- a/src/bootstrap/src/core/builder/cargo.rs +++ b/src/bootstrap/src/core/builder/cargo.rs @@ -433,6 +433,15 @@ impl Builder<'_> { let out_dir = self.stage_out(compiler, mode); cargo.env("CARGO_TARGET_DIR", &out_dir); + // Bootstrap makes a lot of assumptions about the artifacts produced in the target + // directory. If users override the "build directory" using `build-dir` + // (https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#build-dir), then + // bootstrap couldn't find these artifacts. So we forcefully override that option to our + // target directory here. + // In the future, we could attempt to read the build-dir location from Cargo and actually + // respect it. + cargo.env("CARGO_BUILD_BUILD_DIR", &out_dir); + // Found with `rg "init_env_logger\("`. If anyone uses `init_env_logger` // from out of tree it shouldn't matter, since x.py is only used for // building in-tree. diff --git a/src/bootstrap/src/core/builder/mod.rs b/src/bootstrap/src/core/builder/mod.rs index e15941938f1..163a498d4b4 100644 --- a/src/bootstrap/src/core/builder/mod.rs +++ b/src/bootstrap/src/core/builder/mod.rs @@ -959,7 +959,8 @@ impl<'a> Builder<'a> { compile::Std, compile::Rustc, compile::Assemble, - compile::CodegenBackend, + compile::CraneliftCodegenBackend, + compile::GccCodegenBackend, compile::StartupObjects, tool::BuildManifest, tool::Rustbook, @@ -1150,7 +1151,7 @@ impl<'a> Builder<'a> { dist::JsonDocs, dist::Mingw, dist::Rustc, - dist::CodegenBackend, + dist::CraneliftCodegenBackend, dist::Std, dist::RustcDev, dist::Analysis, diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 139ddc9ed24..4c7766e58c1 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -257,31 +257,6 @@ fn parse_config_download_rustc_at(path: &Path, download_rustc: &str, ci: bool) - ) } -mod defaults { - use pretty_assertions::assert_eq; - - use super::{TEST_TRIPLE_1, TEST_TRIPLE_2, configure, first, run_build}; - use crate::Config; - use crate::core::builder::*; - - #[test] - fn doc_default() { - let mut config = configure("doc", &[TEST_TRIPLE_1], &[TEST_TRIPLE_1]); - config.compiler_docs = true; - config.cmd = Subcommand::Doc { open: false, json: false }; - let mut cache = run_build(&[], config); - let a = TargetSelection::from_user(TEST_TRIPLE_1); - - // error_index_generator uses stage 0 to share rustdoc artifacts with the - // rustdoc tool. - assert_eq!(first(cache.all::<doc::ErrorIndex>()), &[doc::ErrorIndex { target: a },]); - assert_eq!( - first(cache.all::<tool::ErrorIndex>()), - &[tool::ErrorIndex { compiler: Compiler::new(1, a) }] - ); - } -} - mod dist { use pretty_assertions::assert_eq; @@ -309,28 +284,6 @@ mod dist { let target = TargetSelection::from_user(TEST_TRIPLE_1); assert!(build.llvm_out(target).ends_with("llvm")); } - - #[test] - fn doc_ci() { - let mut config = configure(&[TEST_TRIPLE_1], &[TEST_TRIPLE_1]); - config.compiler_docs = true; - config.cmd = Subcommand::Doc { open: false, json: false }; - let build = Build::new(config); - let mut builder = Builder::new(&build); - builder.run_step_descriptions(&Builder::get_step_descriptions(Kind::Doc), &[]); - let a = TargetSelection::from_user(TEST_TRIPLE_1); - - // error_index_generator uses stage 1 to share rustdoc artifacts with the - // rustdoc tool. - assert_eq!( - first(builder.cache.all::<doc::ErrorIndex>()), - &[doc::ErrorIndex { target: a },] - ); - assert_eq!( - first(builder.cache.all::<tool::ErrorIndex>()), - &[tool::ErrorIndex { compiler: Compiler::new(1, a) }] - ); - } } mod sysroot_target_dirs { @@ -889,6 +842,19 @@ mod snapshot { } #[test] + fn build_error_index() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("build") + .path("error_index_generator") + .render_steps(), @r" + [build] llvm <host> + [build] rustc 0 <host> -> rustc 1 <host> + [build] rustc 0 <host> -> error-index 1 <host> + "); + } + + #[test] fn build_bootstrap_tool_no_explicit_stage() { let ctx = TestCtx::new(); insta::assert_snapshot!( @@ -1010,6 +976,22 @@ mod snapshot { } #[test] + fn build_cargo_cross() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("build") + .paths(&["cargo"]) + .hosts(&[TEST_TRIPLE_1]) + .render_steps(), @r" + [build] llvm <host> + [build] rustc 0 <host> -> rustc 1 <host> + [build] rustc 1 <host> -> std 1 <host> + [build] rustc 1 <host> -> std 1 <target1> + [build] rustc 1 <host> -> cargo 2 <target1> + "); + } + + #[test] fn dist_default_stage() { let ctx = TestCtx::new(); assert_eq!(ctx.config("dist").path("compiler").create_config().stage, 2); @@ -1032,6 +1014,8 @@ mod snapshot { [build] rustc 1 <host> -> rustc 2 <host> [build] rustdoc 2 <host> [doc] std 2 <host> crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 1 <host> -> error-index 2 <host> + [doc] rustc 1 <host> -> error-index 2 <host> [build] rustc 2 <host> -> std 2 <host> [build] rustc 0 <host> -> LintDocs 1 <host> [build] rustc 0 <host> -> RustInstaller 1 <host> @@ -1074,6 +1058,8 @@ mod snapshot { [build] rustc 1 <host> -> LlvmBitcodeLinker 2 <host> [build] rustdoc 2 <host> [doc] std 2 <host> crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 1 <host> -> error-index 2 <host> + [doc] rustc 1 <host> -> error-index 2 <host> [build] rustc 2 <host> -> std 2 <host> [build] rustc 0 <host> -> LintDocs 1 <host> [build] rustc 0 <host> -> RustInstaller 1 <host> @@ -1114,6 +1100,8 @@ mod snapshot { [build] rustdoc 2 <host> [doc] std 2 <host> crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [doc] std 2 <target1> crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 1 <host> -> error-index 2 <host> + [doc] rustc 1 <host> -> error-index 2 <host> [build] rustc 2 <host> -> std 2 <host> [build] rustc 0 <host> -> LintDocs 1 <host> [build] rustc 0 <host> -> RustInstaller 1 <host> @@ -1150,9 +1138,15 @@ mod snapshot { [build] rustc 1 <host> -> rustc 2 <host> [build] rustdoc 2 <host> [doc] std 2 <host> crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 1 <host> -> error-index 2 <host> + [doc] rustc 1 <host> -> error-index 2 <host> + [build] llvm <target1> + [build] rustc 1 <host> -> std 1 <target1> + [build] rustc 1 <host> -> rustc 2 <target1> + [build] rustc 1 <host> -> error-index 2 <target1> + [doc] rustc 1 <host> -> error-index 2 <target1> [build] rustc 2 <host> -> std 2 <host> [build] rustc 0 <host> -> LintDocs 1 <host> - [build] rustc 1 <host> -> std 1 <target1> [build] rustc 2 <host> -> std 2 <target1> [build] rustc 0 <host> -> RustInstaller 1 <host> [dist] docs <host> @@ -1160,8 +1154,6 @@ mod snapshot { [dist] mingw <host> [build] rustc 0 <host> -> GenerateCopyright 1 <host> [dist] rustc <host> - [build] llvm <target1> - [build] rustc 1 <host> -> rustc 2 <target1> [build] rustdoc 2 <target1> [dist] rustc <target1> [dist] rustc 1 <host> -> std 1 <host> @@ -1188,9 +1180,15 @@ mod snapshot { [build] rustdoc 2 <host> [doc] std 2 <host> crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [doc] std 2 <target1> crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 1 <host> -> error-index 2 <host> + [doc] rustc 1 <host> -> error-index 2 <host> + [build] llvm <target1> + [build] rustc 1 <host> -> std 1 <target1> + [build] rustc 1 <host> -> rustc 2 <target1> + [build] rustc 1 <host> -> error-index 2 <target1> + [doc] rustc 1 <host> -> error-index 2 <target1> [build] rustc 2 <host> -> std 2 <host> [build] rustc 0 <host> -> LintDocs 1 <host> - [build] rustc 1 <host> -> std 1 <target1> [build] rustc 2 <host> -> std 2 <target1> [build] rustc 0 <host> -> RustInstaller 1 <host> [dist] docs <host> @@ -1201,8 +1199,6 @@ mod snapshot { [dist] mingw <target1> [build] rustc 0 <host> -> GenerateCopyright 1 <host> [dist] rustc <host> - [build] llvm <target1> - [build] rustc 1 <host> -> rustc 2 <target1> [build] rustdoc 2 <target1> [dist] rustc <target1> [dist] rustc 1 <host> -> std 1 <host> @@ -1261,17 +1257,19 @@ mod snapshot { [build] rustc 1 <host> -> WasmComponentLd 2 <host> [build] rustdoc 2 <host> [doc] std 2 <target1> crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] - [build] rustc 2 <host> -> std 2 <host> + [build] llvm <target1> [build] rustc 1 <host> -> std 1 <target1> + [build] rustc 1 <host> -> rustc 2 <target1> + [build] rustc 1 <host> -> WasmComponentLd 2 <target1> + [build] rustc 1 <host> -> error-index 2 <target1> + [doc] rustc 1 <host> -> error-index 2 <target1> + [build] rustc 2 <host> -> std 2 <host> [build] rustc 2 <host> -> std 2 <target1> [build] rustc 0 <host> -> LintDocs 1 <host> [build] rustc 0 <host> -> RustInstaller 1 <host> [dist] docs <target1> [doc] std 2 <target1> crates=[] [dist] mingw <target1> - [build] llvm <target1> - [build] rustc 1 <host> -> rustc 2 <target1> - [build] rustc 1 <host> -> WasmComponentLd 2 <target1> [build] rustdoc 2 <target1> [build] rustc 1 <host> -> rust-analyzer-proc-macro-srv 2 <target1> [build] rustc 0 <host> -> GenerateCopyright 1 <host> @@ -1290,6 +1288,42 @@ mod snapshot { "); } + // Enable dist cranelift tarball by default with `x dist` if cranelift is enabled in + // `rust.codegen-backends`. + #[test] + fn dist_cranelift_by_default() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx + .config("dist") + .args(&["--set", "rust.codegen-backends=['llvm', 'cranelift']"]) + .render_steps(), @r" + [build] rustc 0 <host> -> UnstableBookGen 1 <host> + [build] rustc 0 <host> -> Rustbook 1 <host> + [build] llvm <host> + [build] rustc 0 <host> -> rustc 1 <host> + [build] rustc 0 <host> -> rustc_codegen_cranelift 1 <host> + [build] rustc 1 <host> -> std 1 <host> + [build] rustc 1 <host> -> rustc 2 <host> + [build] rustc 1 <host> -> rustc_codegen_cranelift 2 <host> + [build] rustdoc 2 <host> + [doc] std 2 <host> crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 1 <host> -> error-index 2 <host> + [doc] rustc 1 <host> -> error-index 2 <host> + [build] rustc 2 <host> -> std 2 <host> + [build] rustc 0 <host> -> LintDocs 1 <host> + [build] rustc 0 <host> -> RustInstaller 1 <host> + [dist] docs <host> + [doc] std 2 <host> crates=[] + [dist] mingw <host> + [build] rustc 0 <host> -> GenerateCopyright 1 <host> + [dist] rustc <host> + [dist] rustc 1 <host> -> rustc_codegen_cranelift 2 <host> + [dist] rustc 1 <host> -> std 1 <host> + [dist] src <> + "); + } + #[test] fn check_compiler_no_explicit_stage() { let ctx = TestCtx::new(); @@ -1652,6 +1686,25 @@ mod snapshot { } #[test] + fn doc_all() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("doc") + .render_steps(), @r" + [build] rustc 0 <host> -> UnstableBookGen 1 <host> + [build] rustc 0 <host> -> Rustbook 1 <host> + [build] llvm <host> + [build] rustc 0 <host> -> rustc 1 <host> + [build] rustdoc 1 <host> + [doc] std 1 <host> crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 0 <host> -> error-index 1 <host> + [doc] rustc 0 <host> -> error-index 1 <host> + [build] rustc 1 <host> -> std 1 <host> + [build] rustc 0 <host> -> LintDocs 1 <host> + "); + } + + #[test] fn doc_library() { let ctx = TestCtx::new(); insta::assert_snapshot!( diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index 6055876c475..eb1ac8c637f 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -32,13 +32,18 @@ use tracing::{instrument, span}; use crate::core::build_steps::llvm; use crate::core::build_steps::llvm::LLVM_INVALIDATION_PATHS; pub use crate::core::config::flags::Subcommand; -use crate::core::config::flags::{Color, Flags}; +use crate::core::config::flags::{Color, Flags, Warnings}; use crate::core::config::target_selection::TargetSelectionList; use crate::core::config::toml::TomlConfig; use crate::core::config::toml::build::{Build, Tool}; use crate::core::config::toml::change_id::ChangeId; +use crate::core::config::toml::dist::Dist; +use crate::core::config::toml::gcc::Gcc; +use crate::core::config::toml::install::Install; +use crate::core::config::toml::llvm::Llvm; use crate::core::config::toml::rust::{ - LldMode, RustOptimize, check_incompatible_options_for_ci_rustc, + LldMode, Rust, RustOptimize, check_incompatible_options_for_ci_rustc, + default_lld_opt_in_targets, parse_codegen_backends, }; use crate::core::config::toml::target::Target; use crate::core::config::{ @@ -89,7 +94,6 @@ pub struct Config { pub ccache: Option<String>, /// Call Build::ninja() instead of this. pub ninja_in_file: bool, - pub verbose: usize, pub submodules: Option<bool>, pub compiler_docs: bool, pub library_docs_private_items: bool, @@ -442,23 +446,13 @@ impl Config { enable_bolt_settings: flags_enable_bolt_settings, skip_stage0_validation: flags_skip_stage0_validation, reproducible_artifact: flags_reproducible_artifact, - paths: mut flags_paths, + paths: flags_paths, set: flags_set, - free_args: mut flags_free_args, + free_args: flags_free_args, ci: flags_ci, skip_std_check_if_no_download_rustc: flags_skip_std_check_if_no_download_rustc, } = flags; - let mut config = Config::default_opts(); - let mut exec_ctx = ExecutionContext::new(); - exec_ctx.set_verbose(flags_verbose); - exec_ctx.set_fail_fast(flags_cmd.fail_fast()); - - config.exec_ctx = exec_ctx; - - // Set flags. - config.paths = std::mem::take(&mut flags_paths); - #[cfg(feature = "tracing")] span!( target: "CONFIG_HANDLING", @@ -469,14 +463,251 @@ impl Config { "flags.exclude" = ?flags_exclude ); - #[cfg(feature = "tracing")] - span!( - target: "CONFIG_HANDLING", - tracing::Level::TRACE, - "normalizing and combining `flag.skip`/`flag.exclude` paths", - "config.skip" = ?config.skip, + // First initialize the bare minimum that we need for further operation - source directory + // and execution context. + let mut config = Config::default_opts(); + let exec_ctx = ExecutionContext::new(flags_verbose, flags_cmd.fail_fast()); + + config.exec_ctx = exec_ctx; + + if let Some(src) = compute_src_directory(flags_src, &config.exec_ctx) { + config.src = src; + } + + // Now load the TOML config, as soon as possible + let (mut toml, toml_path) = load_toml_config(&config.src, flags_config, &get_toml); + config.config = toml_path.clone(); + + postprocess_toml( + &mut toml, + &config.src, + toml_path, + config.exec_ctx(), + &flags_set, + &get_toml, ); + // Now override TOML values with flags, to make sure that we won't later override flags with + // TOML values by accident instead, because flags have higher priority. + let Build { + description: build_description, + build: mut build_build, + host: build_host, + target: build_target, + build_dir: build_build_dir, + cargo: mut build_cargo, + rustc: mut build_rustc, + rustfmt: build_rustfmt, + cargo_clippy: build_cargo_clippy, + docs: build_docs, + compiler_docs: build_compiler_docs, + library_docs_private_items: build_library_docs_private_items, + docs_minification: build_docs_minification, + submodules: build_submodules, + gdb: build_gdb, + lldb: build_lldb, + nodejs: build_nodejs, + npm: build_npm, + python: build_python, + reuse: build_reuse, + locked_deps: build_locked_deps, + vendor: build_vendor, + full_bootstrap: build_full_bootstrap, + bootstrap_cache_path: build_bootstrap_cache_path, + extended: build_extended, + tools: build_tools, + tool: build_tool, + verbose: build_verbose, + sanitizers: build_sanitizers, + profiler: build_profiler, + cargo_native_static: build_cargo_native_static, + low_priority: build_low_priority, + configure_args: build_configure_args, + local_rebuild: build_local_rebuild, + print_step_timings: build_print_step_timings, + print_step_rusage: build_print_step_rusage, + check_stage: build_check_stage, + doc_stage: build_doc_stage, + build_stage: build_build_stage, + test_stage: build_test_stage, + install_stage: build_install_stage, + dist_stage: build_dist_stage, + bench_stage: build_bench_stage, + patch_binaries_for_nix: build_patch_binaries_for_nix, + // This field is only used by bootstrap.py + metrics: _, + android_ndk: build_android_ndk, + optimized_compiler_builtins: build_optimized_compiler_builtins, + jobs: mut build_jobs, + compiletest_diff_tool: build_compiletest_diff_tool, + compiletest_use_stage0_libtest: build_compiletest_use_stage0_libtest, + tidy_extra_checks: build_tidy_extra_checks, + ccache: build_ccache, + exclude: build_exclude, + compiletest_allow_stage0: build_compiletest_allow_stage0, + } = toml.build.unwrap_or_default(); + + let Install { + prefix: install_prefix, + sysconfdir: install_sysconfdir, + docdir: install_docdir, + bindir: install_bindir, + libdir: install_libdir, + mandir: install_mandir, + datadir: install_datadir, + } = toml.install.unwrap_or_default(); + + let Rust { + optimize: rust_optimize, + debug: rust_debug, + codegen_units: rust_codegen_units, + codegen_units_std: rust_codegen_units_std, + rustc_debug_assertions: rust_rustc_debug_assertions, + std_debug_assertions: rust_std_debug_assertions, + tools_debug_assertions: rust_tools_debug_assertions, + overflow_checks: rust_overflow_checks, + overflow_checks_std: rust_overflow_checks_std, + debug_logging: rust_debug_logging, + debuginfo_level: rust_debuginfo_level, + debuginfo_level_rustc: rust_debuginfo_level_rustc, + debuginfo_level_std: rust_debuginfo_level_std, + debuginfo_level_tools: rust_debuginfo_level_tools, + debuginfo_level_tests: rust_debuginfo_level_tests, + backtrace: rust_backtrace, + incremental: rust_incremental, + randomize_layout: rust_randomize_layout, + default_linker: rust_default_linker, + channel: rust_channel, + musl_root: rust_musl_root, + rpath: rust_rpath, + verbose_tests: rust_verbose_tests, + optimize_tests: rust_optimize_tests, + codegen_tests: rust_codegen_tests, + omit_git_hash: rust_omit_git_hash, + dist_src: rust_dist_src, + save_toolstates: rust_save_toolstates, + codegen_backends: rust_codegen_backends, + lld: rust_lld_enabled, + llvm_tools: rust_llvm_tools, + llvm_bitcode_linker: rust_llvm_bitcode_linker, + deny_warnings: rust_deny_warnings, + backtrace_on_ice: rust_backtrace_on_ice, + verify_llvm_ir: rust_verify_llvm_ir, + thin_lto_import_instr_limit: rust_thin_lto_import_instr_limit, + remap_debuginfo: rust_remap_debuginfo, + jemalloc: rust_jemalloc, + test_compare_mode: rust_test_compare_mode, + llvm_libunwind: rust_llvm_libunwind, + control_flow_guard: rust_control_flow_guard, + ehcont_guard: rust_ehcont_guard, + new_symbol_mangling: rust_new_symbol_mangling, + profile_generate: rust_profile_generate, + profile_use: rust_profile_use, + download_rustc: rust_download_rustc, + lto: rust_lto, + validate_mir_opts: rust_validate_mir_opts, + frame_pointers: rust_frame_pointers, + stack_protector: rust_stack_protector, + strip: rust_strip, + lld_mode: rust_lld_mode, + std_features: rust_std_features, + } = toml.rust.unwrap_or_default(); + + let Llvm { + optimize: llvm_optimize, + thin_lto: llvm_thin_lto, + release_debuginfo: llvm_release_debuginfo, + assertions: llvm_assertions, + tests: llvm_tests, + enzyme: llvm_enzyme, + plugins: llvm_plugin, + static_libstdcpp: llvm_static_libstdcpp, + libzstd: llvm_libzstd, + ninja: llvm_ninja, + targets: llvm_targets, + experimental_targets: llvm_experimental_targets, + link_jobs: llvm_link_jobs, + link_shared: llvm_link_shared, + version_suffix: llvm_version_suffix, + clang_cl: llvm_clang_cl, + cflags: llvm_cflags, + cxxflags: llvm_cxxflags, + ldflags: llvm_ldflags, + use_libcxx: llvm_use_libcxx, + use_linker: llvm_use_linker, + allow_old_toolchain: llvm_allow_old_toolchain, + offload: llvm_offload, + polly: llvm_polly, + clang: llvm_clang, + enable_warnings: llvm_enable_warnings, + download_ci_llvm: llvm_download_ci_llvm, + build_config: llvm_build_config, + } = toml.llvm.unwrap_or_default(); + + let Dist { + sign_folder: dist_sign_folder, + upload_addr: dist_upload_addr, + src_tarball: dist_src_tarball, + compression_formats: dist_compression_formats, + compression_profile: dist_compression_profile, + include_mingw_linker: dist_include_mingw_linker, + vendor: dist_vendor, + } = toml.dist.unwrap_or_default(); + + let Gcc { download_ci_gcc: gcc_download_ci_gcc } = toml.gcc.unwrap_or_default(); + + if cfg!(test) { + // When configuring bootstrap for tests, make sure to set the rustc and Cargo to the + // same ones used to call the tests (if custom ones are not defined in the toml). If we + // don't do that, bootstrap will use its own detection logic to find a suitable rustc + // and Cargo, which doesn't work when the caller is specìfying a custom local rustc or + // Cargo in their bootstrap.toml. + build_rustc = build_rustc.take().or(std::env::var_os("RUSTC").map(|p| p.into())); + build_cargo = build_cargo.take().or(std::env::var_os("CARGO").map(|p| p.into())); + } + + build_jobs = flags_jobs.or(build_jobs); + build_build = flags_build.or(build_build); + + let build_dir = flags_build_dir.or(build_build_dir.map(PathBuf::from)); + let host = if let Some(TargetSelectionList(hosts)) = flags_host { + Some(hosts) + } else { + build_host + .map(|file_host| file_host.iter().map(|h| TargetSelection::from_user(h)).collect()) + }; + let target = if let Some(TargetSelectionList(targets)) = flags_target { + Some(targets) + } else { + build_target.map(|file_target| { + file_target.iter().map(|h| TargetSelection::from_user(h)).collect() + }) + }; + + if let Some(rustc) = &build_rustc + && !flags_skip_stage0_validation + { + check_stage0_version(rustc, "rustc", &config.src, config.exec_ctx()); + } + if let Some(cargo) = &build_cargo + && !flags_skip_stage0_validation + { + check_stage0_version(cargo, "cargo", &config.src, config.exec_ctx()); + } + + // Prefer CLI verbosity flags if set (`flags_verbose` > 0), otherwise take the value from + // TOML. + config + .exec_ctx + .set_verbosity(cmp::max(build_verbose.unwrap_or_default() as u8, flags_verbose)); + + let mut paths: Vec<PathBuf> = flags_skip.into_iter().chain(flags_exclude).collect(); + if let Some(exclude) = build_exclude { + paths.extend(exclude); + } + + // Set config values based on flags. + config.paths = flags_paths; config.include_default_paths = flags_include_default_paths; config.rustc_error_format = flags_rustc_error_format; config.json_output = flags_json_output; @@ -489,7 +720,7 @@ impl Config { config.keep_stage = flags_keep_stage; config.keep_stage_std = flags_keep_stage_std; config.color = flags_color; - config.free_args = std::mem::take(&mut flags_free_args); + config.free_args = flags_free_args; config.llvm_profile_use = flags_llvm_profile_use; config.llvm_profile_generate = flags_llvm_profile_generate; config.enable_bolt_settings = flags_enable_bolt_settings; @@ -499,53 +730,6 @@ impl Config { // Infer the rest of the configuration. - if let Some(src) = flags_src { - config.src = src - } else { - // Infer the source directory. This is non-trivial because we want to support a downloaded bootstrap binary, - // running on a completely different machine from where it was compiled. - let mut cmd = helpers::git(None); - // NOTE: we cannot support running from outside the repository because the only other path we have available - // is set at compile time, which can be wrong if bootstrap was downloaded rather than compiled locally. - // We still support running outside the repository if we find we aren't in a git directory. - - // NOTE: We get a relative path from git to work around an issue on MSYS/mingw. If we used an absolute path, - // and end up using MSYS's git rather than git-for-windows, we would get a unix-y MSYS path. But as bootstrap - // has already been (kinda-cross-)compiled to Windows land, we require a normal Windows path. - cmd.arg("rev-parse").arg("--show-cdup"); - // Discard stderr because we expect this to fail when building from a tarball. - let output = cmd.allow_failure().run_capture_stdout(&config); - if output.is_success() { - let git_root_relative = output.stdout(); - // We need to canonicalize this path to make sure it uses backslashes instead of forward slashes, - // and to resolve any relative components. - let git_root = env::current_dir() - .unwrap() - .join(PathBuf::from(git_root_relative.trim())) - .canonicalize() - .unwrap(); - let s = git_root.to_str().unwrap(); - - // Bootstrap is quite bad at handling /? in front of paths - let git_root = match s.strip_prefix("\\\\?\\") { - Some(p) => PathBuf::from(p), - None => git_root, - }; - // If this doesn't have at least `stage0`, we guessed wrong. This can happen when, - // for example, the build directory is inside of another unrelated git directory. - // In that case keep the original `CARGO_MANIFEST_DIR` handling. - // - // NOTE: this implies that downloadable bootstrap isn't supported when the build directory is outside - // the source directory. We could fix that by setting a variable from all three of python, ./x, and x.ps1. - if git_root.join("src").join("stage0").exists() { - config.src = git_root; - } - } else { - // We're building from a tarball, not git sources. - // We don't support pre-downloaded bootstrap in this case. - } - } - if cfg!(test) { // Use the build directory of the original x.py invocation, so that we can set `initial_rustc` properly. config.out = Path::new( @@ -556,220 +740,11 @@ impl Config { .to_path_buf(); } + config.compiletest_allow_stage0 = build_compiletest_allow_stage0.unwrap_or(false); config.stage0_metadata = build_helper::stage0_parser::parse_stage0_file(); - // Locate the configuration file using the following priority (first match wins): - // 1. `--config <path>` (explicit flag) - // 2. `RUST_BOOTSTRAP_CONFIG` environment variable - // 3. `./bootstrap.toml` (local file) - // 4. `<root>/bootstrap.toml` - // 5. `./config.toml` (fallback for backward compatibility) - // 6. `<root>/config.toml` - let toml_path = flags_config - .clone() - .or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from)); - let using_default_path = toml_path.is_none(); - let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("bootstrap.toml")); - - if using_default_path && !toml_path.exists() { - toml_path = config.src.join(PathBuf::from("bootstrap.toml")); - if !toml_path.exists() { - toml_path = PathBuf::from("config.toml"); - if !toml_path.exists() { - toml_path = config.src.join(PathBuf::from("config.toml")); - } - } - } - - // Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path, - // but not if `bootstrap.toml` hasn't been created. - let mut toml = if !using_default_path || toml_path.exists() { - config.config = Some(if cfg!(not(test)) { - toml_path = toml_path.canonicalize().unwrap(); - toml_path.clone() - } else { - toml_path.clone() - }); - get_toml(&toml_path).unwrap_or_else(|e| { - eprintln!("ERROR: Failed to parse '{}': {e}", toml_path.display()); - exit!(2); - }) - } else { - config.config = None; - TomlConfig::default() - }; - - if cfg!(test) { - // When configuring bootstrap for tests, make sure to set the rustc and Cargo to the - // same ones used to call the tests (if custom ones are not defined in the toml). If we - // don't do that, bootstrap will use its own detection logic to find a suitable rustc - // and Cargo, which doesn't work when the caller is specìfying a custom local rustc or - // Cargo in their bootstrap.toml. - let build = toml.build.get_or_insert_with(Default::default); - build.rustc = build.rustc.take().or(std::env::var_os("RUSTC").map(|p| p.into())); - build.cargo = build.cargo.take().or(std::env::var_os("CARGO").map(|p| p.into())); - } - - if config.git_info(false, &config.src).is_from_tarball() && toml.profile.is_none() { - toml.profile = Some("dist".into()); - } - - // Reverse the list to ensure the last added config extension remains the most dominant. - // For example, given ["a.toml", "b.toml"], "b.toml" should take precedence over "a.toml". - // - // This must be handled before applying the `profile` since `include`s should always take - // precedence over `profile`s. - for include_path in toml.include.clone().unwrap_or_default().iter().rev() { - let include_path = toml_path.parent().unwrap().join(include_path); - - let included_toml = get_toml(&include_path).unwrap_or_else(|e| { - eprintln!("ERROR: Failed to parse '{}': {e}", include_path.display()); - exit!(2); - }); - toml.merge( - Some(include_path), - &mut Default::default(), - included_toml, - ReplaceOpt::IgnoreDuplicate, - ); - } - - if let Some(include) = &toml.profile { - // Allows creating alias for profile names, allowing - // profiles to be renamed while maintaining back compatibility - // Keep in sync with `profile_aliases` in bootstrap.py - let profile_aliases = HashMap::from([("user", "dist")]); - let include = match profile_aliases.get(include.as_str()) { - Some(alias) => alias, - None => include.as_str(), - }; - let mut include_path = config.src.clone(); - include_path.push("src"); - include_path.push("bootstrap"); - include_path.push("defaults"); - include_path.push(format!("bootstrap.{include}.toml")); - let included_toml = get_toml(&include_path).unwrap_or_else(|e| { - eprintln!( - "ERROR: Failed to parse default config profile at '{}': {e}", - include_path.display() - ); - exit!(2); - }); - toml.merge( - Some(include_path), - &mut Default::default(), - included_toml, - ReplaceOpt::IgnoreDuplicate, - ); - } - - let mut override_toml = TomlConfig::default(); - for option in flags_set.iter() { - fn get_table(option: &str) -> Result<TomlConfig, toml::de::Error> { - toml::from_str(option).and_then(|table: toml::Value| TomlConfig::deserialize(table)) - } - - let mut err = match get_table(option) { - Ok(v) => { - override_toml.merge( - None, - &mut Default::default(), - v, - ReplaceOpt::ErrorOnDuplicate, - ); - continue; - } - Err(e) => e, - }; - // We want to be able to set string values without quotes, - // like in `configure.py`. Try adding quotes around the right hand side - if let Some((key, value)) = option.split_once('=') - && !value.contains('"') - { - match get_table(&format!(r#"{key}="{value}""#)) { - Ok(v) => { - override_toml.merge( - None, - &mut Default::default(), - v, - ReplaceOpt::ErrorOnDuplicate, - ); - continue; - } - Err(e) => err = e, - } - } - eprintln!("failed to parse override `{option}`: `{err}"); - exit!(2) - } - toml.merge(None, &mut Default::default(), override_toml, ReplaceOpt::Override); - config.change_id = toml.change_id.inner; - let Build { - description, - build, - host, - target, - build_dir, - cargo, - rustc, - rustfmt, - cargo_clippy, - docs, - compiler_docs, - library_docs_private_items, - docs_minification, - submodules, - gdb, - lldb, - nodejs, - npm, - python, - reuse, - locked_deps, - vendor, - full_bootstrap, - bootstrap_cache_path, - extended, - tools, - tool, - verbose, - sanitizers, - profiler, - cargo_native_static, - low_priority, - configure_args, - local_rebuild, - print_step_timings, - print_step_rusage, - check_stage, - doc_stage, - build_stage, - test_stage, - install_stage, - dist_stage, - bench_stage, - patch_binaries_for_nix, - // This field is only used by bootstrap.py - metrics: _, - android_ndk, - optimized_compiler_builtins, - jobs, - compiletest_diff_tool, - compiletest_allow_stage0, - compiletest_use_stage0_libtest, - tidy_extra_checks, - ccache, - exclude, - } = toml.build.unwrap_or_default(); - - let mut paths: Vec<PathBuf> = flags_skip.into_iter().chain(flags_exclude).collect(); - - if let Some(exclude) = exclude { - paths.extend(exclude); - } - config.skip = paths .into_iter() .map(|p| { @@ -784,15 +759,20 @@ impl Config { }) .collect(); - config.jobs = Some(threads_from_config(flags_jobs.unwrap_or(jobs.unwrap_or(0)))); + #[cfg(feature = "tracing")] + span!( + target: "CONFIG_HANDLING", + tracing::Level::TRACE, + "normalizing and combining `flag.skip`/`flag.exclude` paths", + "config.skip" = ?config.skip, + ); - if let Some(flags_build) = flags_build { - config.host_target = TargetSelection::from_user(&flags_build); - } else if let Some(file_build) = build { - config.host_target = TargetSelection::from_user(&file_build); - }; + config.jobs = Some(threads_from_config(build_jobs.unwrap_or(0))); + if let Some(build) = build_build { + config.host_target = TargetSelection::from_user(&build); + } - set(&mut config.out, flags_build_dir.or_else(|| build_dir.map(PathBuf::from))); + set(&mut config.out, build_dir); // NOTE: Bootstrap spawns various commands with different working directories. // To avoid writing to random places on the file system, `config.out` needs to be an absolute path. if !config.out.is_absolute() { @@ -800,21 +780,13 @@ impl Config { config.out = absolute(&config.out).expect("can't make empty path absolute"); } - if cargo_clippy.is_some() && rustc.is_none() { + if build_cargo_clippy.is_some() && build_rustc.is_none() { println!( "WARNING: Using `build.cargo-clippy` without `build.rustc` usually fails due to toolchain conflict." ); } - config.patch_binaries_for_nix = patch_binaries_for_nix; - config.bootstrap_cache_path = bootstrap_cache_path; - config.llvm_assertions = - toml.llvm.as_ref().is_some_and(|llvm| llvm.assertions.unwrap_or(false)); - - config.initial_rustc = if let Some(rustc) = rustc { - if !flags_skip_stage0_validation { - config.check_stage0_version(&rustc, "rustc"); - } + config.initial_rustc = if let Some(rustc) = build_rustc { rustc } else { let dwn_ctx = DownloadContext::from(&config); @@ -836,12 +808,9 @@ impl Config { .trim() )); - config.initial_cargo_clippy = cargo_clippy; + config.initial_cargo_clippy = build_cargo_clippy; - config.initial_cargo = if let Some(cargo) = cargo { - if !flags_skip_stage0_validation { - config.check_stage0_version(&cargo, "cargo"); - } + config.initial_cargo = if let Some(cargo) = build_cargo { cargo } else { let dwn_ctx = DownloadContext::from(&config); @@ -856,62 +825,60 @@ impl Config { config.out = dir; } - config.hosts = if let Some(TargetSelectionList(arg_host)) = flags_host { - arg_host - } else if let Some(file_host) = host { - file_host.iter().map(|h| TargetSelection::from_user(h)).collect() - } else { - vec![config.host_target] - }; - config.targets = if let Some(TargetSelectionList(arg_target)) = flags_target { - arg_target - } else if let Some(file_target) = target { - file_target.iter().map(|h| TargetSelection::from_user(h)).collect() + config.hosts = if let Some(hosts) = host { hosts } else { vec![config.host_target] }; + config.targets = if let Some(targets) = target { + targets } else { // If target is *not* configured, then default to the host // toolchains. config.hosts.clone() }; - config.nodejs = nodejs.map(PathBuf::from); - config.npm = npm.map(PathBuf::from); - config.gdb = gdb.map(PathBuf::from); - config.lldb = lldb.map(PathBuf::from); - config.python = python.map(PathBuf::from); - config.reuse = reuse.map(PathBuf::from); - config.submodules = submodules; - config.android_ndk = android_ndk; - set(&mut config.low_priority, low_priority); - set(&mut config.compiler_docs, compiler_docs); - set(&mut config.library_docs_private_items, library_docs_private_items); - set(&mut config.docs_minification, docs_minification); - set(&mut config.docs, docs); - set(&mut config.locked_deps, locked_deps); - set(&mut config.full_bootstrap, full_bootstrap); - set(&mut config.extended, extended); - config.tools = tools; - set(&mut config.tool, tool); - set(&mut config.verbose, verbose); - set(&mut config.sanitizers, sanitizers); - set(&mut config.profiler, profiler); - set(&mut config.cargo_native_static, cargo_native_static); - set(&mut config.configure_args, configure_args); - set(&mut config.local_rebuild, local_rebuild); - set(&mut config.print_step_timings, print_step_timings); - set(&mut config.print_step_rusage, print_step_rusage); - - config.verbose = cmp::max(config.verbose, flags_verbose as usize); + config.nodejs = build_nodejs.map(PathBuf::from); + config.npm = build_npm.map(PathBuf::from); + config.gdb = build_gdb.map(PathBuf::from); + config.lldb = build_lldb.map(PathBuf::from); + config.python = build_python.map(PathBuf::from); + config.reuse = build_reuse.map(PathBuf::from); + config.submodules = build_submodules; + config.android_ndk = build_android_ndk; + config.bootstrap_cache_path = build_bootstrap_cache_path; + set(&mut config.low_priority, build_low_priority); + set(&mut config.compiler_docs, build_compiler_docs); + set(&mut config.library_docs_private_items, build_library_docs_private_items); + set(&mut config.docs_minification, build_docs_minification); + set(&mut config.docs, build_docs); + set(&mut config.locked_deps, build_locked_deps); + set(&mut config.full_bootstrap, build_full_bootstrap); + set(&mut config.extended, build_extended); + config.tools = build_tools; + set(&mut config.tool, build_tool); + set(&mut config.sanitizers, build_sanitizers); + set(&mut config.profiler, build_profiler); + set(&mut config.cargo_native_static, build_cargo_native_static); + set(&mut config.configure_args, build_configure_args); + set(&mut config.local_rebuild, build_local_rebuild); + set(&mut config.print_step_timings, build_print_step_timings); + set(&mut config.print_step_rusage, build_print_step_rusage); + config.patch_binaries_for_nix = build_patch_binaries_for_nix; // Verbose flag is a good default for `rust.verbose-tests`. config.verbose_tests = config.is_verbose(); - config.apply_install_config(toml.install); + config.prefix = install_prefix.map(PathBuf::from); + config.sysconfdir = install_sysconfdir.map(PathBuf::from); + config.datadir = install_datadir.map(PathBuf::from); + config.docdir = install_docdir.map(PathBuf::from); + set(&mut config.bindir, install_bindir.map(PathBuf::from)); + config.libdir = install_libdir.map(PathBuf::from); + config.mandir = install_mandir.map(PathBuf::from); + + config.llvm_assertions = llvm_assertions.unwrap_or(false); let file_content = t!(fs::read_to_string(config.src.join("src/ci/channel"))); let ci_channel = file_content.trim_end(); - let toml_channel = toml.rust.as_ref().and_then(|r| r.channel.clone()); - let is_user_configured_rust_channel = match toml_channel { + let is_user_configured_rust_channel = match rust_channel { Some(channel) if channel == "auto-detect" => { config.channel = ci_channel.into(); true @@ -924,7 +891,7 @@ impl Config { }; let default = config.channel == "dev"; - config.omit_git_hash = toml.rust.as_ref().and_then(|r| r.omit_git_hash).unwrap_or(default); + config.omit_git_hash = rust_omit_git_hash.unwrap_or(default); config.rust_info = config.git_info(config.omit_git_hash, &config.src); config.cargo_info = @@ -942,7 +909,7 @@ impl Config { config.in_tree_llvm_info = config.git_info(false, &config.src.join("src/llvm-project")); config.in_tree_gcc_info = config.git_info(false, &config.src.join("src/gcc")); - config.vendor = vendor.unwrap_or( + config.vendor = build_vendor.unwrap_or( config.rust_info.is_from_tarball() && config.src.join("vendor").exists() && config.src.join(".cargo/config.toml").exists(), @@ -955,11 +922,230 @@ impl Config { config.rust_profile_use = flags_rust_profile_use; config.rust_profile_generate = flags_rust_profile_generate; - config.apply_target_config(toml.target); - config.apply_rust_config(toml.rust, flags_warnings); + // FIXME(#133381): alt rustc builds currently do *not* have rustc debug assertions + // enabled. We should not download a CI alt rustc if we need rustc to have debug + // assertions (e.g. for crashes test suite). This can be changed once something like + // [Enable debug assertions on alt + // builds](https://github.com/rust-lang/rust/pull/131077) lands. + // + // Note that `rust.debug = true` currently implies `rust.debug-assertions = true`! + // + // This relies also on the fact that the global default for `download-rustc` will be + // `false` if it's not explicitly set. + let debug_assertions_requested = matches!(rust_rustc_debug_assertions, Some(true)) + || (matches!(rust_debug, Some(true)) + && !matches!(rust_rustc_debug_assertions, Some(false))); + + if debug_assertions_requested + && let Some(ref opt) = rust_download_rustc + && opt.is_string_or_true() + { + eprintln!( + "WARN: currently no CI rustc builds have rustc debug assertions \ + enabled. Please either set `rust.debug-assertions` to `false` if you \ + want to use download CI rustc or set `rust.download-rustc` to `false`." + ); + } + + config.download_rustc_commit = config.download_ci_rustc_commit( + rust_download_rustc, + debug_assertions_requested, + config.llvm_assertions, + ); + + if let Some(t) = toml.target { + for (triple, cfg) in t { + let mut target = Target::from_triple(&triple); + + if let Some(ref s) = cfg.llvm_config { + if config.download_rustc_commit.is_some() + && triple == *config.host_target.triple + { + panic!( + "setting llvm_config for the host is incompatible with download-rustc" + ); + } + target.llvm_config = Some(config.src.join(s)); + } + if let Some(patches) = cfg.llvm_has_rust_patches { + assert!( + config.submodules == Some(false) || cfg.llvm_config.is_some(), + "use of `llvm-has-rust-patches` is restricted to cases where either submodules are disabled or llvm-config been provided" + ); + target.llvm_has_rust_patches = Some(patches); + } + if let Some(ref s) = cfg.llvm_filecheck { + target.llvm_filecheck = Some(config.src.join(s)); + } + target.llvm_libunwind = cfg.llvm_libunwind.as_ref().map(|v| { + v.parse().unwrap_or_else(|_| { + panic!("failed to parse target.{triple}.llvm-libunwind") + }) + }); + if let Some(s) = cfg.no_std { + target.no_std = s; + } + target.cc = cfg.cc.map(PathBuf::from); + target.cxx = cfg.cxx.map(PathBuf::from); + target.ar = cfg.ar.map(PathBuf::from); + target.ranlib = cfg.ranlib.map(PathBuf::from); + target.linker = cfg.linker.map(PathBuf::from); + target.crt_static = cfg.crt_static; + target.musl_root = cfg.musl_root.map(PathBuf::from); + target.musl_libdir = cfg.musl_libdir.map(PathBuf::from); + target.wasi_root = cfg.wasi_root.map(PathBuf::from); + target.qemu_rootfs = cfg.qemu_rootfs.map(PathBuf::from); + target.runner = cfg.runner; + target.sanitizers = cfg.sanitizers; + target.profiler = cfg.profiler; + target.rpath = cfg.rpath; + target.optimized_compiler_builtins = cfg.optimized_compiler_builtins; + target.jemalloc = cfg.jemalloc; + if let Some(backends) = cfg.codegen_backends { + target.codegen_backends = + Some(parse_codegen_backends(backends, &format!("target.{triple}"))) + } + + target.split_debuginfo = cfg.split_debuginfo.as_ref().map(|v| { + v.parse().unwrap_or_else(|_| { + panic!("invalid value for target.{triple}.split-debuginfo") + }) + }); + + config.target_config.insert(TargetSelection::from_user(&triple), target); + } + } + + if rust_optimize.as_ref().is_some_and(|v| matches!(v, RustOptimize::Bool(false))) { + eprintln!( + "WARNING: setting `optimize` to `false` is known to cause errors and \ + should be considered unsupported. Refer to `bootstrap.example.toml` \ + for more details." + ); + } + + config.rust_new_symbol_mangling = rust_new_symbol_mangling; + set(&mut config.rust_optimize_tests, rust_optimize_tests); + set(&mut config.codegen_tests, rust_codegen_tests); + set(&mut config.rust_rpath, rust_rpath); + set(&mut config.rust_strip, rust_strip); + set(&mut config.rust_frame_pointers, rust_frame_pointers); + config.rust_stack_protector = rust_stack_protector; + set(&mut config.jemalloc, rust_jemalloc); + set(&mut config.test_compare_mode, rust_test_compare_mode); + set(&mut config.backtrace, rust_backtrace); + set(&mut config.rust_dist_src, rust_dist_src); + set(&mut config.verbose_tests, rust_verbose_tests); + // in the case "false" is set explicitly, do not overwrite the command line args + if let Some(true) = rust_incremental { + config.incremental = true; + } + set(&mut config.lld_mode, rust_lld_mode); + set(&mut config.llvm_bitcode_linker_enabled, rust_llvm_bitcode_linker); + + config.rust_randomize_layout = rust_randomize_layout.unwrap_or_default(); + config.llvm_tools_enabled = rust_llvm_tools.unwrap_or(true); + + config.llvm_enzyme = config.channel == "dev" || config.channel == "nightly"; + config.rustc_default_linker = rust_default_linker; + config.musl_root = rust_musl_root.map(PathBuf::from); + config.save_toolstates = rust_save_toolstates.map(PathBuf::from); + set( + &mut config.deny_warnings, + match flags_warnings { + Warnings::Deny => Some(true), + Warnings::Warn => Some(false), + Warnings::Default => rust_deny_warnings, + }, + ); + set(&mut config.backtrace_on_ice, rust_backtrace_on_ice); + set(&mut config.rust_verify_llvm_ir, rust_verify_llvm_ir); + config.rust_thin_lto_import_instr_limit = rust_thin_lto_import_instr_limit; + set(&mut config.rust_remap_debuginfo, rust_remap_debuginfo); + set(&mut config.control_flow_guard, rust_control_flow_guard); + set(&mut config.ehcont_guard, rust_ehcont_guard); + config.llvm_libunwind_default = + rust_llvm_libunwind.map(|v| v.parse().expect("failed to parse rust.llvm-libunwind")); + set( + &mut config.rust_codegen_backends, + rust_codegen_backends.map(|backends| parse_codegen_backends(backends, "rust")), + ); + + config.rust_codegen_units = rust_codegen_units.map(threads_from_config); + config.rust_codegen_units_std = rust_codegen_units_std.map(threads_from_config); + + if config.rust_profile_use.is_none() { + config.rust_profile_use = rust_profile_use; + } + + if config.rust_profile_generate.is_none() { + config.rust_profile_generate = rust_profile_generate; + } + + config.rust_lto = + rust_lto.as_deref().map(|value| RustcLto::from_str(value).unwrap()).unwrap_or_default(); + config.rust_validate_mir_opts = rust_validate_mir_opts; + + config.rust_optimize = rust_optimize.unwrap_or(RustOptimize::Bool(true)); + + // We make `x86_64-unknown-linux-gnu` use the self-contained linker by default, so we will + // build our internal lld and use it as the default linker, by setting the `rust.lld` config + // to true by default: + // - on the `x86_64-unknown-linux-gnu` target + // - when building our in-tree llvm (i.e. the target has not set an `llvm-config`), so that + // we're also able to build the corresponding lld + // - or when using an external llvm that's downloaded from CI, which also contains our prebuilt + // lld + // - otherwise, we'd be using an external llvm, and lld would not necessarily available and + // thus, disabled + // - similarly, lld will not be built nor used by default when explicitly asked not to, e.g. + // when the config sets `rust.lld = false` + if default_lld_opt_in_targets().contains(&config.host_target.triple.to_string()) + && config.hosts == [config.host_target] + { + let no_llvm_config = config + .target_config + .get(&config.host_target) + .is_none_or(|target_config| target_config.llvm_config.is_none()); + let enable_lld = config.llvm_from_ci || no_llvm_config; + // Prefer the config setting in case an explicit opt-out is needed. + config.lld_enabled = rust_lld_enabled.unwrap_or(enable_lld); + } else { + set(&mut config.lld_enabled, rust_lld_enabled); + } + + let default_std_features = BTreeSet::from([String::from("panic-unwind")]); + config.rust_std_features = rust_std_features.unwrap_or(default_std_features); + + let default = rust_debug == Some(true); + config.rustc_debug_assertions = rust_rustc_debug_assertions.unwrap_or(default); + config.std_debug_assertions = + rust_std_debug_assertions.unwrap_or(config.rustc_debug_assertions); + config.tools_debug_assertions = + rust_tools_debug_assertions.unwrap_or(config.rustc_debug_assertions); + config.rust_overflow_checks = rust_overflow_checks.unwrap_or(default); + config.rust_overflow_checks_std = + rust_overflow_checks_std.unwrap_or(config.rust_overflow_checks); + + config.rust_debug_logging = rust_debug_logging.unwrap_or(config.rustc_debug_assertions); + + let with_defaults = |debuginfo_level_specific: Option<_>| { + debuginfo_level_specific.or(rust_debuginfo_level).unwrap_or( + if rust_debug == Some(true) { + DebuginfoLevel::Limited + } else { + DebuginfoLevel::None + }, + ) + }; + config.rust_debuginfo_level_rustc = with_defaults(rust_debuginfo_level_rustc); + config.rust_debuginfo_level_std = with_defaults(rust_debuginfo_level_std); + config.rust_debuginfo_level_tools = with_defaults(rust_debuginfo_level_tools); + config.rust_debuginfo_level_tests = + rust_debuginfo_level_tests.unwrap_or(DebuginfoLevel::None); config.reproducible_artifacts = flags_reproducible_artifact; - config.description = description; + config.description = build_description; // We need to override `rust.channel` if it's manually specified when using the CI rustc. // This is because if the compiler uses a different channel than the one specified in bootstrap.toml, @@ -977,11 +1163,90 @@ impl Config { config.channel = channel; } - config.apply_llvm_config(toml.llvm); + set(&mut config.ninja_in_file, llvm_ninja); + set(&mut config.llvm_optimize, llvm_optimize); + set(&mut config.llvm_thin_lto, llvm_thin_lto); + set(&mut config.llvm_release_debuginfo, llvm_release_debuginfo); + set(&mut config.llvm_static_stdcpp, llvm_static_libstdcpp); + set(&mut config.llvm_libzstd, llvm_libzstd); + if let Some(v) = llvm_link_shared { + config.llvm_link_shared.set(Some(v)); + } + config.llvm_targets.clone_from(&llvm_targets); + config.llvm_experimental_targets.clone_from(&llvm_experimental_targets); + config.llvm_link_jobs = llvm_link_jobs; + config.llvm_version_suffix.clone_from(&llvm_version_suffix); + config.llvm_clang_cl.clone_from(&llvm_clang_cl); + config.llvm_tests = llvm_tests.unwrap_or_default(); + config.llvm_enzyme = llvm_enzyme.unwrap_or_default(); + config.llvm_plugins = llvm_plugin.unwrap_or_default(); + + config.llvm_cflags.clone_from(&llvm_cflags); + config.llvm_cxxflags.clone_from(&llvm_cxxflags); + config.llvm_ldflags.clone_from(&llvm_ldflags); + set(&mut config.llvm_use_libcxx, llvm_use_libcxx); + config.llvm_use_linker.clone_from(&llvm_use_linker); + config.llvm_allow_old_toolchain = llvm_allow_old_toolchain.unwrap_or(false); + config.llvm_offload = llvm_offload.unwrap_or(false); + config.llvm_polly = llvm_polly.unwrap_or(false); + config.llvm_clang = llvm_clang.unwrap_or(false); + config.llvm_enable_warnings = llvm_enable_warnings.unwrap_or(false); + config.llvm_build_config = llvm_build_config.clone().unwrap_or(Default::default()); + + config.llvm_from_ci = + config.parse_download_ci_llvm(llvm_download_ci_llvm, config.llvm_assertions); - config.apply_gcc_config(toml.gcc); + if config.llvm_from_ci { + let warn = |option: &str| { + println!( + "WARNING: `{option}` will only be used on `compiler/rustc_llvm` build, not for the LLVM build." + ); + println!( + "HELP: To use `{option}` for LLVM builds, set `download-ci-llvm` option to false." + ); + }; + + if llvm_static_libstdcpp.is_some() { + warn("static-libstdcpp"); + } + + if llvm_link_shared.is_some() { + warn("link-shared"); + } + + // FIXME(#129153): instead of all the ad-hoc `download-ci-llvm` checks that follow, + // use the `builder-config` present in tarballs since #128822 to compare the local + // config to the ones used to build the LLVM artifacts on CI, and only notify users + // if they've chosen a different value. - match ccache { + if llvm_libzstd.is_some() { + println!( + "WARNING: when using `download-ci-llvm`, the local `llvm.libzstd` option, \ + like almost all `llvm.*` options, will be ignored and set by the LLVM CI \ + artifacts builder config." + ); + println!( + "HELP: To use `llvm.libzstd` for LLVM/LLD builds, set `download-ci-llvm` option to false." + ); + } + } + + if !config.llvm_from_ci && config.llvm_thin_lto && llvm_link_shared.is_none() { + // If we're building with ThinLTO on, by default we want to link + // to LLVM shared, to avoid re-doing ThinLTO (which happens in + // the link step) with each stage. + config.llvm_link_shared.set(Some(true)); + } + + config.gcc_ci_mode = match gcc_download_ci_gcc { + Some(value) => match value { + true => GccCiMode::DownloadFromCi, + false => GccCiMode::BuildLocally, + }, + None => GccCiMode::default(), + }; + + match build_ccache { Some(StringOrBool::String(ref s)) => config.ccache = Some(s.to_string()), Some(StringOrBool::Bool(true)) => { config.ccache = Some("ccache".to_string()); @@ -1005,9 +1270,18 @@ impl Config { Some(ci_llvm_bin.join(exe("FileCheck", config.host_target))); } - config.apply_dist_config(toml.dist); + config.dist_sign_folder = dist_sign_folder.map(PathBuf::from); + config.dist_upload_addr = dist_upload_addr; + config.dist_compression_formats = dist_compression_formats; + set(&mut config.dist_compression_profile, dist_compression_profile); + set(&mut config.rust_dist_src, dist_src_tarball); + set(&mut config.dist_include_mingw_linker, dist_include_mingw_linker); + config.dist_vendor = dist_vendor.unwrap_or_else(|| { + // If we're building from git or tarball sources, enable it by default. + config.rust_info.is_managed_git_subrepository() || config.rust_info.is_from_tarball() + }); - config.initial_rustfmt = if let Some(r) = rustfmt { + config.initial_rustfmt = if let Some(r) = build_rustfmt { Some(r) } else { let dwn_ctx = DownloadContext::from(&config); @@ -1028,41 +1302,40 @@ impl Config { } config.optimized_compiler_builtins = - optimized_compiler_builtins.unwrap_or(config.channel != "dev"); - - config.compiletest_diff_tool = compiletest_diff_tool; - - config.compiletest_allow_stage0 = compiletest_allow_stage0.unwrap_or(false); - config.compiletest_use_stage0_libtest = compiletest_use_stage0_libtest.unwrap_or(true); - - config.tidy_extra_checks = tidy_extra_checks; + build_optimized_compiler_builtins.unwrap_or(config.channel != "dev"); + config.compiletest_diff_tool = build_compiletest_diff_tool; + config.compiletest_use_stage0_libtest = + build_compiletest_use_stage0_libtest.unwrap_or(true); + config.tidy_extra_checks = build_tidy_extra_checks; let download_rustc = config.download_rustc_commit.is_some(); config.explicit_stage_from_cli = flags_stage.is_some(); - config.explicit_stage_from_config = test_stage.is_some() - || build_stage.is_some() - || doc_stage.is_some() - || dist_stage.is_some() - || install_stage.is_some() - || check_stage.is_some() - || bench_stage.is_some(); + config.explicit_stage_from_config = build_test_stage.is_some() + || build_build_stage.is_some() + || build_doc_stage.is_some() + || build_dist_stage.is_some() + || build_install_stage.is_some() + || build_check_stage.is_some() + || build_bench_stage.is_some(); config.stage = match config.cmd { - Subcommand::Check { .. } => flags_stage.or(check_stage).unwrap_or(1), - Subcommand::Clippy { .. } | Subcommand::Fix => flags_stage.or(check_stage).unwrap_or(1), + Subcommand::Check { .. } => flags_stage.or(build_check_stage).unwrap_or(1), + Subcommand::Clippy { .. } | Subcommand::Fix => { + flags_stage.or(build_check_stage).unwrap_or(1) + } // `download-rustc` only has a speed-up for stage2 builds. Default to stage2 unless explicitly overridden. Subcommand::Doc { .. } => { - flags_stage.or(doc_stage).unwrap_or(if download_rustc { 2 } else { 1 }) + flags_stage.or(build_doc_stage).unwrap_or(if download_rustc { 2 } else { 1 }) } Subcommand::Build => { - flags_stage.or(build_stage).unwrap_or(if download_rustc { 2 } else { 1 }) + flags_stage.or(build_build_stage).unwrap_or(if download_rustc { 2 } else { 1 }) } Subcommand::Test { .. } | Subcommand::Miri { .. } => { - flags_stage.or(test_stage).unwrap_or(if download_rustc { 2 } else { 1 }) + flags_stage.or(build_test_stage).unwrap_or(if download_rustc { 2 } else { 1 }) } - Subcommand::Bench { .. } => flags_stage.or(bench_stage).unwrap_or(2), - Subcommand::Dist => flags_stage.or(dist_stage).unwrap_or(2), - Subcommand::Install => flags_stage.or(install_stage).unwrap_or(2), + Subcommand::Bench { .. } => flags_stage.or(build_bench_stage).unwrap_or(2), + Subcommand::Dist => flags_stage.or(build_dist_stage).unwrap_or(2), + Subcommand::Install => flags_stage.or(build_install_stage).unwrap_or(2), Subcommand::Perf { .. } => flags_stage.unwrap_or(1), // These are all bootstrap tools, which don't depend on the compiler. // The stage we pass shouldn't matter, but use 0 just in case. @@ -1512,49 +1785,6 @@ impl Config { } } - #[cfg(test)] - pub fn check_stage0_version(&self, _program_path: &Path, _component_name: &'static str) {} - - /// check rustc/cargo version is same or lower with 1 apart from the building one - #[cfg(not(test))] - pub fn check_stage0_version(&self, program_path: &Path, component_name: &'static str) { - use build_helper::util::fail; - - if self.dry_run() { - return; - } - - let stage0_output = - command(program_path).arg("--version").run_capture_stdout(self).stdout(); - let mut stage0_output = stage0_output.lines().next().unwrap().split(' '); - - let stage0_name = stage0_output.next().unwrap(); - if stage0_name != component_name { - fail(&format!( - "Expected to find {component_name} at {} but it claims to be {stage0_name}", - program_path.display() - )); - } - - let stage0_version = - semver::Version::parse(stage0_output.next().unwrap().split('-').next().unwrap().trim()) - .unwrap(); - let source_version = semver::Version::parse( - fs::read_to_string(self.src.join("src/version")).unwrap().trim(), - ) - .unwrap(); - if !(source_version == stage0_version - || (source_version.major == stage0_version.major - && (source_version.minor == stage0_version.minor - || source_version.minor == stage0_version.minor + 1))) - { - let prev_version = format!("{}.{}.x", source_version.major, source_version.minor - 1); - fail(&format!( - "Unexpected {component_name} version: {stage0_version}, we should use {prev_version}/{source_version} to build source with {source_version}" - )); - } - } - /// Returns the commit to download, or `None` if we shouldn't download CI artifacts. pub fn download_ci_rustc_commit( &self, @@ -1747,19 +1977,24 @@ impl Config { .unwrap_or(self.profiler) } - pub fn codegen_backends(&self, target: TargetSelection) -> &[CodegenBackendKind] { + /// Returns codegen backends that should be: + /// - Built and added to the sysroot when we build the compiler. + /// - Distributed when `x dist` is executed (if the codegen backend has a dist step). + pub fn enabled_codegen_backends(&self, target: TargetSelection) -> &[CodegenBackendKind] { self.target_config .get(&target) .and_then(|cfg| cfg.codegen_backends.as_deref()) .unwrap_or(&self.rust_codegen_backends) } - pub fn jemalloc(&self, target: TargetSelection) -> bool { - self.target_config.get(&target).and_then(|cfg| cfg.jemalloc).unwrap_or(self.jemalloc) + /// Returns the codegen backend that should be configured as the *default* codegen backend + /// for a rustc compiled by bootstrap. + pub fn default_codegen_backend(&self, target: TargetSelection) -> Option<CodegenBackendKind> { + self.enabled_codegen_backends(target).first().cloned() } - pub fn default_codegen_backend(&self, target: TargetSelection) -> Option<CodegenBackendKind> { - self.codegen_backends(target).first().cloned() + pub fn jemalloc(&self, target: TargetSelection) -> bool { + self.target_config.get(&target).and_then(|cfg| cfg.jemalloc).unwrap_or(self.jemalloc) } pub fn rpath_enabled(&self, target: TargetSelection) -> bool { @@ -1774,7 +2009,7 @@ impl Config { } pub fn llvm_enabled(&self, target: TargetSelection) -> bool { - self.codegen_backends(target).contains(&CodegenBackendKind::Llvm) + self.enabled_codegen_backends(target).contains(&CodegenBackendKind::Llvm) } pub fn llvm_libunwind(&self, target: TargetSelection) -> LlvmLibunwind { @@ -1846,3 +2081,262 @@ impl AsRef<ExecutionContext> for Config { &self.exec_ctx } } + +fn compute_src_directory(src_dir: Option<PathBuf>, exec_ctx: &ExecutionContext) -> Option<PathBuf> { + if let Some(src) = src_dir { + return Some(src); + } else { + // Infer the source directory. This is non-trivial because we want to support a downloaded bootstrap binary, + // running on a completely different machine from where it was compiled. + let mut cmd = helpers::git(None); + // NOTE: we cannot support running from outside the repository because the only other path we have available + // is set at compile time, which can be wrong if bootstrap was downloaded rather than compiled locally. + // We still support running outside the repository if we find we aren't in a git directory. + + // NOTE: We get a relative path from git to work around an issue on MSYS/mingw. If we used an absolute path, + // and end up using MSYS's git rather than git-for-windows, we would get a unix-y MSYS path. But as bootstrap + // has already been (kinda-cross-)compiled to Windows land, we require a normal Windows path. + cmd.arg("rev-parse").arg("--show-cdup"); + // Discard stderr because we expect this to fail when building from a tarball. + let output = cmd.allow_failure().run_capture_stdout(exec_ctx); + if output.is_success() { + let git_root_relative = output.stdout(); + // We need to canonicalize this path to make sure it uses backslashes instead of forward slashes, + // and to resolve any relative components. + let git_root = env::current_dir() + .unwrap() + .join(PathBuf::from(git_root_relative.trim())) + .canonicalize() + .unwrap(); + let s = git_root.to_str().unwrap(); + + // Bootstrap is quite bad at handling /? in front of paths + let git_root = match s.strip_prefix("\\\\?\\") { + Some(p) => PathBuf::from(p), + None => git_root, + }; + // If this doesn't have at least `stage0`, we guessed wrong. This can happen when, + // for example, the build directory is inside of another unrelated git directory. + // In that case keep the original `CARGO_MANIFEST_DIR` handling. + // + // NOTE: this implies that downloadable bootstrap isn't supported when the build directory is outside + // the source directory. We could fix that by setting a variable from all three of python, ./x, and x.ps1. + if git_root.join("src").join("stage0").exists() { + return Some(git_root); + } + } else { + // We're building from a tarball, not git sources. + // We don't support pre-downloaded bootstrap in this case. + } + }; + None +} + +/// Loads bootstrap TOML config and returns the config together with a path from where +/// it was loaded. +/// `src` is the source root directory, and `config_path` is an optionally provided path to the +/// config. +fn load_toml_config( + src: &Path, + config_path: Option<PathBuf>, + get_toml: &impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>, +) -> (TomlConfig, Option<PathBuf>) { + // Locate the configuration file using the following priority (first match wins): + // 1. `--config <path>` (explicit flag) + // 2. `RUST_BOOTSTRAP_CONFIG` environment variable + // 3. `./bootstrap.toml` (local file) + // 4. `<root>/bootstrap.toml` + // 5. `./config.toml` (fallback for backward compatibility) + // 6. `<root>/config.toml` + let toml_path = config_path.or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from)); + let using_default_path = toml_path.is_none(); + let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("bootstrap.toml")); + + if using_default_path && !toml_path.exists() { + toml_path = src.join(PathBuf::from("bootstrap.toml")); + if !toml_path.exists() { + toml_path = PathBuf::from("config.toml"); + if !toml_path.exists() { + toml_path = src.join(PathBuf::from("config.toml")); + } + } + } + + // Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path, + // but not if `bootstrap.toml` hasn't been created. + if !using_default_path || toml_path.exists() { + let path = Some(if cfg!(not(test)) { + toml_path = toml_path.canonicalize().unwrap(); + toml_path.clone() + } else { + toml_path.clone() + }); + ( + get_toml(&toml_path).unwrap_or_else(|e| { + eprintln!("ERROR: Failed to parse '{}': {e}", toml_path.display()); + exit!(2); + }), + path, + ) + } else { + (TomlConfig::default(), None) + } +} + +fn postprocess_toml( + toml: &mut TomlConfig, + src_dir: &Path, + toml_path: Option<PathBuf>, + exec_ctx: &ExecutionContext, + override_set: &[String], + get_toml: &impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>, +) { + let git_info = GitInfo::new(false, src_dir, exec_ctx); + + if git_info.is_from_tarball() && toml.profile.is_none() { + toml.profile = Some("dist".into()); + } + + // Reverse the list to ensure the last added config extension remains the most dominant. + // For example, given ["a.toml", "b.toml"], "b.toml" should take precedence over "a.toml". + // + // This must be handled before applying the `profile` since `include`s should always take + // precedence over `profile`s. + for include_path in toml.include.clone().unwrap_or_default().iter().rev() { + let include_path = toml_path + .as_ref() + .expect("include found in default TOML config") + .parent() + .unwrap() + .join(include_path); + + let included_toml = get_toml(&include_path).unwrap_or_else(|e| { + eprintln!("ERROR: Failed to parse '{}': {e}", include_path.display()); + exit!(2); + }); + toml.merge( + Some(include_path), + &mut Default::default(), + included_toml, + ReplaceOpt::IgnoreDuplicate, + ); + } + + if let Some(include) = &toml.profile { + // Allows creating alias for profile names, allowing + // profiles to be renamed while maintaining back compatibility + // Keep in sync with `profile_aliases` in bootstrap.py + let profile_aliases = HashMap::from([("user", "dist")]); + let include = match profile_aliases.get(include.as_str()) { + Some(alias) => alias, + None => include.as_str(), + }; + let mut include_path = PathBuf::from(src_dir); + include_path.push("src"); + include_path.push("bootstrap"); + include_path.push("defaults"); + include_path.push(format!("bootstrap.{include}.toml")); + let included_toml = get_toml(&include_path).unwrap_or_else(|e| { + eprintln!( + "ERROR: Failed to parse default config profile at '{}': {e}", + include_path.display() + ); + exit!(2); + }); + toml.merge( + Some(include_path), + &mut Default::default(), + included_toml, + ReplaceOpt::IgnoreDuplicate, + ); + } + + let mut override_toml = TomlConfig::default(); + for option in override_set.iter() { + fn get_table(option: &str) -> Result<TomlConfig, toml::de::Error> { + toml::from_str(option).and_then(|table: toml::Value| TomlConfig::deserialize(table)) + } + + let mut err = match get_table(option) { + Ok(v) => { + override_toml.merge(None, &mut Default::default(), v, ReplaceOpt::ErrorOnDuplicate); + continue; + } + Err(e) => e, + }; + // We want to be able to set string values without quotes, + // like in `configure.py`. Try adding quotes around the right hand side + if let Some((key, value)) = option.split_once('=') + && !value.contains('"') + { + match get_table(&format!(r#"{key}="{value}""#)) { + Ok(v) => { + override_toml.merge( + None, + &mut Default::default(), + v, + ReplaceOpt::ErrorOnDuplicate, + ); + continue; + } + Err(e) => err = e, + } + } + eprintln!("failed to parse override `{option}`: `{err}"); + exit!(2) + } + toml.merge(None, &mut Default::default(), override_toml, ReplaceOpt::Override); +} + +#[cfg(test)] +pub fn check_stage0_version( + _program_path: &Path, + _component_name: &'static str, + _src_dir: &Path, + _exec_ctx: &ExecutionContext, +) { +} + +/// check rustc/cargo version is same or lower with 1 apart from the building one +#[cfg(not(test))] +pub fn check_stage0_version( + program_path: &Path, + component_name: &'static str, + src_dir: &Path, + exec_ctx: &ExecutionContext, +) { + use build_helper::util::fail; + + if exec_ctx.dry_run() { + return; + } + + let stage0_output = + command(program_path).arg("--version").run_capture_stdout(exec_ctx).stdout(); + let mut stage0_output = stage0_output.lines().next().unwrap().split(' '); + + let stage0_name = stage0_output.next().unwrap(); + if stage0_name != component_name { + fail(&format!( + "Expected to find {component_name} at {} but it claims to be {stage0_name}", + program_path.display() + )); + } + + let stage0_version = + semver::Version::parse(stage0_output.next().unwrap().split('-').next().unwrap().trim()) + .unwrap(); + let source_version = + semver::Version::parse(fs::read_to_string(src_dir.join("src/version")).unwrap().trim()) + .unwrap(); + if !(source_version == stage0_version + || (source_version.major == stage0_version.major + && (source_version.minor == stage0_version.minor + || source_version.minor == stage0_version.minor + 1))) + { + let prev_version = format!("{}.{}.x", source_version.major, source_version.minor - 1); + fail(&format!( + "Unexpected {component_name} version: {stage0_version}, we should use {prev_version}/{source_version} to build source with {source_version}" + )); + } +} diff --git a/src/bootstrap/src/core/config/toml/dist.rs b/src/bootstrap/src/core/config/toml/dist.rs index b1429ef1861..934d64d8899 100644 --- a/src/bootstrap/src/core/config/toml/dist.rs +++ b/src/bootstrap/src/core/config/toml/dist.rs @@ -7,11 +7,12 @@ use serde::{Deserialize, Deserializer}; +use crate::core::config::Merge; use crate::core::config::toml::ReplaceOpt; -use crate::core::config::{Merge, set}; -use crate::{Config, HashSet, PathBuf, define_config, exit}; +use crate::{HashSet, PathBuf, define_config, exit}; define_config! { + #[derive(Default)] struct Dist { sign_folder: Option<String> = "sign-folder", upload_addr: Option<String> = "upload-addr", @@ -22,31 +23,3 @@ define_config! { vendor: Option<bool> = "vendor", } } - -impl Config { - /// Applies distribution-related configuration from the `Dist` struct - /// to the global `Config` structure. - pub fn apply_dist_config(&mut self, toml_dist: Option<Dist>) { - if let Some(dist) = toml_dist { - let Dist { - sign_folder, - upload_addr, - src_tarball, - compression_formats, - compression_profile, - include_mingw_linker, - vendor, - } = dist; - self.dist_sign_folder = sign_folder.map(PathBuf::from); - self.dist_upload_addr = upload_addr; - self.dist_compression_formats = compression_formats; - set(&mut self.dist_compression_profile, compression_profile); - set(&mut self.rust_dist_src, src_tarball); - set(&mut self.dist_include_mingw_linker, include_mingw_linker); - self.dist_vendor = vendor.unwrap_or_else(|| { - // If we're building from git or tarball sources, enable it by default. - self.rust_info.is_managed_git_subrepository() || self.rust_info.is_from_tarball() - }); - } - } -} diff --git a/src/bootstrap/src/core/config/toml/gcc.rs b/src/bootstrap/src/core/config/toml/gcc.rs index bb817c2aaef..9ea697edf15 100644 --- a/src/bootstrap/src/core/config/toml/gcc.rs +++ b/src/bootstrap/src/core/config/toml/gcc.rs @@ -6,29 +6,14 @@ use serde::{Deserialize, Deserializer}; +use crate::core::config::Merge; use crate::core::config::toml::ReplaceOpt; -use crate::core::config::{GccCiMode, Merge}; -use crate::{Config, HashSet, PathBuf, define_config, exit}; +use crate::{HashSet, PathBuf, define_config, exit}; define_config! { /// TOML representation of how the GCC build is configured. + #[derive(Default)] struct Gcc { download_ci_gcc: Option<bool> = "download-ci-gcc", } } - -impl Config { - /// Applies GCC-related configuration from the `TomlGcc` struct to the - /// global `Config` structure. - pub fn apply_gcc_config(&mut self, toml_gcc: Option<Gcc>) { - if let Some(gcc) = toml_gcc { - self.gcc_ci_mode = match gcc.download_ci_gcc { - Some(value) => match value { - true => GccCiMode::DownloadFromCi, - false => GccCiMode::BuildLocally, - }, - None => GccCiMode::default(), - }; - } - } -} diff --git a/src/bootstrap/src/core/config/toml/install.rs b/src/bootstrap/src/core/config/toml/install.rs index 6b9ab87a0b6..60fa958bd82 100644 --- a/src/bootstrap/src/core/config/toml/install.rs +++ b/src/bootstrap/src/core/config/toml/install.rs @@ -8,12 +8,13 @@ use serde::{Deserialize, Deserializer}; +use crate::core::config::Merge; use crate::core::config::toml::ReplaceOpt; -use crate::core::config::{Merge, set}; -use crate::{Config, HashSet, PathBuf, define_config, exit}; +use crate::{HashSet, PathBuf, define_config, exit}; define_config! { /// TOML representation of various global install decisions. + #[derive(Default)] struct Install { prefix: Option<String> = "prefix", sysconfdir: Option<String> = "sysconfdir", @@ -24,20 +25,3 @@ define_config! { datadir: Option<String> = "datadir", } } - -impl Config { - /// Applies installation-related configuration from the `Install` struct - /// to the global `Config` structure. - pub fn apply_install_config(&mut self, toml_install: Option<Install>) { - if let Some(install) = toml_install { - let Install { prefix, sysconfdir, docdir, bindir, libdir, mandir, datadir } = install; - self.prefix = prefix.map(PathBuf::from); - self.sysconfdir = sysconfdir.map(PathBuf::from); - self.datadir = datadir.map(PathBuf::from); - self.docdir = docdir.map(PathBuf::from); - set(&mut self.bindir, bindir.map(PathBuf::from)); - self.libdir = libdir.map(PathBuf::from); - self.mandir = mandir.map(PathBuf::from); - } - } -} diff --git a/src/bootstrap/src/core/config/toml/llvm.rs b/src/bootstrap/src/core/config/toml/llvm.rs index 1f0cecd145c..9751837a887 100644 --- a/src/bootstrap/src/core/config/toml/llvm.rs +++ b/src/bootstrap/src/core/config/toml/llvm.rs @@ -3,12 +3,13 @@ use serde::{Deserialize, Deserializer}; +use crate::core::config::StringOrBool; use crate::core::config::toml::{Merge, ReplaceOpt, TomlConfig}; -use crate::core::config::{StringOrBool, set}; -use crate::{Config, HashMap, HashSet, PathBuf, define_config, exit}; +use crate::{HashMap, HashSet, PathBuf, define_config, exit}; define_config! { /// TOML representation of how the LLVM build is configured. + #[derive(Default)] struct Llvm { optimize: Option<bool> = "optimize", thin_lto: Option<bool> = "thin-lto", @@ -144,127 +145,3 @@ pub fn check_incompatible_options_for_ci_llvm( Ok(()) } - -impl Config { - pub fn apply_llvm_config(&mut self, toml_llvm: Option<Llvm>) { - let mut llvm_tests = None; - let mut llvm_enzyme = None; - let mut llvm_offload = None; - let mut llvm_plugins = None; - - if let Some(llvm) = toml_llvm { - let Llvm { - optimize: optimize_toml, - thin_lto, - release_debuginfo, - assertions: _, - tests, - enzyme, - plugins, - static_libstdcpp, - libzstd, - ninja, - targets, - experimental_targets, - link_jobs, - link_shared, - version_suffix, - clang_cl, - cflags, - cxxflags, - ldflags, - use_libcxx, - use_linker, - allow_old_toolchain, - offload, - polly, - clang, - enable_warnings, - download_ci_llvm, - build_config, - } = llvm; - - set(&mut self.ninja_in_file, ninja); - llvm_tests = tests; - llvm_enzyme = enzyme; - llvm_offload = offload; - llvm_plugins = plugins; - set(&mut self.llvm_optimize, optimize_toml); - set(&mut self.llvm_thin_lto, thin_lto); - set(&mut self.llvm_release_debuginfo, release_debuginfo); - set(&mut self.llvm_static_stdcpp, static_libstdcpp); - set(&mut self.llvm_libzstd, libzstd); - if let Some(v) = link_shared { - self.llvm_link_shared.set(Some(v)); - } - self.llvm_targets.clone_from(&targets); - self.llvm_experimental_targets.clone_from(&experimental_targets); - self.llvm_link_jobs = link_jobs; - self.llvm_version_suffix.clone_from(&version_suffix); - self.llvm_clang_cl.clone_from(&clang_cl); - - self.llvm_cflags.clone_from(&cflags); - self.llvm_cxxflags.clone_from(&cxxflags); - self.llvm_ldflags.clone_from(&ldflags); - set(&mut self.llvm_use_libcxx, use_libcxx); - self.llvm_use_linker.clone_from(&use_linker); - self.llvm_allow_old_toolchain = allow_old_toolchain.unwrap_or(false); - self.llvm_offload = offload.unwrap_or(false); - self.llvm_polly = polly.unwrap_or(false); - self.llvm_clang = clang.unwrap_or(false); - self.llvm_enable_warnings = enable_warnings.unwrap_or(false); - self.llvm_build_config = build_config.clone().unwrap_or(Default::default()); - - self.llvm_from_ci = self.parse_download_ci_llvm(download_ci_llvm, self.llvm_assertions); - - if self.llvm_from_ci { - let warn = |option: &str| { - println!( - "WARNING: `{option}` will only be used on `compiler/rustc_llvm` build, not for the LLVM build." - ); - println!( - "HELP: To use `{option}` for LLVM builds, set `download-ci-llvm` option to false." - ); - }; - - if static_libstdcpp.is_some() { - warn("static-libstdcpp"); - } - - if link_shared.is_some() { - warn("link-shared"); - } - - // FIXME(#129153): instead of all the ad-hoc `download-ci-llvm` checks that follow, - // use the `builder-config` present in tarballs since #128822 to compare the local - // config to the ones used to build the LLVM artifacts on CI, and only notify users - // if they've chosen a different value. - - if libzstd.is_some() { - println!( - "WARNING: when using `download-ci-llvm`, the local `llvm.libzstd` option, \ - like almost all `llvm.*` options, will be ignored and set by the LLVM CI \ - artifacts builder config." - ); - println!( - "HELP: To use `llvm.libzstd` for LLVM/LLD builds, set `download-ci-llvm` option to false." - ); - } - } - - if !self.llvm_from_ci && self.llvm_thin_lto && link_shared.is_none() { - // If we're building with ThinLTO on, by default we want to link - // to LLVM shared, to avoid re-doing ThinLTO (which happens in - // the link step) with each stage. - self.llvm_link_shared.set(Some(true)); - } - } else { - self.llvm_from_ci = self.parse_download_ci_llvm(None, false); - } - - self.llvm_tests = llvm_tests.unwrap_or(false); - self.llvm_enzyme = llvm_enzyme.unwrap_or(false); - self.llvm_offload = llvm_offload.unwrap_or(false); - self.llvm_plugins = llvm_plugins.unwrap_or(false); - } -} diff --git a/src/bootstrap/src/core/config/toml/mod.rs b/src/bootstrap/src/core/config/toml/mod.rs index 01eb243159c..7af22432ef8 100644 --- a/src/bootstrap/src/core/config/toml/mod.rs +++ b/src/bootstrap/src/core/config/toml/mod.rs @@ -5,7 +5,7 @@ //! these raw TOML configurations from various sources (the main `bootstrap.toml`, //! included files, profile defaults, and command-line overrides). This processed //! TOML data then serves as an intermediate representation, which is further -//! transformed and applied to the final [`Config`] struct. +//! transformed and applied to the final `Config` struct. use serde::Deserialize; use serde_derive::Deserialize; diff --git a/src/bootstrap/src/core/config/toml/rust.rs b/src/bootstrap/src/core/config/toml/rust.rs index 03da993a17d..3dab8d1d96d 100644 --- a/src/bootstrap/src/core/config/toml/rust.rs +++ b/src/bootstrap/src/core/config/toml/rust.rs @@ -1,22 +1,15 @@ //! This module defines the `Rust` struct, which represents the `[rust]` table //! in the `bootstrap.toml` configuration file. -use std::str::FromStr; - use serde::{Deserialize, Deserializer}; -use crate::core::build_steps::compile::CODEGEN_BACKEND_PREFIX; use crate::core::config::toml::TomlConfig; -use crate::core::config::{ - DebuginfoLevel, Merge, ReplaceOpt, RustcLto, StringOrBool, set, threads_from_config, -}; -use crate::flags::Warnings; -use crate::{ - BTreeSet, CodegenBackendKind, Config, HashSet, PathBuf, TargetSelection, define_config, exit, -}; +use crate::core::config::{DebuginfoLevel, Merge, ReplaceOpt, StringOrBool}; +use crate::{BTreeSet, CodegenBackendKind, HashSet, PathBuf, TargetSelection, define_config, exit}; define_config! { /// TOML representation of how the Rust build is configured. + #[derive(Default)] struct Rust { optimize: Option<RustOptimize> = "optimize", debug: Option<bool> = "debug", @@ -397,6 +390,8 @@ pub(crate) fn parse_codegen_backends( backends: Vec<String>, section: &str, ) -> Vec<CodegenBackendKind> { + const CODEGEN_BACKEND_PREFIX: &str = "rustc_codegen_"; + let mut found_backends = vec![]; for backend in &backends { if let Some(stripped) = backend.strip_prefix(CODEGEN_BACKEND_PREFIX) { @@ -424,7 +419,7 @@ pub(crate) fn parse_codegen_backends( } #[cfg(not(test))] -fn default_lld_opt_in_targets() -> Vec<String> { +pub fn default_lld_opt_in_targets() -> Vec<String> { vec!["x86_64-unknown-linux-gnu".to_string()] } @@ -434,7 +429,7 @@ thread_local! { } #[cfg(test)] -fn default_lld_opt_in_targets() -> Vec<String> { +pub fn default_lld_opt_in_targets() -> Vec<String> { TEST_LLD_OPT_IN_TARGETS.with(|cell| cell.borrow().clone()).unwrap_or_default() } @@ -447,250 +442,3 @@ pub fn with_lld_opt_in_targets<R>(targets: Vec<String>, f: impl FnOnce() -> R) - result }) } - -impl Config { - pub fn apply_rust_config(&mut self, toml_rust: Option<Rust>, warnings: Warnings) { - let mut debug = None; - let mut rustc_debug_assertions = None; - let mut std_debug_assertions = None; - let mut tools_debug_assertions = None; - let mut overflow_checks = None; - let mut overflow_checks_std = None; - let mut debug_logging = None; - let mut debuginfo_level = None; - let mut debuginfo_level_rustc = None; - let mut debuginfo_level_std = None; - let mut debuginfo_level_tools = None; - let mut debuginfo_level_tests = None; - let mut optimize = None; - let mut lld_enabled = None; - let mut std_features = None; - - if let Some(rust) = toml_rust { - let Rust { - optimize: optimize_toml, - debug: debug_toml, - codegen_units, - codegen_units_std, - rustc_debug_assertions: rustc_debug_assertions_toml, - std_debug_assertions: std_debug_assertions_toml, - tools_debug_assertions: tools_debug_assertions_toml, - overflow_checks: overflow_checks_toml, - overflow_checks_std: overflow_checks_std_toml, - debug_logging: debug_logging_toml, - debuginfo_level: debuginfo_level_toml, - debuginfo_level_rustc: debuginfo_level_rustc_toml, - debuginfo_level_std: debuginfo_level_std_toml, - debuginfo_level_tools: debuginfo_level_tools_toml, - debuginfo_level_tests: debuginfo_level_tests_toml, - backtrace, - incremental, - randomize_layout, - default_linker, - channel: _, // already handled above - musl_root, - rpath, - verbose_tests, - optimize_tests, - codegen_tests, - omit_git_hash: _, // already handled above - dist_src, - save_toolstates, - codegen_backends, - lld: lld_enabled_toml, - llvm_tools, - llvm_bitcode_linker, - deny_warnings, - backtrace_on_ice, - verify_llvm_ir, - thin_lto_import_instr_limit, - remap_debuginfo, - jemalloc, - test_compare_mode, - llvm_libunwind, - control_flow_guard, - ehcont_guard, - new_symbol_mangling, - profile_generate, - profile_use, - download_rustc, - lto, - validate_mir_opts, - frame_pointers, - stack_protector, - strip, - lld_mode, - std_features: std_features_toml, - } = rust; - - // FIXME(#133381): alt rustc builds currently do *not* have rustc debug assertions - // enabled. We should not download a CI alt rustc if we need rustc to have debug - // assertions (e.g. for crashes test suite). This can be changed once something like - // [Enable debug assertions on alt - // builds](https://github.com/rust-lang/rust/pull/131077) lands. - // - // Note that `rust.debug = true` currently implies `rust.debug-assertions = true`! - // - // This relies also on the fact that the global default for `download-rustc` will be - // `false` if it's not explicitly set. - let debug_assertions_requested = matches!(rustc_debug_assertions_toml, Some(true)) - || (matches!(debug_toml, Some(true)) - && !matches!(rustc_debug_assertions_toml, Some(false))); - - if debug_assertions_requested - && let Some(ref opt) = download_rustc - && opt.is_string_or_true() - { - eprintln!( - "WARN: currently no CI rustc builds have rustc debug assertions \ - enabled. Please either set `rust.debug-assertions` to `false` if you \ - want to use download CI rustc or set `rust.download-rustc` to `false`." - ); - } - - self.download_rustc_commit = self.download_ci_rustc_commit( - download_rustc, - debug_assertions_requested, - self.llvm_assertions, - ); - - debug = debug_toml; - rustc_debug_assertions = rustc_debug_assertions_toml; - std_debug_assertions = std_debug_assertions_toml; - tools_debug_assertions = tools_debug_assertions_toml; - overflow_checks = overflow_checks_toml; - overflow_checks_std = overflow_checks_std_toml; - debug_logging = debug_logging_toml; - debuginfo_level = debuginfo_level_toml; - debuginfo_level_rustc = debuginfo_level_rustc_toml; - debuginfo_level_std = debuginfo_level_std_toml; - debuginfo_level_tools = debuginfo_level_tools_toml; - debuginfo_level_tests = debuginfo_level_tests_toml; - lld_enabled = lld_enabled_toml; - std_features = std_features_toml; - - if optimize_toml.as_ref().is_some_and(|v| matches!(v, RustOptimize::Bool(false))) { - eprintln!( - "WARNING: setting `optimize` to `false` is known to cause errors and \ - should be considered unsupported. Refer to `bootstrap.example.toml` \ - for more details." - ); - } - - optimize = optimize_toml; - self.rust_new_symbol_mangling = new_symbol_mangling; - set(&mut self.rust_optimize_tests, optimize_tests); - set(&mut self.codegen_tests, codegen_tests); - set(&mut self.rust_rpath, rpath); - set(&mut self.rust_strip, strip); - set(&mut self.rust_frame_pointers, frame_pointers); - self.rust_stack_protector = stack_protector; - set(&mut self.jemalloc, jemalloc); - set(&mut self.test_compare_mode, test_compare_mode); - set(&mut self.backtrace, backtrace); - set(&mut self.rust_dist_src, dist_src); - set(&mut self.verbose_tests, verbose_tests); - // in the case "false" is set explicitly, do not overwrite the command line args - if let Some(true) = incremental { - self.incremental = true; - } - set(&mut self.lld_mode, lld_mode); - set(&mut self.llvm_bitcode_linker_enabled, llvm_bitcode_linker); - - self.rust_randomize_layout = randomize_layout.unwrap_or_default(); - self.llvm_tools_enabled = llvm_tools.unwrap_or(true); - - self.llvm_enzyme = self.channel == "dev" || self.channel == "nightly"; - self.rustc_default_linker = default_linker; - self.musl_root = musl_root.map(PathBuf::from); - self.save_toolstates = save_toolstates.map(PathBuf::from); - set( - &mut self.deny_warnings, - match warnings { - Warnings::Deny => Some(true), - Warnings::Warn => Some(false), - Warnings::Default => deny_warnings, - }, - ); - set(&mut self.backtrace_on_ice, backtrace_on_ice); - set(&mut self.rust_verify_llvm_ir, verify_llvm_ir); - self.rust_thin_lto_import_instr_limit = thin_lto_import_instr_limit; - set(&mut self.rust_remap_debuginfo, remap_debuginfo); - set(&mut self.control_flow_guard, control_flow_guard); - set(&mut self.ehcont_guard, ehcont_guard); - self.llvm_libunwind_default = - llvm_libunwind.map(|v| v.parse().expect("failed to parse rust.llvm-libunwind")); - set( - &mut self.rust_codegen_backends, - codegen_backends.map(|backends| parse_codegen_backends(backends, "rust")), - ); - - self.rust_codegen_units = codegen_units.map(threads_from_config); - self.rust_codegen_units_std = codegen_units_std.map(threads_from_config); - - if self.rust_profile_use.is_none() { - self.rust_profile_use = profile_use; - } - - if self.rust_profile_generate.is_none() { - self.rust_profile_generate = profile_generate; - } - - self.rust_lto = - lto.as_deref().map(|value| RustcLto::from_str(value).unwrap()).unwrap_or_default(); - self.rust_validate_mir_opts = validate_mir_opts; - } - - self.rust_optimize = optimize.unwrap_or(RustOptimize::Bool(true)); - - // We make `x86_64-unknown-linux-gnu` use the self-contained linker by default, so we will - // build our internal lld and use it as the default linker, by setting the `rust.lld` config - // to true by default: - // - on the `x86_64-unknown-linux-gnu` target - // - when building our in-tree llvm (i.e. the target has not set an `llvm-config`), so that - // we're also able to build the corresponding lld - // - or when using an external llvm that's downloaded from CI, which also contains our prebuilt - // lld - // - otherwise, we'd be using an external llvm, and lld would not necessarily available and - // thus, disabled - // - similarly, lld will not be built nor used by default when explicitly asked not to, e.g. - // when the config sets `rust.lld = false` - if default_lld_opt_in_targets().contains(&self.host_target.triple.to_string()) - && self.hosts == [self.host_target] - { - let no_llvm_config = self - .target_config - .get(&self.host_target) - .is_none_or(|target_config| target_config.llvm_config.is_none()); - let enable_lld = self.llvm_from_ci || no_llvm_config; - // Prefer the config setting in case an explicit opt-out is needed. - self.lld_enabled = lld_enabled.unwrap_or(enable_lld); - } else { - set(&mut self.lld_enabled, lld_enabled); - } - - let default_std_features = BTreeSet::from([String::from("panic-unwind")]); - self.rust_std_features = std_features.unwrap_or(default_std_features); - - let default = debug == Some(true); - self.rustc_debug_assertions = rustc_debug_assertions.unwrap_or(default); - self.std_debug_assertions = std_debug_assertions.unwrap_or(self.rustc_debug_assertions); - self.tools_debug_assertions = tools_debug_assertions.unwrap_or(self.rustc_debug_assertions); - self.rust_overflow_checks = overflow_checks.unwrap_or(default); - self.rust_overflow_checks_std = overflow_checks_std.unwrap_or(self.rust_overflow_checks); - - self.rust_debug_logging = debug_logging.unwrap_or(self.rustc_debug_assertions); - - let with_defaults = |debuginfo_level_specific: Option<_>| { - debuginfo_level_specific.or(debuginfo_level).unwrap_or(if debug == Some(true) { - DebuginfoLevel::Limited - } else { - DebuginfoLevel::None - }) - }; - self.rust_debuginfo_level_rustc = with_defaults(debuginfo_level_rustc); - self.rust_debuginfo_level_std = with_defaults(debuginfo_level_std); - self.rust_debuginfo_level_tools = with_defaults(debuginfo_level_tools); - self.rust_debuginfo_level_tests = debuginfo_level_tests.unwrap_or(DebuginfoLevel::None); - } -} diff --git a/src/bootstrap/src/core/config/toml/target.rs b/src/bootstrap/src/core/config/toml/target.rs index 9dedadff3a1..2c06fd083a8 100644 --- a/src/bootstrap/src/core/config/toml/target.rs +++ b/src/bootstrap/src/core/config/toml/target.rs @@ -7,18 +7,12 @@ //! * [`TomlTarget`]: This struct directly mirrors the `[target.<triple>]` sections in your //! `bootstrap.toml`. It's used for deserializing raw TOML data for a specific target. //! * [`Target`]: This struct represents the processed and validated configuration for a -//! build target, which is is stored in the main [`Config`] structure. -//! * [`Config::apply_target_config`]: This method processes the `TomlTarget` data and -//! applies it to the global [`Config`], ensuring proper path resolution, validation, -//! and integration with other build settings. - -use std::collections::HashMap; +//! build target, which is is stored in the main `Config` structure. use serde::{Deserialize, Deserializer}; -use crate::core::config::toml::rust::parse_codegen_backends; use crate::core::config::{LlvmLibunwind, Merge, ReplaceOpt, SplitDebuginfo, StringOrBool}; -use crate::{CodegenBackendKind, Config, HashSet, PathBuf, TargetSelection, define_config, exit}; +use crate::{CodegenBackendKind, HashSet, PathBuf, define_config, exit}; define_config! { /// TOML representation of how each build target is configured. @@ -93,68 +87,3 @@ impl Target { target } } - -impl Config { - pub fn apply_target_config(&mut self, toml_target: Option<HashMap<String, TomlTarget>>) { - if let Some(t) = toml_target { - for (triple, cfg) in t { - let mut target = Target::from_triple(&triple); - - if let Some(ref s) = cfg.llvm_config { - if self.download_rustc_commit.is_some() && triple == *self.host_target.triple { - panic!( - "setting llvm_config for the host is incompatible with download-rustc" - ); - } - target.llvm_config = Some(self.src.join(s)); - } - if let Some(patches) = cfg.llvm_has_rust_patches { - assert!( - self.submodules == Some(false) || cfg.llvm_config.is_some(), - "use of `llvm-has-rust-patches` is restricted to cases where either submodules are disabled or llvm-config been provided" - ); - target.llvm_has_rust_patches = Some(patches); - } - if let Some(ref s) = cfg.llvm_filecheck { - target.llvm_filecheck = Some(self.src.join(s)); - } - target.llvm_libunwind = cfg.llvm_libunwind.as_ref().map(|v| { - v.parse().unwrap_or_else(|_| { - panic!("failed to parse target.{triple}.llvm-libunwind") - }) - }); - if let Some(s) = cfg.no_std { - target.no_std = s; - } - target.cc = cfg.cc.map(PathBuf::from); - target.cxx = cfg.cxx.map(PathBuf::from); - target.ar = cfg.ar.map(PathBuf::from); - target.ranlib = cfg.ranlib.map(PathBuf::from); - target.linker = cfg.linker.map(PathBuf::from); - target.crt_static = cfg.crt_static; - target.musl_root = cfg.musl_root.map(PathBuf::from); - target.musl_libdir = cfg.musl_libdir.map(PathBuf::from); - target.wasi_root = cfg.wasi_root.map(PathBuf::from); - target.qemu_rootfs = cfg.qemu_rootfs.map(PathBuf::from); - target.runner = cfg.runner; - target.sanitizers = cfg.sanitizers; - target.profiler = cfg.profiler; - target.rpath = cfg.rpath; - target.optimized_compiler_builtins = cfg.optimized_compiler_builtins; - target.jemalloc = cfg.jemalloc; - if let Some(backends) = cfg.codegen_backends { - target.codegen_backends = - Some(parse_codegen_backends(backends, &format!("target.{triple}"))) - } - - target.split_debuginfo = cfg.split_debuginfo.as_ref().map(|v| { - v.parse().unwrap_or_else(|_| { - panic!("invalid value for target.{triple}.split-debuginfo") - }) - }); - - self.target_config.insert(TargetSelection::from_user(&triple), target); - } - } - } -} diff --git a/src/bootstrap/src/core/sanity.rs b/src/bootstrap/src/core/sanity.rs index 15e04f59129..3080e641b5b 100644 --- a/src/bootstrap/src/core/sanity.rs +++ b/src/bootstrap/src/core/sanity.rs @@ -33,6 +33,7 @@ pub struct Finder { // // Targets can be removed from this list once they are present in the stage0 compiler (usually by updating the beta compiler of the bootstrap). const STAGE0_MISSING_TARGETS: &[&str] = &[ + "armv7a-vex-v5", // just a dummy comment so the list doesn't get onelined ]; diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index 42192f1d721..4abf386e5de 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -517,7 +517,7 @@ impl Build { local_rebuild: config.local_rebuild, fail_fast: config.cmd.fail_fast(), doc_tests: config.cmd.doc_tests(), - verbosity: config.verbose, + verbosity: config.exec_ctx.verbosity as usize, host_target: config.host_target, hosts: config.hosts.clone(), diff --git a/src/bootstrap/src/utils/exec.rs b/src/bootstrap/src/utils/exec.rs index 209ff393973..03760faec69 100644 --- a/src/bootstrap/src/utils/exec.rs +++ b/src/bootstrap/src/utils/exec.rs @@ -80,11 +80,21 @@ impl CommandFingerprint { /// Helper method to format both Command and BootstrapCommand as a short execution line, /// without all the other details (e.g. environment variables). pub fn format_short_cmd(&self) -> String { - let program = Path::new(&self.program); - let mut line = vec![program.file_name().unwrap().to_str().unwrap().to_owned()]; - line.extend(self.args.iter().map(|arg| arg.to_string_lossy().into_owned())); - line.extend(self.cwd.iter().map(|p| p.to_string_lossy().into_owned())); - line.join(" ") + use std::fmt::Write; + + let mut cmd = self.program.to_string_lossy().to_string(); + for arg in &self.args { + let arg = arg.to_string_lossy(); + if arg.contains(' ') { + write!(cmd, " '{arg}'").unwrap(); + } else { + write!(cmd, " {arg}").unwrap(); + } + } + if let Some(cwd) = &self.cwd { + write!(cmd, " [workdir={}]", cwd.to_string_lossy()).unwrap(); + } + cmd } } @@ -434,8 +444,8 @@ impl From<Command> for BootstrapCommand { enum CommandStatus { /// The command has started and finished with some status. Finished(ExitStatus), - /// It was not even possible to start the command. - DidNotStart, + /// It was not even possible to start the command or wait for it to finish. + DidNotStartOrFinish, } /// Create a new BootstrapCommand. This is a helper function to make command creation @@ -456,9 +466,9 @@ pub struct CommandOutput { impl CommandOutput { #[must_use] - pub fn did_not_start(stdout: OutputMode, stderr: OutputMode) -> Self { + pub fn not_finished(stdout: OutputMode, stderr: OutputMode) -> Self { Self { - status: CommandStatus::DidNotStart, + status: CommandStatus::DidNotStartOrFinish, stdout: match stdout { OutputMode::Print => None, OutputMode::Capture => Some(vec![]), @@ -489,7 +499,7 @@ impl CommandOutput { pub fn is_success(&self) -> bool { match self.status { CommandStatus::Finished(status) => status.success(), - CommandStatus::DidNotStart => false, + CommandStatus::DidNotStartOrFinish => false, } } @@ -501,7 +511,7 @@ impl CommandOutput { pub fn status(&self) -> Option<ExitStatus> { match self.status { CommandStatus::Finished(status) => Some(status), - CommandStatus::DidNotStart => None, + CommandStatus::DidNotStartOrFinish => None, } } @@ -550,7 +560,7 @@ impl Default for CommandOutput { #[derive(Clone, Default)] pub struct ExecutionContext { dry_run: DryRun, - verbose: u8, + pub verbosity: u8, pub fail_fast: bool, delayed_failures: Arc<Mutex<Vec<String>>>, command_cache: Arc<CommandCache>, @@ -603,8 +613,8 @@ impl CommandCache { } impl ExecutionContext { - pub fn new() -> Self { - ExecutionContext::default() + pub fn new(verbosity: u8, fail_fast: bool) -> Self { + Self { verbosity, fail_fast, ..Default::default() } } pub fn dry_run(&self) -> bool { @@ -629,7 +639,7 @@ impl ExecutionContext { } pub fn is_verbose(&self) -> bool { - self.verbose > 0 + self.verbosity > 0 } pub fn fail_fast(&self) -> bool { @@ -640,8 +650,8 @@ impl ExecutionContext { self.dry_run = value; } - pub fn set_verbose(&mut self, value: u8) { - self.verbose = value; + pub fn set_verbosity(&mut self, value: u8) { + self.verbosity = value; } pub fn set_fail_fast(&mut self, value: bool) { @@ -745,25 +755,11 @@ impl ExecutionContext { self.start(command, stdout, stderr).wait_for_output(self) } - fn fail(&self, message: &str, output: CommandOutput) -> ! { - if self.is_verbose() { - println!("{message}"); - } else { - let (stdout, stderr) = (output.stdout_if_present(), output.stderr_if_present()); - // If the command captures output, the user would not see any indication that - // it has failed. In this case, print a more verbose error, since to provide more - // context. - if stdout.is_some() || stderr.is_some() { - if let Some(stdout) = output.stdout_if_present().take_if(|s| !s.trim().is_empty()) { - println!("STDOUT:\n{stdout}\n"); - } - if let Some(stderr) = output.stderr_if_present().take_if(|s| !s.trim().is_empty()) { - println!("STDERR:\n{stderr}\n"); - } - println!("Command has failed. Rerun with -v to see more details."); - } else { - println!("Command has failed. Rerun with -v to see more details."); - } + fn fail(&self, message: &str) -> ! { + println!("{message}"); + + if !self.is_verbose() { + println!("Command has failed. Rerun with -v to see more details."); } exit!(1); } @@ -856,7 +852,7 @@ impl<'a> DeferredCommand<'a> { && command.should_cache { exec_ctx.command_cache.insert(fingerprint.clone(), output.clone()); - exec_ctx.profiler.record_execution(fingerprint.clone(), start_time); + exec_ctx.profiler.record_execution(fingerprint, start_time); } output @@ -872,6 +868,8 @@ impl<'a> DeferredCommand<'a> { executed_at: &'a std::panic::Location<'a>, exec_ctx: &ExecutionContext, ) -> CommandOutput { + use std::fmt::Write; + command.mark_as_executed(); let process = match process.take() { @@ -881,79 +879,82 @@ impl<'a> DeferredCommand<'a> { let created_at = command.get_created_location(); - let mut message = String::new(); + #[allow(clippy::enum_variant_names)] + enum FailureReason { + FailedAtRuntime(ExitStatus), + FailedToFinish(std::io::Error), + FailedToStart(std::io::Error), + } - let output = match process { + let (output, fail_reason) = match process { Ok(child) => match child.wait_with_output() { - Ok(result) if result.status.success() => { + Ok(output) if output.status.success() => { // Successful execution - CommandOutput::from_output(result, stdout, stderr) + (CommandOutput::from_output(output, stdout, stderr), None) } - Ok(result) => { - // Command ran but failed - use std::fmt::Write; - - writeln!( - message, - r#" -Command {command:?} did not execute successfully. -Expected success, got {} -Created at: {created_at} -Executed at: {executed_at}"#, - result.status, + Ok(output) => { + // Command started, but then it failed + let status = output.status; + ( + CommandOutput::from_output(output, stdout, stderr), + Some(FailureReason::FailedAtRuntime(status)), ) - .unwrap(); - - let output = CommandOutput::from_output(result, stdout, stderr); - - if stdout.captures() { - writeln!(message, "\nSTDOUT ----\n{}", output.stdout().trim()).unwrap(); - } - if stderr.captures() { - writeln!(message, "\nSTDERR ----\n{}", output.stderr().trim()).unwrap(); - } - - output } Err(e) => { // Failed to wait for output - use std::fmt::Write; - - writeln!( - message, - "\n\nCommand {command:?} did not execute successfully.\ - \nIt was not possible to execute the command: {e:?}" + ( + CommandOutput::not_finished(stdout, stderr), + Some(FailureReason::FailedToFinish(e)), ) - .unwrap(); - - CommandOutput::did_not_start(stdout, stderr) } }, Err(e) => { // Failed to spawn the command - use std::fmt::Write; - - writeln!( - message, - "\n\nCommand {command:?} did not execute successfully.\ - \nIt was not possible to execute the command: {e:?}" - ) - .unwrap(); - - CommandOutput::did_not_start(stdout, stderr) + (CommandOutput::not_finished(stdout, stderr), Some(FailureReason::FailedToStart(e))) } }; - if !output.is_success() { + if let Some(fail_reason) = fail_reason { + let mut error_message = String::new(); + let command_str = if exec_ctx.is_verbose() { + format!("{command:?}") + } else { + command.fingerprint().format_short_cmd() + }; + let action = match fail_reason { + FailureReason::FailedAtRuntime(e) => { + format!("failed with exit code {}", e.code().unwrap_or(1)) + } + FailureReason::FailedToFinish(e) => { + format!("failed to finish: {e:?}") + } + FailureReason::FailedToStart(e) => { + format!("failed to start: {e:?}") + } + }; + writeln!( + error_message, + r#"Command `{command_str}` {action} +Created at: {created_at} +Executed at: {executed_at}"#, + ) + .unwrap(); + if stdout.captures() { + writeln!(error_message, "\n--- STDOUT vvv\n{}", output.stdout().trim()).unwrap(); + } + if stderr.captures() { + writeln!(error_message, "\n--- STDERR vvv\n{}", output.stderr().trim()).unwrap(); + } + match command.failure_behavior { BehaviorOnFailure::DelayFail => { if exec_ctx.fail_fast { - exec_ctx.fail(&message, output); + exec_ctx.fail(&error_message); } - exec_ctx.add_to_delay_failure(message); + exec_ctx.add_to_delay_failure(error_message); } BehaviorOnFailure::Exit => { - exec_ctx.fail(&message, output); + exec_ctx.fail(&error_message); } BehaviorOnFailure::Ignore => { // If failures are allowed, either the error has been printed already diff --git a/src/doc/rustc/src/SUMMARY.md b/src/doc/rustc/src/SUMMARY.md index 7c688e32bc0..25f154f1180 100644 --- a/src/doc/rustc/src/SUMMARY.md +++ b/src/doc/rustc/src/SUMMARY.md @@ -65,6 +65,7 @@ - [armv7-sony-vita-newlibeabihf](platform-support/armv7-sony-vita-newlibeabihf.md) - [armv7-unknown-linux-uclibceabi](platform-support/armv7-unknown-linux-uclibceabi.md) - [armv7-unknown-linux-uclibceabihf](platform-support/armv7-unknown-linux-uclibceabihf.md) + - [armv7a-vex-v5](platform-support/armv7a-vex-v5.md) - [\*-android and \*-androideabi](platform-support/android.md) - [\*-linux-ohos](platform-support/openharmony.md) - [\*-hurd-gnu](platform-support/hurd.md) diff --git a/src/doc/rustc/src/platform-support.md b/src/doc/rustc/src/platform-support.md index 65b70630153..8ebaa8dd874 100644 --- a/src/doc/rustc/src/platform-support.md +++ b/src/doc/rustc/src/platform-support.md @@ -297,6 +297,7 @@ target | std | host | notes [`armv7a-kmc-solid_asp3-eabi`](platform-support/kmc-solid.md) | ✓ | | ARM SOLID with TOPPERS/ASP3 [`armv7a-kmc-solid_asp3-eabihf`](platform-support/kmc-solid.md) | ✓ | | ARM SOLID with TOPPERS/ASP3, hardfloat [`armv7a-none-eabihf`](platform-support/arm-none-eabi.md) | * | | Bare Armv7-A, hardfloat +[`armv7a-vex-v5`](platform-support/armv7a-vex-v5.md) | ? | | Armv7-A Cortex-A9 VEX V5 Brain, VEXos [`armv7k-apple-watchos`](platform-support/apple-watchos.md) | ✓ | | Armv7-A Apple WatchOS [`armv7s-apple-ios`](platform-support/apple-ios.md) | ✓ | | Armv7-A Apple-A6 Apple iOS [`armv8r-none-eabihf`](platform-support/armv8r-none-eabihf.md) | * | | Bare Armv8-R, hardfloat diff --git a/src/doc/rustc/src/platform-support/apple-ios-macabi.md b/src/doc/rustc/src/platform-support/apple-ios-macabi.md index d4b71dbd4f4..c6f68f7a1e8 100644 --- a/src/doc/rustc/src/platform-support/apple-ios-macabi.md +++ b/src/doc/rustc/src/platform-support/apple-ios-macabi.md @@ -56,6 +56,17 @@ Rust programs can be built for these targets by specifying `--target`, if $ rustc --target aarch64-apple-ios-macabi your-code.rs ``` +The target can be differentiated from the iOS targets with the +`target_env = "macabi"` cfg (or `target_abi = "macabi"` before Rust CURRENT_RUSTC_VERSION). + +```rust +if cfg!(target_env = "macabi") { + // Do something only on Mac Catalyst. +} +``` + +This is similar to the `TARGET_OS_MACCATALYST` define in C code. + ## Testing Mac Catalyst binaries can be run directly on macOS 10.15 Catalina or newer. diff --git a/src/doc/rustc/src/platform-support/apple-ios.md b/src/doc/rustc/src/platform-support/apple-ios.md index 64325554ab6..586afa65226 100644 --- a/src/doc/rustc/src/platform-support/apple-ios.md +++ b/src/doc/rustc/src/platform-support/apple-ios.md @@ -66,6 +66,20 @@ Rust programs can be built for these targets by specifying `--target`, if $ rustc --target aarch64-apple-ios your-code.rs ``` +The simulator variants can be differentiated from the variants running +on-device with the `target_env = "sim"` cfg (or `target_abi = "sim"` before +Rust CURRENT_RUSTC_VERSION). + +```rust +if cfg!(all(target_vendor = "apple", target_env = "sim")) { + // Do something on the iOS/tvOS/visionOS/watchOS Simulator. +} { + // Everything else, like Windows and non-Simulator iOS. +} +``` + +This is similar to the `TARGET_OS_SIMULATOR` define in C code. + ## Testing There is no support for running the Rust or standard library testsuite at the diff --git a/src/doc/rustc/src/platform-support/armv7a-vex-v5.md b/src/doc/rustc/src/platform-support/armv7a-vex-v5.md new file mode 100644 index 00000000000..a7da1b16f7e --- /dev/null +++ b/src/doc/rustc/src/platform-support/armv7a-vex-v5.md @@ -0,0 +1,83 @@ +# `armv7a-vex-v5` + +**Tier: 3** + +Allows compiling user programs for the [VEX V5 Brain](https://www.vexrobotics.com/276-4810.html), a microcontroller for educational and competitive robotics. + +Rust support for this target is not affiliated with VEX Robotics or IFI. + +## Target maintainers + +This target is maintained by members of the [vexide](https://github.com/vexide) organization: + +- [@lewisfm](https://github.com/lewisfm) +- [@Tropix126](https://github.com/Tropix126) +- [@Gavin-Niederman](https://github.com/Gavin-Niederman) +- [@max-niederman](https://github.com/max-niederman) + +## Requirements + +This target is cross-compiled and currently requires `#![no_std]`. Dynamic linking is unsupported. + +When compiling for this target, the "C" calling convention maps to AAPCS with VFP registers (hard float ABI) and the "system" calling convention maps to AAPCS without VFP registers (soft float ABI). + +This target generates binaries in the ELF format that may uploaded to the brain with external tools. + +## Building the target + +You can build Rust with support for this target by adding it to the `target` list in `bootstrap.toml`, and then running `./x build --target armv7a-vex-v5 compiler`. + +## Building Rust programs + +Rust does not yet ship pre-compiled artifacts for this target. To compile for +this target, you will either need to build Rust with the target enabled (see +"Building the target" above), or build your own copy of `core` by using +`build-std` or similar. + +When the compiler builds a binary, an ELF build artifact will be produced. Additional tools are required for this artifact to be recognizable to VEXos as a user program. + +The [cargo-v5](https://github.com/vexide/cargo-v5) tool is capable of creating binaries that can be uploaded to the V5 brain. This tool wraps the `cargo build` command by supplying arguments necessary to build the target and produce an artifact recognizable to VEXos, while also providing functionality for uploading over USB to a V5 Controller or Brain. + +To install the tool, run: + +```sh +cargo install cargo-v5 +``` + +The following fields in your project's `Cargo.toml` are read by `cargo-v5` to configure upload behavior: + +```toml +[package.metadata.v5] +# Slot number to upload the user program to. This should be from 1-8. +slot = 1 +# Program icon/thumbnail that will be displayed on the dashboard. +icon = "cool-x" +# Use gzip compression when uploading binaries. +compress = true +``` + +To build an uploadable BIN file using the release profile, run: + +```sh +cargo v5 build --release +``` + +Programs can also be directly uploaded to the brain over a USB connection immediately after building: + +```sh +cargo v5 upload --release +``` + +## Testing + +Binaries built for this target can be run in an emulator (such as [vex-v5-qemu](https://github.com/vexide/vex-v5-qemu)), or uploaded to a physical device over a serial (USB) connection. + +The default Rust test runner is not supported. + +The Rust test suite for `library/std` is not yet supported. + +## Cross-compilation toolchains and C code + +This target can be cross-compiled from any host. + +Linking to C libraries is not supported. diff --git a/src/doc/unstable-book/src/compiler-environment-variables/COLORTERM.md b/src/doc/unstable-book/src/compiler-environment-variables/COLORTERM.md new file mode 100644 index 00000000000..f5be63b796c --- /dev/null +++ b/src/doc/unstable-book/src/compiler-environment-variables/COLORTERM.md @@ -0,0 +1,5 @@ +# `COLORTERM` + +This environment variable is used by [`-Zterminal-urls`] to detect if URLs are supported by the terminal emulator. + +[`-Zterminal-urls`]: ../compiler-flags/terminal-urls.html diff --git a/src/doc/unstable-book/src/compiler-environment-variables/QNX_TARGET.md b/src/doc/unstable-book/src/compiler-environment-variables/QNX_TARGET.md new file mode 100644 index 00000000000..46511598e70 --- /dev/null +++ b/src/doc/unstable-book/src/compiler-environment-variables/QNX_TARGET.md @@ -0,0 +1,11 @@ +# `QNX_TARGET` + +---- + +This environment variable is mandatory when linking on `nto-qnx*_iosock` platforms. It is used to determine an `-L` path to pass to the QNX linker. + +You should [set this variable] by running `source qnxsdp-env.sh`. +See [the QNX docs] for more background information. + +[set this variable]: https://www.qnx.com/developers/docs/qsc/com.qnx.doc.qsc.inst_larg_org/topic/build_server_developer_steps.html +[the QNX docs]: https://www.qnx.com/developers/docs/7.1/#com.qnx.doc.neutrino.io_sock/topic/migrate_app.html. diff --git a/src/doc/unstable-book/src/compiler-environment-variables/SDKROOT.md b/src/doc/unstable-book/src/compiler-environment-variables/SDKROOT.md new file mode 100644 index 00000000000..be9ed02f54d --- /dev/null +++ b/src/doc/unstable-book/src/compiler-environment-variables/SDKROOT.md @@ -0,0 +1,6 @@ +# `SDKROOT` + +This environment variable is used on Apple targets. +It is passed through to the linker (currently either as `-isysroot` or `-syslibroot`). + +Note that this variable is not always respected. When the SDKROOT is clearly wrong (e.g. when the platform of the SDK does not match the `--target` used by rustc), this is ignored and rustc does its own detection. diff --git a/src/doc/unstable-book/src/compiler-environment-variables/TERM.md b/src/doc/unstable-book/src/compiler-environment-variables/TERM.md new file mode 100644 index 00000000000..9208fd378a3 --- /dev/null +++ b/src/doc/unstable-book/src/compiler-environment-variables/TERM.md @@ -0,0 +1,5 @@ +# `TERM` + +This environment variable is used by [`-Zterminal-urls`] to detect if URLs are supported by the terminal emulator. + +[`-Zterminal-urls`]: ../compiler-flags/terminal-urls.html diff --git a/src/doc/unstable-book/src/compiler-flags/coverage-options.md b/src/doc/unstable-book/src/compiler-flags/coverage-options.md index 87a9a00b3c0..693a0a58b49 100644 --- a/src/doc/unstable-book/src/compiler-flags/coverage-options.md +++ b/src/doc/unstable-book/src/compiler-flags/coverage-options.md @@ -5,7 +5,7 @@ This option controls details of the coverage instrumentation performed by Multiple options can be passed, separated by commas. Valid options are: -- `block`, `branch`, `condition`, `mcdc`: +- `block`, `branch`, `condition`: Sets the level of coverage instrumentation. Setting the level will override any previously-specified level. - `block` (default): @@ -15,6 +15,3 @@ Multiple options can be passed, separated by commas. Valid options are: - `condition`: In addition to branch coverage, also instruments some boolean expressions as branches, even if they are not directly used as branch conditions. - - `mcdc`: - In addition to condition coverage, also enables MC/DC instrumentation. - (Branch coverage instrumentation may differ in some cases.) diff --git a/src/doc/unstable-book/src/compiler-flags/terminal-urls.md b/src/doc/unstable-book/src/compiler-flags/terminal-urls.md new file mode 100644 index 00000000000..a5427978f25 --- /dev/null +++ b/src/doc/unstable-book/src/compiler-flags/terminal-urls.md @@ -0,0 +1,13 @@ +# `-Z terminal-urls` + +The tracking feature for this issue is [#125586] + +[#125586]: https://github.com/rust-lang/rust/issues/125586 + +--- + +This flag takes either a boolean or the string "auto". + +When enabled, use the OSC 8 hyperlink terminal specification to print hyperlinks in the compiler output. +Use "auto" to try and autodetect whether the terminal emulator supports hyperlinks. +Currently, "auto" only enables hyperlinks if `COLORTERM=truecolor` and `TERM=xterm-256color`. diff --git a/src/doc/unstable-book/src/language-features/asm-experimental-arch.md b/src/doc/unstable-book/src/language-features/asm-experimental-arch.md index 121f9493435..d9566c9f55c 100644 --- a/src/doc/unstable-book/src/language-features/asm-experimental-arch.md +++ b/src/doc/unstable-book/src/language-features/asm-experimental-arch.md @@ -19,7 +19,6 @@ This feature tracks `asm!` and `global_asm!` support for the following architect - M68k - CSKY - SPARC -- LoongArch32 ## Register classes @@ -54,8 +53,6 @@ This feature tracks `asm!` and `global_asm!` support for the following architect | CSKY | `freg` | `f[0-31]` | `f` | | SPARC | `reg` | `r[2-29]` | `r` | | SPARC | `yreg` | `y` | Only clobbers | -| LoongArch32 | `reg` | `$r1`, `$r[4-20]`, `$r[23,30]` | `r` | -| LoongArch32 | `freg` | `$f[0-31]` | `f` | > **Notes**: > - NVPTX doesn't have a fixed register set, so named registers are not supported. @@ -94,8 +91,6 @@ This feature tracks `asm!` and `global_asm!` support for the following architect | CSKY | `freg` | None | `f32`, | | SPARC | `reg` | None | `i8`, `i16`, `i32`, `i64` (SPARC64 only) | | SPARC | `yreg` | N/A | Only clobbers | -| LoongArch32 | `reg` | None | `i8`, `i16`, `i32`, `f32` | -| LoongArch32 | `freg` | None | `f32`, `f64` | ## Register aliases diff --git a/src/doc/unstable-book/src/library-features/duration-constructors-lite.md b/src/doc/unstable-book/src/library-features/duration-constructors-lite.md deleted file mode 100644 index 5238b84f776..00000000000 --- a/src/doc/unstable-book/src/library-features/duration-constructors-lite.md +++ /dev/null @@ -1,11 +0,0 @@ -# `duration_constructors_lite` - -The tracking issue for this feature is: [#140881] - -[#140881]: https://github.com/rust-lang/rust/issues/140881 - ------------------------- - -Add the methods `from_mins`, `from_hours` to `Duration`. - -For `from_days` and `from_weeks` see [`duration_constructors`](https://github.com/rust-lang/rust/issues/120301). diff --git a/src/doc/unstable-book/src/library-features/duration-constructors.md b/src/doc/unstable-book/src/library-features/duration-constructors.md index 49ad78d1961..2626e0500b5 100644 --- a/src/doc/unstable-book/src/library-features/duration-constructors.md +++ b/src/doc/unstable-book/src/library-features/duration-constructors.md @@ -7,4 +7,3 @@ The tracking issue for this feature is: [#120301] ------------------------ Add the methods `from_days` and `from_weeks` to `Duration`. -For `from_mins` and `from_hours` see [duration-constructors-lite.md](./duration-constructors-lite.md) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 743ed2b5045..7194c2fcede 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1662,7 +1662,7 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type } let trait_segments = &p.segments[..p.segments.len() - 1]; - let trait_def = cx.tcx.associated_item(p.res.def_id()).container_id(cx.tcx); + let trait_def = cx.tcx.parent(p.res.def_id()); let trait_ = self::Path { res: Res::Def(DefKind::Trait, trait_def), segments: trait_segments.iter().map(|x| clean_path_segment(x, cx)).collect(), diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 35ace656638..73ce62cdcde 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -409,9 +409,7 @@ pub(crate) fn run_tests( // We ensure temp dir destructor is called. std::mem::drop(temp_dir); times.display_times(); - // FIXME(GuillaumeGomez): Uncomment the next line once #144297 has been merged. - // std::process::exit(test::ERROR_EXIT_CODE); - std::process::exit(101); + std::process::exit(test::ERROR_EXIT_CODE); } } diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 80399cf3842..918b292466d 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -602,6 +602,7 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It search_type, aliases, deprecation, + is_unstable: item.stability(tcx).map(|x| x.is_unstable()).unwrap_or(false), }; cache.search_index.push(index_item); } diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index a46253237db..95259847075 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -139,6 +139,7 @@ pub(crate) struct IndexItem { pub(crate) search_type: Option<IndexItemFunctionType>, pub(crate) aliases: Box<[Symbol]>, pub(crate) deprecation: Option<Deprecation>, + pub(crate) is_unstable: bool, } /// A type used for the search index. diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index e2f86b8a854..93c9df8c049 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -93,6 +93,7 @@ pub(crate) fn build_index( ), aliases: item.attrs.get_doc_aliases(), deprecation: item.deprecation(tcx), + is_unstable: item.stability(tcx).is_some_and(|x| x.is_unstable()), }); } } @@ -655,6 +656,7 @@ pub(crate) fn build_index( let mut parents_backref_queue = VecDeque::new(); let mut functions = String::with_capacity(self.items.len()); let mut deprecated = Vec::with_capacity(self.items.len()); + let mut unstable = Vec::with_capacity(self.items.len()); let mut type_backref_queue = VecDeque::new(); @@ -711,6 +713,9 @@ pub(crate) fn build_index( // bitmasks always use 1-indexing for items, with 0 as the crate itself deprecated.push(u32::try_from(index + 1).unwrap()); } + if item.is_unstable { + unstable.push(u32::try_from(index + 1).unwrap()); + } } for (index, path) in &revert_extra_paths { @@ -749,6 +754,7 @@ pub(crate) fn build_index( crate_data.serialize_field("r", &re_exports)?; crate_data.serialize_field("b", &self.associated_item_disambiguators)?; crate_data.serialize_field("c", &bitmap_to_string(&deprecated))?; + crate_data.serialize_field("u", &bitmap_to_string(&unstable))?; crate_data.serialize_field("e", &bitmap_to_string(&self.empty_desc))?; crate_data.serialize_field("P", ¶m_names)?; if has_aliases { diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index a9589764547..b082b65ab57 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -449,6 +449,8 @@ declare namespace rustdoc { * of `p`) but is used for modules items like free functions. * * `c` is an array of item indices that are deprecated. + * + * `u` is an array of item indices that are unstable. */ type RawSearchIndexCrate = { doc: string, @@ -463,6 +465,7 @@ declare namespace rustdoc { p: Array<[number, string] | [number, string, number] | [number, string, number, number] | [number, string, number, number, string]>, b: Array<[number, String]>, c: string, + u: string, r: Array<[number, number]>, P: Array<[number, string]>, }; diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 2caf214ff73..10e01b4e262 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -1465,6 +1465,11 @@ class DocSearch { */ this.searchIndexEmptyDesc = new Map(); /** + * @type {Map<String, RoaringBitmap>} + */ + this.searchIndexUnstable = new Map(); + + /** * @type {Uint32Array} */ this.functionTypeFingerprint = new Uint32Array(0); @@ -2052,9 +2057,10 @@ class DocSearch { }; const descShardList = [descShard]; - // Deprecated items and items with no description + // Deprecated and unstable items and items with no description this.searchIndexDeprecated.set(crate, new RoaringBitmap(crateCorpus.c)); this.searchIndexEmptyDesc.set(crate, new RoaringBitmap(crateCorpus.e)); + this.searchIndexUnstable.set(crate, new RoaringBitmap(crateCorpus.u)); let descIndex = 0; /** @@ -3326,6 +3332,25 @@ class DocSearch { return a - b; } + // sort unstable items later + // FIXME: there is some doubt if this is the most effecient way to implement this. + // alternative options include: + // * put is_unstable on each item when the index is built. + // increases memory usage but avoids a hashmap lookup. + // * put is_unstable on each item before sorting. + // better worst case performance but worse average case performance. + a = Number( + // @ts-expect-error + this.searchIndexUnstable.get(aaa.item.crate).contains(aaa.item.bitIndex), + ); + b = Number( + // @ts-expect-error + this.searchIndexUnstable.get(bbb.item.crate).contains(bbb.item.bitIndex), + ); + if (a !== b) { + return a - b; + } + // sort by crate (current crate comes first) a = Number(aaa.item.crate !== preferredCrate); b = Number(bbb.item.crate !== preferredCrate); @@ -3340,6 +3365,13 @@ class DocSearch { return a - b; } + // sort doc alias items later + a = Number(aaa.item.is_alias === true); + b = Number(bbb.item.is_alias === true); + if (a !== b) { + return a - b; + } + // sort by item name (lexicographically larger goes later) let aw = aaa.word; let bw = bbb.word; diff --git a/src/tools/clippy/clippy_lints/src/single_component_path_imports.rs b/src/tools/clippy/clippy_lints/src/single_component_path_imports.rs index 38cf7e3822a..07a8eb5d886 100644 --- a/src/tools/clippy/clippy_lints/src/single_component_path_imports.rs +++ b/src/tools/clippy/clippy_lints/src/single_component_path_imports.rs @@ -1,6 +1,5 @@ use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg}; use rustc_ast::node_id::{NodeId, NodeMap}; -use rustc_ast::ptr::P; use rustc_ast::visit::{Visitor, walk_expr}; use rustc_ast::{Crate, Expr, ExprKind, Item, ItemKind, MacroDef, ModKind, Ty, TyKind, UseTreeKind}; use rustc_errors::Applicability; @@ -124,7 +123,7 @@ impl Visitor<'_> for ImportUsageVisitor { } impl SingleComponentPathImports { - fn check_mod(&mut self, items: &[P<Item>]) { + fn check_mod(&mut self, items: &[Box<Item>]) { // keep track of imports reused with `self` keyword, such as `self::crypto_hash` in the example // below. Removing the `use crypto_hash;` would make this a compile error // ``` diff --git a/src/tools/clippy/clippy_lints/src/unnested_or_patterns.rs b/src/tools/clippy/clippy_lints/src/unnested_or_patterns.rs index bd8420917f5..e9ad578da2f 100644 --- a/src/tools/clippy/clippy_lints/src/unnested_or_patterns.rs +++ b/src/tools/clippy/clippy_lints/src/unnested_or_patterns.rs @@ -7,13 +7,14 @@ use clippy_utils::msrvs::{self, MsrvStack}; use clippy_utils::over; use rustc_ast::PatKind::*; use rustc_ast::mut_visit::*; -use rustc_ast::ptr::P; use rustc_ast::{self as ast, DUMMY_NODE_ID, Mutability, Pat, PatKind}; use rustc_ast_pretty::pprust; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::impl_lint_pass; use rustc_span::DUMMY_SP; +// import needed to shadow `PatKind::Box` glob-imported above +use std::boxed::Box; use std::cell::Cell; use std::mem; use thin_vec::{ThinVec, thin_vec}; @@ -97,7 +98,7 @@ fn lint_unnested_or_patterns(cx: &EarlyContext<'_>, pat: &Pat) { return; } - let mut pat = P(pat.clone()); + let mut pat = Box::new(pat.clone()); // Nix all the paren patterns everywhere so that they aren't in our way. remove_all_parens(&mut pat); @@ -119,7 +120,7 @@ fn lint_unnested_or_patterns(cx: &EarlyContext<'_>, pat: &Pat) { } /// Remove all `(p)` patterns in `pat`. -fn remove_all_parens(pat: &mut P<Pat>) { +fn remove_all_parens(pat: &mut Box<Pat>) { #[derive(Default)] struct Visitor { /// If is not in the outer most pattern. This is needed to avoid removing the outermost @@ -142,7 +143,7 @@ fn remove_all_parens(pat: &mut P<Pat>) { } /// Insert parens where necessary according to Rust's precedence rules for patterns. -fn insert_necessary_parens(pat: &mut P<Pat>) { +fn insert_necessary_parens(pat: &mut Box<Pat>) { struct Visitor; impl MutVisitor for Visitor { fn visit_pat(&mut self, pat: &mut Pat) { @@ -154,7 +155,7 @@ fn insert_necessary_parens(pat: &mut P<Pat>) { Ref(p, Mutability::Not) if matches!(p.kind, Ident(BindingMode::MUT, ..)) => p, // `&(mut x)` _ => return, }; - target.kind = Paren(P(take_pat(target))); + target.kind = Paren(Box::new(take_pat(target))); } } Visitor.visit_pat(pat); @@ -162,7 +163,7 @@ fn insert_necessary_parens(pat: &mut P<Pat>) { /// Unnest or-patterns `p0 | ... | p1` in the pattern `pat`. /// For example, this would transform `Some(0) | FOO | Some(2)` into `Some(0 | 2) | FOO`. -fn unnest_or_patterns(pat: &mut P<Pat>) -> bool { +fn unnest_or_patterns(pat: &mut Box<Pat>) -> bool { struct Visitor { changed: bool, } @@ -222,7 +223,7 @@ macro_rules! always_pat { /// Focus on `focus_idx` in `alternatives`, /// attempting to extend it with elements of the same constructor `C` /// in `alternatives[focus_idx + 1..]`. -fn transform_with_focus_on_idx(alternatives: &mut ThinVec<P<Pat>>, focus_idx: usize) -> bool { +fn transform_with_focus_on_idx(alternatives: &mut ThinVec<Box<Pat>>, focus_idx: usize) -> bool { // Extract the kind; we'll need to make some changes in it. let mut focus_kind = mem::replace(&mut alternatives[focus_idx].kind, Wild); // We'll focus on `alternatives[focus_idx]`, @@ -303,12 +304,12 @@ fn transform_with_focus_on_idx(alternatives: &mut ThinVec<P<Pat>>, focus_idx: us /// So when we fixate on some `ident_k: pat_k`, we try to find `ident_k` in the other pattern /// and check that all `fp_i` where `i ∈ ((0...n) \ k)` between two patterns are equal. fn extend_with_struct_pat( - qself1: Option<&P<ast::QSelf>>, + qself1: Option<&Box<ast::QSelf>>, path1: &ast::Path, fps1: &mut [ast::PatField], rest1: ast::PatFieldsRest, start: usize, - alternatives: &mut ThinVec<P<Pat>>, + alternatives: &mut ThinVec<Box<Pat>>, ) -> bool { (0..fps1.len()).any(|idx| { let pos_in_2 = Cell::new(None); // The element `k`. @@ -346,11 +347,11 @@ fn extend_with_struct_pat( /// while also requiring `ps1[..n] ~ ps2[..n]` (pre) and `ps1[n + 1..] ~ ps2[n + 1..]` (post), /// where `~` denotes semantic equality. fn extend_with_matching_product( - targets: &mut [P<Pat>], + targets: &mut [Box<Pat>], start: usize, - alternatives: &mut ThinVec<P<Pat>>, - predicate: impl Fn(&PatKind, &[P<Pat>], usize) -> bool, - extract: impl Fn(PatKind) -> ThinVec<P<Pat>>, + alternatives: &mut ThinVec<Box<Pat>>, + predicate: impl Fn(&PatKind, &[Box<Pat>], usize) -> bool, + extract: impl Fn(PatKind) -> ThinVec<Box<Pat>>, ) -> bool { (0..targets.len()).any(|idx| { let tail_or = drain_matching( @@ -377,14 +378,14 @@ fn take_pat(from: &mut Pat) -> Pat { /// Extend `target` as an or-pattern with the alternatives /// in `tail_or` if there are any and return if there were. -fn extend_with_tail_or(target: &mut Pat, tail_or: ThinVec<P<Pat>>) -> bool { - fn extend(target: &mut Pat, mut tail_or: ThinVec<P<Pat>>) { +fn extend_with_tail_or(target: &mut Pat, tail_or: ThinVec<Box<Pat>>) -> bool { + fn extend(target: &mut Pat, mut tail_or: ThinVec<Box<Pat>>) { match target { // On an existing or-pattern in the target, append to it. Pat { kind: Or(ps), .. } => ps.append(&mut tail_or), // Otherwise convert the target to an or-pattern. target => { - let mut init_or = thin_vec![P(take_pat(target))]; + let mut init_or = thin_vec![Box::new(take_pat(target))]; init_or.append(&mut tail_or); target.kind = Or(init_or); }, @@ -403,10 +404,10 @@ fn extend_with_tail_or(target: &mut Pat, tail_or: ThinVec<P<Pat>>) -> bool { // Only elements beginning with `start` are considered for extraction. fn drain_matching( start: usize, - alternatives: &mut ThinVec<P<Pat>>, + alternatives: &mut ThinVec<Box<Pat>>, predicate: impl Fn(&PatKind) -> bool, - extract: impl Fn(PatKind) -> P<Pat>, -) -> ThinVec<P<Pat>> { + extract: impl Fn(PatKind) -> Box<Pat>, +) -> ThinVec<Box<Pat>> { let mut tail_or = ThinVec::new(); let mut idx = 0; @@ -438,15 +439,15 @@ fn drain_matching( fn extend_with_matching( target: &mut Pat, start: usize, - alternatives: &mut ThinVec<P<Pat>>, + alternatives: &mut ThinVec<Box<Pat>>, predicate: impl Fn(&PatKind) -> bool, - extract: impl Fn(PatKind) -> P<Pat>, + extract: impl Fn(PatKind) -> Box<Pat>, ) -> bool { extend_with_tail_or(target, drain_matching(start, alternatives, predicate, extract)) } /// Are the patterns in `ps1` and `ps2` equal save for `ps1[idx]` compared to `ps2[idx]`? -fn eq_pre_post(ps1: &[P<Pat>], ps2: &[P<Pat>], idx: usize) -> bool { +fn eq_pre_post(ps1: &[Box<Pat>], ps2: &[Box<Pat>], idx: usize) -> bool { ps1.len() == ps2.len() && ps1[idx].is_rest() == ps2[idx].is_rest() // Avoid `[x, ..] | [x, 0]` => `[x, .. | 0]`. && over(&ps1[..idx], &ps2[..idx], |l, r| eq_pat(l, r)) diff --git a/src/tools/clippy/clippy_utils/src/ast_utils/mod.rs b/src/tools/clippy/clippy_utils/src/ast_utils/mod.rs index 96f0273c439..0312bf56e59 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils/mod.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils/mod.rs @@ -5,7 +5,6 @@ #![allow(clippy::wildcard_imports, clippy::enum_glob_use)] use crate::{both, over}; -use rustc_ast::ptr::P; use rustc_ast::{self as ast, *}; use rustc_span::symbol::Ident; use std::mem; @@ -83,11 +82,11 @@ pub fn eq_field_pat(l: &PatField, r: &PatField) -> bool { && over(&l.attrs, &r.attrs, eq_attr) } -pub fn eq_qself(l: &P<QSelf>, r: &P<QSelf>) -> bool { +pub fn eq_qself(l: &Box<QSelf>, r: &Box<QSelf>) -> bool { l.position == r.position && eq_ty(&l.ty, &r.ty) } -pub fn eq_maybe_qself(l: Option<&P<QSelf>>, r: Option<&P<QSelf>>) -> bool { +pub fn eq_maybe_qself(l: Option<&Box<QSelf>>, r: Option<&Box<QSelf>>) -> bool { match (l, r) { (Some(l), Some(r)) => eq_qself(l, r), (None, None) => true, @@ -130,7 +129,7 @@ pub fn eq_generic_arg(l: &GenericArg, r: &GenericArg) -> bool { } } -pub fn eq_expr_opt(l: Option<&P<Expr>>, r: Option<&P<Expr>>) -> bool { +pub fn eq_expr_opt(l: Option<&Box<Expr>>, r: Option<&Box<Expr>>) -> bool { both(l, r, |l, r| eq_expr(l, r)) } @@ -727,7 +726,7 @@ pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool { } #[expect(clippy::ref_option, reason = "This is the type how it is stored in the AST")] -pub fn eq_opt_fn_contract(l: &Option<P<FnContract>>, r: &Option<P<FnContract>>) -> bool { +pub fn eq_opt_fn_contract(l: &Option<Box<FnContract>>, r: &Option<Box<FnContract>>) -> bool { match (l, r) { (Some(l), Some(r)) => { eq_expr_opt(l.requires.as_ref(), r.requires.as_ref()) && eq_expr_opt(l.ensures.as_ref(), r.ensures.as_ref()) diff --git a/src/tools/compiletest/src/directives/tests.rs b/src/tools/compiletest/src/directives/tests.rs index 30d8537b51a..33a02eb29fd 100644 --- a/src/tools/compiletest/src/directives/tests.rs +++ b/src/tools/compiletest/src/directives/tests.rs @@ -637,6 +637,7 @@ fn matches_env() { ("x86_64-unknown-linux-gnu", "gnu"), ("x86_64-fortanix-unknown-sgx", "sgx"), ("arm-unknown-linux-musleabi", "musl"), + ("aarch64-apple-ios-macabi", "macabi"), ]; for (target, env) in envs { let config: Config = cfg().target(target).build(); @@ -647,11 +648,7 @@ fn matches_env() { #[test] fn matches_abi() { - let abis = [ - ("aarch64-apple-ios-macabi", "macabi"), - ("x86_64-unknown-linux-gnux32", "x32"), - ("arm-unknown-linux-gnueabi", "eabi"), - ]; + let abis = [("x86_64-unknown-linux-gnux32", "x32"), ("arm-unknown-linux-gnueabi", "eabi")]; for (target, abi) in abis { let config: Config = cfg().target(target).build(); assert!(config.matches_abi(abi), "{target} {abi}"); diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index f283a625f97..debf67e2741 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -1766,7 +1766,7 @@ impl<'test> TestCx<'test> { match self.config.compare_mode { Some(CompareMode::Polonius) => { - rustc.args(&["-Zpolonius"]); + rustc.args(&["-Zpolonius=next"]); } Some(CompareMode::NextSolver) => { rustc.args(&["-Znext-solver"]); diff --git a/src/tools/enzyme b/src/tools/enzyme -Subproject 2cccfba93c1650f26f1cf8be8aa875a7c1d23fb +Subproject 58af4e9e6c047534ba059b12af17cecb8a2e9f9 diff --git a/src/tools/miri/src/intrinsics/atomic.rs b/src/tools/miri/src/intrinsics/atomic.rs index bcc3e9ec885..e6341252927 100644 --- a/src/tools/miri/src/intrinsics/atomic.rs +++ b/src/tools/miri/src/intrinsics/atomic.rs @@ -105,27 +105,27 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { } "or" => { - let ord = get_ord_at(1); + let ord = get_ord_at(2); this.atomic_rmw_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), rw_ord(ord))?; } "xor" => { - let ord = get_ord_at(1); + let ord = get_ord_at(2); this.atomic_rmw_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), rw_ord(ord))?; } "and" => { - let ord = get_ord_at(1); + let ord = get_ord_at(2); this.atomic_rmw_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), rw_ord(ord))?; } "nand" => { - let ord = get_ord_at(1); + let ord = get_ord_at(2); this.atomic_rmw_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), rw_ord(ord))?; } "xadd" => { - let ord = get_ord_at(1); + let ord = get_ord_at(2); this.atomic_rmw_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), rw_ord(ord))?; } "xsub" => { - let ord = get_ord_at(1); + let ord = get_ord_at(2); this.atomic_rmw_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), rw_ord(ord))?; } "min" => { @@ -231,15 +231,14 @@ trait EvalContextPrivExt<'tcx>: MiriInterpCxExt<'tcx> { let place = this.deref_pointer(place)?; let rhs = this.read_immediate(rhs)?; - if !place.layout.ty.is_integral() && !place.layout.ty.is_raw_ptr() { + if !(place.layout.ty.is_integral() || place.layout.ty.is_raw_ptr()) + || !(rhs.layout.ty.is_integral() || rhs.layout.ty.is_raw_ptr()) + { span_bug!( this.cur_span(), "atomic arithmetic operations only work on integer and raw pointer types", ); } - if rhs.layout.ty != place.layout.ty { - span_bug!(this.cur_span(), "atomic arithmetic operation type mismatch"); - } let old = match atomic_op { AtomicOp::Min => diff --git a/src/tools/miri/src/intrinsics/mod.rs b/src/tools/miri/src/intrinsics/mod.rs index e0e09ac6835..b5e81460773 100644 --- a/src/tools/miri/src/intrinsics/mod.rs +++ b/src/tools/miri/src/intrinsics/mod.rs @@ -3,16 +3,20 @@ mod atomic; mod simd; +use std::ops::Neg; + use rand::Rng; use rustc_abi::Size; +use rustc_apfloat::ieee::{IeeeFloat, Semantics}; use rustc_apfloat::{self, Float, Round}; use rustc_middle::mir; -use rustc_middle::ty::{self, FloatTy}; +use rustc_middle::ty::{self, FloatTy, ScalarInt}; use rustc_span::{Symbol, sym}; use self::atomic::EvalContextExt as _; use self::helpers::{ToHost, ToSoft}; use self::simd::EvalContextExt as _; +use crate::math::{IeeeExt, apply_random_float_error_ulp}; use crate::*; /// Check that the number of args is what we expect. @@ -205,7 +209,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { let [f] = check_intrinsic_arg_count(args)?; let f = this.read_scalar(f)?.to_f32()?; - let res = math::fixed_float_value(this, intrinsic_name, &[f]).unwrap_or_else(|| { + let res = fixed_float_value(this, intrinsic_name, &[f]).unwrap_or_else(|| { // Using host floats (but it's fine, these operations do not have // guaranteed precision). let host = f.to_host(); @@ -223,7 +227,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { // Apply a relative error of 4ULP to introduce some non-determinism // simulating imprecise implementations and optimizations. - let res = math::apply_random_float_error_ulp( + let res = apply_random_float_error_ulp( this, res, 2, // log2(4) @@ -231,7 +235,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { // Clamp the result to the guaranteed range of this function according to the C standard, // if any. - math::clamp_float_value(intrinsic_name, res) + clamp_float_value(intrinsic_name, res) }); let res = this.adjust_nan(res, &[f]); this.write_scalar(res, dest)?; @@ -249,7 +253,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { let [f] = check_intrinsic_arg_count(args)?; let f = this.read_scalar(f)?.to_f64()?; - let res = math::fixed_float_value(this, intrinsic_name, &[f]).unwrap_or_else(|| { + let res = fixed_float_value(this, intrinsic_name, &[f]).unwrap_or_else(|| { // Using host floats (but it's fine, these operations do not have // guaranteed precision). let host = f.to_host(); @@ -267,7 +271,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { // Apply a relative error of 4ULP to introduce some non-determinism // simulating imprecise implementations and optimizations. - let res = math::apply_random_float_error_ulp( + let res = apply_random_float_error_ulp( this, res, 2, // log2(4) @@ -275,7 +279,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { // Clamp the result to the guaranteed range of this function according to the C standard, // if any. - math::clamp_float_value(intrinsic_name, res) + clamp_float_value(intrinsic_name, res) }); let res = this.adjust_nan(res, &[f]); this.write_scalar(res, dest)?; @@ -326,17 +330,16 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { let f1 = this.read_scalar(f1)?.to_f32()?; let f2 = this.read_scalar(f2)?.to_f32()?; - let res = - math::fixed_float_value(this, intrinsic_name, &[f1, f2]).unwrap_or_else(|| { - // Using host floats (but it's fine, this operation does not have guaranteed precision). - let res = f1.to_host().powf(f2.to_host()).to_soft(); + let res = fixed_float_value(this, intrinsic_name, &[f1, f2]).unwrap_or_else(|| { + // Using host floats (but it's fine, this operation does not have guaranteed precision). + let res = f1.to_host().powf(f2.to_host()).to_soft(); - // Apply a relative error of 4ULP to introduce some non-determinism - // simulating imprecise implementations and optimizations. - math::apply_random_float_error_ulp( - this, res, 2, // log2(4) - ) - }); + // Apply a relative error of 4ULP to introduce some non-determinism + // simulating imprecise implementations and optimizations. + apply_random_float_error_ulp( + this, res, 2, // log2(4) + ) + }); let res = this.adjust_nan(res, &[f1, f2]); this.write_scalar(res, dest)?; } @@ -345,17 +348,16 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { let f1 = this.read_scalar(f1)?.to_f64()?; let f2 = this.read_scalar(f2)?.to_f64()?; - let res = - math::fixed_float_value(this, intrinsic_name, &[f1, f2]).unwrap_or_else(|| { - // Using host floats (but it's fine, this operation does not have guaranteed precision). - let res = f1.to_host().powf(f2.to_host()).to_soft(); + let res = fixed_float_value(this, intrinsic_name, &[f1, f2]).unwrap_or_else(|| { + // Using host floats (but it's fine, this operation does not have guaranteed precision). + let res = f1.to_host().powf(f2.to_host()).to_soft(); - // Apply a relative error of 4ULP to introduce some non-determinism - // simulating imprecise implementations and optimizations. - math::apply_random_float_error_ulp( - this, res, 2, // log2(4) - ) - }); + // Apply a relative error of 4ULP to introduce some non-determinism + // simulating imprecise implementations and optimizations. + apply_random_float_error_ulp( + this, res, 2, // log2(4) + ) + }); let res = this.adjust_nan(res, &[f1, f2]); this.write_scalar(res, dest)?; } @@ -365,13 +367,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { let f = this.read_scalar(f)?.to_f32()?; let i = this.read_scalar(i)?.to_i32()?; - let res = math::fixed_powi_value(this, f, i).unwrap_or_else(|| { + let res = fixed_powi_float_value(this, f, i).unwrap_or_else(|| { // Using host floats (but it's fine, this operation does not have guaranteed precision). let res = f.to_host().powi(i).to_soft(); // Apply a relative error of 4ULP to introduce some non-determinism // simulating imprecise implementations and optimizations. - math::apply_random_float_error_ulp( + apply_random_float_error_ulp( this, res, 2, // log2(4) ) }); @@ -383,13 +385,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { let f = this.read_scalar(f)?.to_f64()?; let i = this.read_scalar(i)?.to_i32()?; - let res = math::fixed_powi_value(this, f, i).unwrap_or_else(|| { + let res = fixed_powi_float_value(this, f, i).unwrap_or_else(|| { // Using host floats (but it's fine, this operation does not have guaranteed precision). let res = f.to_host().powi(i).to_soft(); // Apply a relative error of 4ULP to introduce some non-determinism // simulating imprecise implementations and optimizations. - math::apply_random_float_error_ulp( + apply_random_float_error_ulp( this, res, 2, // log2(4) ) }); @@ -446,7 +448,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { } // Apply a relative error of 4ULP to simulate non-deterministic precision loss // due to optimizations. - let res = math::apply_random_float_error_to_imm(this, res, 2 /* log2(4) */)?; + let res = apply_random_float_error_to_imm(this, res, 2 /* log2(4) */)?; this.write_immediate(*res, dest)?; } @@ -483,3 +485,133 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { interp_ok(EmulateItemResult::NeedsReturn) } } + +/// Applies a random ULP floating point error to `val` and returns the new value. +/// So if you want an X ULP error, `ulp_exponent` should be log2(X). +/// +/// Will fail if `val` is not a floating point number. +fn apply_random_float_error_to_imm<'tcx>( + ecx: &mut MiriInterpCx<'tcx>, + val: ImmTy<'tcx>, + ulp_exponent: u32, +) -> InterpResult<'tcx, ImmTy<'tcx>> { + let scalar = val.to_scalar_int()?; + let res: ScalarInt = match val.layout.ty.kind() { + ty::Float(FloatTy::F16) => + apply_random_float_error_ulp(ecx, scalar.to_f16(), ulp_exponent).into(), + ty::Float(FloatTy::F32) => + apply_random_float_error_ulp(ecx, scalar.to_f32(), ulp_exponent).into(), + ty::Float(FloatTy::F64) => + apply_random_float_error_ulp(ecx, scalar.to_f64(), ulp_exponent).into(), + ty::Float(FloatTy::F128) => + apply_random_float_error_ulp(ecx, scalar.to_f128(), ulp_exponent).into(), + _ => bug!("intrinsic called with non-float input type"), + }; + + interp_ok(ImmTy::from_scalar_int(res, val.layout)) +} + +/// For the intrinsics: +/// - sinf32, sinf64 +/// - cosf32, cosf64 +/// - expf32, expf64, exp2f32, exp2f64 +/// - logf32, logf64, log2f32, log2f64, log10f32, log10f64 +/// - powf32, powf64 +/// +/// # Return +/// +/// Returns `Some(output)` if the `intrinsic` results in a defined fixed `output` specified in the C standard +/// (specifically, C23 annex F.10) when given `args` as arguments. Outputs that are unaffected by a relative error +/// (such as INF and zero) are not handled here, they are assumed to be handled by the underlying +/// implementation. Returns `None` if no specific value is guaranteed. +/// +/// # Note +/// +/// For `powf*` operations of the form: +/// +/// - `(SNaN)^(±0)` +/// - `1^(SNaN)` +/// +/// The result is implementation-defined: +/// - musl returns for both `1.0` +/// - glibc returns for both `NaN` +/// +/// This discrepancy exists because SNaN handling is not consistently defined across platforms, +/// and the C standard leaves behavior for SNaNs unspecified. +/// +/// Miri chooses to adhere to both implementations and returns either one of them non-deterministically. +fn fixed_float_value<S: Semantics>( + ecx: &mut MiriInterpCx<'_>, + intrinsic_name: &str, + args: &[IeeeFloat<S>], +) -> Option<IeeeFloat<S>> { + let one = IeeeFloat::<S>::one(); + Some(match (intrinsic_name, args) { + // cos(+- 0) = 1 + ("cosf32" | "cosf64", [input]) if input.is_zero() => one, + + // e^0 = 1 + ("expf32" | "expf64" | "exp2f32" | "exp2f64", [input]) if input.is_zero() => one, + + // (-1)^(±INF) = 1 + ("powf32" | "powf64", [base, exp]) if *base == -one && exp.is_infinite() => one, + + // 1^y = 1 for any y, even a NaN + ("powf32" | "powf64", [base, exp]) if *base == one => { + let rng = ecx.machine.rng.get_mut(); + // SNaN exponents get special treatment: they might return 1, or a NaN. + let return_nan = exp.is_signaling() && ecx.machine.float_nondet && rng.random(); + // Handle both the musl and glibc cases non-deterministically. + if return_nan { ecx.generate_nan(args) } else { one } + } + + // x^(±0) = 1 for any x, even a NaN + ("powf32" | "powf64", [base, exp]) if exp.is_zero() => { + let rng = ecx.machine.rng.get_mut(); + // SNaN bases get special treatment: they might return 1, or a NaN. + let return_nan = base.is_signaling() && ecx.machine.float_nondet && rng.random(); + // Handle both the musl and glibc cases non-deterministically. + if return_nan { ecx.generate_nan(args) } else { one } + } + + // There are a lot of cases for fixed outputs according to the C Standard, but these are + // mainly INF or zero which are not affected by the applied error. + _ => return None, + }) +} + +/// Returns `Some(output)` if `powi` (called `pown` in C) results in a fixed value specified in the +/// C standard (specifically, C23 annex F.10.4.6) when doing `base^exp`. Otherwise, returns `None`. +fn fixed_powi_float_value<S: Semantics>( + ecx: &mut MiriInterpCx<'_>, + base: IeeeFloat<S>, + exp: i32, +) -> Option<IeeeFloat<S>> { + Some(match exp { + 0 => { + let one = IeeeFloat::<S>::one(); + let rng = ecx.machine.rng.get_mut(); + let return_nan = ecx.machine.float_nondet && rng.random() && base.is_signaling(); + // For SNaN treatment, we are consistent with `powf`above. + // (We wouldn't have two, unlike powf all implementations seem to agree for powi, + // but for now we are maximally conservative.) + if return_nan { ecx.generate_nan(&[base]) } else { one } + } + + _ => return None, + }) +} + +/// Given an floating-point operation and a floating-point value, clamps the result to the output +/// range of the given operation. +fn clamp_float_value<S: Semantics>(intrinsic_name: &str, val: IeeeFloat<S>) -> IeeeFloat<S> { + match intrinsic_name { + // sin and cos: [-1, 1] + "sinf32" | "cosf32" | "sinf64" | "cosf64" => + val.clamp(IeeeFloat::<S>::one().neg(), IeeeFloat::<S>::one()), + // exp: [0, +INF] + "expf32" | "exp2f32" | "expf64" | "exp2f64" => + IeeeFloat::<S>::maximum(val, IeeeFloat::<S>::ZERO), + _ => val, + } +} diff --git a/src/tools/miri/src/lib.rs b/src/tools/miri/src/lib.rs index 9272cd29788..5ed6d6b346c 100644 --- a/src/tools/miri/src/lib.rs +++ b/src/tools/miri/src/lib.rs @@ -18,8 +18,6 @@ #![feature(derive_coerce_pointee)] #![feature(arbitrary_self_types)] #![feature(iter_advance_by)] -#![feature(f16)] -#![feature(f128)] // Configure clippy and other lints #![allow( clippy::collapsible_else_if, diff --git a/src/tools/miri/src/math.rs b/src/tools/miri/src/math.rs index 3eee6b82e8c..e9e5a1070c9 100644 --- a/src/tools/miri/src/math.rs +++ b/src/tools/miri/src/math.rs @@ -1,9 +1,6 @@ -use std::ops::Neg; -use std::{f16, f32, f64, f128}; - use rand::Rng as _; use rustc_apfloat::Float as _; -use rustc_apfloat::ieee::{DoubleS, HalfS, IeeeFloat, QuadS, Semantics, SingleS}; +use rustc_apfloat::ieee::IeeeFloat; use rustc_middle::ty::{self, FloatTy, ScalarInt}; use crate::*; @@ -53,236 +50,29 @@ pub(crate) fn apply_random_float_error_ulp<F: rustc_apfloat::Float>( apply_random_float_error(ecx, val, err_scale) } -/// Applies a random ULP floating point error to `val` and returns the new value. -/// So if you want an X ULP error, `ulp_exponent` should be log2(X). -/// +/// Applies a random 16ULP floating point error to `val` and returns the new value. /// Will fail if `val` is not a floating point number. pub(crate) fn apply_random_float_error_to_imm<'tcx>( ecx: &mut MiriInterpCx<'tcx>, val: ImmTy<'tcx>, ulp_exponent: u32, ) -> InterpResult<'tcx, ImmTy<'tcx>> { - let this = ecx.eval_context_mut(); let scalar = val.to_scalar_int()?; let res: ScalarInt = match val.layout.ty.kind() { ty::Float(FloatTy::F16) => - apply_random_float_error_ulp(this, scalar.to_f16(), ulp_exponent).into(), + apply_random_float_error_ulp(ecx, scalar.to_f16(), ulp_exponent).into(), ty::Float(FloatTy::F32) => - apply_random_float_error_ulp(this, scalar.to_f32(), ulp_exponent).into(), + apply_random_float_error_ulp(ecx, scalar.to_f32(), ulp_exponent).into(), ty::Float(FloatTy::F64) => - apply_random_float_error_ulp(this, scalar.to_f64(), ulp_exponent).into(), + apply_random_float_error_ulp(ecx, scalar.to_f64(), ulp_exponent).into(), ty::Float(FloatTy::F128) => - apply_random_float_error_ulp(this, scalar.to_f128(), ulp_exponent).into(), + apply_random_float_error_ulp(ecx, scalar.to_f128(), ulp_exponent).into(), _ => bug!("intrinsic called with non-float input type"), }; interp_ok(ImmTy::from_scalar_int(res, val.layout)) } -/// Given a floating-point operation and a floating-point value, clamps the result to the output -/// range of the given operation according to the C standard, if any. -pub(crate) fn clamp_float_value<S: Semantics>( - intrinsic_name: &str, - val: IeeeFloat<S>, -) -> IeeeFloat<S> -where - IeeeFloat<S>: IeeeExt, -{ - let zero = IeeeFloat::<S>::ZERO; - let one = IeeeFloat::<S>::one(); - let two = IeeeFloat::<S>::two(); - let pi = IeeeFloat::<S>::pi(); - let pi_over_2 = (pi / two).value; - - match intrinsic_name { - // sin, cos, tanh: [-1, 1] - #[rustfmt::skip] - | "sinf32" - | "sinf64" - | "cosf32" - | "cosf64" - | "tanhf" - | "tanh" - => val.clamp(one.neg(), one), - - // exp: [0, +INF) - "expf32" | "exp2f32" | "expf64" | "exp2f64" => val.maximum(zero), - - // cosh: [1, +INF) - "coshf" | "cosh" => val.maximum(one), - - // acos: [0, π] - "acosf" | "acos" => val.clamp(zero, pi), - - // asin: [-π, +π] - "asinf" | "asin" => val.clamp(pi.neg(), pi), - - // atan: (-π/2, +π/2) - "atanf" | "atan" => val.clamp(pi_over_2.neg(), pi_over_2), - - // erfc: (-1, 1) - "erff" | "erf" => val.clamp(one.neg(), one), - - // erfc: (0, 2) - "erfcf" | "erfc" => val.clamp(zero, two), - - // atan2(y, x): arctan(y/x) in [−π, +π] - "atan2f" | "atan2" => val.clamp(pi.neg(), pi), - - _ => val, - } -} - -/// For the intrinsics: -/// - sinf32, sinf64, sinhf, sinh -/// - cosf32, cosf64, coshf, cosh -/// - tanhf, tanh, atanf, atan, atan2f, atan2 -/// - expf32, expf64, exp2f32, exp2f64 -/// - logf32, logf64, log2f32, log2f64, log10f32, log10f64 -/// - powf32, powf64 -/// - erff, erf, erfcf, erfc -/// - hypotf, hypot -/// -/// # Return -/// -/// Returns `Some(output)` if the `intrinsic` results in a defined fixed `output` specified in the C standard -/// (specifically, C23 annex F.10) when given `args` as arguments. Outputs that are unaffected by a relative error -/// (such as INF and zero) are not handled here, they are assumed to be handled by the underlying -/// implementation. Returns `None` if no specific value is guaranteed. -/// -/// # Note -/// -/// For `powf*` operations of the form: -/// -/// - `(SNaN)^(±0)` -/// - `1^(SNaN)` -/// -/// The result is implementation-defined: -/// - musl returns for both `1.0` -/// - glibc returns for both `NaN` -/// -/// This discrepancy exists because SNaN handling is not consistently defined across platforms, -/// and the C standard leaves behavior for SNaNs unspecified. -/// -/// Miri chooses to adhere to both implementations and returns either one of them non-deterministically. -pub(crate) fn fixed_float_value<S: Semantics>( - ecx: &mut MiriInterpCx<'_>, - intrinsic_name: &str, - args: &[IeeeFloat<S>], -) -> Option<IeeeFloat<S>> -where - IeeeFloat<S>: IeeeExt, -{ - let this = ecx.eval_context_mut(); - let one = IeeeFloat::<S>::one(); - let two = IeeeFloat::<S>::two(); - let three = IeeeFloat::<S>::three(); - let pi = IeeeFloat::<S>::pi(); - let pi_over_2 = (pi / two).value; - let pi_over_4 = (pi_over_2 / two).value; - - Some(match (intrinsic_name, args) { - // cos(±0) and cosh(±0)= 1 - ("cosf32" | "cosf64" | "coshf" | "cosh", [input]) if input.is_zero() => one, - - // e^0 = 1 - ("expf32" | "expf64" | "exp2f32" | "exp2f64", [input]) if input.is_zero() => one, - - // tanh(±INF) = ±1 - ("tanhf" | "tanh", [input]) if input.is_infinite() => one.copy_sign(*input), - - // atan(±INF) = ±π/2 - ("atanf" | "atan", [input]) if input.is_infinite() => pi_over_2.copy_sign(*input), - - // erf(±INF) = ±1 - ("erff" | "erf", [input]) if input.is_infinite() => one.copy_sign(*input), - - // erfc(-INF) = 2 - ("erfcf" | "erfc", [input]) if input.is_neg_infinity() => (one + one).value, - - // hypot(x, ±0) = abs(x), if x is not a NaN. - ("_hypotf" | "hypotf" | "_hypot" | "hypot", [x, y]) if !x.is_nan() && y.is_zero() => - x.abs(), - - // atan2(±0,−0) = ±π. - // atan2(±0, y) = ±π for y < 0. - // Must check for non NaN because `y.is_negative()` also applies to NaN. - ("atan2f" | "atan2", [x, y]) if (x.is_zero() && (y.is_negative() && !y.is_nan())) => - pi.copy_sign(*x), - - // atan2(±x,−∞) = ±π for finite x > 0. - ("atan2f" | "atan2", [x, y]) - if (!x.is_zero() && !x.is_infinite()) && y.is_neg_infinity() => - pi.copy_sign(*x), - - // atan2(x, ±0) = −π/2 for x < 0. - // atan2(x, ±0) = π/2 for x > 0. - ("atan2f" | "atan2", [x, y]) if !x.is_zero() && y.is_zero() => pi_over_2.copy_sign(*x), - - //atan2(±∞, −∞) = ±3π/4 - ("atan2f" | "atan2", [x, y]) if x.is_infinite() && y.is_neg_infinity() => - (pi_over_4 * three).value.copy_sign(*x), - - //atan2(±∞, +∞) = ±π/4 - ("atan2f" | "atan2", [x, y]) if x.is_infinite() && y.is_pos_infinity() => - pi_over_4.copy_sign(*x), - - // atan2(±∞, y) returns ±π/2 for finite y. - ("atan2f" | "atan2", [x, y]) if x.is_infinite() && (!y.is_infinite() && !y.is_nan()) => - pi_over_2.copy_sign(*x), - - // (-1)^(±INF) = 1 - ("powf32" | "powf64", [base, exp]) if *base == -one && exp.is_infinite() => one, - - // 1^y = 1 for any y, even a NaN - ("powf32" | "powf64", [base, exp]) if *base == one => { - let rng = this.machine.rng.get_mut(); - // SNaN exponents get special treatment: they might return 1, or a NaN. - let return_nan = exp.is_signaling() && this.machine.float_nondet && rng.random(); - // Handle both the musl and glibc cases non-deterministically. - if return_nan { this.generate_nan(args) } else { one } - } - - // x^(±0) = 1 for any x, even a NaN - ("powf32" | "powf64", [base, exp]) if exp.is_zero() => { - let rng = this.machine.rng.get_mut(); - // SNaN bases get special treatment: they might return 1, or a NaN. - let return_nan = base.is_signaling() && this.machine.float_nondet && rng.random(); - // Handle both the musl and glibc cases non-deterministically. - if return_nan { this.generate_nan(args) } else { one } - } - - // There are a lot of cases for fixed outputs according to the C Standard, but these are - // mainly INF or zero which are not affected by the applied error. - _ => return None, - }) -} - -/// Returns `Some(output)` if `powi` (called `pown` in C) results in a fixed value specified in the -/// C standard (specifically, C23 annex F.10.4.6) when doing `base^exp`. Otherwise, returns `None`. -pub(crate) fn fixed_powi_value<S: Semantics>( - ecx: &mut MiriInterpCx<'_>, - base: IeeeFloat<S>, - exp: i32, -) -> Option<IeeeFloat<S>> -where - IeeeFloat<S>: IeeeExt, -{ - match exp { - 0 => { - let one = IeeeFloat::<S>::one(); - let rng = ecx.machine.rng.get_mut(); - let return_nan = ecx.machine.float_nondet && rng.random() && base.is_signaling(); - // For SNaN treatment, we are consistent with `powf`above. - // (We wouldn't have two, unlike powf all implementations seem to agree for powi, - // but for now we are maximally conservative.) - Some(if return_nan { ecx.generate_nan(&[base]) } else { one }) - } - - _ => return None, - } -} - pub(crate) fn sqrt<S: rustc_apfloat::ieee::Semantics>(x: IeeeFloat<S>) -> IeeeFloat<S> { match x.category() { // preserve zero sign @@ -365,49 +155,19 @@ pub(crate) fn sqrt<S: rustc_apfloat::ieee::Semantics>(x: IeeeFloat<S>) -> IeeeFl } } -/// Extend functionality of `rustc_apfloat` softfloats for IEEE float types. +/// Extend functionality of rustc_apfloat softfloats pub trait IeeeExt: rustc_apfloat::Float { - // Some values we use: - #[inline] fn one() -> Self { Self::from_u128(1).value } #[inline] - fn two() -> Self { - Self::from_u128(2).value - } - - #[inline] - fn three() -> Self { - Self::from_u128(3).value - } - - fn pi() -> Self; - - #[inline] fn clamp(self, min: Self, max: Self) -> Self { self.maximum(min).minimum(max) } } - -macro_rules! impl_ieee_pi { - ($float_ty:ident, $semantic:ty) => { - impl IeeeExt for IeeeFloat<$semantic> { - #[inline] - fn pi() -> Self { - // We take the value from the standard library as the most reasonable source for an exact π here. - Self::from_bits($float_ty::consts::PI.to_bits() as _) - } - } - }; -} - -impl_ieee_pi!(f16, HalfS); -impl_ieee_pi!(f32, SingleS); -impl_ieee_pi!(f64, DoubleS); -impl_ieee_pi!(f128, QuadS); +impl<S: rustc_apfloat::ieee::Semantics> IeeeExt for IeeeFloat<S> {} #[cfg(test)] mod tests { diff --git a/src/tools/miri/src/operator.rs b/src/tools/miri/src/operator.rs index 73d671121f6..3c3f2c28535 100644 --- a/src/tools/miri/src/operator.rs +++ b/src/tools/miri/src/operator.rs @@ -50,17 +50,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { } // Some more operations are possible with atomics. - // The return value always has the provenance of the *left* operand. + // The RHS must be `usize`. Add | Sub | BitOr | BitAnd | BitXor => { assert!(left.layout.ty.is_raw_ptr()); - assert!(right.layout.ty.is_raw_ptr()); + assert_eq!(right.layout.ty, this.tcx.types.usize); let ptr = left.to_scalar().to_pointer(this)?; // We do the actual operation with usize-typed scalars. let left = ImmTy::from_uint(ptr.addr().bytes(), this.machine.layouts.usize); - let right = ImmTy::from_uint( - right.to_scalar().to_target_usize(this)?, - this.machine.layouts.usize, - ); let result = this.binary_op(bin_op, &left, &right)?; // Construct a new pointer with the provenance of `ptr` (the LHS). let result_ptr = Pointer::new( diff --git a/src/tools/miri/src/shims/foreign_items.rs b/src/tools/miri/src/shims/foreign_items.rs index bf4db2de2d7..21545b68029 100644 --- a/src/tools/miri/src/shims/foreign_items.rs +++ b/src/tools/miri/src/shims/foreign_items.rs @@ -17,7 +17,6 @@ use rustc_target::callconv::FnAbi; use self::helpers::{ToHost, ToSoft}; use super::alloc::EvalContextExt as _; use super::backtrace::EvalContextExt as _; -use crate::helpers::EvalContextExt as _; use crate::*; /// Type of dynamic symbols (for `dlsym` et al) @@ -780,38 +779,33 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> { => { let [f] = this.check_shim_sig_lenient(abi, CanonAbi::C , link_name, args)?; let f = this.read_scalar(f)?.to_f32()?; - - let res = math::fixed_float_value(this, link_name.as_str(), &[f]).unwrap_or_else(|| { - // Using host floats (but it's fine, these operations do not have - // guaranteed precision). - let f_host = f.to_host(); - let res = match link_name.as_str() { - "cbrtf" => f_host.cbrt(), - "coshf" => f_host.cosh(), - "sinhf" => f_host.sinh(), - "tanf" => f_host.tan(), - "tanhf" => f_host.tanh(), - "acosf" => f_host.acos(), - "asinf" => f_host.asin(), - "atanf" => f_host.atan(), - "log1pf" => f_host.ln_1p(), - "expm1f" => f_host.exp_m1(), - "tgammaf" => f_host.gamma(), - "erff" => f_host.erf(), - "erfcf" => f_host.erfc(), - _ => bug!(), - }; - let res = res.to_soft(); - // Apply a relative error of 4ULP to introduce some non-determinism - // simulating imprecise implementations and optimizations. - let res = math::apply_random_float_error_ulp( - this, res, 2, // log2(4) - ); - - // Clamp the result to the guaranteed range of this function according to the C standard, - // if any. - math::clamp_float_value(link_name.as_str(), res) - }); + // Using host floats (but it's fine, these operations do not have guaranteed precision). + let f_host = f.to_host(); + let res = match link_name.as_str() { + "cbrtf" => f_host.cbrt(), + "coshf" => f_host.cosh(), + "sinhf" => f_host.sinh(), + "tanf" => f_host.tan(), + "tanhf" => f_host.tanh(), + "acosf" => f_host.acos(), + "asinf" => f_host.asin(), + "atanf" => f_host.atan(), + "log1pf" => f_host.ln_1p(), + "expm1f" => f_host.exp_m1(), + "tgammaf" => f_host.gamma(), + "erff" => f_host.erf(), + "erfcf" => f_host.erfc(), + _ => bug!(), + }; + let res = res.to_soft(); + // Apply a relative error of 16ULP to introduce some non-determinism + // simulating imprecise implementations and optimizations. + // FIXME: temporarily disabled as it breaks std tests. + // let res = math::apply_random_float_error_ulp( + // this, + // res, + // 4, // log2(16) + // ); let res = this.adjust_nan(res, &[f]); this.write_scalar(res, dest)?; } @@ -824,28 +818,24 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> { let [f1, f2] = this.check_shim_sig_lenient(abi, CanonAbi::C , link_name, args)?; let f1 = this.read_scalar(f1)?.to_f32()?; let f2 = this.read_scalar(f2)?.to_f32()?; - - let res = math::fixed_float_value(this, link_name.as_str(), &[f1, f2]).unwrap_or_else(|| { - let res = match link_name.as_str() { - // underscore case for windows, here and below - // (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019) - // Using host floats (but it's fine, these operations do not have guaranteed precision). - "_hypotf" | "hypotf" => f1.to_host().hypot(f2.to_host()).to_soft(), - "atan2f" => f1.to_host().atan2(f2.to_host()).to_soft(), - #[allow(deprecated)] - "fdimf" => f1.to_host().abs_sub(f2.to_host()).to_soft(), - _ => bug!(), - }; - // Apply a relative error of 4ULP to introduce some non-determinism - // simulating imprecise implementations and optimizations. - let res = math::apply_random_float_error_ulp( - this, res, 2, // log2(4) - ); - - // Clamp the result to the guaranteed range of this function according to the C standard, - // if any. - math::clamp_float_value(link_name.as_str(), res) - }); + // underscore case for windows, here and below + // (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019) + // Using host floats (but it's fine, these operations do not have guaranteed precision). + let res = match link_name.as_str() { + "_hypotf" | "hypotf" => f1.to_host().hypot(f2.to_host()).to_soft(), + "atan2f" => f1.to_host().atan2(f2.to_host()).to_soft(), + #[allow(deprecated)] + "fdimf" => f1.to_host().abs_sub(f2.to_host()).to_soft(), + _ => bug!(), + }; + // Apply a relative error of 16ULP to introduce some non-determinism + // simulating imprecise implementations and optimizations. + // FIXME: temporarily disabled as it breaks std tests. + // let res = math::apply_random_float_error_ulp( + // this, + // res, + // 4, // log2(16) + // ); let res = this.adjust_nan(res, &[f1, f2]); this.write_scalar(res, dest)?; } @@ -866,38 +856,33 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> { => { let [f] = this.check_shim_sig_lenient(abi, CanonAbi::C , link_name, args)?; let f = this.read_scalar(f)?.to_f64()?; - - let res = math::fixed_float_value(this, link_name.as_str(), &[f]).unwrap_or_else(|| { - // Using host floats (but it's fine, these operations do not have - // guaranteed precision). - let f_host = f.to_host(); - let res = match link_name.as_str() { - "cbrt" => f_host.cbrt(), - "cosh" => f_host.cosh(), - "sinh" => f_host.sinh(), - "tan" => f_host.tan(), - "tanh" => f_host.tanh(), - "acos" => f_host.acos(), - "asin" => f_host.asin(), - "atan" => f_host.atan(), - "log1p" => f_host.ln_1p(), - "expm1" => f_host.exp_m1(), - "tgamma" => f_host.gamma(), - "erf" => f_host.erf(), - "erfc" => f_host.erfc(), - _ => bug!(), - }; - let res = res.to_soft(); - // Apply a relative error of 4ULP to introduce some non-determinism - // simulating imprecise implementations and optimizations. - let res = math::apply_random_float_error_ulp( - this, res, 2, // log2(4) - ); - - // Clamp the result to the guaranteed range of this function according to the C standard, - // if any. - math::clamp_float_value(link_name.as_str(), res) - }); + // Using host floats (but it's fine, these operations do not have guaranteed precision). + let f_host = f.to_host(); + let res = match link_name.as_str() { + "cbrt" => f_host.cbrt(), + "cosh" => f_host.cosh(), + "sinh" => f_host.sinh(), + "tan" => f_host.tan(), + "tanh" => f_host.tanh(), + "acos" => f_host.acos(), + "asin" => f_host.asin(), + "atan" => f_host.atan(), + "log1p" => f_host.ln_1p(), + "expm1" => f_host.exp_m1(), + "tgamma" => f_host.gamma(), + "erf" => f_host.erf(), + "erfc" => f_host.erfc(), + _ => bug!(), + }; + let res = res.to_soft(); + // Apply a relative error of 16ULP to introduce some non-determinism + // simulating imprecise implementations and optimizations. + // FIXME: temporarily disabled as it breaks std tests. + // let res = math::apply_random_float_error_ulp( + // this, + // res.to_soft(), + // 4, // log2(16) + // ); let res = this.adjust_nan(res, &[f]); this.write_scalar(res, dest)?; } @@ -910,28 +895,24 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> { let [f1, f2] = this.check_shim_sig_lenient(abi, CanonAbi::C , link_name, args)?; let f1 = this.read_scalar(f1)?.to_f64()?; let f2 = this.read_scalar(f2)?.to_f64()?; - - let res = math::fixed_float_value(this, link_name.as_str(), &[f1, f2]).unwrap_or_else(|| { - let res = match link_name.as_str() { - // underscore case for windows, here and below - // (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019) - // Using host floats (but it's fine, these operations do not have guaranteed precision). - "_hypot" | "hypot" => f1.to_host().hypot(f2.to_host()).to_soft(), - "atan2" => f1.to_host().atan2(f2.to_host()).to_soft(), - #[allow(deprecated)] - "fdim" => f1.to_host().abs_sub(f2.to_host()).to_soft(), - _ => bug!(), - }; - // Apply a relative error of 4ULP to introduce some non-determinism - // simulating imprecise implementations and optimizations. - let res = math::apply_random_float_error_ulp( - this, res, 2, // log2(4) - ); - - // Clamp the result to the guaranteed range of this function according to the C standard, - // if any. - math::clamp_float_value(link_name.as_str(), res) - }); + // underscore case for windows, here and below + // (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019) + // Using host floats (but it's fine, these operations do not have guaranteed precision). + let res = match link_name.as_str() { + "_hypot" | "hypot" => f1.to_host().hypot(f2.to_host()).to_soft(), + "atan2" => f1.to_host().atan2(f2.to_host()).to_soft(), + #[allow(deprecated)] + "fdim" => f1.to_host().abs_sub(f2.to_host()).to_soft(), + _ => bug!(), + }; + // Apply a relative error of 16ULP to introduce some non-determinism + // simulating imprecise implementations and optimizations. + // FIXME: temporarily disabled as it breaks std tests. + // let res = math::apply_random_float_error_ulp( + // this, + // res, + // 4, // log2(16) + // ); let res = this.adjust_nan(res, &[f1, f2]); this.write_scalar(res, dest)?; } @@ -957,14 +938,11 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> { // Using host floats (but it's fine, these operations do not have guaranteed precision). let (res, sign) = x.to_host().ln_gamma(); this.write_int(sign, &signp)?; - let res = res.to_soft(); - // Apply a relative error of 4ULP to introduce some non-determinism + // Apply a relative error of 16ULP to introduce some non-determinism // simulating imprecise implementations and optimizations. - let res = math::apply_random_float_error_ulp(this, res, 2 /* log2(4) */); - // Clamp the result to the guaranteed range of this function according to the C standard, - // if any. - let res = math::clamp_float_value(link_name.as_str(), res); + // FIXME: temporarily disabled as it breaks std tests. + // let res = math::apply_random_float_error_ulp(this, res, 4 /* log2(16) */); let res = this.adjust_nan(res, &[x]); this.write_scalar(res, dest)?; } @@ -976,14 +954,11 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> { // Using host floats (but it's fine, these operations do not have guaranteed precision). let (res, sign) = x.to_host().ln_gamma(); this.write_int(sign, &signp)?; - let res = res.to_soft(); - // Apply a relative error of 4ULP to introduce some non-determinism + // Apply a relative error of 16ULP to introduce some non-determinism // simulating imprecise implementations and optimizations. - let res = math::apply_random_float_error_ulp(this, res, 2 /* log2(4) */); - // Clamp the result to the guaranteed range of this function according to the C standard, - // if any. - let res = math::clamp_float_value(link_name.as_str(), res); + // FIXME: temporarily disabled as it breaks std tests. + // let res = math::apply_random_float_error_ulp(this, res, 4 /* log2(16) */); let res = this.adjust_nan(res, &[x]); this.write_scalar(res, dest)?; } diff --git a/src/tools/miri/tests/fail/alloc/alloc_error_handler_custom.stderr b/src/tools/miri/tests/fail/alloc/alloc_error_handler_custom.stderr index 29c56ca81f7..a2a4be30eca 100644 --- a/src/tools/miri/tests/fail/alloc/alloc_error_handler_custom.stderr +++ b/src/tools/miri/tests/fail/alloc/alloc_error_handler_custom.stderr @@ -11,7 +11,7 @@ note: inside `_::__rg_oom` --> tests/fail/alloc/alloc_error_handler_custom.rs:LL:CC | LL | #[alloc_error_handler] - | ---------------------- in this procedural macro expansion + | ---------------------- in this attribute macro expansion LL | fn alloc_error_handler(layout: Layout) -> ! { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: inside `alloc::alloc::handle_alloc_error::rt_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC diff --git a/src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.both.stderr b/src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.both.stderr index 1246ad5a713..a1571173a53 100644 --- a/src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.both.stderr +++ b/src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.both.stderr @@ -18,8 +18,8 @@ LL | ABORT() = note: inside `std::sys::pal::PLATFORM::abort_internal` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC = note: inside `std::panicking::panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC - = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::begin_panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC - = note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC + = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC + = note: inside `std::panicking::panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside `core::panicking::panic_nounwind` at RUSTLIB/core/src/panicking.rs:LL:CC = note: inside `core::panicking::panic_cannot_unwind` at RUSTLIB/core/src/panicking.rs:LL:CC note: inside `nounwind` diff --git a/src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.definition.stderr b/src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.definition.stderr index 1246ad5a713..a1571173a53 100644 --- a/src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.definition.stderr +++ b/src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.definition.stderr @@ -18,8 +18,8 @@ LL | ABORT() = note: inside `std::sys::pal::PLATFORM::abort_internal` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC = note: inside `std::panicking::panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC - = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::begin_panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC - = note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC + = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC + = note: inside `std::panicking::panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside `core::panicking::panic_nounwind` at RUSTLIB/core/src/panicking.rs:LL:CC = note: inside `core::panicking::panic_cannot_unwind` at RUSTLIB/core/src/panicking.rs:LL:CC note: inside `nounwind` diff --git a/src/tools/miri/tests/fail/panic/abort_unwind.stderr b/src/tools/miri/tests/fail/panic/abort_unwind.stderr index 09f19a6e494..287f7d5432a 100644 --- a/src/tools/miri/tests/fail/panic/abort_unwind.stderr +++ b/src/tools/miri/tests/fail/panic/abort_unwind.stderr @@ -18,8 +18,8 @@ LL | ABORT() = note: inside `std::sys::pal::PLATFORM::abort_internal` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC = note: inside `std::panicking::panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC - = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::begin_panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC - = note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC + = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC + = note: inside `std::panicking::panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside `core::panicking::panic_nounwind` at RUSTLIB/core/src/panicking.rs:LL:CC = note: inside `core::panicking::panic_cannot_unwind` at RUSTLIB/core/src/panicking.rs:LL:CC = note: inside `std::panic::abort_unwind::<{closure@tests/fail/panic/abort_unwind.rs:LL:CC}, ()>` at RUSTLIB/core/src/panic.rs:LL:CC diff --git a/src/tools/miri/tests/fail/panic/double_panic.stderr b/src/tools/miri/tests/fail/panic/double_panic.stderr index f6aa0a34749..b76ece7f1e5 100644 --- a/src/tools/miri/tests/fail/panic/double_panic.stderr +++ b/src/tools/miri/tests/fail/panic/double_panic.stderr @@ -21,8 +21,8 @@ LL | ABORT() = note: inside `std::sys::pal::PLATFORM::abort_internal` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC = note: inside `std::panicking::panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC - = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::begin_panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC - = note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC + = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC + = note: inside `std::panicking::panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside `core::panicking::panic_nounwind_nobacktrace` at RUSTLIB/core/src/panicking.rs:LL:CC = note: inside `core::panicking::panic_in_cleanup` at RUSTLIB/core/src/panicking.rs:LL:CC note: inside `main` diff --git a/src/tools/miri/tests/fail/panic/panic_abort1.stderr b/src/tools/miri/tests/fail/panic/panic_abort1.stderr index ebdf57f0f03..c469d24287f 100644 --- a/src/tools/miri/tests/fail/panic/panic_abort1.stderr +++ b/src/tools/miri/tests/fail/panic/panic_abort1.stderr @@ -17,8 +17,8 @@ LL | ABORT() = note: inside `std::panicking::rust_panic` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside `std::panicking::panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC - = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::begin_panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC - = note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC + = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC + = note: inside `std::panicking::panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC note: inside `main` --> tests/fail/panic/panic_abort1.rs:LL:CC | diff --git a/src/tools/miri/tests/fail/panic/panic_abort2.stderr b/src/tools/miri/tests/fail/panic/panic_abort2.stderr index d63e4ed79b2..bc7918f5f86 100644 --- a/src/tools/miri/tests/fail/panic/panic_abort2.stderr +++ b/src/tools/miri/tests/fail/panic/panic_abort2.stderr @@ -17,8 +17,8 @@ LL | ABORT() = note: inside `std::panicking::rust_panic` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside `std::panicking::panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC - = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::begin_panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC - = note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC + = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC + = note: inside `std::panicking::panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC note: inside `main` --> tests/fail/panic/panic_abort2.rs:LL:CC | diff --git a/src/tools/miri/tests/fail/panic/panic_abort3.stderr b/src/tools/miri/tests/fail/panic/panic_abort3.stderr index 9751053396a..553bfa61635 100644 --- a/src/tools/miri/tests/fail/panic/panic_abort3.stderr +++ b/src/tools/miri/tests/fail/panic/panic_abort3.stderr @@ -17,8 +17,8 @@ LL | ABORT() = note: inside `std::panicking::rust_panic` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside `std::panicking::panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC - = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::begin_panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC - = note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC + = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC + = note: inside `std::panicking::panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC note: inside `main` --> tests/fail/panic/panic_abort3.rs:LL:CC | diff --git a/src/tools/miri/tests/fail/panic/panic_abort4.stderr b/src/tools/miri/tests/fail/panic/panic_abort4.stderr index a84824fcc81..07ecab6661e 100644 --- a/src/tools/miri/tests/fail/panic/panic_abort4.stderr +++ b/src/tools/miri/tests/fail/panic/panic_abort4.stderr @@ -17,8 +17,8 @@ LL | ABORT() = note: inside `std::panicking::rust_panic` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside `std::panicking::panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC - = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::begin_panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC - = note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC + = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC + = note: inside `std::panicking::panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC note: inside `main` --> tests/fail/panic/panic_abort4.rs:LL:CC | diff --git a/src/tools/miri/tests/fail/ptr_swap_nonoverlapping.stderr b/src/tools/miri/tests/fail/ptr_swap_nonoverlapping.stderr index d2b557f13f3..70f5d498da3 100644 --- a/src/tools/miri/tests/fail/ptr_swap_nonoverlapping.stderr +++ b/src/tools/miri/tests/fail/ptr_swap_nonoverlapping.stderr @@ -16,8 +16,8 @@ LL | ABORT() = note: inside `std::sys::pal::PLATFORM::abort_internal` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC = note: inside `std::panicking::panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC - = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::begin_panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC - = note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC + = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC + = note: inside `std::panicking::panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC note: inside `main` --> tests/fail/ptr_swap_nonoverlapping.rs:LL:CC | diff --git a/src/tools/miri/tests/fail/terminate-terminator.stderr b/src/tools/miri/tests/fail/terminate-terminator.stderr index 96df302c397..228e2d3de1f 100644 --- a/src/tools/miri/tests/fail/terminate-terminator.stderr +++ b/src/tools/miri/tests/fail/terminate-terminator.stderr @@ -20,8 +20,8 @@ LL | ABORT() = note: inside `std::sys::pal::PLATFORM::abort_internal` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC = note: inside `std::panicking::panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC - = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::begin_panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC - = note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC + = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC + = note: inside `std::panicking::panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside `core::panicking::panic_nounwind` at RUSTLIB/core/src/panicking.rs:LL:CC = note: inside `core::panicking::panic_cannot_unwind` at RUSTLIB/core/src/panicking.rs:LL:CC note: inside `has_cleanup` diff --git a/src/tools/miri/tests/fail/unwind-action-terminate.stderr b/src/tools/miri/tests/fail/unwind-action-terminate.stderr index 06c63c70ede..1c0f8cb7ec6 100644 --- a/src/tools/miri/tests/fail/unwind-action-terminate.stderr +++ b/src/tools/miri/tests/fail/unwind-action-terminate.stderr @@ -18,8 +18,8 @@ LL | ABORT() = note: inside `std::sys::pal::PLATFORM::abort_internal` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC = note: inside `std::panicking::panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC - = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::begin_panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC - = note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC + = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC + = note: inside `std::panicking::panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC = note: inside `core::panicking::panic_nounwind` at RUSTLIB/core/src/panicking.rs:LL:CC = note: inside `core::panicking::panic_cannot_unwind` at RUSTLIB/core/src/panicking.rs:LL:CC note: inside `panic_abort` diff --git a/src/tools/miri/tests/panic/panic1.stderr b/src/tools/miri/tests/panic/panic1.stderr index 9ca5234dd03..ff7e287b5a5 100644 --- a/src/tools/miri/tests/panic/panic1.stderr +++ b/src/tools/miri/tests/panic/panic1.stderr @@ -2,7 +2,7 @@ thread 'main' ($TID) panicked at tests/panic/panic1.rs:LL:CC: panicking from libstd stack backtrace: - 0: std::panicking::begin_panic_handler + 0: std::panicking::panic_handler at RUSTLIB/std/src/panicking.rs:LL:CC 1: std::rt::panic_fmt at RUSTLIB/core/src/panicking.rs:LL:CC diff --git a/src/tools/miri/tests/pass/float.rs b/src/tools/miri/tests/pass/float.rs index 96590b4bf2b..fe7316c6680 100644 --- a/src/tools/miri/tests/pass/float.rs +++ b/src/tools/miri/tests/pass/float.rs @@ -1088,8 +1088,6 @@ pub fn libm() { assert_approx_eq!(1f32.exp_m1(), f32::consts::E - 1.0); assert_approx_eq!(1f64.exp_m1(), f64::consts::E - 1.0); - assert_approx_eq!(f32::NEG_INFINITY.exp_m1(), -1.0); - assert_approx_eq!(f64::NEG_INFINITY.exp_m1(), -1.0); assert_approx_eq!(10f32.exp2(), 1024f32); assert_approx_eq!(50f64.exp2(), 1125899906842624f64); @@ -1125,7 +1123,6 @@ pub fn libm() { assert_eq!(ldexp(0.65f64, 3i32), 5.2f64); assert_eq!(ldexp(1.42, 0xFFFF), f64::INFINITY); assert_eq!(ldexp(1.42, -0xFFFF), 0f64); - assert_eq!(ldexp(42.0, 0), 42.0); // Trigonometric functions. @@ -1134,14 +1131,8 @@ pub fn libm() { assert_approx_eq!((f64::consts::PI / 2f64).sin(), 1f64); assert_approx_eq!(f32::consts::FRAC_PI_6.sin(), 0.5); assert_approx_eq!(f64::consts::FRAC_PI_6.sin(), 0.5); - // Increase error tolerance from 12ULP to 16ULP because of the extra operation. - assert_approx_eq!(f32::consts::FRAC_PI_4.sin().asin(), f32::consts::FRAC_PI_4, 16); - assert_approx_eq!(f64::consts::FRAC_PI_4.sin().asin(), f64::consts::FRAC_PI_4, 16); - assert_biteq(0.0f32.asin(), 0.0f32, "asin(+0) = +0"); - assert_biteq((-0.0f32).asin(), -0.0, "asin(-0) = -0"); - assert_biteq(0.0f64.asin(), 0.0, "asin(+0) = +0"); - assert_biteq((-0.0f64).asin(), -0.0, "asin(-0) = -0"); - + assert_approx_eq!(f32::consts::FRAC_PI_4.sin().asin(), f32::consts::FRAC_PI_4); + assert_approx_eq!(f64::consts::FRAC_PI_4.sin().asin(), f64::consts::FRAC_PI_4); assert_approx_eq!(1.0f32.sinh(), 1.1752012f32); assert_approx_eq!(1.0f64.sinh(), 1.1752011936438014f64); @@ -1168,18 +1159,11 @@ pub fn libm() { assert_approx_eq!((f64::consts::PI * 2f64).cos(), 1f64); assert_approx_eq!(f32::consts::FRAC_PI_3.cos(), 0.5); assert_approx_eq!(f64::consts::FRAC_PI_3.cos(), 0.5); - // Increase error tolerance from 12ULP to 16ULP because of the extra operation. - assert_approx_eq!(f32::consts::FRAC_PI_4.cos().acos(), f32::consts::FRAC_PI_4, 16); - assert_approx_eq!(f64::consts::FRAC_PI_4.cos().acos(), f64::consts::FRAC_PI_4, 16); - assert_biteq(1.0f32.acos(), 0.0, "acos(1) = 0"); - assert_biteq(1.0f64.acos(), 0.0, "acos(1) = 0"); + assert_approx_eq!(f32::consts::FRAC_PI_4.cos().acos(), f32::consts::FRAC_PI_4); + assert_approx_eq!(f64::consts::FRAC_PI_4.cos().acos(), f64::consts::FRAC_PI_4); assert_approx_eq!(1.0f32.cosh(), 1.54308f32); assert_approx_eq!(1.0f64.cosh(), 1.5430806348152437f64); - assert_eq!(0.0f32.cosh(), 1.0); - assert_eq!(0.0f64.cosh(), 1.0); - assert_eq!((-0.0f32).cosh(), 1.0); - assert_eq!((-0.0f64).cosh(), 1.0); assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32); assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64); @@ -1189,47 +1173,6 @@ pub fn libm() { assert_approx_eq!(1.0_f64, 1.0_f64.tan().atan()); assert_approx_eq!(1.0f32.atan2(2.0f32), 0.46364761f32); assert_approx_eq!(1.0f32.atan2(2.0f32), 0.46364761f32); - // C standard defines a bunch of fixed outputs for atan2 - macro_rules! fixed_atan2_cases{ - ($float_type:ident) => {{ - use std::$float_type::consts::{PI, FRAC_PI_2, FRAC_PI_4}; - use $float_type::{INFINITY, NEG_INFINITY}; - - // atan2(±0,−0) = ±π. - assert_eq!($float_type::atan2(0.0, -0.0), PI, "atan2(0,−0) = π"); - assert_eq!($float_type::atan2(-0.0, -0.0), -PI, "atan2(-0,−0) = -π"); - - // atan2(±0, y) = ±π for y < 0. - assert_eq!($float_type::atan2(0.0, -1.0), PI, "atan2(0, y) = π for y < 0."); - assert_eq!($float_type::atan2(-0.0, -1.0), -PI, "atan2(-0, y) = -π for y < 0."); - - // atan2(x, ±0) = −π/2 for x < 0. - assert_eq!($float_type::atan2(-1.0, 0.0), -FRAC_PI_2, "atan2(x, 0) = −π/2 for x < 0"); - assert_eq!($float_type::atan2(-1.0, -0.0), -FRAC_PI_2, "atan2(x, -0) = −π/2 for x < 0"); - - // atan2(x, ±0) = π/2 for x > 0. - assert_eq!($float_type::atan2(1.0, 0.0), FRAC_PI_2, "atan2(x, 0) = π/2 for x > 0."); - assert_eq!($float_type::atan2(1.0, -0.0), FRAC_PI_2, "atan2(x, -0) = π/2 for x > 0."); - - // atan2(±x,−∞) = ±π for finite x > 0. - assert_eq!($float_type::atan2(1.0, NEG_INFINITY), PI, "atan2(x, −∞) = π for finite x > 0"); - assert_eq!($float_type::atan2(-1.0, NEG_INFINITY), -PI, "atan2(-x, −∞) = -π for finite x > 0"); - - // atan2(±∞, y) returns ±π/2 for finite y. - assert_eq!($float_type::atan2(INFINITY, 1.0), FRAC_PI_2, "atan2(+∞, y) returns π/2 for finite y"); - assert_eq!($float_type::atan2(NEG_INFINITY, 1.0), -FRAC_PI_2, "atan2(-∞, y) returns -π/2 for finite y"); - - // atan2(±∞, −∞) = ±3π/4 - assert_eq!($float_type::atan2(INFINITY, NEG_INFINITY), 3.0 * FRAC_PI_4, "atan2(+∞, −∞) = 3π/4"); - assert_eq!($float_type::atan2(NEG_INFINITY, NEG_INFINITY), -3.0 * FRAC_PI_4, "atan2(-∞, −∞) = -3π/4"); - - // atan2(±∞, +∞) = ±π/4 - assert_eq!($float_type::atan2(INFINITY, INFINITY), FRAC_PI_4, "atan2(+∞, +∞) = π/4"); - assert_eq!($float_type::atan2(NEG_INFINITY, INFINITY), -FRAC_PI_4, "atan2(-∞, +∞) = -π/4"); - }} - } - fixed_atan2_cases!(f32); - fixed_atan2_cases!(f64); assert_approx_eq!( 1.0f32.tanh(), @@ -1239,11 +1182,6 @@ pub fn libm() { 1.0f64.tanh(), (1.0 - f64::consts::E.powi(-2)) / (1.0 + f64::consts::E.powi(-2)) ); - assert_eq!(f32::INFINITY.tanh(), 1.0); - assert_eq!(f32::NEG_INFINITY.tanh(), -1.0); - assert_eq!(f64::INFINITY.tanh(), 1.0); - assert_eq!(f64::NEG_INFINITY.tanh(), -1.0); - assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32); assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64); @@ -1264,14 +1202,8 @@ pub fn libm() { assert_approx_eq!(1.0f32.erf(), 0.84270079294971486934122063508260926f32); assert_approx_eq!(1.0f64.erf(), 0.84270079294971486934122063508260926f64); - assert_eq!(f32::INFINITY.erf(), 1.0); - assert_eq!(f64::INFINITY.erf(), 1.0); assert_approx_eq!(1.0f32.erfc(), 0.15729920705028513065877936491739074f32); assert_approx_eq!(1.0f64.erfc(), 0.15729920705028513065877936491739074f64); - assert_eq!(f32::NEG_INFINITY.erfc(), 2.0); - assert_eq!(f64::NEG_INFINITY.erfc(), 2.0); - assert_eq!(f32::INFINITY.erfc(), 0.0); - assert_eq!(f64::INFINITY.erfc(), 0.0); } fn test_fast() { @@ -1481,6 +1413,7 @@ fn test_non_determinism() { } pub fn test_operations_f32(a: f32, b: f32) { test_operations_f!(a, b); + // FIXME: some are temporarily disabled as it breaks std tests. ensure_nondet(|| a.powf(b)); ensure_nondet(|| a.powi(2)); ensure_nondet(|| a.log(b)); @@ -1489,34 +1422,35 @@ fn test_non_determinism() { ensure_nondet(|| f32::consts::E.ln()); ensure_nondet(|| 10f32.log10()); ensure_nondet(|| 8f32.log2()); - ensure_nondet(|| 1f32.ln_1p()); - ensure_nondet(|| 27.0f32.cbrt()); - ensure_nondet(|| 3.0f32.hypot(4.0f32)); + // ensure_nondet(|| 1f32.ln_1p()); + // ensure_nondet(|| 27.0f32.cbrt()); + // ensure_nondet(|| 3.0f32.hypot(4.0f32)); ensure_nondet(|| 1f32.sin()); ensure_nondet(|| 1f32.cos()); // On i686-pc-windows-msvc , these functions are implemented by calling the `f64` version, // which means the little rounding errors Miri introduces are discarded by the cast down to // `f32`. Just skip the test for them. - if !cfg!(all(target_os = "windows", target_env = "msvc", target_arch = "x86")) { - ensure_nondet(|| 1.0f32.tan()); - ensure_nondet(|| 1.0f32.asin()); - ensure_nondet(|| 5.0f32.acos()); - ensure_nondet(|| 1.0f32.atan()); - ensure_nondet(|| 1.0f32.atan2(2.0f32)); - ensure_nondet(|| 1.0f32.sinh()); - ensure_nondet(|| 1.0f32.cosh()); - ensure_nondet(|| 1.0f32.tanh()); - } - ensure_nondet(|| 1.0f32.asinh()); - ensure_nondet(|| 2.0f32.acosh()); - ensure_nondet(|| 0.5f32.atanh()); - ensure_nondet(|| 5.0f32.gamma()); - ensure_nondet(|| 5.0f32.ln_gamma()); - ensure_nondet(|| 5.0f32.erf()); - ensure_nondet(|| 5.0f32.erfc()); + // if !cfg!(all(target_os = "windows", target_env = "msvc", target_arch = "x86")) { + // ensure_nondet(|| 1.0f32.tan()); + // ensure_nondet(|| 1.0f32.asin()); + // ensure_nondet(|| 5.0f32.acos()); + // ensure_nondet(|| 1.0f32.atan()); + // ensure_nondet(|| 1.0f32.atan2(2.0f32)); + // ensure_nondet(|| 1.0f32.sinh()); + // ensure_nondet(|| 1.0f32.cosh()); + // ensure_nondet(|| 1.0f32.tanh()); + // } + // ensure_nondet(|| 1.0f32.asinh()); + // ensure_nondet(|| 2.0f32.acosh()); + // ensure_nondet(|| 0.5f32.atanh()); + // ensure_nondet(|| 5.0f32.gamma()); + // ensure_nondet(|| 5.0f32.ln_gamma()); + // ensure_nondet(|| 5.0f32.erf()); + // ensure_nondet(|| 5.0f32.erfc()); } pub fn test_operations_f64(a: f64, b: f64) { test_operations_f!(a, b); + // FIXME: some are temporarily disabled as it breaks std tests. ensure_nondet(|| a.powf(b)); ensure_nondet(|| a.powi(2)); ensure_nondet(|| a.log(b)); @@ -1525,26 +1459,26 @@ fn test_non_determinism() { ensure_nondet(|| 3f64.ln()); ensure_nondet(|| f64::consts::E.log10()); ensure_nondet(|| f64::consts::E.log2()); - ensure_nondet(|| 1f64.ln_1p()); - ensure_nondet(|| 27.0f64.cbrt()); - ensure_nondet(|| 3.0f64.hypot(4.0f64)); + // ensure_nondet(|| 1f64.ln_1p()); + // ensure_nondet(|| 27.0f64.cbrt()); + // ensure_nondet(|| 3.0f64.hypot(4.0f64)); ensure_nondet(|| 1f64.sin()); ensure_nondet(|| 1f64.cos()); - ensure_nondet(|| 1.0f64.tan()); - ensure_nondet(|| 1.0f64.asin()); - ensure_nondet(|| 5.0f64.acos()); - ensure_nondet(|| 1.0f64.atan()); - ensure_nondet(|| 1.0f64.atan2(2.0f64)); - ensure_nondet(|| 1.0f64.sinh()); - ensure_nondet(|| 1.0f64.cosh()); - ensure_nondet(|| 1.0f64.tanh()); - ensure_nondet(|| 1.0f64.asinh()); - ensure_nondet(|| 3.0f64.acosh()); - ensure_nondet(|| 0.5f64.atanh()); - ensure_nondet(|| 5.0f64.gamma()); - ensure_nondet(|| 5.0f64.ln_gamma()); - ensure_nondet(|| 5.0f64.erf()); - ensure_nondet(|| 5.0f64.erfc()); + // ensure_nondet(|| 1.0f64.tan()); + // ensure_nondet(|| 1.0f64.asin()); + // ensure_nondet(|| 5.0f64.acos()); + // ensure_nondet(|| 1.0f64.atan()); + // ensure_nondet(|| 1.0f64.atan2(2.0f64)); + // ensure_nondet(|| 1.0f64.sinh()); + // ensure_nondet(|| 1.0f64.cosh()); + // ensure_nondet(|| 1.0f64.tanh()); + // ensure_nondet(|| 1.0f64.asinh()); + // ensure_nondet(|| 3.0f64.acosh()); + // ensure_nondet(|| 0.5f64.atanh()); + // ensure_nondet(|| 5.0f64.gamma()); + // ensure_nondet(|| 5.0f64.ln_gamma()); + // ensure_nondet(|| 5.0f64.erf()); + // ensure_nondet(|| 5.0f64.erfc()); } pub fn test_operations_f128(a: f128, b: f128) { test_operations_f!(a, b); diff --git a/src/tools/run-make-support/src/external_deps/c_cxx_compiler/extras.rs b/src/tools/run-make-support/src/external_deps/c_cxx_compiler/extras.rs index ac7392641c0..5c980068e4b 100644 --- a/src/tools/run-make-support/src/external_deps/c_cxx_compiler/extras.rs +++ b/src/tools/run-make-support/src/external_deps/c_cxx_compiler/extras.rs @@ -1,15 +1,23 @@ -use crate::{is_win7, is_windows, is_windows_msvc, uname}; +use crate::{is_arm64ec, is_win7, is_windows, is_windows_msvc, uname}; + +fn get_windows_msvc_libs() -> Vec<&'static str> { + let mut libs = + vec!["ws2_32.lib", "userenv.lib", "bcrypt.lib", "ntdll.lib", "synchronization.lib"]; + if is_win7() { + libs.push("advapi32.lib"); + } + libs +} /// `EXTRACFLAGS` pub fn extra_c_flags() -> Vec<&'static str> { if is_windows() { if is_windows_msvc() { - let mut libs = - vec!["ws2_32.lib", "userenv.lib", "bcrypt.lib", "ntdll.lib", "synchronization.lib"]; - if is_win7() { - libs.push("advapi32.lib"); + let mut args = get_windows_msvc_libs(); + if is_arm64ec() { + args.push("/arm64EC"); } - libs + args } else { vec!["-lws2_32", "-luserenv", "-lbcrypt", "-lntdll", "-lsynchronization"] } @@ -26,6 +34,18 @@ pub fn extra_c_flags() -> Vec<&'static str> { } } +pub fn extra_linker_flags() -> Vec<&'static str> { + if is_windows_msvc() { + let mut args = get_windows_msvc_libs(); + if is_arm64ec() { + args.push("/MACHINE:ARM64EC"); + } + args + } else { + vec![] + } +} + /// `EXTRACXXFLAGS` pub fn extra_cxx_flags() -> Vec<&'static str> { if is_windows() { diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index b7d89b130c6..191e205f257 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -56,7 +56,7 @@ pub use crate::external_deps::c_build::{ }; // Re-exports of external dependencies. pub use crate::external_deps::c_cxx_compiler::{ - Cc, Gcc, cc, cxx, extra_c_flags, extra_cxx_flags, gcc, + Cc, Gcc, cc, cxx, extra_c_flags, extra_cxx_flags, extra_linker_flags, gcc, }; pub use crate::external_deps::cargo::cargo; pub use crate::external_deps::clang::{Clang, clang}; @@ -84,6 +84,6 @@ pub use crate::string::{ }; // Helpers for checking target information. pub use crate::targets::{ - apple_os, is_aix, is_darwin, is_win7, is_windows, is_windows_gnu, is_windows_msvc, + apple_os, is_aix, is_arm64ec, is_darwin, is_win7, is_windows, is_windows_gnu, is_windows_msvc, llvm_components_contain, target, uname, }; diff --git a/src/tools/run-make-support/src/targets.rs b/src/tools/run-make-support/src/targets.rs index b20e12561fb..6288f5f7c59 100644 --- a/src/tools/run-make-support/src/targets.rs +++ b/src/tools/run-make-support/src/targets.rs @@ -46,6 +46,12 @@ pub fn is_aix() -> bool { target().contains("aix") } +/// Check if target is arm64ec. +#[must_use] +pub fn is_arm64ec() -> bool { + target().starts_with("arm64ec") +} + /// Get the target OS on Apple operating systems. #[must_use] pub fn apple_os() -> &'static str { diff --git a/src/tools/rust-analyzer/crates/syntax-bridge/src/lib.rs b/src/tools/rust-analyzer/crates/syntax-bridge/src/lib.rs index bdff671802c..4e525be3fe3 100644 --- a/src/tools/rust-analyzer/crates/syntax-bridge/src/lib.rs +++ b/src/tools/rust-analyzer/crates/syntax-bridge/src/lib.rs @@ -127,11 +127,11 @@ where // The following items are what `rustc` macro can be parsed into : // link: https://github.com/rust-lang/rust/blob/9ebf47851a357faa4cd97f4b1dc7835f6376e639/src/libsyntax/ext/expand.rs#L141 -// * Expr(P<ast::Expr>) -> token_tree_to_expr -// * Pat(P<ast::Pat>) -> token_tree_to_pat -// * Ty(P<ast::Ty>) -> token_tree_to_ty +// * Expr(Box<ast::Expr>) -> token_tree_to_expr +// * Pat(Box<ast::Pat>) -> token_tree_to_pat +// * Ty(Box<ast::Ty>) -> token_tree_to_ty // * Stmts(SmallVec<[ast::Stmt; 1]>) -> token_tree_to_stmts -// * Items(SmallVec<[P<ast::Item>; 1]>) -> token_tree_to_items +// * Items(SmallVec<[Box<ast::Item>; 1]>) -> token_tree_to_items // // * TraitItems(SmallVec<[ast::TraitItem; 1]>) // * AssocItems(SmallVec<[ast::AssocItem; 1]>) diff --git a/src/tools/rustfmt/src/chains.rs b/src/tools/rustfmt/src/chains.rs index 034ecde068a..2f388197ea1 100644 --- a/src/tools/rustfmt/src/chains.rs +++ b/src/tools/rustfmt/src/chains.rs @@ -58,7 +58,7 @@ use std::borrow::Cow; use std::cmp::min; -use rustc_ast::{ast, ptr}; +use rustc_ast::ast; use rustc_span::{BytePos, Span, symbol}; use tracing::debug; @@ -187,7 +187,7 @@ enum ChainItemKind { MethodCall( ast::PathSegment, Vec<ast::GenericArg>, - ThinVec<ptr::P<ast::Expr>>, + ThinVec<Box<ast::Expr>>, ), StructField(symbol::Ident), TupleField(symbol::Ident, bool), @@ -343,7 +343,7 @@ impl ChainItem { fn rewrite_method_call( method_name: symbol::Ident, types: &[ast::GenericArg], - args: &[ptr::P<ast::Expr>], + args: &[Box<ast::Expr>], span: Span, context: &RewriteContext<'_>, shape: Shape, diff --git a/src/tools/rustfmt/src/closures.rs b/src/tools/rustfmt/src/closures.rs index 61e148cdf18..bb10a7946b8 100644 --- a/src/tools/rustfmt/src/closures.rs +++ b/src/tools/rustfmt/src/closures.rs @@ -1,4 +1,4 @@ -use rustc_ast::{ast, ptr}; +use rustc_ast::ast; use rustc_span::Span; use thin_vec::thin_vec; use tracing::debug; @@ -165,7 +165,7 @@ fn rewrite_closure_with_block( let block = ast::Block { stmts: thin_vec![ast::Stmt { id: ast::NodeId::root(), - kind: ast::StmtKind::Expr(ptr::P(body.clone())), + kind: ast::StmtKind::Expr(Box::new(body.clone())), span: body.span, }], id: ast::NodeId::root(), diff --git a/src/tools/rustfmt/src/expr.rs b/src/tools/rustfmt/src/expr.rs index 08aedff2b20..975f9be44e4 100644 --- a/src/tools/rustfmt/src/expr.rs +++ b/src/tools/rustfmt/src/expr.rs @@ -3,7 +3,7 @@ use std::cmp::min; use itertools::Itertools; use rustc_ast::token::{Delimiter, Lit, LitKind}; -use rustc_ast::{ForLoopKind, MatchKind, ast, ptr, token}; +use rustc_ast::{ForLoopKind, MatchKind, ast, token}; use rustc_span::{BytePos, Span}; use tracing::debug; @@ -1368,7 +1368,7 @@ fn choose_separator_tactic(context: &RewriteContext<'_>, span: Span) -> Option<S pub(crate) fn rewrite_call( context: &RewriteContext<'_>, callee: &str, - args: &[ptr::P<ast::Expr>], + args: &[Box<ast::Expr>], span: Span, shape: Shape, ) -> RewriteResult { @@ -1634,7 +1634,7 @@ fn struct_lit_can_be_aligned(fields: &[ast::ExprField], has_base: bool) -> bool fn rewrite_struct_lit<'a>( context: &RewriteContext<'_>, path: &ast::Path, - qself: &Option<ptr::P<ast::QSelf>>, + qself: &Option<Box<ast::QSelf>>, fields: &'a [ast::ExprField], struct_rest: &ast::StructRest, attrs: &[ast::Attribute], diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs index 7084639aca9..57d4142ebe4 100644 --- a/src/tools/rustfmt/src/items.rs +++ b/src/tools/rustfmt/src/items.rs @@ -4,8 +4,8 @@ use std::borrow::Cow; use std::cmp::{Ordering, max, min}; use regex::Regex; +use rustc_ast::ast; use rustc_ast::visit; -use rustc_ast::{ast, ptr}; use rustc_span::{BytePos, DUMMY_SP, Ident, Span, symbol}; use tracing::debug; @@ -725,9 +725,9 @@ impl<'a> FmtVisitor<'a> { .ok() } - fn visit_impl_items(&mut self, items: &[ptr::P<ast::AssocItem>]) { + fn visit_impl_items(&mut self, items: &[Box<ast::AssocItem>]) { if self.get_context().config.reorder_impl_items() { - type TyOpt = Option<ptr::P<ast::Ty>>; + type TyOpt = Option<Box<ast::Ty>>; use crate::ast::AssocItemKind::*; let is_type = |ty: &TyOpt| opaque_ty(ty).is_none(); let is_opaque = |ty: &TyOpt| opaque_ty(ty).is_some(); @@ -934,7 +934,7 @@ pub(crate) fn format_impl( fn is_impl_single_line( context: &RewriteContext<'_>, - items: &[ptr::P<ast::AssocItem>], + items: &[Box<ast::AssocItem>], result: &str, where_clause_str: &str, item: &ast::Item, @@ -2024,7 +2024,7 @@ pub(crate) struct StaticParts<'a> { generics: Option<&'a ast::Generics>, ty: &'a ast::Ty, mutability: ast::Mutability, - expr_opt: Option<&'a ptr::P<ast::Expr>>, + expr_opt: Option<&'a Box<ast::Expr>>, defaultness: Option<ast::Defaultness>, span: Span, } diff --git a/src/tools/rustfmt/src/macros.rs b/src/tools/rustfmt/src/macros.rs index 0ff0aad7a2d..2e7ac90f596 100644 --- a/src/tools/rustfmt/src/macros.rs +++ b/src/tools/rustfmt/src/macros.rs @@ -12,9 +12,9 @@ use std::collections::HashMap; use std::panic::{AssertUnwindSafe, catch_unwind}; +use rustc_ast::ast; use rustc_ast::token::{Delimiter, Token, TokenKind}; use rustc_ast::tokenstream::{TokenStream, TokenStreamIter, TokenTree}; -use rustc_ast::{ast, ptr}; use rustc_ast_pretty::pprust; use rustc_span::{BytePos, DUMMY_SP, Ident, Span, Symbol}; use tracing::debug; @@ -53,10 +53,10 @@ pub(crate) enum MacroPosition { #[derive(Debug)] pub(crate) enum MacroArg { - Expr(ptr::P<ast::Expr>), - Ty(ptr::P<ast::Ty>), - Pat(ptr::P<ast::Pat>), - Item(ptr::P<ast::Item>), + Expr(Box<ast::Expr>), + Ty(Box<ast::Ty>), + Pat(Box<ast::Pat>), + Item(Box<ast::Item>), Keyword(Ident, Span), } diff --git a/src/tools/rustfmt/src/matches.rs b/src/tools/rustfmt/src/matches.rs index 8f62648e576..209cb2f9287 100644 --- a/src/tools/rustfmt/src/matches.rs +++ b/src/tools/rustfmt/src/matches.rs @@ -2,7 +2,7 @@ use std::iter::repeat; -use rustc_ast::{MatchKind, ast, ptr}; +use rustc_ast::{MatchKind, ast}; use rustc_span::{BytePos, Span}; use tracing::debug; @@ -394,7 +394,7 @@ fn flatten_arm_body<'a>( fn rewrite_match_body( context: &RewriteContext<'_>, - body: &ptr::P<ast::Expr>, + body: &Box<ast::Expr>, pats_str: &str, shape: Shape, has_guard: bool, @@ -569,7 +569,7 @@ fn rewrite_match_body( // The `if ...` guard on a match arm. fn rewrite_guard( context: &RewriteContext<'_>, - guard: &Option<ptr::P<ast::Expr>>, + guard: &Option<Box<ast::Expr>>, shape: Shape, // The amount of space used up on this line for the pattern in // the arm (excludes offset). diff --git a/src/tools/rustfmt/src/modules.rs b/src/tools/rustfmt/src/modules.rs index 44c8123517c..3bc656b64b3 100644 --- a/src/tools/rustfmt/src/modules.rs +++ b/src/tools/rustfmt/src/modules.rs @@ -26,7 +26,7 @@ type FileModMap<'ast> = BTreeMap<FileName, Module<'ast>>; #[derive(Debug, Clone)] pub(crate) struct Module<'a> { ast_mod_kind: Option<Cow<'a, ast::ModKind>>, - pub(crate) items: Cow<'a, ThinVec<rustc_ast::ptr::P<ast::Item>>>, + pub(crate) items: Cow<'a, ThinVec<Box<ast::Item>>>, inner_attr: ast::AttrVec, pub(crate) span: Span, } @@ -35,7 +35,7 @@ impl<'a> Module<'a> { pub(crate) fn new( mod_span: Span, ast_mod_kind: Option<Cow<'a, ast::ModKind>>, - mod_items: Cow<'a, ThinVec<rustc_ast::ptr::P<ast::Item>>>, + mod_items: Cow<'a, ThinVec<Box<ast::Item>>>, mod_attrs: Cow<'a, ast::AttrVec>, ) -> Self { let inner_attr = mod_attrs @@ -170,7 +170,7 @@ impl<'ast, 'psess, 'c> ModResolver<'ast, 'psess> { /// Visit modules defined inside macro calls. fn visit_mod_outside_ast( &mut self, - items: ThinVec<rustc_ast::ptr::P<ast::Item>>, + items: ThinVec<Box<ast::Item>>, ) -> Result<(), ModuleResolutionError> { for item in items { if is_cfg_if(&item) { @@ -197,7 +197,7 @@ impl<'ast, 'psess, 'c> ModResolver<'ast, 'psess> { /// Visit modules from AST. fn visit_mod_from_ast( &mut self, - items: &'ast [rustc_ast::ptr::P<ast::Item>], + items: &'ast [Box<ast::Item>], ) -> Result<(), ModuleResolutionError> { for item in items { if is_cfg_if(item) { diff --git a/src/tools/rustfmt/src/overflow.rs b/src/tools/rustfmt/src/overflow.rs index 728adff2c7d..bb1ebc87f67 100644 --- a/src/tools/rustfmt/src/overflow.rs +++ b/src/tools/rustfmt/src/overflow.rs @@ -3,8 +3,8 @@ use std::cmp::min; use itertools::Itertools; +use rustc_ast::ast; use rustc_ast::token::Delimiter; -use rustc_ast::{ast, ptr}; use rustc_span::Span; use tracing::debug; @@ -219,7 +219,7 @@ pub(crate) trait IntoOverflowableItem<'a>: Rewrite + Spanned { fn into_overflowable_item(&'a self) -> OverflowableItem<'a>; } -impl<'a, T: 'a + IntoOverflowableItem<'a>> IntoOverflowableItem<'a> for ptr::P<T> { +impl<'a, T: 'a + IntoOverflowableItem<'a>> IntoOverflowableItem<'a> for Box<T> { fn into_overflowable_item(&'a self) -> OverflowableItem<'a> { (**self).into_overflowable_item() } diff --git a/src/tools/rustfmt/src/parse/macros/lazy_static.rs b/src/tools/rustfmt/src/parse/macros/lazy_static.rs index cbe81004e22..8611615d123 100644 --- a/src/tools/rustfmt/src/parse/macros/lazy_static.rs +++ b/src/tools/rustfmt/src/parse/macros/lazy_static.rs @@ -1,5 +1,4 @@ use rustc_ast::ast; -use rustc_ast::ptr::P; use rustc_ast::token; use rustc_ast::tokenstream::TokenStream; use rustc_parse::exp; @@ -10,7 +9,7 @@ use crate::rewrite::RewriteContext; pub(crate) fn parse_lazy_static( context: &RewriteContext<'_>, ts: TokenStream, -) -> Option<Vec<(ast::Visibility, symbol::Ident, P<ast::Ty>, P<ast::Expr>)>> { +) -> Option<Vec<(ast::Visibility, symbol::Ident, Box<ast::Ty>, Box<ast::Expr>)>> { let mut result = vec![]; let mut parser = super::build_parser(context, ts); macro_rules! parse_or { diff --git a/src/tools/rustfmt/src/parse/macros/mod.rs b/src/tools/rustfmt/src/parse/macros/mod.rs index d7964484b26..724d7a09e4a 100644 --- a/src/tools/rustfmt/src/parse/macros/mod.rs +++ b/src/tools/rustfmt/src/parse/macros/mod.rs @@ -1,6 +1,6 @@ +use rustc_ast::ast; use rustc_ast::token::{Delimiter, NonterminalKind, NtExprKind::*, NtPatKind::*, TokenKind}; use rustc_ast::tokenstream::TokenStream; -use rustc_ast::{ast, ptr}; use rustc_parse::MACRO_ARGUMENTS; use rustc_parse::parser::{ForceCollect, Parser, Recovery}; use rustc_session::parse::ParseSess; @@ -49,26 +49,26 @@ fn parse_macro_arg<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> { Expr, NonterminalKind::Expr(Expr), |parser: &mut Parser<'b>| parser.parse_expr(), - |x: ptr::P<ast::Expr>| Some(x) + |x: Box<ast::Expr>| Some(x) ); parse_macro_arg!( Ty, NonterminalKind::Ty, |parser: &mut Parser<'b>| parser.parse_ty(), - |x: ptr::P<ast::Ty>| Some(x) + |x: Box<ast::Ty>| Some(x) ); parse_macro_arg!( Pat, NonterminalKind::Pat(PatParam { inferred: false }), |parser: &mut Parser<'b>| parser.parse_pat_no_top_alt(None, None), - |x: ptr::P<ast::Pat>| Some(x) + |x: Box<ast::Pat>| Some(x) ); - // `parse_item` returns `Option<ptr::P<ast::Item>>`. + // `parse_item` returns `Option<Box<ast::Item>>`. parse_macro_arg!( Item, NonterminalKind::Item, |parser: &mut Parser<'b>| parser.parse_item(ForceCollect::No), - |x: Option<ptr::P<ast::Item>>| x + |x: Option<Box<ast::Item>>| x ); None @@ -164,7 +164,7 @@ pub(crate) fn parse_macro_args( pub(crate) fn parse_expr( context: &RewriteContext<'_>, tokens: TokenStream, -) -> Option<ptr::P<ast::Expr>> { +) -> Option<Box<ast::Expr>> { let mut parser = build_parser(context, tokens); parser.parse_expr().ok() } diff --git a/src/tools/rustfmt/src/parse/parser.rs b/src/tools/rustfmt/src/parse/parser.rs index f357aed66c2..2ec8769c45f 100644 --- a/src/tools/rustfmt/src/parse/parser.rs +++ b/src/tools/rustfmt/src/parse/parser.rs @@ -1,7 +1,7 @@ use std::panic::{AssertUnwindSafe, catch_unwind}; use std::path::{Path, PathBuf}; -use rustc_ast::{ast, attr, ptr}; +use rustc_ast::{ast, attr}; use rustc_errors::Diag; use rustc_parse::parser::Parser as RawParser; use rustc_parse::{exp, new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal}; @@ -102,7 +102,7 @@ impl<'a> Parser<'a> { psess: &'a ParseSess, path: &Path, span: Span, - ) -> Result<(ast::AttrVec, ThinVec<ptr::P<ast::Item>>, Span), ParserError> { + ) -> Result<(ast::AttrVec, ThinVec<Box<ast::Item>>, Span), ParserError> { let result = catch_unwind(AssertUnwindSafe(|| { let mut parser = unwrap_or_emit_fatal(new_parser_from_file(psess.inner(), path, Some(span))); diff --git a/src/tools/rustfmt/src/patterns.rs b/src/tools/rustfmt/src/patterns.rs index cb3879f4be8..d212ecf392a 100644 --- a/src/tools/rustfmt/src/patterns.rs +++ b/src/tools/rustfmt/src/patterns.rs @@ -1,5 +1,4 @@ use rustc_ast::ast::{self, BindingMode, ByRef, Pat, PatField, PatKind, RangeEnd, RangeSyntax}; -use rustc_ast::ptr; use rustc_span::{BytePos, Span}; use crate::comment::{FindUncommented, combine_strs_with_missing_comments}; @@ -77,7 +76,7 @@ fn is_short_pattern_inner(context: &RewriteContext<'_>, pat: &ast::Pat) -> bool } pub(crate) struct RangeOperand<'a, T> { - pub operand: &'a Option<ptr::P<T>>, + pub operand: &'a Option<Box<T>>, pub span: Span, } @@ -329,8 +328,8 @@ impl Rewrite for Pat { pub fn rewrite_range_pat<T: Rewrite>( context: &RewriteContext<'_>, shape: Shape, - lhs: &Option<ptr::P<T>>, - rhs: &Option<ptr::P<T>>, + lhs: &Option<Box<T>>, + rhs: &Option<Box<T>>, end_kind: &rustc_span::source_map::Spanned<RangeEnd>, span: Span, ) -> RewriteResult { @@ -371,7 +370,7 @@ pub fn rewrite_range_pat<T: Rewrite>( } fn rewrite_struct_pat( - qself: &Option<ptr::P<ast::QSelf>>, + qself: &Option<Box<ast::QSelf>>, path: &ast::Path, fields: &[ast::PatField], ellipsis: bool, @@ -505,7 +504,7 @@ impl Rewrite for PatField { #[derive(Debug)] pub(crate) enum TuplePatField<'a> { - Pat(&'a ptr::P<ast::Pat>), + Pat(&'a Box<ast::Pat>), Dotdot(Span), } @@ -562,7 +561,7 @@ pub(crate) fn can_be_overflowed_pat( } fn rewrite_tuple_pat( - pats: &[ptr::P<ast::Pat>], + pats: &[Box<ast::Pat>], path_str: Option<String>, span: Span, context: &RewriteContext<'_>, diff --git a/src/tools/rustfmt/src/rewrite.rs b/src/tools/rustfmt/src/rewrite.rs index 83020709797..f0accd62d1a 100644 --- a/src/tools/rustfmt/src/rewrite.rs +++ b/src/tools/rustfmt/src/rewrite.rs @@ -3,7 +3,6 @@ use std::cell::{Cell, RefCell}; use std::rc::Rc; -use rustc_ast::ptr; use rustc_span::Span; use thiserror::Error; @@ -24,7 +23,7 @@ pub(crate) trait Rewrite { } } -impl<T: Rewrite> Rewrite for ptr::P<T> { +impl<T: Rewrite> Rewrite for Box<T> { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> { (**self).rewrite(context, shape) } diff --git a/src/tools/rustfmt/src/spanned.rs b/src/tools/rustfmt/src/spanned.rs index ac132999b62..020651e2daa 100644 --- a/src/tools/rustfmt/src/spanned.rs +++ b/src/tools/rustfmt/src/spanned.rs @@ -1,6 +1,6 @@ use std::cmp::max; -use rustc_ast::{ast, ptr}; +use rustc_ast::ast; use rustc_span::{Span, source_map}; use crate::macros::MacroArg; @@ -12,7 +12,7 @@ pub(crate) trait Spanned { fn span(&self) -> Span; } -impl<T: Spanned> Spanned for ptr::P<T> { +impl<T: Spanned> Spanned for Box<T> { fn span(&self) -> Span { (**self).span() } diff --git a/src/tools/rustfmt/src/types.rs b/src/tools/rustfmt/src/types.rs index 9ee10d86270..5dce9a0c8d0 100644 --- a/src/tools/rustfmt/src/types.rs +++ b/src/tools/rustfmt/src/types.rs @@ -1,7 +1,6 @@ use std::ops::Deref; use rustc_ast::ast::{self, FnRetTy, Mutability, Term}; -use rustc_ast::ptr; use rustc_span::{BytePos, Pos, Span, symbol::kw}; use tracing::debug; @@ -39,7 +38,7 @@ pub(crate) enum PathContext { pub(crate) fn rewrite_path( context: &RewriteContext<'_>, path_context: PathContext, - qself: &Option<ptr::P<ast::QSelf>>, + qself: &Option<Box<ast::QSelf>>, path: &ast::Path, shape: Shape, ) -> RewriteResult { @@ -1340,7 +1339,7 @@ fn join_bounds_inner( } } -pub(crate) fn opaque_ty(ty: &Option<ptr::P<ast::Ty>>) -> Option<&ast::GenericBounds> { +pub(crate) fn opaque_ty(ty: &Option<Box<ast::Ty>>) -> Option<&ast::GenericBounds> { ty.as_ref().and_then(|t| match &t.kind { ast::TyKind::ImplTrait(_, bounds) => Some(bounds), _ => None, diff --git a/src/tools/rustfmt/src/utils.rs b/src/tools/rustfmt/src/utils.rs index fcd475b1784..b9950e94d0c 100644 --- a/src/tools/rustfmt/src/utils.rs +++ b/src/tools/rustfmt/src/utils.rs @@ -1,10 +1,10 @@ use std::borrow::Cow; +use rustc_ast::YieldKind; use rustc_ast::ast::{ self, Attribute, MetaItem, MetaItemInner, MetaItemKind, NodeId, Path, Visibility, VisibilityKind, }; -use rustc_ast::{YieldKind, ptr}; use rustc_ast_pretty::pprust; use rustc_span::{BytePos, LocalExpnId, Span, Symbol, SyntaxContext, sym, symbol}; use unicode_width::UnicodeWidthStr; @@ -149,8 +149,8 @@ pub(crate) fn format_extern(ext: ast::Extern, explicit_abi: bool) -> Cow<'static } #[inline] -// Transform `Vec<rustc_ast::ptr::P<T>>` into `Vec<&T>` -pub(crate) fn ptr_vec_to_ref_vec<T>(vec: &[ptr::P<T>]) -> Vec<&T> { +// Transform `Vec<Box<T>>` into `Vec<&T>` +pub(crate) fn ptr_vec_to_ref_vec<T>(vec: &[Box<T>]) -> Vec<&T> { vec.iter().map(|x| &**x).collect::<Vec<_>>() } diff --git a/src/tools/rustfmt/src/visitor.rs b/src/tools/rustfmt/src/visitor.rs index f6a9a3f2cd1..23d07c930d9 100644 --- a/src/tools/rustfmt/src/visitor.rs +++ b/src/tools/rustfmt/src/visitor.rs @@ -874,7 +874,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { !is_skip_attr(segments) } - fn walk_mod_items(&mut self, items: &[rustc_ast::ptr::P<ast::Item>]) { + fn walk_mod_items(&mut self, items: &[Box<ast::Item>]) { self.visit_items_with_reordering(&ptr_vec_to_ref_vec(items)); } diff --git a/src/tools/tidy/src/features.rs b/src/tools/tidy/src/features.rs index fb00b3a943f..6618ba24be6 100644 --- a/src/tools/tidy/src/features.rs +++ b/src/tools/tidy/src/features.rs @@ -9,6 +9,7 @@ //! * All unstable lang features have tests to ensure they are actually unstable. //! * Language features in a group are sorted by feature name. +use std::collections::BTreeSet; use std::collections::hash_map::{Entry, HashMap}; use std::ffi::OsStr; use std::num::NonZeroU32; @@ -21,6 +22,7 @@ use crate::walk::{filter_dirs, filter_not_rust, walk, walk_many}; mod tests; mod version; +use regex::Regex; use version::Version; const FEATURE_GROUP_START_PREFIX: &str = "// feature-group-start"; @@ -623,3 +625,36 @@ fn map_lib_features( }, ); } + +fn should_document(var: &str) -> bool { + if var.starts_with("RUSTC_") || var.starts_with("RUST_") || var.starts_with("UNSTABLE_RUSTDOC_") + { + return true; + } + ["SDKROOT", "QNX_TARGET", "COLORTERM", "TERM"].contains(&var) +} + +pub fn collect_env_vars(compiler: &Path) -> BTreeSet<String> { + let env_var_regex: Regex = Regex::new(r#"env::var(_os)?\("([^"]+)"#).unwrap(); + + let mut vars = BTreeSet::new(); + walk( + compiler, + // skip build scripts, tests, and non-rust files + |path, _is_dir| { + filter_dirs(path) + || filter_not_rust(path) + || path.ends_with("build.rs") + || path.ends_with("tests.rs") + }, + &mut |_entry, contents| { + for env_var in env_var_regex.captures_iter(contents).map(|c| c.get(2).unwrap().as_str()) + { + if should_document(env_var) { + vars.insert(env_var.to_owned()); + } + } + }, + ); + vars +} diff --git a/src/tools/tidy/src/unstable_book.rs b/src/tools/tidy/src/unstable_book.rs index 9dc9d42d466..0ed954d48de 100644 --- a/src/tools/tidy/src/unstable_book.rs +++ b/src/tools/tidy/src/unstable_book.rs @@ -6,6 +6,8 @@ use crate::features::{CollectedFeatures, Features, Status}; pub const PATH_STR: &str = "doc/unstable-book"; +pub const ENV_VARS_DIR: &str = "src/compiler-environment-variables"; + pub const COMPILER_FLAGS_DIR: &str = "src/compiler-flags"; pub const LANG_FEATURES_DIR: &str = "src/language-features"; diff --git a/src/tools/unstable-book-gen/src/main.rs b/src/tools/unstable-book-gen/src/main.rs index 159a1d0fa17..a7c6173d88c 100644 --- a/src/tools/unstable-book-gen/src/main.rs +++ b/src/tools/unstable-book-gen/src/main.rs @@ -5,11 +5,11 @@ use std::env; use std::fs::{self, write}; use std::path::Path; -use tidy::features::{Features, collect_lang_features, collect_lib_features}; +use tidy::features::{Features, collect_env_vars, collect_lang_features, collect_lib_features}; use tidy::t; use tidy::unstable_book::{ - LANG_FEATURES_DIR, LIB_FEATURES_DIR, PATH_STR, collect_unstable_book_section_file_names, - collect_unstable_feature_names, + ENV_VARS_DIR, LANG_FEATURES_DIR, LIB_FEATURES_DIR, PATH_STR, + collect_unstable_book_section_file_names, collect_unstable_feature_names, }; fn generate_stub_issue(path: &Path, name: &str, issue: u32, description: &str) { @@ -27,6 +27,11 @@ fn generate_stub_no_issue(path: &Path, name: &str, description: &str) { t!(write(path, content), path); } +fn generate_stub_env_var(path: &Path, name: &str) { + let content = format!(include_str!("stub-env-var.md"), name = name); + t!(write(path, content), path); +} + fn set_to_summary_str(set: &BTreeSet<String>, dir: &str) -> String { set.iter() .map(|ref n| format!(" - [{}]({}/{}.md)", n.replace('-', "_"), dir, n)) @@ -59,7 +64,7 @@ fn generate_summary(path: &Path, lang_features: &Features, lib_features: &Featur t!(write(&summary_path, content), summary_path); } -fn generate_unstable_book_files(src: &Path, out: &Path, features: &Features) { +fn generate_feature_files(src: &Path, out: &Path, features: &Features) { let unstable_features = collect_unstable_feature_names(features); let unstable_section_file_names = collect_unstable_book_section_file_names(src); t!(fs::create_dir_all(&out)); @@ -83,6 +88,16 @@ fn generate_unstable_book_files(src: &Path, out: &Path, features: &Features) { } } +fn generate_env_files(src: &Path, out: &Path, env_vars: &BTreeSet<String>) { + let env_var_file_names = collect_unstable_book_section_file_names(src); + t!(fs::create_dir_all(&out)); + for env_var in env_vars - &env_var_file_names { + let file_name = format!("{env_var}.md"); + let out_file_path = out.join(&file_name); + generate_stub_env_var(&out_file_path, &env_var); + } +} + fn copy_recursive(from: &Path, to: &Path) { for entry in t!(fs::read_dir(from)) { let e = t!(entry); @@ -112,21 +127,23 @@ fn main() { .into_iter() .filter(|&(ref name, _)| !lang_features.contains_key(name)) .collect(); + let env_vars = collect_env_vars(compiler_path); let doc_src_path = src_path.join(PATH_STR); t!(fs::create_dir_all(&dest_path)); - generate_unstable_book_files( + generate_feature_files( &doc_src_path.join(LANG_FEATURES_DIR), &dest_path.join(LANG_FEATURES_DIR), &lang_features, ); - generate_unstable_book_files( + generate_feature_files( &doc_src_path.join(LIB_FEATURES_DIR), &dest_path.join(LIB_FEATURES_DIR), &lib_features, ); + generate_env_files(&doc_src_path.join(ENV_VARS_DIR), &dest_path.join(ENV_VARS_DIR), &env_vars); copy_recursive(&doc_src_path, &dest_path); diff --git a/src/tools/unstable-book-gen/src/stub-env-var.md b/src/tools/unstable-book-gen/src/stub-env-var.md new file mode 100644 index 00000000000..e8a7ddb855a --- /dev/null +++ b/src/tools/unstable-book-gen/src/stub-env-var.md @@ -0,0 +1,9 @@ +# `{name}` + +Environment variables have no tracking issue. This environment variable has no documentation, and therefore is likely internal to the compiler and not meant for general use. + +See [the code][github search] for more information. + +[github search]: https://github.com/search?q=repo%3Arust-lang%2Frust+%22{name}%22+path%3Acompiler&type=code + +------------------------ |
