From d2cfe486f0e856d6828a544b12b0b8b1f8277260 Mon Sep 17 00:00:00 2001 From: Kivooeo Date: Mon, 4 Aug 2025 16:41:10 +0500 Subject: [PATCH 01/13] remove feature gate --- library/core/src/num/uint_macros.rs | 4 ++-- library/std/src/lib.rs | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index 584cd60fbe5cc..bf5dc43f40739 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -850,7 +850,6 @@ macro_rules! uint_impl { /// # Examples /// /// ``` - /// #![feature(unsigned_signed_diff)] #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_signed_diff(2), Some(8));")] #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_signed_diff(10), Some(-8));")] #[doc = concat!( @@ -888,7 +887,8 @@ macro_rules! uint_impl { "::MAX), Some(0));" )] /// ``` - #[unstable(feature = "unsigned_signed_diff", issue = "126041")] + #[stable(feature = "unsigned_signed_diff", since = "CURRENT_RUSTC_VERSION")] + #[rustc_const_stable(feature = "unsigned_signed_diff", since = "CURRENT_RUSTC_VERSION")] #[inline] pub const fn checked_signed_diff(self, rhs: Self) -> Option<$SignedT> { let res = self.wrapping_sub(rhs) as $SignedT; diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 77301d7228ead..5ceb2b3095547 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -319,7 +319,6 @@ #![feature(try_blocks)] #![feature(try_trait_v2)] #![feature(type_alias_impl_trait)] -#![feature(unsigned_signed_diff)] // tidy-alphabetical-end // // Library features (core): From 3b50253b57b130fdcef167fc0c03a9a19210fae2 Mon Sep 17 00:00:00 2001 From: Paul Murphy Date: Mon, 4 Aug 2025 13:00:06 -0500 Subject: [PATCH 02/13] compiler-builtins: plumb LSE support for aarch64 on linux Add dynamic support for aarch64 LSE atomic ops on linux targets when optimized-compiler-builtins is not enabled. A hook, __enable_rust_lse, is provided for the runtime to enable them if available. A future patch will use this to enable them if available. The resulting asm should exactly match that of LLVM's compiler-rt builtins, though the symbol naming for the support function and global does not. --- .../compiler-builtins/src/aarch64_linux.rs | 76 ++++++++++++++++--- 1 file changed, 66 insertions(+), 10 deletions(-) diff --git a/library/compiler-builtins/compiler-builtins/src/aarch64_linux.rs b/library/compiler-builtins/compiler-builtins/src/aarch64_linux.rs index 38fcab152aed2..01d7fb4732918 100644 --- a/library/compiler-builtins/compiler-builtins/src/aarch64_linux.rs +++ b/library/compiler-builtins/compiler-builtins/src/aarch64_linux.rs @@ -6,9 +6,6 @@ //! which is supported on the current CPU. //! See for more discussion. //! -//! Currently we only support LL/SC, because LSE requires `getauxval` from libc in order to do runtime detection. -//! Use the `compiler-rt` intrinsics if you want LSE support. -//! //! Ported from `aarch64/lse.S` in LLVM's compiler-rt. //! //! Generate functions for each of the following symbols: @@ -24,7 +21,18 @@ //! We do something similar, but with macro arguments. #![cfg_attr(feature = "c", allow(unused_macros))] // avoid putting the macros into a submodule -// We don't do runtime dispatch so we don't have to worry about the `__aarch64_have_lse_atomics` global ctor. +use core::sync::atomic::{AtomicU8, Ordering}; + +/// non-zero if the host supports LSE atomics. +static HAVE_LSE_ATOMICS: AtomicU8 = AtomicU8::new(0); + +intrinsics! { + /// Call to enable LSE in outline atomic operations. The caller must verify + /// LSE operations are supported. + pub extern "C" fn __rust_enable_lse() { + HAVE_LSE_ATOMICS.store(1, Ordering::Relaxed); + } +} /// Translate a byte size to a Rust type. #[rustfmt::skip] @@ -45,6 +53,7 @@ macro_rules! reg { (2, $num:literal) => { concat!("w", $num) }; (4, $num:literal) => { concat!("w", $num) }; (8, $num:literal) => { concat!("x", $num) }; + (16, $num:literal) => { concat!("x", $num) }; } /// Given an atomic ordering, translate it to the acquire suffix for the lxdr aarch64 ASM instruction. @@ -126,6 +135,41 @@ macro_rules! stxp { }; } +// If supported, perform the requested LSE op and return, or fallthrough. +macro_rules! try_lse_op { + ($op: literal, $ordering:ident, $bytes:tt, $($reg:literal,)* [ $mem:ident ] ) => { + concat!( + ".arch_extension lse; ", + "adrp x16, {have_lse}; ", + "ldrb w16, [x16, :lo12:{have_lse}]; ", + "cbz w16, 8f; ", + // LSE_OP s(reg),* [$mem] + concat!(lse!($op, $ordering, $bytes), $( " ", reg!($bytes, $reg), ", " ,)* "[", stringify!($mem), "]; ",), + "ret; ", + "8:" + ) + }; +} + +// Translate memory ordering to the LSE suffix +#[rustfmt::skip] +macro_rules! lse_mem_sfx { + (Relaxed) => { "" }; + (Acquire) => { "a" }; + (Release) => { "l" }; + (AcqRel) => { "al" }; +} + +// Generate the aarch64 LSE operation for memory ordering and width +macro_rules! lse { + ($op:literal, $order:ident, 16) => { + concat!($op, "p", lse_mem_sfx!($order)) + }; + ($op:literal, $order:ident, $bytes:tt) => { + concat!($op, lse_mem_sfx!($order), size!($bytes)) + }; +} + /// See . macro_rules! compare_and_swap { ($ordering:ident, $bytes:tt, $name:ident) => { @@ -137,7 +181,9 @@ macro_rules! compare_and_swap { ) -> int_ty!($bytes) { // We can't use `AtomicI8::compare_and_swap`; we *are* compare_and_swap. core::arch::naked_asm! { - // UXT s(tmp0), s(0) + // CAS s(0), s(1), [x2]; if LSE supported. + try_lse_op!("cas", $ordering, $bytes, 0, 1, [x2]), + // UXT s(tmp0), s(0) concat!(uxt!($bytes), " ", reg!($bytes, 16), ", ", reg!($bytes, 0)), "0:", // LDXR s(0), [x2] @@ -150,6 +196,7 @@ macro_rules! compare_and_swap { "cbnz w17, 0b", "1:", "ret", + have_lse = sym crate::aarch64_linux::HAVE_LSE_ATOMICS, } } } @@ -166,6 +213,8 @@ macro_rules! compare_and_swap_i128 { expected: i128, desired: i128, ptr: *mut i128 ) -> i128 { core::arch::naked_asm! { + // CASP x0, x1, x2, x3, [x4]; if LSE supported. + try_lse_op!("cas", $ordering, 16, 0, 1, 2, 3, [x4]), "mov x16, x0", "mov x17, x1", "0:", @@ -179,6 +228,7 @@ macro_rules! compare_and_swap_i128 { "cbnz w15, 0b", "1:", "ret", + have_lse = sym crate::aarch64_linux::HAVE_LSE_ATOMICS, } } } @@ -195,6 +245,8 @@ macro_rules! swap { left: int_ty!($bytes), right_ptr: *mut int_ty!($bytes) ) -> int_ty!($bytes) { core::arch::naked_asm! { + // SWP s(0), s(0), [x1]; if LSE supported. + try_lse_op!("swp", $ordering, $bytes, 0, 0, [x1]), // mov s(tmp0), s(0) concat!("mov ", reg!($bytes, 16), ", ", reg!($bytes, 0)), "0:", @@ -204,6 +256,7 @@ macro_rules! swap { concat!(stxr!($ordering, $bytes), " w17, ", reg!($bytes, 16), ", [x1]"), "cbnz w17, 0b", "ret", + have_lse = sym crate::aarch64_linux::HAVE_LSE_ATOMICS, } } } @@ -212,7 +265,7 @@ macro_rules! swap { /// See (e.g.) . macro_rules! fetch_op { - ($ordering:ident, $bytes:tt, $name:ident, $op:literal) => { + ($ordering:ident, $bytes:tt, $name:ident, $op:literal, $lse_op:literal) => { intrinsics! { #[maybe_use_optimized_c_shim] #[unsafe(naked)] @@ -220,6 +273,8 @@ macro_rules! fetch_op { val: int_ty!($bytes), ptr: *mut int_ty!($bytes) ) -> int_ty!($bytes) { core::arch::naked_asm! { + // LSEOP s(0), s(0), [x1]; if LSE supported. + try_lse_op!($lse_op, $ordering, $bytes, 0, 0, [x1]), // mov s(tmp0), s(0) concat!("mov ", reg!($bytes, 16), ", ", reg!($bytes, 0)), "0:", @@ -231,6 +286,7 @@ macro_rules! fetch_op { concat!(stxr!($ordering, $bytes), " w15, ", reg!($bytes, 17), ", [x1]"), "cbnz w15, 0b", "ret", + have_lse = sym crate::aarch64_linux::HAVE_LSE_ATOMICS, } } } @@ -240,25 +296,25 @@ macro_rules! fetch_op { // We need a single macro to pass to `foreach_ldadd`. macro_rules! add { ($ordering:ident, $bytes:tt, $name:ident) => { - fetch_op! { $ordering, $bytes, $name, "add" } + fetch_op! { $ordering, $bytes, $name, "add", "ldadd" } }; } macro_rules! and { ($ordering:ident, $bytes:tt, $name:ident) => { - fetch_op! { $ordering, $bytes, $name, "bic" } + fetch_op! { $ordering, $bytes, $name, "bic", "ldclr" } }; } macro_rules! xor { ($ordering:ident, $bytes:tt, $name:ident) => { - fetch_op! { $ordering, $bytes, $name, "eor" } + fetch_op! { $ordering, $bytes, $name, "eor", "ldeor" } }; } macro_rules! or { ($ordering:ident, $bytes:tt, $name:ident) => { - fetch_op! { $ordering, $bytes, $name, "orr" } + fetch_op! { $ordering, $bytes, $name, "orr", "ldset" } }; } From 6936bb975a50bf3c575d5642018ce19c58dcacf1 Mon Sep 17 00:00:00 2001 From: Paul Murphy Date: Thu, 31 Jul 2025 17:25:00 -0400 Subject: [PATCH 03/13] Dynamically enable LSE for aarch64 rust provided intrinsics Create a private module to hold the bootstrap code needed enable LSE at startup on aarch64-*-linux-* targets when rust implements the intrinsics. This is a bit more heavyweight than compiler-rt's LSE initialization, but has the benefit of initializing the aarch64 cpu feature detection for other uses. Using the rust initialization code does use some atomic operations, that's OK. Mixing LSE and non-LSE operations should work while the update flag propagates. --- library/std/src/sys/configure_builtins.rs | 22 ++++++++++++++++++++++ library/std/src/sys/mod.rs | 5 +++++ 2 files changed, 27 insertions(+) create mode 100644 library/std/src/sys/configure_builtins.rs diff --git a/library/std/src/sys/configure_builtins.rs b/library/std/src/sys/configure_builtins.rs new file mode 100644 index 0000000000000..9d776b778dcbe --- /dev/null +++ b/library/std/src/sys/configure_builtins.rs @@ -0,0 +1,22 @@ +/// Hook into .init_array to enable LSE atomic operations at startup, if +/// supported. +#[cfg(all(target_arch = "aarch64", target_os = "linux", not(feature = "compiler-builtins-c")))] +#[used] +#[unsafe(link_section = ".init_array.90")] +static RUST_LSE_INIT: extern "C" fn() = { + extern "C" fn init_lse() { + use crate::arch; + + // This is provided by compiler-builtins::aarch64_linux. + unsafe extern "C" { + fn __rust_enable_lse(); + } + + if arch::is_aarch64_feature_detected!("lse") { + unsafe { + __rust_enable_lse(); + } + } + } + init_lse +}; diff --git a/library/std/src/sys/mod.rs b/library/std/src/sys/mod.rs index f9a02b522e5e1..8ec0a0e33023f 100644 --- a/library/std/src/sys/mod.rs +++ b/library/std/src/sys/mod.rs @@ -1,5 +1,10 @@ #![allow(unsafe_op_in_unsafe_fn)] +/// The configure builtins provides runtime support compiler-builtin features +/// which require dynamic intialization to work as expected, e.g. aarch64 +/// outline-atomics. +mod configure_builtins; + /// The PAL (platform abstraction layer) contains platform-specific abstractions /// for implementing the features in the other submodules, e.g. UNIX file /// descriptors. From 2472d19625fa39ed635a211be397319d2b62cb24 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 6 Aug 2025 13:38:26 +0000 Subject: [PATCH 04/13] Include allocator module in LLVM_passes timer --- compiler/rustc_codegen_ssa/src/back/write.rs | 7 +-- compiler/rustc_codegen_ssa/src/base.rs | 56 ++++++++++---------- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index aa29afb7f5b11..a2ae03a7611bc 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -110,7 +110,6 @@ pub struct ModuleConfig { pub lint_llvm_ir: bool, pub no_prepopulate_passes: bool, pub no_builtins: bool, - pub time_module: bool, pub vectorize_loop: bool, pub vectorize_slp: bool, pub merge_functions: bool, @@ -228,10 +227,6 @@ impl ModuleConfig { no_prepopulate_passes: sess.opts.cg.no_prepopulate_passes, no_builtins: no_builtins || sess.target.no_builtins, - // Exclude metadata and allocator modules from time_passes output, - // since they throw off the "LLVM passes" measurement. - time_module: if_regular!(true, false), - // Copy what clang does by turning on loop vectorization at O2 and // slp vectorization at O3. vectorize_loop: !sess.opts.cg.no_vectorize_loops @@ -1740,7 +1735,7 @@ fn spawn_work<'a, B: ExtraBackendMethods>( llvm_start_time: &mut Option>, work: WorkItem, ) { - if cgcx.config(work.module_kind()).time_module && llvm_start_time.is_none() { + if llvm_start_time.is_none() { *llvm_start_time = Some(cgcx.prof.verbose_generic_activity("LLVM_passes")); } diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index b4556ced0b3fb..10a25bb3caaa1 100644 --- a/compiler/rustc_codegen_ssa/src/base.rs +++ b/compiler/rustc_codegen_ssa/src/base.rs @@ -681,33 +681,6 @@ pub fn codegen_crate( let ongoing_codegen = start_async_codegen(backend.clone(), tcx, target_cpu, autodiff_items); - // Codegen an allocator shim, if necessary. - if let Some(kind) = allocator_kind_for_codegen(tcx) { - let llmod_id = - cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("allocator")).to_string(); - let module_llvm = tcx.sess.time("write_allocator_module", || { - backend.codegen_allocator( - tcx, - &llmod_id, - kind, - // If allocator_kind is Some then alloc_error_handler_kind must - // also be Some. - tcx.alloc_error_handler_kind(()).unwrap(), - ) - }); - - ongoing_codegen.wait_for_signal_to_codegen_item(); - ongoing_codegen.check_for_errors(tcx.sess); - - // These modules are generally cheap and won't throw off scheduling. - let cost = 0; - submit_codegened_module_to_llvm( - &ongoing_codegen.coordinator, - ModuleCodegen::new_allocator(llmod_id, module_llvm), - cost, - ); - } - // For better throughput during parallel processing by LLVM, we used to sort // CGUs largest to smallest. This would lead to better thread utilization // by, for example, preventing a large CGU from being processed last and @@ -823,6 +796,35 @@ pub fn codegen_crate( } } + // Codegen an allocator shim, if necessary. + // Do this last to ensure the LLVM_passes timer doesn't start while no CGUs have been codegened + // yet for the backend to optimize. + if let Some(kind) = allocator_kind_for_codegen(tcx) { + let llmod_id = + cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("allocator")).to_string(); + let module_llvm = tcx.sess.time("write_allocator_module", || { + backend.codegen_allocator( + tcx, + &llmod_id, + kind, + // If allocator_kind is Some then alloc_error_handler_kind must + // also be Some. + tcx.alloc_error_handler_kind(()).unwrap(), + ) + }); + + ongoing_codegen.wait_for_signal_to_codegen_item(); + ongoing_codegen.check_for_errors(tcx.sess); + + // These modules are generally cheap and won't throw off scheduling. + let cost = 0; + submit_codegened_module_to_llvm( + &ongoing_codegen.coordinator, + ModuleCodegen::new_allocator(llmod_id, module_llvm), + cost, + ); + } + ongoing_codegen.codegen_finished(tcx); // Since the main thread is sometimes blocked during codegen, we keep track From ee38bc06ce68d3f426f191a012ab529eceecb700 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 6 Aug 2025 14:03:35 +0000 Subject: [PATCH 05/13] Remove unused field from ModuleConfig --- compiler/rustc_codegen_ssa/src/back/write.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index a2ae03a7611bc..17dcca753987b 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -76,12 +76,9 @@ pub struct ModuleConfig { /// Names of additional optimization passes to run. pub passes: Vec, /// Some(level) to optimize at a certain level, or None to run - /// absolutely no optimizations (used for the metadata module). + /// absolutely no optimizations (used for the allocator module). pub opt_level: Option, - /// Some(level) to optimize binary size, or None to not affect program size. - pub opt_size: Option, - pub pgo_gen: SwitchWithOptPath, pub pgo_use: Option, pub pgo_sample_use: Option, @@ -170,7 +167,6 @@ impl ModuleConfig { passes: if_regular!(sess.opts.cg.passes.clone(), vec![]), opt_level: opt_level_and_size, - opt_size: opt_level_and_size, pgo_gen: if_regular!( sess.opts.cg.profile_generate.clone(), From 6fbea4ffe84133edf74a0069ffd18f982527e53c Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 6 Aug 2025 14:13:30 +0000 Subject: [PATCH 06/13] Remove bitcode_llvm_cmdline It used to be necessary on Apple platforms to ship with the App Store, but XCode 15 has stopped embedding LLVM bitcode and the App Store no longer accepts apps with bitcode embedded. --- compiler/rustc_codegen_llvm/src/back/write.rs | 57 +++++++------------ compiler/rustc_codegen_ssa/src/back/write.rs | 2 - compiler/rustc_target/src/spec/json.rs | 3 - compiler/rustc_target/src/spec/mod.rs | 3 - 4 files changed, 20 insertions(+), 45 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index 85a06f457ebea..ad8df12ca2bfb 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -862,7 +862,7 @@ pub(crate) fn codegen( .generic_activity_with_arg("LLVM_module_codegen_embed_bitcode", &*module.name); let thin_bc = module.thin_lto_buffer.as_deref().expect("cannot find embedded bitcode"); - embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, &thin_bc); + embed_bitcode(cgcx, llcx, llmod, &thin_bc); } } @@ -1058,40 +1058,38 @@ fn embed_bitcode( cgcx: &CodegenContext, llcx: &llvm::Context, llmod: &llvm::Module, - cmdline: &str, bitcode: &[u8], ) { // We're adding custom sections to the output object file, but we definitely // do not want these custom sections to make their way into the final linked - // executable. The purpose of these custom sections is for tooling - // surrounding object files to work with the LLVM IR, if necessary. For - // example rustc's own LTO will look for LLVM IR inside of the object file - // in these sections by default. + // executable. The purpose of this custom section is for tooling surrounding + // object files to work with the LLVM IR, if necessary. For example rustc's + // own LTO will look for LLVM IR inside of the object file in this section + // by default. // // To handle this is a bit different depending on the object file format // used by the backend, broken down into a few different categories: // // * Mach-O - this is for macOS. Inspecting the source code for the native - // linker here shows that the `.llvmbc` and `.llvmcmd` sections are - // automatically skipped by the linker. In that case there's nothing extra - // that we need to do here. + // linker here shows that the `.llvmbc` section is automatically skipped + // by the linker. In that case there's nothing extra that we need to do + // here. // - // * Wasm - the native LLD linker is hard-coded to skip `.llvmbc` and - // `.llvmcmd` sections, so there's nothing extra we need to do. + // * Wasm - the native LLD linker is hard-coded to skip `.llvmbc` section, + // so there's nothing extra we need to do. // - // * COFF - if we don't do anything the linker will by default copy all - // these sections to the output artifact, not what we want! To subvert - // this we want to flag the sections we inserted here as - // `IMAGE_SCN_LNK_REMOVE`. + // * COFF - if we don't do anything the linker will by default copy this + // section to the output artifact, not what we want! To subvert this we + // want to flag the section we inserted here as `IMAGE_SCN_LNK_REMOVE`. // - // * ELF - this is very similar to COFF above. One difference is that these - // sections are removed from the output linked artifact when - // `--gc-sections` is passed, which we pass by default. If that flag isn't - // passed though then these sections will show up in the final output. - // Additionally the flag that we need to set here is `SHF_EXCLUDE`. + // * ELF - this is very similar to COFF above. One difference is that this + // section is removed from the output linked artifact when `--gc-sections` + // is passed, which we pass by default. If that flag isn't passed through + // then this section will show up in the final output. Additionally the + // flag that we need to set here is `SHF_EXCLUDE`. // - // * XCOFF - AIX linker ignores content in .ipa and .info if no auxiliary - // symbol associated with these sections. + // * XCOFF - AIX linker ignores content in .ipa if no auxiliary symbol + // associated with this section. // // Unfortunately, LLVM provides no way to set custom section flags. For ELF // and COFF we emit the sections using module level inline assembly for that @@ -1110,26 +1108,11 @@ fn embed_bitcode( llvm::set_section(llglobal, bitcode_section_name(cgcx)); llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage); llvm::LLVMSetGlobalConstant(llglobal, llvm::True); - - let llconst = common::bytes_in_context(llcx, cmdline.as_bytes()); - let llglobal = llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.cmdline"); - llvm::set_initializer(llglobal, llconst); - let section = if cgcx.target_is_like_darwin { - c"__LLVM,__cmdline" - } else if cgcx.target_is_like_aix { - c".info" - } else { - c".llvmcmd" - }; - llvm::set_section(llglobal, section); - llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage); } else { // We need custom section flags, so emit module-level inline assembly. let section_flags = if cgcx.is_pe_coff { "n" } else { "e" }; let asm = create_section_with_flags_asm(".llvmbc", section_flags, bitcode); llvm::append_module_inline_asm(llmod, &asm); - let asm = create_section_with_flags_asm(".llvmcmd", section_flags, cmdline.as_bytes()); - llvm::append_module_inline_asm(llmod, &asm); } } diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index 17dcca753987b..7ceb9885716eb 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -99,7 +99,6 @@ pub struct ModuleConfig { pub emit_obj: EmitObj, pub emit_thin_lto: bool, pub emit_thin_lto_summary: bool, - pub bc_cmdline: String, // Miscellaneous flags. These are mostly copied from command-line // options. @@ -216,7 +215,6 @@ impl ModuleConfig { sess.opts.output_types.contains_key(&OutputType::ThinLinkBitcode), false ), - bc_cmdline: sess.target.bitcode_llvm_cmdline.to_string(), verify_llvm_ir: sess.verify_llvm_ir(), lint_llvm_ir: sess.opts.unstable_opts.lint_llvm_ir, diff --git a/compiler/rustc_target/src/spec/json.rs b/compiler/rustc_target/src/spec/json.rs index d27c1929aef74..ba7a3a3969e38 100644 --- a/compiler/rustc_target/src/spec/json.rs +++ b/compiler/rustc_target/src/spec/json.rs @@ -167,7 +167,6 @@ impl Target { forward!(main_needs_argc_argv); forward!(has_thread_local); forward!(obj_is_bitcode); - forward!(bitcode_llvm_cmdline); forward_opt!(max_atomic_width); forward_opt!(min_atomic_width); forward!(atomic_cas); @@ -359,7 +358,6 @@ impl ToJson for Target { target_option_val!(main_needs_argc_argv); target_option_val!(has_thread_local); target_option_val!(obj_is_bitcode); - target_option_val!(bitcode_llvm_cmdline); target_option_val!(min_atomic_width); target_option_val!(max_atomic_width); target_option_val!(atomic_cas); @@ -552,7 +550,6 @@ struct TargetSpecJson { main_needs_argc_argv: Option, has_thread_local: Option, obj_is_bitcode: Option, - bitcode_llvm_cmdline: Option>, max_atomic_width: Option, min_atomic_width: Option, atomic_cas: Option, diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 033590e01a67d..0c7ff7dd16564 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -2620,8 +2620,6 @@ pub struct TargetOptions { /// If we give emcc .o files that are actually .bc files it /// will 'just work'. pub obj_is_bitcode: bool, - /// Content of the LLVM cmdline section associated with embedded bitcode. - pub bitcode_llvm_cmdline: StaticCow, /// Don't use this field; instead use the `.min_atomic_width()` method. pub min_atomic_width: Option, @@ -2984,7 +2982,6 @@ impl Default for TargetOptions { allow_asm: true, has_thread_local: false, obj_is_bitcode: false, - bitcode_llvm_cmdline: "".into(), min_atomic_width: None, max_atomic_width: None, atomic_cas: true, From b1f49d872869f5ed52776473c9003d8b351c77c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 6 Aug 2025 17:10:13 +0200 Subject: [PATCH 07/13] Fix build/doc/test of error index generator It is essentially a RustcPrivate tool, so it should be treated as such using the new `RustcPrivateCompilers` infra. --- src/bootstrap/src/core/build_steps/doc.rs | 30 ++++-- src/bootstrap/src/core/build_steps/test.rs | 30 ++++-- src/bootstrap/src/core/build_steps/tool.rs | 36 ++++--- src/bootstrap/src/core/builder/tests.rs | 115 +++++++++++---------- 4 files changed, 124 insertions(+), 87 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/doc.rs b/src/bootstrap/src/core/build_steps/doc.rs index d4539a0eb34d1..b170de91cb365 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 { + Some( + StepMetadata::doc("error-index", self.compilers.target()) + .built_by(self.compilers.build_compiler()), + ) } } diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 09d2657b666d4..3cb6877682a62 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -2514,7 +2514,7 @@ test_book!( #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ErrorIndex { - compiler: Compiler, + compilers: RustcPrivateCompilers, } impl Step for ErrorIndex { @@ -2532,8 +2532,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 @@ -2543,24 +2547,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); } } diff --git a/src/bootstrap/src/core/build_steps/tool.rs b/src/bootstrap/src/core/build_steps/tool.rs index e3f49fa126ed8..7b0ef942abecf 100644 --- a/src/bootstrap/src/core/build_steps/tool.rs +++ b/src/bootstrap/src/core/build_steps/tool.rs @@ -588,20 +588,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 +620,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 +643,13 @@ impl Step for ErrorIndex { artifact_kind: ToolArtifactKind::Binary, }) } + + fn metadata(&self) -> Option { + 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/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 139ddc9ed2496..5361347da9047 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 { target: a },]); - assert_eq!( - first(cache.all::()), - &[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 { target: a },] - ); - assert_eq!( - first(builder.cache.all::()), - &[tool::ErrorIndex { compiler: Compiler::new(1, a) }] - ); - } } mod sysroot_target_dirs { @@ -888,6 +841,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 + [build] rustc 0 -> rustc 1 + [build] rustc 0 -> error-index 1 + "); + } + #[test] fn build_bootstrap_tool_no_explicit_stage() { let ctx = TestCtx::new(); @@ -1032,6 +998,8 @@ mod snapshot { [build] rustc 1 -> rustc 2 [build] rustdoc 2 [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 1 -> error-index 2 + [doc] rustc 1 -> error-index 2 [build] rustc 2 -> std 2 [build] rustc 0 -> LintDocs 1 [build] rustc 0 -> RustInstaller 1 @@ -1074,6 +1042,8 @@ mod snapshot { [build] rustc 1 -> LlvmBitcodeLinker 2 [build] rustdoc 2 [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 1 -> error-index 2 + [doc] rustc 1 -> error-index 2 [build] rustc 2 -> std 2 [build] rustc 0 -> LintDocs 1 [build] rustc 0 -> RustInstaller 1 @@ -1114,6 +1084,8 @@ mod snapshot { [build] rustdoc 2 [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 1 -> error-index 2 + [doc] rustc 1 -> error-index 2 [build] rustc 2 -> std 2 [build] rustc 0 -> LintDocs 1 [build] rustc 0 -> RustInstaller 1 @@ -1150,9 +1122,15 @@ mod snapshot { [build] rustc 1 -> rustc 2 [build] rustdoc 2 [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 1 -> error-index 2 + [doc] rustc 1 -> error-index 2 + [build] llvm + [build] rustc 1 -> std 1 + [build] rustc 1 -> rustc 2 + [build] rustc 1 -> error-index 2 + [doc] rustc 1 -> error-index 2 [build] rustc 2 -> std 2 [build] rustc 0 -> LintDocs 1 - [build] rustc 1 -> std 1 [build] rustc 2 -> std 2 [build] rustc 0 -> RustInstaller 1 [dist] docs @@ -1160,8 +1138,6 @@ mod snapshot { [dist] mingw [build] rustc 0 -> GenerateCopyright 1 [dist] rustc - [build] llvm - [build] rustc 1 -> rustc 2 [build] rustdoc 2 [dist] rustc [dist] rustc 1 -> std 1 @@ -1188,9 +1164,15 @@ mod snapshot { [build] rustdoc 2 [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 1 -> error-index 2 + [doc] rustc 1 -> error-index 2 + [build] llvm + [build] rustc 1 -> std 1 + [build] rustc 1 -> rustc 2 + [build] rustc 1 -> error-index 2 + [doc] rustc 1 -> error-index 2 [build] rustc 2 -> std 2 [build] rustc 0 -> LintDocs 1 - [build] rustc 1 -> std 1 [build] rustc 2 -> std 2 [build] rustc 0 -> RustInstaller 1 [dist] docs @@ -1201,8 +1183,6 @@ mod snapshot { [dist] mingw [build] rustc 0 -> GenerateCopyright 1 [dist] rustc - [build] llvm - [build] rustc 1 -> rustc 2 [build] rustdoc 2 [dist] rustc [dist] rustc 1 -> std 1 @@ -1261,17 +1241,19 @@ mod snapshot { [build] rustc 1 -> WasmComponentLd 2 [build] rustdoc 2 [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] - [build] rustc 2 -> std 2 + [build] llvm [build] rustc 1 -> std 1 + [build] rustc 1 -> rustc 2 + [build] rustc 1 -> WasmComponentLd 2 + [build] rustc 1 -> error-index 2 + [doc] rustc 1 -> error-index 2 + [build] rustc 2 -> std 2 [build] rustc 2 -> std 2 [build] rustc 0 -> LintDocs 1 [build] rustc 0 -> RustInstaller 1 [dist] docs [doc] std 2 crates=[] [dist] mingw - [build] llvm - [build] rustc 1 -> rustc 2 - [build] rustc 1 -> WasmComponentLd 2 [build] rustdoc 2 [build] rustc 1 -> rust-analyzer-proc-macro-srv 2 [build] rustc 0 -> GenerateCopyright 1 @@ -1651,6 +1633,25 @@ mod snapshot { "); } + #[test] + fn doc_all() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("doc") + .render_steps(), @r" + [build] rustc 0 -> UnstableBookGen 1 + [build] rustc 0 -> Rustbook 1 + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustdoc 1 + [doc] std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 0 -> error-index 1 + [doc] rustc 0 -> error-index 1 + [build] rustc 1 -> std 1 + [build] rustc 0 -> LintDocs 1 + "); + } + #[test] fn doc_library() { let ctx = TestCtx::new(); From 56d5aab31db69ad4404a93c54f1c4f48646f1ad9 Mon Sep 17 00:00:00 2001 From: AlexanderPortland Date: Wed, 6 Aug 2025 12:02:38 -0700 Subject: [PATCH 08/13] make rustc_public types derive `Hash` --- compiler/rustc_public/src/mir/body.rs | 32 ++++++++++---------- compiler/rustc_public/src/ty.rs | 42 +++++++++++++++++---------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_public/src/mir/body.rs b/compiler/rustc_public/src/mir/body.rs index 3d595286041c7..276adacd99e06 100644 --- a/compiler/rustc_public/src/mir/body.rs +++ b/compiler/rustc_public/src/mir/body.rs @@ -349,7 +349,7 @@ impl AssertMessage { } } -#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum BinOp { Add, AddUnchecked, @@ -384,7 +384,7 @@ impl BinOp { } } -#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum UnOp { Not, Neg, @@ -490,7 +490,7 @@ pub enum StatementKind { Nop, } -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum Rvalue { /// Creates a pointer with the indicated mutability to the place. /// @@ -666,7 +666,7 @@ impl Rvalue { } } -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum AggregateKind { Array(Ty), Tuple, @@ -677,14 +677,14 @@ pub enum AggregateKind { RawPtr(Ty, Mutability), } -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum Operand { Copy(Place), Move(Place), Constant(ConstOperand), } -#[derive(Clone, Eq, PartialEq, Serialize)] +#[derive(Clone, Eq, PartialEq, Hash, Serialize)] pub struct Place { pub local: Local, /// projection out of a place (access a field, deref a pointer, etc) @@ -697,7 +697,7 @@ impl From for Place { } } -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub struct ConstOperand { pub span: Span, pub user_ty: Option, @@ -770,7 +770,7 @@ pub enum VarDebugInfoContents { // ProjectionElem) and user-provided type annotations (for which the projection elements // are of type ProjectionElem<(), ()>). // In rustc_public's IR we don't need this generality, so we just use ProjectionElem for Places. -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum ProjectionElem { /// Dereference projections (e.g. `*_1`) project to the address referenced by the base place. Deref, @@ -913,7 +913,7 @@ impl SwitchTargets { } } -#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum BorrowKind { /// Data must be immutable and is aliasable. Shared, @@ -940,7 +940,7 @@ impl BorrowKind { } } -#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum RawPtrKind { Mut, Const, @@ -958,14 +958,14 @@ impl RawPtrKind { } } -#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum MutBorrowKind { Default, TwoPhaseBorrow, ClosureCapture, } -#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum FakeBorrowKind { /// A shared (deep) borrow. Data must be immutable and is aliasable. Deep, @@ -982,13 +982,13 @@ pub enum Mutability { Mut, } -#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum Safety { Safe, Unsafe, } -#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum PointerCoercion { /// Go from a fn-item type to a fn-pointer type. ReifyFnPointer, @@ -1015,7 +1015,7 @@ pub enum PointerCoercion { Unsize, } -#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum CastKind { // FIXME(smir-rename): rename this to PointerExposeProvenance PointerExposeAddress, @@ -1030,7 +1030,7 @@ pub enum CastKind { Transmute, } -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum NullOp { /// Returns the size of a value of that type. SizeOf, diff --git a/compiler/rustc_public/src/ty.rs b/compiler/rustc_public/src/ty.rs index de4b21b176472..957940ef22f66 100644 --- a/compiler/rustc_public/src/ty.rs +++ b/compiler/rustc_public/src/ty.rs @@ -113,7 +113,7 @@ pub enum Pattern { } /// Represents a constant in the type system -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub struct TyConst { pub(crate) kind: TyConstKind, pub id: TyConstId, @@ -140,7 +140,7 @@ impl TyConst { } } -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum TyConstKind { Param(ParamConst), Bound(DebruijnIndex, BoundVar), @@ -151,11 +151,11 @@ pub enum TyConstKind { ZSTValue(Ty), } -#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub struct TyConstId(usize); /// Represents a constant in MIR -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub struct MirConst { /// The constant kind. pub(crate) kind: ConstantKind, @@ -212,17 +212,17 @@ impl MirConst { } } -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize)] pub struct MirConstId(usize); type Ident = Opaque; -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub struct Region { pub kind: RegionKind, } -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum RegionKind { ReEarlyParam(EarlyParamRegion), ReBound(DebruijnIndex, BoundRegion), @@ -233,7 +233,7 @@ pub enum RegionKind { pub(crate) type DebruijnIndex = u32; -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub struct EarlyParamRegion { pub index: u32, pub name: Symbol, @@ -241,7 +241,7 @@ pub struct EarlyParamRegion { pub(crate) type BoundVar = u32; -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub struct BoundRegion { pub var: BoundVar, pub kind: BoundRegionKind, @@ -255,7 +255,17 @@ pub struct Placeholder { pub bound: T, } -#[derive(Clone, Copy, PartialEq, Eq, Serialize)] +impl std::hash::Hash for Placeholder { + fn hash(&self, state: &mut H) + where + H: std::hash::Hasher, + { + self.universe.hash(state); + self.bound.hash(state); + } +} + +#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize)] pub struct Span(usize); impl Debug for Span { @@ -997,7 +1007,7 @@ crate_def! { } /// A list of generic arguments. -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub struct GenericArgs(pub Vec); impl std::ops::Index for GenericArgs { @@ -1016,7 +1026,7 @@ impl std::ops::Index for GenericArgs { } } -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum GenericArgKind { Lifetime(Region), Type(Ty), @@ -1199,7 +1209,7 @@ pub enum BoundTyKind { Param(ParamDef, String), } -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum BoundRegionKind { BrAnon, BrNamed(BrNamedDef, String), @@ -1354,7 +1364,7 @@ impl Allocation { } } -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub enum ConstantKind { Ty(TyConst), Allocated(Allocation), @@ -1365,13 +1375,13 @@ pub enum ConstantKind { ZeroSized, } -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub struct ParamConst { pub index: u32, pub name: String, } -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub struct UnevaluatedConst { pub def: ConstDef, pub args: GenericArgs, From c65102e745c5637d0da3e9649aaba7afdce7aa08 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 7 Aug 2025 13:20:09 +0200 Subject: [PATCH 09/13] doc(library): Fix Markdown in `Iterator::by_ref`. This patch fixes the Markdown formatting in `std::core::iter::Iterator::by_ref`. Code is used inside a link without the backticks around the code. --- library/core/src/iter/traits/iterator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 29313867ff266..7fb162a653fb9 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -1875,7 +1875,7 @@ pub trait Iterator { /// without giving up ownership of the original iterator, /// so you can use the original iterator afterwards. /// - /// Uses [impl Iterator for &mut I { type Item = I::Item; ...}](https://doc.rust-lang.org/nightly/std/iter/trait.Iterator.html#impl-Iterator-for-%26mut+I). + /// Uses [`impl Iterator for &mut I { type Item = I::Item; ...}`](https://doc.rust-lang.org/nightly/std/iter/trait.Iterator.html#impl-Iterator-for-%26mut+I). /// /// # Examples /// From 488876281660590f08083578084163be5266b854 Mon Sep 17 00:00:00 2001 From: Ulrich Stark Date: Thu, 7 Aug 2025 14:04:12 +0200 Subject: [PATCH 10/13] Fix doc comment of File::try_lock and File::try_lock_shared --- library/std/src/fs.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index b3ca118a45295..a220a3f56e9a8 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -814,7 +814,7 @@ impl File { /// /// If this file handle/descriptor, or a clone of it, already holds a lock, the exact behavior /// is unspecified and platform dependent, including the possibility that it will deadlock. - /// However, if this method returns `Ok(true)`, then it has acquired an exclusive lock. + /// However, if this method returns `Ok(())`, then it has acquired an exclusive lock. /// /// If the file is not open for writing, it is unspecified whether this function returns an error. /// @@ -879,7 +879,7 @@ impl File { /// /// If this file handle, or a clone of it, already holds a lock, the exact behavior is /// unspecified and platform dependent, including the possibility that it will deadlock. - /// However, if this method returns `Ok(true)`, then it has acquired a shared lock. + /// However, if this method returns `Ok(())`, then it has acquired a shared lock. /// /// The lock will be released when this file (along with any other file descriptors/handles /// duplicated or inherited from it) is closed, or if the [`unlock`] method is called. From 4f7a6ace9e2f2192af7b5d32f4b1664189e0e143 Mon Sep 17 00:00:00 2001 From: Sasha Pourcelot Date: Sun, 3 Aug 2025 11:54:07 +0200 Subject: [PATCH 11/13] Port `#[allow_internal_unsafe]` to the new attribute system --- .../src/attributes/macro_attrs.rs | 8 +++ compiler/rustc_attr_parsing/src/context.rs | 5 +- compiler/rustc_expand/src/base.rs | 5 +- .../rustc_hir/src/attrs/data_structures.rs | 3 ++ .../rustc_hir/src/attrs/encode_cross_crate.rs | 1 + compiler/rustc_lint/src/builtin.rs | 13 ++++- compiler/rustc_passes/messages.ftl | 2 +- compiler/rustc_passes/src/check_attr.rs | 49 ++++++++++++++++--- compiler/rustc_passes/src/errors.rs | 4 +- tests/ui/attributes/malformed-attrs.stderr | 15 +++--- 10 files changed, 82 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs index 886f7a889d303..0779248e1a95e 100644 --- a/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs @@ -113,3 +113,11 @@ impl AttributeParser for MacroUseParser { Some(AttributeKind::MacroUse { span: self.first_span?, arguments: self.state }) } } + +pub(crate) struct AllowInternalUnsafeParser; + +impl NoArgsAttributeParser for AllowInternalUnsafeParser { + const PATH: &[Symbol] = &[sym::allow_internal_unsafe]; + const ON_DUPLICATE: OnDuplicate = OnDuplicate::Ignore; + const CREATE: fn(Span) -> AttributeKind = |span| AttributeKind::AllowInternalUnsafe(span); +} diff --git a/compiler/rustc_attr_parsing/src/context.rs b/compiler/rustc_attr_parsing/src/context.rs index b51db9b4b9e38..de46647cd068e 100644 --- a/compiler/rustc_attr_parsing/src/context.rs +++ b/compiler/rustc_attr_parsing/src/context.rs @@ -32,7 +32,9 @@ use crate::attributes::lint_helpers::{ AsPtrParser, AutomaticallyDerivedParser, PassByValueParser, PubTransparentParser, }; use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser}; -use crate::attributes::macro_attrs::{MacroEscapeParser, MacroUseParser}; +use crate::attributes::macro_attrs::{ + AllowInternalUnsafeParser, MacroEscapeParser, MacroUseParser, +}; use crate::attributes::must_use::MustUseParser; use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser; use crate::attributes::non_exhaustive::NonExhaustiveParser; @@ -176,6 +178,7 @@ attribute_parsers!( Single, Single, Single>, + Single>, Single>, Single>, Single>, diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index 1a9832b2fe26a..163fe34c19401 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -905,10 +905,7 @@ impl SyntaxExtension { find_attr!(attrs, AttributeKind::AllowInternalUnstable(i, _) => i) .map(|i| i.as_slice()) .unwrap_or_default(); - // FIXME(jdonszelman): allow_internal_unsafe isn't yet new-style - // let allow_internal_unsafe = find_attr!(attrs, AttributeKind::AllowInternalUnsafe); - let allow_internal_unsafe = - ast::attr::find_by_name(attrs, sym::allow_internal_unsafe).is_some(); + let allow_internal_unsafe = find_attr!(attrs, AttributeKind::AllowInternalUnsafe(_)); let local_inner_macros = ast::attr::find_by_name(attrs, sym::macro_export) .and_then(|macro_export| macro_export.meta_item_list()) diff --git a/compiler/rustc_hir/src/attrs/data_structures.rs b/compiler/rustc_hir/src/attrs/data_structures.rs index 80618422b56d6..41e6915f18654 100644 --- a/compiler/rustc_hir/src/attrs/data_structures.rs +++ b/compiler/rustc_hir/src/attrs/data_structures.rs @@ -249,6 +249,9 @@ pub enum AttributeKind { /// Represents `#[rustc_allow_incoherent_impl]`. AllowIncoherentImpl(Span), + /// Represents `#[allow_internal_unsafe]`. + AllowInternalUnsafe(Span), + /// Represents `#[allow_internal_unstable]`. AllowInternalUnstable(ThinVec<(Symbol, Span)>, Span), diff --git a/compiler/rustc_hir/src/attrs/encode_cross_crate.rs b/compiler/rustc_hir/src/attrs/encode_cross_crate.rs index 9644a597a3117..2d1f2158fe781 100644 --- a/compiler/rustc_hir/src/attrs/encode_cross_crate.rs +++ b/compiler/rustc_hir/src/attrs/encode_cross_crate.rs @@ -16,6 +16,7 @@ impl AttributeKind { Align { .. } => No, AllowConstFnUnstable(..) => No, AllowIncoherentImpl(..) => No, + AllowInternalUnsafe(..) => Yes, AllowInternalUnstable(..) => Yes, AsPtr(..) => Yes, AutomaticallyDerived(..) => Yes, diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index c893b7233755d..51b1019104652 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -14,6 +14,7 @@ //! [`crate::late_lint_methods!`] invocation in `lib.rs`. use std::fmt::Write; +use std::slice; use ast::token::TokenKind; use rustc_abi::BackendRepr; @@ -21,6 +22,7 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree}; use rustc_ast::visit::{FnCtxt, FnKind}; use rustc_ast::{self as ast, *}; use rustc_ast_pretty::pprust::expr_to_string; +use rustc_attr_parsing::AttributeParser; use rustc_errors::{Applicability, LintDiagnostic}; use rustc_feature::GateIssue; use rustc_hir as hir; @@ -249,7 +251,16 @@ impl UnsafeCode { impl EarlyLintPass for UnsafeCode { fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) { - if attr.has_name(sym::allow_internal_unsafe) { + if AttributeParser::parse_limited( + cx.builder.sess(), + slice::from_ref(attr), + sym::allow_internal_unsafe, + attr.span, + DUMMY_NODE_ID, + Some(cx.builder.features()), + ) + .is_some() + { self.report_unsafe(cx, attr.span, BuiltinUnsafe::AllowInternalUnsafe); } } diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl index 6a28fe2617edf..4a1f01cc5c857 100644 --- a/compiler/rustc_passes/messages.ftl +++ b/compiler/rustc_passes/messages.ftl @@ -29,7 +29,7 @@ passes_allow_incoherent_impl = `rustc_allow_incoherent_impl` attribute should be applied to impl items .label = the only currently supported targets are inherent methods -passes_allow_internal_unstable = +passes_macro_only_attribute = attribute should be applied to a macro .label = not a macro diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 2663d5fe99c7a..cec5797f5178d 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -207,6 +207,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> { Attribute::Parsed(AttributeKind::ConstContinue(attr_span)) => { self.check_const_continue(hir_id, *attr_span, target) } + Attribute::Parsed(AttributeKind::AllowInternalUnsafe(attr_span)) => { + self.check_allow_internal_unsafe(hir_id, *attr_span, span, target, attrs) + } Attribute::Parsed(AttributeKind::AllowInternalUnstable(_, first_span)) => { self.check_allow_internal_unstable(hir_id, *first_span, span, target, attrs) } @@ -414,7 +417,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> { // internal | sym::prelude_import | sym::panic_handler - | sym::allow_internal_unsafe | sym::lang | sym::needs_allocator | sym::default_lib_allocator @@ -2213,7 +2215,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> { /// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros. /// (Allows proc_macro functions) - // FIXME(jdonszelmann): if possible, move to attr parsing fn check_allow_internal_unstable( &self, hir_id: HirId, @@ -2221,6 +2222,42 @@ impl<'tcx> CheckAttrVisitor<'tcx> { span: Span, target: Target, attrs: &[Attribute], + ) { + self.check_macro_only_attr( + hir_id, + attr_span, + span, + target, + attrs, + "allow_internal_unstable", + ) + } + + /// Outputs an error for `#[allow_internal_unsafe]` which can only be applied to macros. + /// (Allows proc_macro functions) + fn check_allow_internal_unsafe( + &self, + hir_id: HirId, + attr_span: Span, + span: Span, + target: Target, + attrs: &[Attribute], + ) { + self.check_macro_only_attr(hir_id, attr_span, span, target, attrs, "allow_internal_unsafe") + } + + /// Outputs an error for attributes that can only be applied to macros, such as + /// `#[allow_internal_unsafe]` and `#[allow_internal_unstable]`. + /// (Allows proc_macro functions) + // FIXME(jdonszelmann): if possible, move to attr parsing + fn check_macro_only_attr( + &self, + hir_id: HirId, + attr_span: Span, + span: Span, + target: Target, + attrs: &[Attribute], + attr_name: &str, ) { match target { Target::Fn => { @@ -2239,18 +2276,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> { // erroneously allowed it and some crates used it accidentally, to be compatible // with crates depending on them, we can't throw an error here. Target::Field | Target::Arm => { - self.inline_attr_str_error_without_macro_def( - hir_id, - attr_span, - "allow_internal_unstable", - ); + self.inline_attr_str_error_without_macro_def(hir_id, attr_span, attr_name); return; } // otherwise continue out of the match _ => {} } - self.tcx.dcx().emit_err(errors::AllowInternalUnstable { attr_span, span }); + self.tcx.dcx().emit_err(errors::MacroOnlyAttribute { attr_span, span }); } /// Checks if the items on the `#[debugger_visualizer]` attribute are valid. diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index c6ab6b0d60179..10b30fbe8c94e 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -643,8 +643,8 @@ pub(crate) struct UsedStatic { } #[derive(Diagnostic)] -#[diag(passes_allow_internal_unstable)] -pub(crate) struct AllowInternalUnstable { +#[diag(passes_macro_only_attribute)] +pub(crate) struct MacroOnlyAttribute { #[primary_span] pub attr_span: Span, #[label] diff --git a/tests/ui/attributes/malformed-attrs.stderr b/tests/ui/attributes/malformed-attrs.stderr index 1b51075b4e888..088da854a370c 100644 --- a/tests/ui/attributes/malformed-attrs.stderr +++ b/tests/ui/attributes/malformed-attrs.stderr @@ -151,12 +151,6 @@ LL - #[macro_export = 18] LL + #[macro_export] | -error: malformed `allow_internal_unsafe` attribute input - --> $DIR/malformed-attrs.rs:213:1 - | -LL | #[allow_internal_unsafe = 1] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[allow_internal_unsafe]` - error: the `#[proc_macro]` attribute is only usable with crates of the `proc-macro` crate type --> $DIR/malformed-attrs.rs:96:1 | @@ -558,6 +552,15 @@ error: valid forms for the attribute are `#[macro_use(name1, name2, ...)]` and ` LL | #[macro_use = 1] | ^^^^^^^^^^^^^^^^ +error[E0565]: malformed `allow_internal_unsafe` attribute input + --> $DIR/malformed-attrs.rs:213:1 + | +LL | #[allow_internal_unsafe = 1] + | ^^^^^^^^^^^^^^^^^^^^^^^^---^ + | | | + | | didn't expect any arguments here + | help: must be of the form: `#[allow_internal_unsafe]` + error[E0565]: malformed `type_const` attribute input --> $DIR/malformed-attrs.rs:140:5 | From b9e6bd7fe254d43a8804c8bf33db2f2d0845f894 Mon Sep 17 00:00:00 2001 From: AlexanderPortland Date: Thu, 7 Aug 2025 09:13:02 -0700 Subject: [PATCH 12/13] derive hash for placeholder automatically --- compiler/rustc_public/src/ty.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/compiler/rustc_public/src/ty.rs b/compiler/rustc_public/src/ty.rs index 957940ef22f66..1b5f0ed14299e 100644 --- a/compiler/rustc_public/src/ty.rs +++ b/compiler/rustc_public/src/ty.rs @@ -249,22 +249,12 @@ pub struct BoundRegion { pub(crate) type UniverseIndex = u32; -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)] pub struct Placeholder { pub universe: UniverseIndex, pub bound: T, } -impl std::hash::Hash for Placeholder { - fn hash(&self, state: &mut H) - where - H: std::hash::Hasher, - { - self.universe.hash(state); - self.bound.hash(state); - } -} - #[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize)] pub struct Span(usize); From 7ea5e79c31f810be1401705762664fe7b5f96c4d Mon Sep 17 00:00:00 2001 From: jackh726 Date: Thu, 7 Aug 2025 20:19:43 +0000 Subject: [PATCH 13/13] Readd myself to review queue --- triagebot.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/triagebot.toml b/triagebot.toml index e8c15bb9bfdbe..7b1325899f334 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -1325,6 +1325,7 @@ compiler = [ "@eholk", "@fee1-dead", "@fmease", + "@jackh726", "@jieyouxu", "@jdonszelmann", "@lcnr",