From 30033b45bb2fcbc0f267acaf9fab22411422e166 Mon Sep 17 00:00:00 2001 From: gewitternacht <60887951+gewitternacht@users.noreply.github.com> Date: Tue, 22 Jul 2025 23:16:49 +0200 Subject: [PATCH 01/24] document assumptions about `Clone` and `Eq` traits --- library/core/src/clone.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/library/core/src/clone.rs b/library/core/src/clone.rs index a34d1b4a06497..7afdf50ce51d8 100644 --- a/library/core/src/clone.rs +++ b/library/core/src/clone.rs @@ -139,6 +139,30 @@ mod uninit; /// // Note: With the manual implementations the above line will compile. /// ``` /// +/// ## `Clone` and `PartialEq`/`Eq` +/// `Clone` is intended for the duplication of objects. Consequently, when implementing +/// both `Clone` and [`PartialEq`], the following property is expected to hold: +/// ```text +/// x == x -> x.clone() == x +/// ``` +/// In other words, if an object compares equal to itself, +/// its clone must also compare equal to the original. +/// +/// For types that also implement [`Eq`] – for which `x == x` always holds – +/// this implies that `x.clone() == x` must always be true. +/// Standard library collections such as +/// [`HashMap`], [`HashSet`], [`BTreeMap`], [`BTreeSet`] and [`BinaryHeap`] +/// rely on their keys respecting this property for correct behavior. +/// +/// This property is automatically satisfied when deriving both `Clone` and [`PartialEq`] +/// using `#[derive(Clone, PartialEq)]` or when additionally deriving [`Eq`] +/// using `#[derive(Clone, PartialEq, Eq)]`. +/// +/// Violating this property is a logic error. The behavior resulting from a logic error is not +/// specified, but users of the trait must ensure that such logic errors do *not* result in +/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these +/// methods. +/// /// ## Additional implementors /// /// In addition to the [implementors listed below][impls], From cdbcd72f341c949e4fa11f58d76a094bd435d18f Mon Sep 17 00:00:00 2001 From: gewitternacht <60887951+gewitternacht@users.noreply.github.com> Date: Wed, 23 Jul 2025 00:41:48 +0200 Subject: [PATCH 02/24] remove trailing whitespace --- library/core/src/clone.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/clone.rs b/library/core/src/clone.rs index 7afdf50ce51d8..64916f49f3a5a 100644 --- a/library/core/src/clone.rs +++ b/library/core/src/clone.rs @@ -155,7 +155,7 @@ mod uninit; /// rely on their keys respecting this property for correct behavior. /// /// This property is automatically satisfied when deriving both `Clone` and [`PartialEq`] -/// using `#[derive(Clone, PartialEq)]` or when additionally deriving [`Eq`] +/// using `#[derive(Clone, PartialEq)]` or when additionally deriving [`Eq`] /// using `#[derive(Clone, PartialEq, Eq)]`. /// /// Violating this property is a logic error. The behavior resulting from a logic error is not From 623317eb5e2bf73b53a5cd52d693ac032cf5aa32 Mon Sep 17 00:00:00 2001 From: gewitternacht <60887951+gewitternacht@users.noreply.github.com> Date: Wed, 23 Jul 2025 01:44:18 +0200 Subject: [PATCH 03/24] add links to collections --- library/core/src/clone.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/library/core/src/clone.rs b/library/core/src/clone.rs index 64916f49f3a5a..c4778edf0fccb 100644 --- a/library/core/src/clone.rs +++ b/library/core/src/clone.rs @@ -176,6 +176,11 @@ mod uninit; /// (even if the referent doesn't), /// while variables captured by mutable reference never implement `Clone`. /// +/// [`HashMap`]: ../../std/collections/struct.HashMap.html +/// [`HashSet`]: ../../std/collections/struct.HashSet.html +/// [`BTreeMap`]: ../../std/collections/struct.BTreeMap.html +/// [`BTreeSet`]: ../../std/collections/struct.BTreeSet.html +/// [`BinaryHeap`]: ../../std/collections/struct.BinaryHeap.html /// [impls]: #implementors #[stable(feature = "rust1", since = "1.0.0")] #[lang = "clone"] From c72bb70ffd284538519242663fa9443037f4d41e Mon Sep 17 00:00:00 2001 From: gewitternacht <60887951+gewitternacht@users.noreply.github.com> Date: Tue, 29 Jul 2025 08:39:28 +0200 Subject: [PATCH 04/24] clearer wording for `unsafe` code --- library/core/src/clone.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/clone.rs b/library/core/src/clone.rs index c4778edf0fccb..6fcba298a4324 100644 --- a/library/core/src/clone.rs +++ b/library/core/src/clone.rs @@ -160,8 +160,8 @@ mod uninit; /// /// Violating this property is a logic error. The behavior resulting from a logic error is not /// specified, but users of the trait must ensure that such logic errors do *not* result in -/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these -/// methods. +/// undefined behavior. This means that `unsafe` code **must not** rely on this property +/// being satisfied. /// /// ## Additional implementors /// From 6e75abd0aa4f22f6291f1a25ff81e0c6b959ad26 Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Sun, 20 Jul 2025 22:35:43 +0530 Subject: [PATCH 05/24] std: sys: io: io_slice: Add UEFI types UEFI networking APIs do support vectored read/write. While the types for UDP4, UDP6, TCP4 and TCP6 are defined separately, they are essentially the same C struct. So we can map IoSlice and IoSliceMut to have the same binary representation. Since all UEFI networking types for read/write are DSTs, `IoSlice` and `IoSliceMut` will need to be copied to the end of the transmit/receive structures. So having the same binary representation just allows us to do a single memcpy instead of having to loop and set the DST. Signed-off-by: Ayush Singh --- library/std/src/sys/io/io_slice/uefi.rs | 74 +++++++++++++++ library/std/src/sys/io/mod.rs | 3 + library/std/src/sys/pal/uefi/tests.rs | 119 ++++++++++++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 library/std/src/sys/io/io_slice/uefi.rs diff --git a/library/std/src/sys/io/io_slice/uefi.rs b/library/std/src/sys/io/io_slice/uefi.rs new file mode 100644 index 0000000000000..909cfbea0b7ba --- /dev/null +++ b/library/std/src/sys/io/io_slice/uefi.rs @@ -0,0 +1,74 @@ +//! A buffer type used with `Write::write_vectored` for UEFI Networking APIs. Vectored writing to +//! File is not supported as of UEFI Spec 2.11. + +use crate::marker::PhantomData; +use crate::slice; + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct IoSlice<'a> { + len: u32, + data: *const u8, + _p: PhantomData<&'a [u8]>, +} + +impl<'a> IoSlice<'a> { + #[inline] + pub fn new(buf: &'a [u8]) -> IoSlice<'a> { + let len = buf.len().try_into().unwrap(); + Self { len, data: buf.as_ptr(), _p: PhantomData } + } + + #[inline] + pub fn advance(&mut self, n: usize) { + self.len = u32::try_from(n) + .ok() + .and_then(|n| self.len.checked_sub(n)) + .expect("advancing IoSlice beyond its length"); + unsafe { self.data = self.data.add(n) }; + } + + #[inline] + pub const fn as_slice(&self) -> &'a [u8] { + unsafe { slice::from_raw_parts(self.data, self.len as usize) } + } +} + +#[repr(C)] +pub struct IoSliceMut<'a> { + len: u32, + data: *mut u8, + _p: PhantomData<&'a mut [u8]>, +} + +impl<'a> IoSliceMut<'a> { + #[inline] + pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> { + let len = buf.len().try_into().unwrap(); + Self { len, data: buf.as_mut_ptr(), _p: PhantomData } + } + + #[inline] + pub fn advance(&mut self, n: usize) { + self.len = u32::try_from(n) + .ok() + .and_then(|n| self.len.checked_sub(n)) + .expect("advancing IoSlice beyond its length"); + unsafe { self.data = self.data.add(n) }; + } + + #[inline] + pub fn as_slice(&self) -> &[u8] { + unsafe { slice::from_raw_parts(self.data, self.len as usize) } + } + + #[inline] + pub const fn into_slice(self) -> &'a mut [u8] { + unsafe { slice::from_raw_parts_mut(self.data, self.len as usize) } + } + + #[inline] + pub fn as_mut_slice(&mut self) -> &mut [u8] { + unsafe { slice::from_raw_parts_mut(self.data, self.len as usize) } + } +} diff --git a/library/std/src/sys/io/mod.rs b/library/std/src/sys/io/mod.rs index 4d0365d42fd9b..ae75f4d97b431 100644 --- a/library/std/src/sys/io/mod.rs +++ b/library/std/src/sys/io/mod.rs @@ -11,6 +11,9 @@ mod io_slice { } else if #[cfg(target_os = "wasi")] { mod wasi; pub use wasi::*; + } else if #[cfg(target_os = "uefi")] { + mod uefi; + pub use uefi::*; } else { mod unsupported; pub use unsupported::*; diff --git a/library/std/src/sys/pal/uefi/tests.rs b/library/std/src/sys/pal/uefi/tests.rs index 38658cc4e9ac4..49e75a1a70d7d 100644 --- a/library/std/src/sys/pal/uefi/tests.rs +++ b/library/std/src/sys/pal/uefi/tests.rs @@ -1,5 +1,6 @@ use super::alloc::*; use super::time::*; +use crate::io::{IoSlice, IoSliceMut}; use crate::time::Duration; #[test] @@ -39,3 +40,121 @@ fn epoch() { }; assert_eq!(system_time_internal::uefi_time_to_duration(t), Duration::new(0, 0)); } + +// UEFI IoSlice and IoSliceMut Tests +// +// Strictly speaking, vectored read/write types for UDP4, UDP6, TCP4, TCP6 are defined +// separately in the UEFI Spec. However, they have the same signature. These tests just ensure +// that `IoSlice` and `IoSliceMut` are compatible with the vectored types for all the +// networking protocols. + +unsafe fn to_slice(val: &T) -> &[u8] { + let len = size_of_val(val); + unsafe { crate::slice::from_raw_parts(crate::ptr::from_ref(val).cast(), len) } +} + +#[test] +fn io_slice_single() { + let mut data = [0, 1, 2, 3, 4]; + + let tcp4_frag = r_efi::protocols::tcp4::FragmentData { + fragment_length: data.len().try_into().unwrap(), + fragment_buffer: data.as_mut_ptr().cast(), + }; + let tcp6_frag = r_efi::protocols::tcp6::FragmentData { + fragment_length: data.len().try_into().unwrap(), + fragment_buffer: data.as_mut_ptr().cast(), + }; + let udp4_frag = r_efi::protocols::udp4::FragmentData { + fragment_length: data.len().try_into().unwrap(), + fragment_buffer: data.as_mut_ptr().cast(), + }; + let udp6_frag = r_efi::protocols::udp6::FragmentData { + fragment_length: data.len().try_into().unwrap(), + fragment_buffer: data.as_mut_ptr().cast(), + }; + let io_slice = IoSlice::new(&data); + + unsafe { + assert_eq!(to_slice(&io_slice), to_slice(&tcp4_frag)); + assert_eq!(to_slice(&io_slice), to_slice(&tcp6_frag)); + assert_eq!(to_slice(&io_slice), to_slice(&udp4_frag)); + assert_eq!(to_slice(&io_slice), to_slice(&udp6_frag)); + } +} + +#[test] +fn io_slice_mut_single() { + let mut data = [0, 1, 2, 3, 4]; + + let tcp4_frag = r_efi::protocols::tcp4::FragmentData { + fragment_length: data.len().try_into().unwrap(), + fragment_buffer: data.as_mut_ptr().cast(), + }; + let tcp6_frag = r_efi::protocols::tcp6::FragmentData { + fragment_length: data.len().try_into().unwrap(), + fragment_buffer: data.as_mut_ptr().cast(), + }; + let udp4_frag = r_efi::protocols::udp4::FragmentData { + fragment_length: data.len().try_into().unwrap(), + fragment_buffer: data.as_mut_ptr().cast(), + }; + let udp6_frag = r_efi::protocols::udp6::FragmentData { + fragment_length: data.len().try_into().unwrap(), + fragment_buffer: data.as_mut_ptr().cast(), + }; + let io_slice_mut = IoSliceMut::new(&mut data); + + unsafe { + assert_eq!(to_slice(&io_slice_mut), to_slice(&tcp4_frag)); + assert_eq!(to_slice(&io_slice_mut), to_slice(&tcp6_frag)); + assert_eq!(to_slice(&io_slice_mut), to_slice(&udp4_frag)); + assert_eq!(to_slice(&io_slice_mut), to_slice(&udp6_frag)); + } +} + +#[test] +fn io_slice_multi() { + let mut data = [0, 1, 2, 3, 4]; + + let tcp4_frag = r_efi::protocols::tcp4::FragmentData { + fragment_length: data.len().try_into().unwrap(), + fragment_buffer: data.as_mut_ptr().cast(), + }; + let rhs = + [tcp4_frag.clone(), tcp4_frag.clone(), tcp4_frag.clone(), tcp4_frag.clone(), tcp4_frag]; + let lhs = [ + IoSlice::new(&data), + IoSlice::new(&data), + IoSlice::new(&data), + IoSlice::new(&data), + IoSlice::new(&data), + ]; + + unsafe { + assert_eq!(to_slice(&lhs), to_slice(&rhs)); + } +} + +#[test] +fn io_slice_basic() { + let data = [0, 1, 2, 3, 4]; + let mut io_slice = IoSlice::new(&data); + + assert_eq!(data, io_slice.as_slice()); + io_slice.advance(2); + assert_eq!(&data[2..], io_slice.as_slice()); +} + +#[test] +fn io_slice_mut_basic() { + let data = [0, 1, 2, 3, 4]; + let mut data_clone = [0, 1, 2, 3, 4]; + let mut io_slice_mut = IoSliceMut::new(&mut data_clone); + + assert_eq!(data, io_slice_mut.as_slice()); + assert_eq!(data, io_slice_mut.as_mut_slice()); + + io_slice_mut.advance(2); + assert_eq!(&data[2..], io_slice_mut.into_slice()); +} From b4675dcac87378d1e936ae928fe06f38c4cf6a30 Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Fri, 8 Aug 2025 22:44:48 +0530 Subject: [PATCH 06/24] add download_ci_rustc_commit function and invoke from parse_inner --- src/bootstrap/src/core/config/config.rs | 129 +++++++++++++++++++++++- 1 file changed, 128 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index 2c008f957d98b..e7b7ab4f4d4bc 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -947,7 +947,14 @@ impl Config { ); } - config.download_rustc_commit = config.download_ci_rustc_commit( + config.download_rustc_commit = download_ci_rustc_commit( + &config.stage0_metadata, + config.path_modification_cache.clone(), + &config.src, + config.is_running_on_ci, + &config.exec_ctx, + &config.rust_info, + &config.host_target, rust_download_rustc, debug_assertions_requested, config.llvm_assertions, @@ -2335,3 +2342,123 @@ pub fn check_stage0_version( )); } } + +pub fn download_ci_rustc_commit( + stage0_metadata: &build_helper::stage0_parser::Stage0, + path_modification_cache: Arc, PathFreshness>>>, + src: &Path, + is_running_on_ci: bool, + exec_ctx: &ExecutionContext, + rust_info: &channel::GitInfo, + host_target: &TargetSelection, + download_rustc: Option, + debug_assertions_requested: bool, + llvm_assertions: bool, +) -> Option { + if !is_download_ci_available(&host_target.triple, llvm_assertions) { + return None; + } + + // If `download-rustc` is not set, default to rebuilding. + let if_unchanged = match download_rustc { + // Globally default `download-rustc` to `false`, because some contributors don't use + // profiles for reasons such as: + // - They need to seamlessly switch between compiler/library work. + // - They don't want to use compiler profile because they need to override too many + // things and it's easier to not use a profile. + None | Some(StringOrBool::Bool(false)) => return None, + Some(StringOrBool::Bool(true)) => false, + Some(StringOrBool::String(s)) if s == "if-unchanged" => { + if !rust_info.is_managed_git_subrepository() { + println!( + "ERROR: `download-rustc=if-unchanged` is only compatible with Git managed sources." + ); + crate::exit!(1); + } + + true + } + Some(StringOrBool::String(other)) => { + panic!("unrecognized option for download-rustc: {other}") + } + }; + + let commit = if rust_info.is_managed_git_subrepository() { + // Look for a version to compare to based on the current commit. + // Only commits merged by bors will have CI artifacts. + let freshness = check_path_modifications_( + stage0_metadata, + src, + path_modification_cache, + RUSTC_IF_UNCHANGED_ALLOWED_PATHS, + ); + exec_ctx.verbose(|| { + eprintln!("rustc freshness: {freshness:?}"); + }); + match freshness { + PathFreshness::LastModifiedUpstream { upstream } => upstream, + PathFreshness::HasLocalModifications { upstream } => { + if if_unchanged { + return None; + } + + if is_running_on_ci { + eprintln!("CI rustc commit matches with HEAD and we are in CI."); + eprintln!( + "`rustc.download-ci` functionality will be skipped as artifacts are not available." + ); + return None; + } + + upstream + } + PathFreshness::MissingUpstream => { + eprintln!("No upstream commit found"); + return None; + } + } + } else { + channel::read_commit_info_file(src) + .map(|info| info.sha.trim().to_owned()) + .expect("git-commit-info is missing in the project root") + }; + + if debug_assertions_requested { + eprintln!( + "WARN: `rust.debug-assertions = true` will prevent downloading CI rustc as alt CI \ + rustc is not currently built with debug assertions." + ); + return None; + } + + Some(commit) +} + +pub fn check_path_modifications_( + stage0_metadata: &build_helper::stage0_parser::Stage0, + src: &Path, + path_modification_cache: Arc, PathFreshness>>>, + paths: &[&'static str], +) -> PathFreshness { + // Checking path modifications through git can be relatively expensive (>100ms). + // We do not assume that the sources would change during bootstrap's execution, + // so we can cache the results here. + // Note that we do not use a static variable for the cache, because it would cause problems + // in tests that create separate `Config` instsances. + path_modification_cache + .lock() + .unwrap() + .entry(paths.to_vec()) + .or_insert_with(|| { + check_path_modifications(src, &git_config(stage0_metadata), paths, CiEnv::current()) + .unwrap() + }) + .clone() +} + +pub fn git_config(stage0_metadata: &build_helper::stage0_parser::Stage0) -> GitConfig<'_> { + GitConfig { + nightly_branch: &stage0_metadata.config.nightly_branch, + git_merge_commit_email: &stage0_metadata.config.git_merge_commit_email, + } +} From 111a0e8f2347c7e5996fe1e63d9ade86fba81e01 Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Fri, 8 Aug 2025 23:21:57 +0530 Subject: [PATCH 07/24] add parse_download_ci_llvm function and invoke from parse_inner --- src/bootstrap/src/core/build_steps/llvm.rs | 9 +- src/bootstrap/src/core/config/config.rs | 246 ++++++++++++++++++++- 2 files changed, 248 insertions(+), 7 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/llvm.rs b/src/bootstrap/src/core/build_steps/llvm.rs index 721ba6ca459e4..513d7260a3ece 100644 --- a/src/bootstrap/src/core/build_steps/llvm.rs +++ b/src/bootstrap/src/core/build_steps/llvm.rs @@ -196,7 +196,10 @@ pub(crate) fn detect_llvm_freshness(config: &Config, is_git: bool) -> PathFreshn /// /// This checks the build triple platform to confirm we're usable at all, and if LLVM /// with/without assertions is available. -pub(crate) fn is_ci_llvm_available_for_target(config: &Config, asserts: bool) -> bool { +pub(crate) fn is_ci_llvm_available_for_target( + host_target: &TargetSelection, + asserts: bool, +) -> bool { // This is currently all tier 1 targets and tier 2 targets with host tools // (since others may not have CI artifacts) // https://doc.rust-lang.org/rustc/platform-support.html#tier-1 @@ -235,8 +238,8 @@ pub(crate) fn is_ci_llvm_available_for_target(config: &Config, asserts: bool) -> ("x86_64-unknown-netbsd", false), ]; - if !supported_platforms.contains(&(&*config.host_target.triple, asserts)) - && (asserts || !supported_platforms.contains(&(&*config.host_target.triple, true))) + if !supported_platforms.contains(&(&*host_target.triple, asserts)) + && (asserts || !supported_platforms.contains(&(&*host_target.triple, true))) { return false; } diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index e7b7ab4f4d4bc..bfee43148c0e9 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -1200,8 +1200,19 @@ impl Config { 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.llvm_from_ci = parse_download_ci_llvm( + &config.exec_ctx, + &config.submodules, + &config.stage0_metadata, + &config.src, + config.path_modification_cache.clone(), + &config.host_target, + &config.download_rustc_commit, + &config.rust_info, + config.is_running_on_ci, + llvm_download_ci_llvm, + config.llvm_assertions, + ); if config.llvm_from_ci { let warn = |option: &str| { @@ -1902,7 +1913,11 @@ impl Config { let has_changes = self.has_changes_from_upstream(LLVM_INVALIDATION_PATHS); // Return false if there are untracked changes, otherwise check if CI LLVM is available. - if has_changes { false } else { llvm::is_ci_llvm_available_for_target(self, asserts) } + if has_changes { + false + } else { + llvm::is_ci_llvm_available_for_target(&self.host_target, asserts) + } }; match download_ci_llvm { @@ -1921,7 +1936,7 @@ impl Config { } // If download-ci-llvm=true we also want to check that CI llvm is available - b && llvm::is_ci_llvm_available_for_target(self, asserts) + b && llvm::is_ci_llvm_available_for_target(&self.host_target, asserts) } StringOrBool::String(s) if s == "if-unchanged" => if_unchanged(), StringOrBool::String(other) => { @@ -2462,3 +2477,226 @@ pub fn git_config(stage0_metadata: &build_helper::stage0_parser::Stage0) -> GitC git_merge_commit_email: &stage0_metadata.config.git_merge_commit_email, } } + +pub fn parse_download_ci_llvm( + exec_ctx: &ExecutionContext, + submodules: &Option, + stage0_metadata: &build_helper::stage0_parser::Stage0, + src: &Path, + path_modification_cache: Arc, PathFreshness>>>, + host_target: &TargetSelection, + download_rustc_commit: &Option, + rust_info: &channel::GitInfo, + is_running_on_ci: bool, + download_ci_llvm: Option, + asserts: bool, +) -> bool { + // We don't ever want to use `true` on CI, as we should not + // download upstream artifacts if there are any local modifications. + let default = if is_running_on_ci { + StringOrBool::String("if-unchanged".to_string()) + } else { + StringOrBool::Bool(true) + }; + let download_ci_llvm = download_ci_llvm.unwrap_or(default); + + let if_unchanged = || { + if rust_info.is_from_tarball() { + // Git is needed for running "if-unchanged" logic. + println!("ERROR: 'if-unchanged' is only compatible with Git managed sources."); + crate::exit!(1); + } + + // Fetching the LLVM submodule is unnecessary for self-tests. + #[cfg(not(test))] + update_submodule(submodules, exec_ctx, src, rust_info, "src/llvm-project"); + + // Check for untracked changes in `src/llvm-project` and other important places. + let has_changes = has_changes_from_upstream( + stage0_metadata, + src, + path_modification_cache, + LLVM_INVALIDATION_PATHS, + ); + + // Return false if there are untracked changes, otherwise check if CI LLVM is available. + if has_changes { + false + } else { + llvm::is_ci_llvm_available_for_target(host_target, asserts) + } + }; + + match download_ci_llvm { + StringOrBool::Bool(b) => { + if !b && download_rustc_commit.is_some() { + panic!( + "`llvm.download-ci-llvm` cannot be set to `false` if `rust.download-rustc` is set to `true` or `if-unchanged`." + ); + } + + if b && is_running_on_ci { + // On CI, we must always rebuild LLVM if there were any modifications to it + panic!( + "`llvm.download-ci-llvm` cannot be set to `true` on CI. Use `if-unchanged` instead." + ); + } + + // If download-ci-llvm=true we also want to check that CI llvm is available + b && llvm::is_ci_llvm_available_for_target(host_target, asserts) + } + StringOrBool::String(s) if s == "if-unchanged" => if_unchanged(), + StringOrBool::String(other) => { + panic!("unrecognized option for download-ci-llvm: {other:?}") + } + } +} + +pub fn has_changes_from_upstream( + stage0_metadata: &build_helper::stage0_parser::Stage0, + src: &Path, + path_modification_cache: Arc, PathFreshness>>>, + paths: &[&'static str], +) -> bool { + match check_path_modifications_(stage0_metadata, src, path_modification_cache, paths) { + PathFreshness::LastModifiedUpstream { .. } => false, + PathFreshness::HasLocalModifications { .. } | PathFreshness::MissingUpstream => true, + } +} + +#[cfg_attr( + feature = "tracing", + instrument( + level = "trace", + name = "Config::update_submodule", + skip_all, + fields(relative_path = ?relative_path), + ), +)] +pub(crate) fn update_submodule( + submodules: &Option, + exec_ctx: &ExecutionContext, + src: &Path, + rust_info: &channel::GitInfo, + relative_path: &str, +) { + if rust_info.is_from_tarball() || !submodules_(submodules, rust_info) { + return; + } + + let absolute_path = src.join(relative_path); + + // NOTE: This check is required because `jj git clone` doesn't create directories for + // submodules, they are completely ignored. The code below assumes this directory exists, + // so create it here. + if !absolute_path.exists() { + t!(fs::create_dir_all(&absolute_path)); + } + + // NOTE: The check for the empty directory is here because when running x.py the first time, + // the submodule won't be checked out. Check it out now so we can build it. + if !git_info(exec_ctx, false, &absolute_path).is_managed_git_subrepository() + && !helpers::dir_is_empty(&absolute_path) + { + return; + } + + // Submodule updating actually happens during in the dry run mode. We need to make sure that + // all the git commands below are actually executed, because some follow-up code + // in bootstrap might depend on the submodules being checked out. Furthermore, not all + // the command executions below work with an empty output (produced during dry run). + // Therefore, all commands below are marked with `run_in_dry_run()`, so that they also run in + // dry run mode. + let submodule_git = || { + let mut cmd = helpers::git(Some(&absolute_path)); + cmd.run_in_dry_run(); + cmd + }; + + // Determine commit checked out in submodule. + let checked_out_hash = + submodule_git().args(["rev-parse", "HEAD"]).run_capture_stdout(exec_ctx).stdout(); + let checked_out_hash = checked_out_hash.trim_end(); + // Determine commit that the submodule *should* have. + let recorded = helpers::git(Some(src)) + .run_in_dry_run() + .args(["ls-tree", "HEAD"]) + .arg(relative_path) + .run_capture_stdout(exec_ctx) + .stdout(); + + let actual_hash = recorded + .split_whitespace() + .nth(2) + .unwrap_or_else(|| panic!("unexpected output `{recorded}`")); + + if actual_hash == checked_out_hash { + // already checked out + return; + } + + println!("Updating submodule {relative_path}"); + + helpers::git(Some(src)) + .allow_failure() + .run_in_dry_run() + .args(["submodule", "-q", "sync"]) + .arg(relative_path) + .run(exec_ctx); + + // Try passing `--progress` to start, then run git again without if that fails. + let update = |progress: bool| { + // Git is buggy and will try to fetch submodules from the tracking branch for *this* repository, + // even though that has no relation to the upstream for the submodule. + let current_branch = helpers::git(Some(src)) + .allow_failure() + .run_in_dry_run() + .args(["symbolic-ref", "--short", "HEAD"]) + .run_capture(exec_ctx); + + let mut git = helpers::git(Some(&src)).allow_failure(); + git.run_in_dry_run(); + if current_branch.is_success() { + // If there is a tag named after the current branch, git will try to disambiguate by prepending `heads/` to the branch name. + // This syntax isn't accepted by `branch.{branch}`. Strip it. + let branch = current_branch.stdout(); + let branch = branch.trim(); + let branch = branch.strip_prefix("heads/").unwrap_or(branch); + git.arg("-c").arg(format!("branch.{branch}.remote=origin")); + } + git.args(["submodule", "update", "--init", "--recursive", "--depth=1"]); + if progress { + git.arg("--progress"); + } + git.arg(relative_path); + git + }; + if !update(true).allow_failure().run(exec_ctx) { + update(false).allow_failure().run(exec_ctx); + } + + // Save any local changes, but avoid running `git stash pop` if there are none (since it will exit with an error). + // diff-index reports the modifications through the exit status + let has_local_modifications = + !submodule_git().allow_failure().args(["diff-index", "--quiet", "HEAD"]).run(exec_ctx); + if has_local_modifications { + submodule_git().allow_failure().args(["stash", "push"]).run(exec_ctx); + } + + submodule_git().allow_failure().args(["reset", "-q", "--hard"]).run(exec_ctx); + submodule_git().allow_failure().args(["clean", "-qdfx"]).run(exec_ctx); + + if has_local_modifications { + submodule_git().allow_failure().args(["stash", "pop"]).run(exec_ctx); + } +} + +pub fn git_info(exec_ctx: &ExecutionContext, omit_git_hash: bool, dir: &Path) -> GitInfo { + GitInfo::new(omit_git_hash, dir, exec_ctx) +} + +pub fn submodules_(submodules: &Option, rust_info: &channel::GitInfo) -> bool { + // If not specified in config, the default is to only manage + // submodules if we're currently inside a git repository. + submodules.unwrap_or(rust_info.is_managed_git_subrepository()) +} From 7eb8f6001e528e811018824e71e36547ea9b7e50 Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Sat, 9 Aug 2025 07:23:05 +0530 Subject: [PATCH 08/24] add is_system_llvm function and invoke from parse_inner --- src/bootstrap/src/core/config/config.rs | 34 ++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index bfee43148c0e9..56c549297e27b 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -1315,7 +1315,14 @@ impl Config { ); } - if config.lld_enabled && config.is_system_llvm(config.host_target) { + if config.lld_enabled + && is_system_llvm( + &config.host_target, + config.llvm_from_ci, + &config.target_config, + config.host_target, + ) + { panic!("Cannot enable LLD with `rust.lld = true` when using external llvm-config."); } @@ -2700,3 +2707,28 @@ pub fn submodules_(submodules: &Option, rust_info: &channel::GitInfo) -> b // submodules if we're currently inside a git repository. submodules.unwrap_or(rust_info.is_managed_git_subrepository()) } + +/// Returns `true` if this is an external version of LLVM not managed by bootstrap. +/// In particular, we expect llvm sources to be available when this is false. +/// +/// NOTE: this is not the same as `!is_rust_llvm` when `llvm_has_patches` is set. +pub fn is_system_llvm( + host_target: &TargetSelection, + llvm_from_ci: bool, + target_config: &HashMap, + target: TargetSelection, +) -> bool { + match target_config.get(&target) { + Some(Target { llvm_config: Some(_), .. }) => { + let ci_llvm = llvm_from_ci && is_host_target(host_target, &target); + !ci_llvm + } + // We're building from the in-tree src/llvm-project sources. + Some(Target { llvm_config: None, .. }) => false, + None => false, + } +} + +pub fn is_host_target(host_target: &TargetSelection, target: &TargetSelection) -> bool { + host_target == target +} From 134fcc8c66808bb781ae67e786e46d63fc48b4b7 Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Sat, 9 Aug 2025 07:31:55 +0530 Subject: [PATCH 09/24] add git_info function and invoke from parse_inner --- src/bootstrap/src/core/config/config.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index 56c549297e27b..e5bee0e7ce56a 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -893,21 +893,25 @@ impl Config { let default = config.channel == "dev"; config.omit_git_hash = rust_omit_git_hash.unwrap_or(default); - config.rust_info = config.git_info(config.omit_git_hash, &config.src); + config.rust_info = git_info(&config.exec_ctx, config.omit_git_hash, &config.src); config.cargo_info = - config.git_info(config.omit_git_hash, &config.src.join("src/tools/cargo")); - config.rust_analyzer_info = - config.git_info(config.omit_git_hash, &config.src.join("src/tools/rust-analyzer")); + git_info(&config.exec_ctx, config.omit_git_hash, &config.src.join("src/tools/cargo")); + config.rust_analyzer_info = git_info( + &config.exec_ctx, + config.omit_git_hash, + &config.src.join("src/tools/rust-analyzer"), + ); config.clippy_info = - config.git_info(config.omit_git_hash, &config.src.join("src/tools/clippy")); + git_info(&config.exec_ctx, config.omit_git_hash, &config.src.join("src/tools/clippy")); config.miri_info = - config.git_info(config.omit_git_hash, &config.src.join("src/tools/miri")); + git_info(&config.exec_ctx, config.omit_git_hash, &config.src.join("src/tools/miri")); config.rustfmt_info = - config.git_info(config.omit_git_hash, &config.src.join("src/tools/rustfmt")); + git_info(&config.exec_ctx, config.omit_git_hash, &config.src.join("src/tools/rustfmt")); config.enzyme_info = - config.git_info(config.omit_git_hash, &config.src.join("src/tools/enzyme")); - 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")); + git_info(&config.exec_ctx, config.omit_git_hash, &config.src.join("src/tools/enzyme")); + config.in_tree_llvm_info = + git_info(&config.exec_ctx, false, &config.src.join("src/llvm-project")); + config.in_tree_gcc_info = git_info(&config.exec_ctx, false, &config.src.join("src/gcc")); config.vendor = build_vendor.unwrap_or( config.rust_info.is_from_tarball() From 2213b7a8f9c3e778a32cddfc19ec58efefcb902c Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Sat, 9 Aug 2025 07:47:26 +0530 Subject: [PATCH 10/24] add ci_llvm_root function and invoke from parse_inner --- src/bootstrap/src/core/config/config.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index e5bee0e7ce56a..ef8d541500f90 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -1278,7 +1278,8 @@ impl Config { if config.llvm_from_ci { let triple = &config.host_target.triple; - let ci_llvm_bin = config.ci_llvm_root().join("bin"); + let ci_llvm_bin = + ci_llvm_root(config.llvm_from_ci, &config.out, &config.host_target).join("bin"); let build_target = config .target_config .entry(config.host_target) @@ -2736,3 +2737,12 @@ pub fn is_system_llvm( pub fn is_host_target(host_target: &TargetSelection, target: &TargetSelection) -> bool { host_target == target } + +pub(crate) fn ci_llvm_root( + llvm_from_ci: bool, + out: &Path, + host_target: &TargetSelection, +) -> PathBuf { + assert!(llvm_from_ci); + out.join(host_target).join("ci-llvm") +} From 621af4a580436013e7af848f5abea9bc21b21c9b Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Sat, 9 Aug 2025 07:53:56 +0530 Subject: [PATCH 11/24] add read_file_by_commit function and invoke from parse_inner --- src/bootstrap/src/core/config/config.rs | 29 +++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index ef8d541500f90..af6edb1242cc5 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -1168,8 +1168,15 @@ impl Config { "WARNING: `rust.download-rustc` is enabled. The `rust.channel` option will be overridden by the CI rustc's channel." ); - let channel = - config.read_file_by_commit(Path::new("src/ci/channel"), commit).trim().to_owned(); + let channel = read_file_by_commit( + &config.exec_ctx, + &config.src, + &config.rust_info, + Path::new("src/ci/channel"), + commit, + ) + .trim() + .to_owned(); config.channel = channel; } @@ -2746,3 +2753,21 @@ pub(crate) fn ci_llvm_root( assert!(llvm_from_ci); out.join(host_target).join("ci-llvm") } + +/// Returns the content of the given file at a specific commit. +pub(crate) fn read_file_by_commit( + exec_ctx: &ExecutionContext, + src: &Path, + rust_info: &channel::GitInfo, + file: &Path, + commit: &str, +) -> String { + assert!( + rust_info.is_managed_git_subrepository(), + "`Config::read_file_by_commit` is not supported in non-git sources." + ); + + let mut git = helpers::git(Some(src)); + git.arg("show").arg(format!("{commit}:{}", file.to_str().unwrap())); + git.run_capture_stdout(exec_ctx).stdout() +} From aad54a8ff4d25d4a20b25ccfcfbae01c9a2310ba Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Sat, 9 Aug 2025 08:41:16 +0530 Subject: [PATCH 12/24] invoke functions from methods --- src/bootstrap/src/core/config/config.rs | 232 +++--------------------- 1 file changed, 28 insertions(+), 204 deletions(-) diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index af6edb1242cc5..2912b3f30c303 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -1469,14 +1469,7 @@ impl Config { /// Returns the content of the given file at a specific commit. pub(crate) fn read_file_by_commit(&self, file: &Path, commit: &str) -> String { - assert!( - self.rust_info.is_managed_git_subrepository(), - "`Config::read_file_by_commit` is not supported in non-git sources." - ); - - let mut git = helpers::git(Some(&self.src)); - git.arg("show").arg(format!("{commit}:{}", file.to_str().unwrap())); - git.run_capture_stdout(self).stdout() + read_file_by_commit(&self.exec_ctx, &self.src, &self.rust_info, file, commit) } /// Bootstrap embeds a version number into the name of shared libraries it uploads in CI. @@ -1547,8 +1540,7 @@ impl Config { /// The absolute path to the downloaded LLVM artifacts. pub(crate) fn ci_llvm_root(&self) -> PathBuf { - assert!(self.llvm_from_ci); - self.out.join(self.host_target).join("ci-llvm") + ci_llvm_root(self.llvm_from_ci, &self.out, &self.host_target) } /// Directory where the extracted `rustc-dev` component is stored. @@ -1711,115 +1703,13 @@ impl Config { ), )] pub(crate) fn update_submodule(&self, relative_path: &str) { - if self.rust_info.is_from_tarball() || !self.submodules() { - return; - } - - let absolute_path = self.src.join(relative_path); - - // NOTE: This check is required because `jj git clone` doesn't create directories for - // submodules, they are completely ignored. The code below assumes this directory exists, - // so create it here. - if !absolute_path.exists() { - t!(fs::create_dir_all(&absolute_path)); - } - - // NOTE: The check for the empty directory is here because when running x.py the first time, - // the submodule won't be checked out. Check it out now so we can build it. - if !self.git_info(false, &absolute_path).is_managed_git_subrepository() - && !helpers::dir_is_empty(&absolute_path) - { - return; - } - - // Submodule updating actually happens during in the dry run mode. We need to make sure that - // all the git commands below are actually executed, because some follow-up code - // in bootstrap might depend on the submodules being checked out. Furthermore, not all - // the command executions below work with an empty output (produced during dry run). - // Therefore, all commands below are marked with `run_in_dry_run()`, so that they also run in - // dry run mode. - let submodule_git = || { - let mut cmd = helpers::git(Some(&absolute_path)); - cmd.run_in_dry_run(); - cmd - }; - - // Determine commit checked out in submodule. - let checked_out_hash = - submodule_git().args(["rev-parse", "HEAD"]).run_capture_stdout(self).stdout(); - let checked_out_hash = checked_out_hash.trim_end(); - // Determine commit that the submodule *should* have. - let recorded = helpers::git(Some(&self.src)) - .run_in_dry_run() - .args(["ls-tree", "HEAD"]) - .arg(relative_path) - .run_capture_stdout(self) - .stdout(); - - let actual_hash = recorded - .split_whitespace() - .nth(2) - .unwrap_or_else(|| panic!("unexpected output `{recorded}`")); - - if actual_hash == checked_out_hash { - // already checked out - return; - } - - println!("Updating submodule {relative_path}"); - - helpers::git(Some(&self.src)) - .allow_failure() - .run_in_dry_run() - .args(["submodule", "-q", "sync"]) - .arg(relative_path) - .run(self); - - // Try passing `--progress` to start, then run git again without if that fails. - let update = |progress: bool| { - // Git is buggy and will try to fetch submodules from the tracking branch for *this* repository, - // even though that has no relation to the upstream for the submodule. - let current_branch = helpers::git(Some(&self.src)) - .allow_failure() - .run_in_dry_run() - .args(["symbolic-ref", "--short", "HEAD"]) - .run_capture(self); - - let mut git = helpers::git(Some(&self.src)).allow_failure(); - git.run_in_dry_run(); - if current_branch.is_success() { - // If there is a tag named after the current branch, git will try to disambiguate by prepending `heads/` to the branch name. - // This syntax isn't accepted by `branch.{branch}`. Strip it. - let branch = current_branch.stdout(); - let branch = branch.trim(); - let branch = branch.strip_prefix("heads/").unwrap_or(branch); - git.arg("-c").arg(format!("branch.{branch}.remote=origin")); - } - git.args(["submodule", "update", "--init", "--recursive", "--depth=1"]); - if progress { - git.arg("--progress"); - } - git.arg(relative_path); - git - }; - if !update(true).allow_failure().run(self) { - update(false).allow_failure().run(self); - } - - // Save any local changes, but avoid running `git stash pop` if there are none (since it will exit with an error). - // diff-index reports the modifications through the exit status - let has_local_modifications = - !submodule_git().allow_failure().args(["diff-index", "--quiet", "HEAD"]).run(self); - if has_local_modifications { - submodule_git().allow_failure().args(["stash", "push"]).run(self); - } - - submodule_git().allow_failure().args(["reset", "-q", "--hard"]).run(self); - submodule_git().allow_failure().args(["clean", "-qdfx"]).run(self); - - if has_local_modifications { - submodule_git().allow_failure().args(["stash", "pop"]).run(self); - } + update_submodule( + &self.submodules, + &self.exec_ctx, + &self.src, + &self.rust_info, + relative_path, + ); } /// Returns the commit to download, or `None` if we shouldn't download CI artifacts. @@ -1829,78 +1719,18 @@ impl Config { debug_assertions_requested: bool, llvm_assertions: bool, ) -> Option { - if !is_download_ci_available(&self.host_target.triple, llvm_assertions) { - return None; - } - - // If `download-rustc` is not set, default to rebuilding. - let if_unchanged = match download_rustc { - // Globally default `download-rustc` to `false`, because some contributors don't use - // profiles for reasons such as: - // - They need to seamlessly switch between compiler/library work. - // - They don't want to use compiler profile because they need to override too many - // things and it's easier to not use a profile. - None | Some(StringOrBool::Bool(false)) => return None, - Some(StringOrBool::Bool(true)) => false, - Some(StringOrBool::String(s)) if s == "if-unchanged" => { - if !self.rust_info.is_managed_git_subrepository() { - println!( - "ERROR: `download-rustc=if-unchanged` is only compatible with Git managed sources." - ); - crate::exit!(1); - } - - true - } - Some(StringOrBool::String(other)) => { - panic!("unrecognized option for download-rustc: {other}") - } - }; - - let commit = if self.rust_info.is_managed_git_subrepository() { - // Look for a version to compare to based on the current commit. - // Only commits merged by bors will have CI artifacts. - let freshness = self.check_path_modifications(RUSTC_IF_UNCHANGED_ALLOWED_PATHS); - self.verbose(|| { - eprintln!("rustc freshness: {freshness:?}"); - }); - match freshness { - PathFreshness::LastModifiedUpstream { upstream } => upstream, - PathFreshness::HasLocalModifications { upstream } => { - if if_unchanged { - return None; - } - - if self.is_running_on_ci { - eprintln!("CI rustc commit matches with HEAD and we are in CI."); - eprintln!( - "`rustc.download-ci` functionality will be skipped as artifacts are not available." - ); - return None; - } - - upstream - } - PathFreshness::MissingUpstream => { - eprintln!("No upstream commit found"); - return None; - } - } - } else { - channel::read_commit_info_file(&self.src) - .map(|info| info.sha.trim().to_owned()) - .expect("git-commit-info is missing in the project root") - }; - - if debug_assertions_requested { - eprintln!( - "WARN: `rust.debug-assertions = true` will prevent downloading CI rustc as alt CI \ - rustc is not currently built with debug assertions." - ); - return None; - } - - Some(commit) + download_ci_rustc_commit( + &self.stage0_metadata, + self.path_modification_cache.clone(), + &self.src, + self.is_running_on_ci, + &self.exec_ctx, + &self.rust_info, + &self.host_target, + download_rustc, + debug_assertions_requested, + llvm_assertions, + ) } pub fn parse_download_ci_llvm( @@ -1966,10 +1796,12 @@ impl Config { /// Returns true if any of the `paths` have been modified locally. pub fn has_changes_from_upstream(&self, paths: &[&'static str]) -> bool { - match self.check_path_modifications(paths) { - PathFreshness::LastModifiedUpstream { .. } => false, - PathFreshness::HasLocalModifications { .. } | PathFreshness::MissingUpstream => true, - } + has_changes_from_upstream( + &self.stage0_metadata, + &self.src, + self.path_modification_cache.clone(), + paths, + ) } /// Checks whether any of the given paths have been modified w.r.t. upstream. @@ -2077,15 +1909,7 @@ impl Config { /// /// NOTE: this is not the same as `!is_rust_llvm` when `llvm_has_patches` is set. pub fn is_system_llvm(&self, target: TargetSelection) -> bool { - match self.target_config.get(&target) { - Some(Target { llvm_config: Some(_), .. }) => { - let ci_llvm = self.llvm_from_ci && self.is_host_target(target); - !ci_llvm - } - // We're building from the in-tree src/llvm-project sources. - Some(Target { llvm_config: None, .. }) => false, - None => false, - } + is_system_llvm(&self.host_target, self.llvm_from_ci, &self.target_config, target) } /// Returns `true` if this is our custom, patched, version of LLVM. From 8612db2f3e5e85713e9aacfc794b328810e651f8 Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Sat, 9 Aug 2025 09:52:13 +0530 Subject: [PATCH 13/24] extend download context and change functions to directly use that --- src/bootstrap/src/core/config/config.rs | 320 ++++++++---------------- src/bootstrap/src/core/download.rs | 37 ++- 2 files changed, 130 insertions(+), 227 deletions(-) diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index 2912b3f30c303..73a20df9b4e30 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -951,14 +951,9 @@ impl Config { ); } + let dwn_ctx = DownloadContext::from(&config); config.download_rustc_commit = download_ci_rustc_commit( - &config.stage0_metadata, - config.path_modification_cache.clone(), - &config.src, - config.is_running_on_ci, - &config.exec_ctx, - &config.rust_info, - &config.host_target, + dwn_ctx, rust_download_rustc, debug_assertions_requested, config.llvm_assertions, @@ -1168,15 +1163,9 @@ impl Config { "WARNING: `rust.download-rustc` is enabled. The `rust.channel` option will be overridden by the CI rustc's channel." ); - let channel = read_file_by_commit( - &config.exec_ctx, - &config.src, - &config.rust_info, - Path::new("src/ci/channel"), - commit, - ) - .trim() - .to_owned(); + let dwn_ctx = DownloadContext::from(&config); + let channel = + read_file_by_commit(dwn_ctx, Path::new("src/ci/channel"), commit).trim().to_owned(); config.channel = channel; } @@ -1211,19 +1200,9 @@ impl Config { 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 = parse_download_ci_llvm( - &config.exec_ctx, - &config.submodules, - &config.stage0_metadata, - &config.src, - config.path_modification_cache.clone(), - &config.host_target, - &config.download_rustc_commit, - &config.rust_info, - config.is_running_on_ci, - llvm_download_ci_llvm, - config.llvm_assertions, - ); + let dwn_ctx = DownloadContext::from(&config); + config.llvm_from_ci = + parse_download_ci_llvm(dwn_ctx, llvm_download_ci_llvm, config.llvm_assertions); if config.llvm_from_ci { let warn = |option: &str| { @@ -1285,8 +1264,8 @@ impl Config { if config.llvm_from_ci { let triple = &config.host_target.triple; - let ci_llvm_bin = - ci_llvm_root(config.llvm_from_ci, &config.out, &config.host_target).join("bin"); + let dwn_ctx = DownloadContext::from(&config); + let ci_llvm_bin = ci_llvm_root(dwn_ctx).join("bin"); let build_target = config .target_config .entry(config.host_target) @@ -1327,14 +1306,8 @@ impl Config { ); } - if config.lld_enabled - && is_system_llvm( - &config.host_target, - config.llvm_from_ci, - &config.target_config, - config.host_target, - ) - { + let dwn_ctx = DownloadContext::from(&config); + if config.lld_enabled && is_system_llvm(dwn_ctx, config.host_target) { panic!("Cannot enable LLD with `rust.lld = true` when using external llvm-config."); } @@ -1469,7 +1442,8 @@ impl Config { /// Returns the content of the given file at a specific commit. pub(crate) fn read_file_by_commit(&self, file: &Path, commit: &str) -> String { - read_file_by_commit(&self.exec_ctx, &self.src, &self.rust_info, file, commit) + let dwn_ctx = DownloadContext::from(self); + read_file_by_commit(dwn_ctx, file, commit) } /// Bootstrap embeds a version number into the name of shared libraries it uploads in CI. @@ -1540,7 +1514,8 @@ impl Config { /// The absolute path to the downloaded LLVM artifacts. pub(crate) fn ci_llvm_root(&self) -> PathBuf { - ci_llvm_root(self.llvm_from_ci, &self.out, &self.host_target) + let dwn_ctx = DownloadContext::from(self); + ci_llvm_root(dwn_ctx) } /// Directory where the extracted `rustc-dev` component is stored. @@ -1703,13 +1678,8 @@ impl Config { ), )] pub(crate) fn update_submodule(&self, relative_path: &str) { - update_submodule( - &self.submodules, - &self.exec_ctx, - &self.src, - &self.rust_info, - relative_path, - ); + let dwn_ctx = DownloadContext::from(self); + update_submodule(dwn_ctx, relative_path); } /// Returns the commit to download, or `None` if we shouldn't download CI artifacts. @@ -1719,14 +1689,9 @@ impl Config { debug_assertions_requested: bool, llvm_assertions: bool, ) -> Option { + let dwn_ctx = DownloadContext::from(self); download_ci_rustc_commit( - &self.stage0_metadata, - self.path_modification_cache.clone(), - &self.src, - self.is_running_on_ci, - &self.exec_ctx, - &self.rust_info, - &self.host_target, + dwn_ctx, download_rustc, debug_assertions_requested, llvm_assertions, @@ -1738,70 +1703,14 @@ impl Config { download_ci_llvm: Option, asserts: bool, ) -> bool { - // We don't ever want to use `true` on CI, as we should not - // download upstream artifacts if there are any local modifications. - let default = if self.is_running_on_ci { - StringOrBool::String("if-unchanged".to_string()) - } else { - StringOrBool::Bool(true) - }; - let download_ci_llvm = download_ci_llvm.unwrap_or(default); - - let if_unchanged = || { - if self.rust_info.is_from_tarball() { - // Git is needed for running "if-unchanged" logic. - println!("ERROR: 'if-unchanged' is only compatible with Git managed sources."); - crate::exit!(1); - } - - // Fetching the LLVM submodule is unnecessary for self-tests. - #[cfg(not(test))] - self.update_submodule("src/llvm-project"); - - // Check for untracked changes in `src/llvm-project` and other important places. - let has_changes = self.has_changes_from_upstream(LLVM_INVALIDATION_PATHS); - - // Return false if there are untracked changes, otherwise check if CI LLVM is available. - if has_changes { - false - } else { - llvm::is_ci_llvm_available_for_target(&self.host_target, asserts) - } - }; - - match download_ci_llvm { - StringOrBool::Bool(b) => { - if !b && self.download_rustc_commit.is_some() { - panic!( - "`llvm.download-ci-llvm` cannot be set to `false` if `rust.download-rustc` is set to `true` or `if-unchanged`." - ); - } - - if b && self.is_running_on_ci { - // On CI, we must always rebuild LLVM if there were any modifications to it - panic!( - "`llvm.download-ci-llvm` cannot be set to `true` on CI. Use `if-unchanged` instead." - ); - } - - // If download-ci-llvm=true we also want to check that CI llvm is available - b && llvm::is_ci_llvm_available_for_target(&self.host_target, asserts) - } - StringOrBool::String(s) if s == "if-unchanged" => if_unchanged(), - StringOrBool::String(other) => { - panic!("unrecognized option for download-ci-llvm: {other:?}") - } - } + let dwn_ctx = DownloadContext::from(self); + parse_download_ci_llvm(dwn_ctx, download_ci_llvm, asserts) } /// Returns true if any of the `paths` have been modified locally. pub fn has_changes_from_upstream(&self, paths: &[&'static str]) -> bool { - has_changes_from_upstream( - &self.stage0_metadata, - &self.src, - self.path_modification_cache.clone(), - paths, - ) + let dwn_ctx = DownloadContext::from(self); + has_changes_from_upstream(dwn_ctx, paths) } /// Checks whether any of the given paths have been modified w.r.t. upstream. @@ -1909,7 +1818,8 @@ impl Config { /// /// NOTE: this is not the same as `!is_rust_llvm` when `llvm_has_patches` is set. pub fn is_system_llvm(&self, target: TargetSelection) -> bool { - is_system_llvm(&self.host_target, self.llvm_from_ci, &self.target_config, target) + let dwn_ctx = DownloadContext::from(self); + is_system_llvm(dwn_ctx, target) } /// Returns `true` if this is our custom, patched, version of LLVM. @@ -2201,19 +2111,15 @@ pub fn check_stage0_version( } } -pub fn download_ci_rustc_commit( - stage0_metadata: &build_helper::stage0_parser::Stage0, - path_modification_cache: Arc, PathFreshness>>>, - src: &Path, - is_running_on_ci: bool, - exec_ctx: &ExecutionContext, - rust_info: &channel::GitInfo, - host_target: &TargetSelection, +pub fn download_ci_rustc_commit<'a>( + dwn_ctx: impl AsRef>, download_rustc: Option, debug_assertions_requested: bool, llvm_assertions: bool, ) -> Option { - if !is_download_ci_available(&host_target.triple, llvm_assertions) { + let dwn_ctx = dwn_ctx.as_ref(); + + if !is_download_ci_available(&dwn_ctx.host_target.triple, llvm_assertions) { return None; } @@ -2227,7 +2133,7 @@ pub fn download_ci_rustc_commit( None | Some(StringOrBool::Bool(false)) => return None, Some(StringOrBool::Bool(true)) => false, Some(StringOrBool::String(s)) if s == "if-unchanged" => { - if !rust_info.is_managed_git_subrepository() { + if !dwn_ctx.rust_info.is_managed_git_subrepository() { println!( "ERROR: `download-rustc=if-unchanged` is only compatible with Git managed sources." ); @@ -2241,16 +2147,11 @@ pub fn download_ci_rustc_commit( } }; - let commit = if rust_info.is_managed_git_subrepository() { + let commit = if dwn_ctx.rust_info.is_managed_git_subrepository() { // Look for a version to compare to based on the current commit. // Only commits merged by bors will have CI artifacts. - let freshness = check_path_modifications_( - stage0_metadata, - src, - path_modification_cache, - RUSTC_IF_UNCHANGED_ALLOWED_PATHS, - ); - exec_ctx.verbose(|| { + let freshness = check_path_modifications_(dwn_ctx, RUSTC_IF_UNCHANGED_ALLOWED_PATHS); + dwn_ctx.exec_ctx.verbose(|| { eprintln!("rustc freshness: {freshness:?}"); }); match freshness { @@ -2260,7 +2161,7 @@ pub fn download_ci_rustc_commit( return None; } - if is_running_on_ci { + if dwn_ctx.is_running_on_ci { eprintln!("CI rustc commit matches with HEAD and we are in CI."); eprintln!( "`rustc.download-ci` functionality will be skipped as artifacts are not available." @@ -2276,7 +2177,7 @@ pub fn download_ci_rustc_commit( } } } else { - channel::read_commit_info_file(src) + channel::read_commit_info_file(dwn_ctx.src) .map(|info| info.sha.trim().to_owned()) .expect("git-commit-info is missing in the project root") }; @@ -2292,24 +2193,29 @@ pub fn download_ci_rustc_commit( Some(commit) } -pub fn check_path_modifications_( - stage0_metadata: &build_helper::stage0_parser::Stage0, - src: &Path, - path_modification_cache: Arc, PathFreshness>>>, +pub fn check_path_modifications_<'a>( + dwn_ctx: impl AsRef>, paths: &[&'static str], ) -> PathFreshness { + let dwn_ctx = dwn_ctx.as_ref(); // Checking path modifications through git can be relatively expensive (>100ms). // We do not assume that the sources would change during bootstrap's execution, // so we can cache the results here. // Note that we do not use a static variable for the cache, because it would cause problems // in tests that create separate `Config` instsances. - path_modification_cache + dwn_ctx + .path_modification_cache .lock() .unwrap() .entry(paths.to_vec()) .or_insert_with(|| { - check_path_modifications(src, &git_config(stage0_metadata), paths, CiEnv::current()) - .unwrap() + check_path_modifications( + dwn_ctx.src, + &git_config(dwn_ctx.stage0_metadata), + paths, + CiEnv::current(), + ) + .unwrap() }) .clone() } @@ -2321,22 +2227,16 @@ pub fn git_config(stage0_metadata: &build_helper::stage0_parser::Stage0) -> GitC } } -pub fn parse_download_ci_llvm( - exec_ctx: &ExecutionContext, - submodules: &Option, - stage0_metadata: &build_helper::stage0_parser::Stage0, - src: &Path, - path_modification_cache: Arc, PathFreshness>>>, - host_target: &TargetSelection, - download_rustc_commit: &Option, - rust_info: &channel::GitInfo, - is_running_on_ci: bool, +pub fn parse_download_ci_llvm<'a>( + dwn_ctx: impl AsRef>, download_ci_llvm: Option, asserts: bool, ) -> bool { + let dwn_ctx = dwn_ctx.as_ref(); + // We don't ever want to use `true` on CI, as we should not // download upstream artifacts if there are any local modifications. - let default = if is_running_on_ci { + let default = if dwn_ctx.is_running_on_ci { StringOrBool::String("if-unchanged".to_string()) } else { StringOrBool::Bool(true) @@ -2344,7 +2244,7 @@ pub fn parse_download_ci_llvm( let download_ci_llvm = download_ci_llvm.unwrap_or(default); let if_unchanged = || { - if rust_info.is_from_tarball() { + if dwn_ctx.rust_info.is_from_tarball() { // Git is needed for running "if-unchanged" logic. println!("ERROR: 'if-unchanged' is only compatible with Git managed sources."); crate::exit!(1); @@ -2352,33 +2252,28 @@ pub fn parse_download_ci_llvm( // Fetching the LLVM submodule is unnecessary for self-tests. #[cfg(not(test))] - update_submodule(submodules, exec_ctx, src, rust_info, "src/llvm-project"); + update_submodule(dwn_ctx, "src/llvm-project"); // Check for untracked changes in `src/llvm-project` and other important places. - let has_changes = has_changes_from_upstream( - stage0_metadata, - src, - path_modification_cache, - LLVM_INVALIDATION_PATHS, - ); + let has_changes = has_changes_from_upstream(dwn_ctx, LLVM_INVALIDATION_PATHS); // Return false if there are untracked changes, otherwise check if CI LLVM is available. if has_changes { false } else { - llvm::is_ci_llvm_available_for_target(host_target, asserts) + llvm::is_ci_llvm_available_for_target(&dwn_ctx.host_target, asserts) } }; match download_ci_llvm { StringOrBool::Bool(b) => { - if !b && download_rustc_commit.is_some() { + if !b && dwn_ctx.download_rustc_commit.is_some() { panic!( "`llvm.download-ci-llvm` cannot be set to `false` if `rust.download-rustc` is set to `true` or `if-unchanged`." ); } - if b && is_running_on_ci { + if b && dwn_ctx.is_running_on_ci { // On CI, we must always rebuild LLVM if there were any modifications to it panic!( "`llvm.download-ci-llvm` cannot be set to `true` on CI. Use `if-unchanged` instead." @@ -2386,7 +2281,7 @@ pub fn parse_download_ci_llvm( } // If download-ci-llvm=true we also want to check that CI llvm is available - b && llvm::is_ci_llvm_available_for_target(host_target, asserts) + b && llvm::is_ci_llvm_available_for_target(&dwn_ctx.host_target, asserts) } StringOrBool::String(s) if s == "if-unchanged" => if_unchanged(), StringOrBool::String(other) => { @@ -2395,13 +2290,12 @@ pub fn parse_download_ci_llvm( } } -pub fn has_changes_from_upstream( - stage0_metadata: &build_helper::stage0_parser::Stage0, - src: &Path, - path_modification_cache: Arc, PathFreshness>>>, +pub fn has_changes_from_upstream<'a>( + dwn_ctx: impl AsRef>, paths: &[&'static str], ) -> bool { - match check_path_modifications_(stage0_metadata, src, path_modification_cache, paths) { + let dwn_ctx = dwn_ctx.as_ref(); + match check_path_modifications_(dwn_ctx, paths) { PathFreshness::LastModifiedUpstream { .. } => false, PathFreshness::HasLocalModifications { .. } | PathFreshness::MissingUpstream => true, } @@ -2416,18 +2310,13 @@ pub fn has_changes_from_upstream( fields(relative_path = ?relative_path), ), )] -pub(crate) fn update_submodule( - submodules: &Option, - exec_ctx: &ExecutionContext, - src: &Path, - rust_info: &channel::GitInfo, - relative_path: &str, -) { - if rust_info.is_from_tarball() || !submodules_(submodules, rust_info) { +pub(crate) fn update_submodule<'a>(dwn_ctx: impl AsRef>, relative_path: &str) { + let dwn_ctx = dwn_ctx.as_ref(); + if dwn_ctx.rust_info.is_from_tarball() || !submodules_(dwn_ctx.submodules, dwn_ctx.rust_info) { return; } - let absolute_path = src.join(relative_path); + let absolute_path = dwn_ctx.src.join(relative_path); // NOTE: This check is required because `jj git clone` doesn't create directories for // submodules, they are completely ignored. The code below assumes this directory exists, @@ -2438,7 +2327,7 @@ pub(crate) fn update_submodule( // NOTE: The check for the empty directory is here because when running x.py the first time, // the submodule won't be checked out. Check it out now so we can build it. - if !git_info(exec_ctx, false, &absolute_path).is_managed_git_subrepository() + if !git_info(dwn_ctx.exec_ctx, false, &absolute_path).is_managed_git_subrepository() && !helpers::dir_is_empty(&absolute_path) { return; @@ -2458,14 +2347,14 @@ pub(crate) fn update_submodule( // Determine commit checked out in submodule. let checked_out_hash = - submodule_git().args(["rev-parse", "HEAD"]).run_capture_stdout(exec_ctx).stdout(); + submodule_git().args(["rev-parse", "HEAD"]).run_capture_stdout(dwn_ctx.exec_ctx).stdout(); let checked_out_hash = checked_out_hash.trim_end(); // Determine commit that the submodule *should* have. - let recorded = helpers::git(Some(src)) + let recorded = helpers::git(Some(dwn_ctx.src)) .run_in_dry_run() .args(["ls-tree", "HEAD"]) .arg(relative_path) - .run_capture_stdout(exec_ctx) + .run_capture_stdout(dwn_ctx.exec_ctx) .stdout(); let actual_hash = recorded @@ -2480,24 +2369,24 @@ pub(crate) fn update_submodule( println!("Updating submodule {relative_path}"); - helpers::git(Some(src)) + helpers::git(Some(dwn_ctx.src)) .allow_failure() .run_in_dry_run() .args(["submodule", "-q", "sync"]) .arg(relative_path) - .run(exec_ctx); + .run(dwn_ctx.exec_ctx); // Try passing `--progress` to start, then run git again without if that fails. let update = |progress: bool| { // Git is buggy and will try to fetch submodules from the tracking branch for *this* repository, // even though that has no relation to the upstream for the submodule. - let current_branch = helpers::git(Some(src)) + let current_branch = helpers::git(Some(dwn_ctx.src)) .allow_failure() .run_in_dry_run() .args(["symbolic-ref", "--short", "HEAD"]) - .run_capture(exec_ctx); + .run_capture(dwn_ctx.exec_ctx); - let mut git = helpers::git(Some(&src)).allow_failure(); + let mut git = helpers::git(Some(dwn_ctx.src)).allow_failure(); git.run_in_dry_run(); if current_branch.is_success() { // If there is a tag named after the current branch, git will try to disambiguate by prepending `heads/` to the branch name. @@ -2514,23 +2403,25 @@ pub(crate) fn update_submodule( git.arg(relative_path); git }; - if !update(true).allow_failure().run(exec_ctx) { - update(false).allow_failure().run(exec_ctx); + if !update(true).allow_failure().run(dwn_ctx.exec_ctx) { + update(false).allow_failure().run(dwn_ctx.exec_ctx); } // Save any local changes, but avoid running `git stash pop` if there are none (since it will exit with an error). // diff-index reports the modifications through the exit status - let has_local_modifications = - !submodule_git().allow_failure().args(["diff-index", "--quiet", "HEAD"]).run(exec_ctx); + let has_local_modifications = !submodule_git() + .allow_failure() + .args(["diff-index", "--quiet", "HEAD"]) + .run(dwn_ctx.exec_ctx); if has_local_modifications { - submodule_git().allow_failure().args(["stash", "push"]).run(exec_ctx); + submodule_git().allow_failure().args(["stash", "push"]).run(dwn_ctx.exec_ctx); } - submodule_git().allow_failure().args(["reset", "-q", "--hard"]).run(exec_ctx); - submodule_git().allow_failure().args(["clean", "-qdfx"]).run(exec_ctx); + submodule_git().allow_failure().args(["reset", "-q", "--hard"]).run(dwn_ctx.exec_ctx); + submodule_git().allow_failure().args(["clean", "-qdfx"]).run(dwn_ctx.exec_ctx); if has_local_modifications { - submodule_git().allow_failure().args(["stash", "pop"]).run(exec_ctx); + submodule_git().allow_failure().args(["stash", "pop"]).run(dwn_ctx.exec_ctx); } } @@ -2548,15 +2439,14 @@ pub fn submodules_(submodules: &Option, rust_info: &channel::GitInfo) -> b /// In particular, we expect llvm sources to be available when this is false. /// /// NOTE: this is not the same as `!is_rust_llvm` when `llvm_has_patches` is set. -pub fn is_system_llvm( - host_target: &TargetSelection, - llvm_from_ci: bool, - target_config: &HashMap, +pub fn is_system_llvm<'a>( + dwn_ctx: impl AsRef>, target: TargetSelection, ) -> bool { - match target_config.get(&target) { + let dwn_ctx = dwn_ctx.as_ref(); + match dwn_ctx.target_config.get(&target) { Some(Target { llvm_config: Some(_), .. }) => { - let ci_llvm = llvm_from_ci && is_host_target(host_target, &target); + let ci_llvm = dwn_ctx.llvm_from_ci && is_host_target(&dwn_ctx.host_target, &target); !ci_llvm } // We're building from the in-tree src/llvm-project sources. @@ -2569,29 +2459,25 @@ pub fn is_host_target(host_target: &TargetSelection, target: &TargetSelection) - host_target == target } -pub(crate) fn ci_llvm_root( - llvm_from_ci: bool, - out: &Path, - host_target: &TargetSelection, -) -> PathBuf { - assert!(llvm_from_ci); - out.join(host_target).join("ci-llvm") +pub(crate) fn ci_llvm_root<'a>(dwn_ctx: impl AsRef>) -> PathBuf { + let dwn_ctx = dwn_ctx.as_ref(); + assert!(dwn_ctx.llvm_from_ci); + dwn_ctx.out.join(dwn_ctx.host_target).join("ci-llvm") } /// Returns the content of the given file at a specific commit. -pub(crate) fn read_file_by_commit( - exec_ctx: &ExecutionContext, - src: &Path, - rust_info: &channel::GitInfo, +pub(crate) fn read_file_by_commit<'a>( + dwn_ctx: impl AsRef>, file: &Path, commit: &str, ) -> String { + let dwn_ctx = dwn_ctx.as_ref(); assert!( - rust_info.is_managed_git_subrepository(), + dwn_ctx.rust_info.is_managed_git_subrepository(), "`Config::read_file_by_commit` is not supported in non-git sources." ); - let mut git = helpers::git(Some(src)); + let mut git = helpers::git(Some(dwn_ctx.src)); git.arg("show").arg(format!("{commit}:{}", file.to_str().unwrap())); - git.run_capture_stdout(exec_ctx).stdout() + git.run_capture_stdout(dwn_ctx.exec_ctx).stdout() } diff --git a/src/bootstrap/src/core/download.rs b/src/bootstrap/src/core/download.rs index 7ec6c62a07d02..5ded44cef1447 100644 --- a/src/bootstrap/src/core/download.rs +++ b/src/bootstrap/src/core/download.rs @@ -1,14 +1,17 @@ +use std::collections::HashMap; use std::env; use std::ffi::OsString; use std::fs::{self, File}; use std::io::{BufRead, BufReader, BufWriter, ErrorKind, Write}; use std::path::{Path, PathBuf}; -use std::sync::OnceLock; +use std::sync::{Arc, Mutex, OnceLock}; +use build_helper::git::PathFreshness; use xz2::bufread::XzDecoder; -use crate::core::config::{BUILDER_CONFIG_FILENAME, TargetSelection}; +use crate::core::config::{BUILDER_CONFIG_FILENAME, Target, TargetSelection}; use crate::utils::build_stamp::BuildStamp; +use crate::utils::channel; use crate::utils::exec::{ExecutionContext, command}; use crate::utils::helpers::{exe, hex_encode, move_file}; use crate::{Config, t}; @@ -398,14 +401,21 @@ impl Config { /// Only should be used for pre config initialization downloads. pub(crate) struct DownloadContext<'a> { - host_target: TargetSelection, - out: &'a Path, - patch_binaries_for_nix: Option, - exec_ctx: &'a ExecutionContext, - stage0_metadata: &'a build_helper::stage0_parser::Stage0, - llvm_assertions: bool, - bootstrap_cache_path: &'a Option, - is_running_on_ci: bool, + pub path_modification_cache: Arc, PathFreshness>>>, + pub src: &'a Path, + pub rust_info: &'a channel::GitInfo, + pub submodules: &'a Option, + pub download_rustc_commit: &'a Option, + pub host_target: TargetSelection, + pub llvm_from_ci: bool, + pub target_config: &'a HashMap, + pub out: &'a Path, + pub patch_binaries_for_nix: Option, + pub exec_ctx: &'a ExecutionContext, + pub stage0_metadata: &'a build_helper::stage0_parser::Stage0, + pub llvm_assertions: bool, + pub bootstrap_cache_path: &'a Option, + pub is_running_on_ci: bool, } impl<'a> AsRef> for DownloadContext<'a> { @@ -417,7 +427,14 @@ impl<'a> AsRef> for DownloadContext<'a> { impl<'a> From<&'a Config> for DownloadContext<'a> { fn from(value: &'a Config) -> Self { DownloadContext { + path_modification_cache: value.path_modification_cache.clone(), + src: &value.src, host_target: value.host_target, + rust_info: &value.rust_info, + download_rustc_commit: &value.download_rustc_commit, + submodules: &value.submodules, + llvm_from_ci: value.llvm_from_ci, + target_config: &value.target_config, out: &value.out, patch_binaries_for_nix: value.patch_binaries_for_nix, exec_ctx: &value.exec_ctx, From bfa6b56920d8eb736c271e2285b7bf4fb52bb90c Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Sat, 9 Aug 2025 18:12:05 +0530 Subject: [PATCH 14/24] add review comments --- src/bootstrap/src/core/config/config.rs | 55 +++++-------------------- 1 file changed, 11 insertions(+), 44 deletions(-) diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index 73a20df9b4e30..25b41a642d6d6 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -952,12 +952,17 @@ impl Config { } let dwn_ctx = DownloadContext::from(&config); - config.download_rustc_commit = download_ci_rustc_commit( - dwn_ctx, - rust_download_rustc, - debug_assertions_requested, - config.llvm_assertions, - ); + config.download_rustc_commit = + download_ci_rustc_commit(dwn_ctx, rust_download_rustc, config.llvm_assertions); + + if debug_assertions_requested { + eprintln!( + "WARN: `rust.debug-assertions = true` will prevent downloading CI rustc as alt CI \ + rustc is not currently built with debug assertions." + ); + // We need to put this later down_ci_rustc_commit. + config.download_rustc_commit = None; + } if let Some(t) = toml.target { for (triple, cfg) in t { @@ -1682,31 +1687,6 @@ impl Config { update_submodule(dwn_ctx, relative_path); } - /// Returns the commit to download, or `None` if we shouldn't download CI artifacts. - pub fn download_ci_rustc_commit( - &self, - download_rustc: Option, - debug_assertions_requested: bool, - llvm_assertions: bool, - ) -> Option { - let dwn_ctx = DownloadContext::from(self); - download_ci_rustc_commit( - dwn_ctx, - download_rustc, - debug_assertions_requested, - llvm_assertions, - ) - } - - pub fn parse_download_ci_llvm( - &self, - download_ci_llvm: Option, - asserts: bool, - ) -> bool { - let dwn_ctx = DownloadContext::from(self); - parse_download_ci_llvm(dwn_ctx, download_ci_llvm, asserts) - } - /// Returns true if any of the `paths` have been modified locally. pub fn has_changes_from_upstream(&self, paths: &[&'static str]) -> bool { let dwn_ctx = DownloadContext::from(self); @@ -1731,10 +1711,6 @@ impl Config { .clone() } - pub fn ci_env(&self) -> CiEnv { - if self.is_running_on_ci { CiEnv::GitHubActions } else { CiEnv::None } - } - pub fn sanitizers_enabled(&self, target: TargetSelection) -> bool { self.target_config.get(&target).and_then(|t| t.sanitizers).unwrap_or(self.sanitizers) } @@ -2114,7 +2090,6 @@ pub fn check_stage0_version( pub fn download_ci_rustc_commit<'a>( dwn_ctx: impl AsRef>, download_rustc: Option, - debug_assertions_requested: bool, llvm_assertions: bool, ) -> Option { let dwn_ctx = dwn_ctx.as_ref(); @@ -2182,14 +2157,6 @@ pub fn download_ci_rustc_commit<'a>( .expect("git-commit-info is missing in the project root") }; - if debug_assertions_requested { - eprintln!( - "WARN: `rust.debug-assertions = true` will prevent downloading CI rustc as alt CI \ - rustc is not currently built with debug assertions." - ); - return None; - } - Some(commit) } From f604dd117d9c015ed22568bfc8b472bd6defcfdc Mon Sep 17 00:00:00 2001 From: ltdk Date: Mon, 14 Jul 2025 19:59:25 -0400 Subject: [PATCH 15/24] Let forward_ref_* macros accept multiple attributes, and require attributes explicitly --- library/core/src/internal_macros.rs | 28 +++------- library/core/src/num/saturating.rs | 81 ++++++++++++++++++----------- library/core/src/num/wrapping.rs | 80 +++++++++++++++++----------- library/core/src/ops/arith.rs | 39 +++++++++----- library/core/src/ops/bit.rs | 33 ++++++++---- 5 files changed, 156 insertions(+), 105 deletions(-) diff --git a/library/core/src/internal_macros.rs b/library/core/src/internal_macros.rs index 2aaefba2468bb..dd1c863f2ea87 100644 --- a/library/core/src/internal_macros.rs +++ b/library/core/src/internal_macros.rs @@ -1,12 +1,8 @@ // implements the unary operator "op &T" // based on "op T" where T is expected to be `Copy`able macro_rules! forward_ref_unop { - (impl $imp:ident, $method:ident for $t:ty) => { - forward_ref_unop!(impl $imp, $method for $t, - #[stable(feature = "rust1", since = "1.0.0")]); - }; - (impl $imp:ident, $method:ident for $t:ty, #[$attr:meta]) => { - #[$attr] + (impl $imp:ident, $method:ident for $t:ty, $(#[$attr:meta])+) => { + $(#[$attr])+ impl $imp for &$t { type Output = <$t as $imp>::Output; @@ -21,12 +17,8 @@ macro_rules! forward_ref_unop { // implements binary operators "&T op U", "T op &U", "&T op &U" // based on "T op U" where T and U are expected to be `Copy`able macro_rules! forward_ref_binop { - (impl $imp:ident, $method:ident for $t:ty, $u:ty) => { - forward_ref_binop!(impl $imp, $method for $t, $u, - #[stable(feature = "rust1", since = "1.0.0")]); - }; - (impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => { - #[$attr] + (impl $imp:ident, $method:ident for $t:ty, $u:ty, $(#[$attr:meta])+) => { + $(#[$attr])+ impl<'a> $imp<$u> for &'a $t { type Output = <$t as $imp<$u>>::Output; @@ -37,7 +29,7 @@ macro_rules! forward_ref_binop { } } - #[$attr] + $(#[$attr])+ impl $imp<&$u> for $t { type Output = <$t as $imp<$u>>::Output; @@ -48,7 +40,7 @@ macro_rules! forward_ref_binop { } } - #[$attr] + $(#[$attr])+ impl $imp<&$u> for &$t { type Output = <$t as $imp<$u>>::Output; @@ -64,12 +56,8 @@ macro_rules! forward_ref_binop { // implements "T op= &U", based on "T op= U" // where U is expected to be `Copy`able macro_rules! forward_ref_op_assign { - (impl $imp:ident, $method:ident for $t:ty, $u:ty) => { - forward_ref_op_assign!(impl $imp, $method for $t, $u, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]); - }; - (impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => { - #[$attr] + (impl $imp:ident, $method:ident for $t:ty, $u:ty, $(#[$attr:meta])+) => { + $(#[$attr])+ impl $imp<&$u> for $t { #[inline] #[track_caller] diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 4460e430aecfa..25c2bcc03698c 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -109,7 +109,8 @@ impl fmt::UpperHex for Saturating { // // *self = *self << other; // // } // // } -// // forward_ref_op_assign! { impl ShlAssign, shl_assign for Saturating<$t>, $f } +// // forward_ref_op_assign! { impl ShlAssign, shl_assign for Saturating<$t>, $f, +// // #[unstable(feature = "saturating_int_impl", issue = "87920")] } // // #[unstable(feature = "saturating_int_impl", issue = "87920")] // impl Shr<$f> for Saturating<$t> { @@ -134,7 +135,8 @@ impl fmt::UpperHex for Saturating { // *self = *self >> other; // } // } -// forward_ref_op_assign! { impl ShrAssign, shr_assign for Saturating<$t>, $f } +// forward_ref_op_assign! { impl ShrAssign, shr_assign for Saturating<$t>, $f, +// #[unstable(feature = "saturating_int_impl", issue = "87920")] } // }; // } // @@ -159,7 +161,8 @@ impl fmt::UpperHex for Saturating { // *self = *self << other; // } // } -// forward_ref_op_assign! { impl ShlAssign, shl_assign for Saturating<$t>, $f } +// forward_ref_op_assign! { impl ShlAssign, shl_assign for Saturating<$t>, $f, +// #[unstable(feature = "saturating_int_impl", issue = "87920")] } // // #[unstable(feature = "saturating_int_impl", issue = "87920")] // impl Shr<$f> for Saturating<$t> { @@ -180,7 +183,8 @@ impl fmt::UpperHex for Saturating { // *self = *self >> other; // } // } -// forward_ref_op_assign! { impl ShrAssign, shr_assign for Saturating<$t>, $f } +// forward_ref_op_assign! { impl ShrAssign, shr_assign for Saturating<$t>, $f, +// #[unstable(feature = "saturating_int_impl", issue = "87920")] } // }; // } // @@ -218,7 +222,7 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Add, add for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl AddAssign for Saturating<$t> { @@ -227,7 +231,8 @@ macro_rules! saturating_impl { *self = *self + other; } } - forward_ref_op_assign! { impl AddAssign, add_assign for Saturating<$t>, Saturating<$t> } + forward_ref_op_assign! { impl AddAssign, add_assign for Saturating<$t>, Saturating<$t>, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] impl AddAssign<$t> for Saturating<$t> { @@ -236,7 +241,8 @@ macro_rules! saturating_impl { *self = *self + Saturating(other); } } - forward_ref_op_assign! { impl AddAssign, add_assign for Saturating<$t>, $t } + forward_ref_op_assign! { impl AddAssign, add_assign for Saturating<$t>, $t, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl Sub for Saturating<$t> { @@ -248,7 +254,7 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Sub, sub for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl SubAssign for Saturating<$t> { @@ -257,7 +263,8 @@ macro_rules! saturating_impl { *self = *self - other; } } - forward_ref_op_assign! { impl SubAssign, sub_assign for Saturating<$t>, Saturating<$t> } + forward_ref_op_assign! { impl SubAssign, sub_assign for Saturating<$t>, Saturating<$t>, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] impl SubAssign<$t> for Saturating<$t> { @@ -266,7 +273,8 @@ macro_rules! saturating_impl { *self = *self - Saturating(other); } } - forward_ref_op_assign! { impl SubAssign, sub_assign for Saturating<$t>, $t } + forward_ref_op_assign! { impl SubAssign, sub_assign for Saturating<$t>, $t, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl Mul for Saturating<$t> { @@ -278,7 +286,7 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Mul, mul for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl MulAssign for Saturating<$t> { @@ -287,7 +295,8 @@ macro_rules! saturating_impl { *self = *self * other; } } - forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, Saturating<$t> } + forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, Saturating<$t>, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] impl MulAssign<$t> for Saturating<$t> { @@ -296,7 +305,8 @@ macro_rules! saturating_impl { *self = *self * Saturating(other); } } - forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, $t } + forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, $t, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } /// # Examples /// @@ -323,8 +333,7 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Div, div for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } - + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl DivAssign for Saturating<$t> { @@ -333,7 +342,8 @@ macro_rules! saturating_impl { *self = *self / other; } } - forward_ref_op_assign! { impl DivAssign, div_assign for Saturating<$t>, Saturating<$t> } + forward_ref_op_assign! { impl DivAssign, div_assign for Saturating<$t>, Saturating<$t>, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] impl DivAssign<$t> for Saturating<$t> { @@ -342,7 +352,8 @@ macro_rules! saturating_impl { *self = *self / Saturating(other); } } - forward_ref_op_assign! { impl DivAssign, div_assign for Saturating<$t>, $t } + forward_ref_op_assign! { impl DivAssign, div_assign for Saturating<$t>, $t, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl Rem for Saturating<$t> { @@ -354,7 +365,7 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Rem, rem for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl RemAssign for Saturating<$t> { @@ -363,7 +374,8 @@ macro_rules! saturating_impl { *self = *self % other; } } - forward_ref_op_assign! { impl RemAssign, rem_assign for Saturating<$t>, Saturating<$t> } + forward_ref_op_assign! { impl RemAssign, rem_assign for Saturating<$t>, Saturating<$t>, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] impl RemAssign<$t> for Saturating<$t> { @@ -372,7 +384,8 @@ macro_rules! saturating_impl { *self = *self % Saturating(other); } } - forward_ref_op_assign! { impl RemAssign, rem_assign for Saturating<$t>, $t } + forward_ref_op_assign! { impl RemAssign, rem_assign for Saturating<$t>, $t, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl Not for Saturating<$t> { @@ -384,7 +397,7 @@ macro_rules! saturating_impl { } } forward_ref_unop! { impl Not, not for Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl BitXor for Saturating<$t> { @@ -396,7 +409,7 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl BitXor, bitxor for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl BitXorAssign for Saturating<$t> { @@ -405,7 +418,8 @@ macro_rules! saturating_impl { *self = *self ^ other; } } - forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Saturating<$t>, Saturating<$t> } + forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Saturating<$t>, Saturating<$t>, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] impl BitXorAssign<$t> for Saturating<$t> { @@ -414,7 +428,8 @@ macro_rules! saturating_impl { *self = *self ^ Saturating(other); } } - forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Saturating<$t>, $t } + forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Saturating<$t>, $t, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl BitOr for Saturating<$t> { @@ -426,7 +441,7 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl BitOr, bitor for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl BitOrAssign for Saturating<$t> { @@ -435,7 +450,8 @@ macro_rules! saturating_impl { *self = *self | other; } } - forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Saturating<$t>, Saturating<$t> } + forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Saturating<$t>, Saturating<$t>, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] impl BitOrAssign<$t> for Saturating<$t> { @@ -444,7 +460,8 @@ macro_rules! saturating_impl { *self = *self | Saturating(other); } } - forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Saturating<$t>, $t } + forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Saturating<$t>, $t, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl BitAnd for Saturating<$t> { @@ -456,7 +473,7 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl BitAnd, bitand for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] impl BitAndAssign for Saturating<$t> { @@ -465,7 +482,8 @@ macro_rules! saturating_impl { *self = *self & other; } } - forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Saturating<$t>, Saturating<$t> } + forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Saturating<$t>, Saturating<$t>, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] impl BitAndAssign<$t> for Saturating<$t> { @@ -474,7 +492,8 @@ macro_rules! saturating_impl { *self = *self & Saturating(other); } } - forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Saturating<$t>, $t } + forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Saturating<$t>, $t, + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } )*) } @@ -939,7 +958,7 @@ macro_rules! saturating_int_impl_signed { } } forward_ref_unop! { impl Neg, neg for Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] } )*) } diff --git a/library/core/src/num/wrapping.rs b/library/core/src/num/wrapping.rs index c460f38bd2e4d..d12c4a0506fcd 100644 --- a/library/core/src/num/wrapping.rs +++ b/library/core/src/num/wrapping.rs @@ -110,7 +110,8 @@ macro_rules! sh_impl_signed { *self = *self << other; } } - forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f } + forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "rust1", since = "1.0.0")] impl Shr<$f> for Wrapping<$t> { @@ -135,7 +136,8 @@ macro_rules! sh_impl_signed { *self = *self >> other; } } - forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f } + forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } }; } @@ -160,7 +162,8 @@ macro_rules! sh_impl_unsigned { *self = *self << other; } } - forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f } + forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "rust1", since = "1.0.0")] impl Shr<$f> for Wrapping<$t> { @@ -181,7 +184,8 @@ macro_rules! sh_impl_unsigned { *self = *self >> other; } } - forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f } + forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } }; } @@ -219,7 +223,7 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl Add, add for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] impl AddAssign for Wrapping<$t> { @@ -228,7 +232,8 @@ macro_rules! wrapping_impl { *self = *self + other; } } - forward_ref_op_assign! { impl AddAssign, add_assign for Wrapping<$t>, Wrapping<$t> } + forward_ref_op_assign! { impl AddAssign, add_assign for Wrapping<$t>, Wrapping<$t>, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] impl AddAssign<$t> for Wrapping<$t> { @@ -237,7 +242,8 @@ macro_rules! wrapping_impl { *self = *self + Wrapping(other); } } - forward_ref_op_assign! { impl AddAssign, add_assign for Wrapping<$t>, $t } + forward_ref_op_assign! { impl AddAssign, add_assign for Wrapping<$t>, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "rust1", since = "1.0.0")] impl Sub for Wrapping<$t> { @@ -249,7 +255,7 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl Sub, sub for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] impl SubAssign for Wrapping<$t> { @@ -258,7 +264,8 @@ macro_rules! wrapping_impl { *self = *self - other; } } - forward_ref_op_assign! { impl SubAssign, sub_assign for Wrapping<$t>, Wrapping<$t> } + forward_ref_op_assign! { impl SubAssign, sub_assign for Wrapping<$t>, Wrapping<$t>, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] impl SubAssign<$t> for Wrapping<$t> { @@ -267,7 +274,8 @@ macro_rules! wrapping_impl { *self = *self - Wrapping(other); } } - forward_ref_op_assign! { impl SubAssign, sub_assign for Wrapping<$t>, $t } + forward_ref_op_assign! { impl SubAssign, sub_assign for Wrapping<$t>, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "rust1", since = "1.0.0")] impl Mul for Wrapping<$t> { @@ -279,7 +287,7 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl Mul, mul for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] impl MulAssign for Wrapping<$t> { @@ -288,7 +296,8 @@ macro_rules! wrapping_impl { *self = *self * other; } } - forward_ref_op_assign! { impl MulAssign, mul_assign for Wrapping<$t>, Wrapping<$t> } + forward_ref_op_assign! { impl MulAssign, mul_assign for Wrapping<$t>, Wrapping<$t>, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] impl MulAssign<$t> for Wrapping<$t> { @@ -297,7 +306,8 @@ macro_rules! wrapping_impl { *self = *self * Wrapping(other); } } - forward_ref_op_assign! { impl MulAssign, mul_assign for Wrapping<$t>, $t } + forward_ref_op_assign! { impl MulAssign, mul_assign for Wrapping<$t>, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "wrapping_div", since = "1.3.0")] impl Div for Wrapping<$t> { @@ -309,7 +319,7 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl Div, div for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] impl DivAssign for Wrapping<$t> { @@ -318,7 +328,8 @@ macro_rules! wrapping_impl { *self = *self / other; } } - forward_ref_op_assign! { impl DivAssign, div_assign for Wrapping<$t>, Wrapping<$t> } + forward_ref_op_assign! { impl DivAssign, div_assign for Wrapping<$t>, Wrapping<$t>, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] impl DivAssign<$t> for Wrapping<$t> { @@ -327,7 +338,8 @@ macro_rules! wrapping_impl { *self = *self / Wrapping(other); } } - forward_ref_op_assign! { impl DivAssign, div_assign for Wrapping<$t>, $t } + forward_ref_op_assign! { impl DivAssign, div_assign for Wrapping<$t>, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "wrapping_impls", since = "1.7.0")] impl Rem for Wrapping<$t> { @@ -339,7 +351,7 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl Rem, rem for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] impl RemAssign for Wrapping<$t> { @@ -348,7 +360,8 @@ macro_rules! wrapping_impl { *self = *self % other; } } - forward_ref_op_assign! { impl RemAssign, rem_assign for Wrapping<$t>, Wrapping<$t> } + forward_ref_op_assign! { impl RemAssign, rem_assign for Wrapping<$t>, Wrapping<$t>, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] impl RemAssign<$t> for Wrapping<$t> { @@ -357,7 +370,8 @@ macro_rules! wrapping_impl { *self = *self % Wrapping(other); } } - forward_ref_op_assign! { impl RemAssign, rem_assign for Wrapping<$t>, $t } + forward_ref_op_assign! { impl RemAssign, rem_assign for Wrapping<$t>, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "rust1", since = "1.0.0")] impl Not for Wrapping<$t> { @@ -369,7 +383,7 @@ macro_rules! wrapping_impl { } } forward_ref_unop! { impl Not, not for Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] } #[stable(feature = "rust1", since = "1.0.0")] impl BitXor for Wrapping<$t> { @@ -381,7 +395,7 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl BitXor, bitxor for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] impl BitXorAssign for Wrapping<$t> { @@ -390,7 +404,8 @@ macro_rules! wrapping_impl { *self = *self ^ other; } } - forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Wrapping<$t>, Wrapping<$t> } + forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Wrapping<$t>, Wrapping<$t>, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] impl BitXorAssign<$t> for Wrapping<$t> { @@ -399,7 +414,8 @@ macro_rules! wrapping_impl { *self = *self ^ Wrapping(other); } } - forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Wrapping<$t>, $t } + forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Wrapping<$t>, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "rust1", since = "1.0.0")] impl BitOr for Wrapping<$t> { @@ -411,7 +427,7 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl BitOr, bitor for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] impl BitOrAssign for Wrapping<$t> { @@ -420,7 +436,8 @@ macro_rules! wrapping_impl { *self = *self | other; } } - forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Wrapping<$t>, Wrapping<$t> } + forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Wrapping<$t>, Wrapping<$t>, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] impl BitOrAssign<$t> for Wrapping<$t> { @@ -429,7 +446,8 @@ macro_rules! wrapping_impl { *self = *self | Wrapping(other); } } - forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Wrapping<$t>, $t } + forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Wrapping<$t>, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "rust1", since = "1.0.0")] impl BitAnd for Wrapping<$t> { @@ -441,7 +459,7 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl BitAnd, bitand for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] impl BitAndAssign for Wrapping<$t> { @@ -450,7 +468,8 @@ macro_rules! wrapping_impl { *self = *self & other; } } - forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Wrapping<$t>, Wrapping<$t> } + forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Wrapping<$t>, Wrapping<$t>, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] impl BitAndAssign<$t> for Wrapping<$t> { @@ -459,7 +478,8 @@ macro_rules! wrapping_impl { *self = *self & Wrapping(other); } } - forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Wrapping<$t>, $t } + forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Wrapping<$t>, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } #[stable(feature = "wrapping_neg", since = "1.10.0")] impl Neg for Wrapping<$t> { @@ -470,7 +490,7 @@ macro_rules! wrapping_impl { } } forward_ref_unop! { impl Neg, neg for Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] } )*) } diff --git a/library/core/src/ops/arith.rs b/library/core/src/ops/arith.rs index 7d44b1733b9c6..d3c65d503de6d 100644 --- a/library/core/src/ops/arith.rs +++ b/library/core/src/ops/arith.rs @@ -106,7 +106,8 @@ macro_rules! add_impl { fn add(self, other: $t) -> $t { self + other } } - forward_ref_binop! { impl Add, add for $t, $t } + forward_ref_binop! { impl Add, add for $t, $t, + #[stable(feature = "rust1", since = "1.0.0")] } )*) } @@ -218,7 +219,8 @@ macro_rules! sub_impl { fn sub(self, other: $t) -> $t { self - other } } - forward_ref_binop! { impl Sub, sub for $t, $t } + forward_ref_binop! { impl Sub, sub for $t, $t, + #[stable(feature = "rust1", since = "1.0.0")] } )*) } @@ -351,7 +353,8 @@ macro_rules! mul_impl { fn mul(self, other: $t) -> $t { self * other } } - forward_ref_binop! { impl Mul, mul for $t, $t } + forward_ref_binop! { impl Mul, mul for $t, $t, + #[stable(feature = "rust1", since = "1.0.0")] } )*) } @@ -493,7 +496,8 @@ macro_rules! div_impl_integer { fn div(self, other: $t) -> $t { self / other } } - forward_ref_binop! { impl Div, div for $t, $t } + forward_ref_binop! { impl Div, div for $t, $t, + #[stable(feature = "rust1", since = "1.0.0")] } )*)*) } @@ -513,7 +517,8 @@ macro_rules! div_impl_float { fn div(self, other: $t) -> $t { self / other } } - forward_ref_binop! { impl Div, div for $t, $t } + forward_ref_binop! { impl Div, div for $t, $t, + #[stable(feature = "rust1", since = "1.0.0")] } )*) } @@ -599,7 +604,8 @@ macro_rules! rem_impl_integer { fn rem(self, other: $t) -> $t { self % other } } - forward_ref_binop! { impl Rem, rem for $t, $t } + forward_ref_binop! { impl Rem, rem for $t, $t, + #[stable(feature = "rust1", since = "1.0.0")] } )*)*) } @@ -634,7 +640,8 @@ macro_rules! rem_impl_float { fn rem(self, other: $t) -> $t { self % other } } - forward_ref_binop! { impl Rem, rem for $t, $t } + forward_ref_binop! { impl Rem, rem for $t, $t, + #[stable(feature = "rust1", since = "1.0.0")] } )*) } @@ -709,7 +716,8 @@ macro_rules! neg_impl { fn neg(self) -> $t { -self } } - forward_ref_unop! { impl Neg, neg for $t } + forward_ref_unop! { impl Neg, neg for $t, + #[stable(feature = "rust1", since = "1.0.0")] } )*) } @@ -776,7 +784,8 @@ macro_rules! add_assign_impl { fn add_assign(&mut self, other: $t) { *self += other } } - forward_ref_op_assign! { impl AddAssign, add_assign for $t, $t } + forward_ref_op_assign! { impl AddAssign, add_assign for $t, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } )+) } @@ -843,7 +852,8 @@ macro_rules! sub_assign_impl { fn sub_assign(&mut self, other: $t) { *self -= other } } - forward_ref_op_assign! { impl SubAssign, sub_assign for $t, $t } + forward_ref_op_assign! { impl SubAssign, sub_assign for $t, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } )+) } @@ -901,7 +911,8 @@ macro_rules! mul_assign_impl { fn mul_assign(&mut self, other: $t) { *self *= other } } - forward_ref_op_assign! { impl MulAssign, mul_assign for $t, $t } + forward_ref_op_assign! { impl MulAssign, mul_assign for $t, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } )+) } @@ -958,7 +969,8 @@ macro_rules! div_assign_impl { fn div_assign(&mut self, other: $t) { *self /= other } } - forward_ref_op_assign! { impl DivAssign, div_assign for $t, $t } + forward_ref_op_assign! { impl DivAssign, div_assign for $t, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } )+) } @@ -1019,7 +1031,8 @@ macro_rules! rem_assign_impl { fn rem_assign(&mut self, other: $t) { *self %= other } } - forward_ref_op_assign! { impl RemAssign, rem_assign for $t, $t } + forward_ref_op_assign! { impl RemAssign, rem_assign for $t, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } )+) } diff --git a/library/core/src/ops/bit.rs b/library/core/src/ops/bit.rs index deb54c8ba348e..1a44243e0dc40 100644 --- a/library/core/src/ops/bit.rs +++ b/library/core/src/ops/bit.rs @@ -61,7 +61,8 @@ macro_rules! not_impl { fn not(self) -> $t { !self } } - forward_ref_unop! { impl Not, not for $t } + forward_ref_unop! { impl Not, not for $t, + #[stable(feature = "rust1", since = "1.0.0")] } )*) } @@ -171,7 +172,8 @@ macro_rules! bitand_impl { fn bitand(self, rhs: $t) -> $t { self & rhs } } - forward_ref_binop! { impl BitAnd, bitand for $t, $t } + forward_ref_binop! { impl BitAnd, bitand for $t, $t, + #[stable(feature = "rust1", since = "1.0.0")] } )*) } @@ -271,7 +273,8 @@ macro_rules! bitor_impl { fn bitor(self, rhs: $t) -> $t { self | rhs } } - forward_ref_binop! { impl BitOr, bitor for $t, $t } + forward_ref_binop! { impl BitOr, bitor for $t, $t, + #[stable(feature = "rust1", since = "1.0.0")] } )*) } @@ -371,7 +374,8 @@ macro_rules! bitxor_impl { fn bitxor(self, other: $t) -> $t { self ^ other } } - forward_ref_binop! { impl BitXor, bitxor for $t, $t } + forward_ref_binop! { impl BitXor, bitxor for $t, $t, + #[stable(feature = "rust1", since = "1.0.0")] } )*) } @@ -471,7 +475,8 @@ macro_rules! shl_impl { } } - forward_ref_binop! { impl Shl, shl for $t, $f } + forward_ref_binop! { impl Shl, shl for $t, $f, + #[stable(feature = "rust1", since = "1.0.0")] } }; } @@ -589,7 +594,8 @@ macro_rules! shr_impl { } } - forward_ref_binop! { impl Shr, shr for $t, $f } + forward_ref_binop! { impl Shr, shr for $t, $f, + #[stable(feature = "rust1", since = "1.0.0")] } }; } @@ -719,7 +725,8 @@ macro_rules! bitand_assign_impl { fn bitand_assign(&mut self, other: $t) { *self &= other } } - forward_ref_op_assign! { impl BitAndAssign, bitand_assign for $t, $t } + forward_ref_op_assign! { impl BitAndAssign, bitand_assign for $t, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } )+) } @@ -790,7 +797,8 @@ macro_rules! bitor_assign_impl { fn bitor_assign(&mut self, other: $t) { *self |= other } } - forward_ref_op_assign! { impl BitOrAssign, bitor_assign for $t, $t } + forward_ref_op_assign! { impl BitOrAssign, bitor_assign for $t, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } )+) } @@ -861,7 +869,8 @@ macro_rules! bitxor_assign_impl { fn bitxor_assign(&mut self, other: $t) { *self ^= other } } - forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for $t, $t } + forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for $t, $t, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } )+) } @@ -925,7 +934,8 @@ macro_rules! shl_assign_impl { } } - forward_ref_op_assign! { impl ShlAssign, shl_assign for $t, $f } + forward_ref_op_assign! { impl ShlAssign, shl_assign for $t, $f, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } }; } @@ -1007,7 +1017,8 @@ macro_rules! shr_assign_impl { } } - forward_ref_op_assign! { impl ShrAssign, shr_assign for $t, $f } + forward_ref_op_assign! { impl ShrAssign, shr_assign for $t, $f, + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } }; } From bb32e31e6528f7121f97ba48fc25ff2e016d6045 Mon Sep 17 00:00:00 2001 From: ltdk Date: Mon, 14 Jul 2025 20:02:06 -0400 Subject: [PATCH 16/24] Constify remaining operators --- library/core/src/internal_macros.rs | 10 +- library/core/src/net/ip_addr.rs | 47 ++-- library/core/src/num/nonzero.rs | 43 ++-- library/core/src/num/saturating.rs | 156 +++++++++----- library/core/src/num/wrapping.rs | 204 ++++++++++++------ library/core/src/ops/arith.rs | 69 ++++-- library/core/src/ops/bit.rs | 91 ++++++-- library/core/src/time.rs | 27 ++- .../src/operators/assign_op_pattern.rs | 9 +- 9 files changed, 441 insertions(+), 215 deletions(-) diff --git a/library/core/src/internal_macros.rs b/library/core/src/internal_macros.rs index dd1c863f2ea87..f90818c7969c9 100644 --- a/library/core/src/internal_macros.rs +++ b/library/core/src/internal_macros.rs @@ -3,7 +3,7 @@ macro_rules! forward_ref_unop { (impl $imp:ident, $method:ident for $t:ty, $(#[$attr:meta])+) => { $(#[$attr])+ - impl $imp for &$t { + impl const $imp for &$t { type Output = <$t as $imp>::Output; #[inline] @@ -19,7 +19,7 @@ macro_rules! forward_ref_unop { macro_rules! forward_ref_binop { (impl $imp:ident, $method:ident for $t:ty, $u:ty, $(#[$attr:meta])+) => { $(#[$attr])+ - impl<'a> $imp<$u> for &'a $t { + impl const $imp<$u> for &$t { type Output = <$t as $imp<$u>>::Output; #[inline] @@ -30,7 +30,7 @@ macro_rules! forward_ref_binop { } $(#[$attr])+ - impl $imp<&$u> for $t { + impl const $imp<&$u> for $t { type Output = <$t as $imp<$u>>::Output; #[inline] @@ -41,7 +41,7 @@ macro_rules! forward_ref_binop { } $(#[$attr])+ - impl $imp<&$u> for &$t { + impl const $imp<&$u> for &$t { type Output = <$t as $imp<$u>>::Output; #[inline] @@ -58,7 +58,7 @@ macro_rules! forward_ref_binop { macro_rules! forward_ref_op_assign { (impl $imp:ident, $method:ident for $t:ty, $u:ty, $(#[$attr:meta])+) => { $(#[$attr])+ - impl $imp<&$u> for $t { + impl const $imp<&$u> for $t { #[inline] #[track_caller] fn $method(&mut self, other: &$u) { diff --git a/library/core/src/net/ip_addr.rs b/library/core/src/net/ip_addr.rs index 6adeb2aa3fdad..87f2110034c5a 100644 --- a/library/core/src/net/ip_addr.rs +++ b/library/core/src/net/ip_addr.rs @@ -2,7 +2,6 @@ use super::display_buffer::DisplayBuffer; use crate::cmp::Ordering; use crate::fmt::{self, Write}; use crate::hash::{Hash, Hasher}; -use crate::iter; use crate::mem::transmute; use crate::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not}; @@ -2348,20 +2347,24 @@ impl const From<[u16; 8]> for IpAddr { } #[stable(feature = "ip_bitops", since = "1.75.0")] -impl Not for Ipv4Addr { +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const Not for Ipv4Addr { type Output = Ipv4Addr; #[inline] fn not(mut self) -> Ipv4Addr { - for octet in &mut self.octets { - *octet = !*octet; + let mut idx = 0; + while idx < 4 { + self.octets[idx] = !self.octets[idx]; + idx += 1; } self } } #[stable(feature = "ip_bitops", since = "1.75.0")] -impl Not for &'_ Ipv4Addr { +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const Not for &'_ Ipv4Addr { type Output = Ipv4Addr; #[inline] @@ -2371,20 +2374,24 @@ impl Not for &'_ Ipv4Addr { } #[stable(feature = "ip_bitops", since = "1.75.0")] -impl Not for Ipv6Addr { +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const Not for Ipv6Addr { type Output = Ipv6Addr; #[inline] fn not(mut self) -> Ipv6Addr { - for octet in &mut self.octets { - *octet = !*octet; + let mut idx = 0; + while idx < 16 { + self.octets[idx] = !self.octets[idx]; + idx += 1; } self } } #[stable(feature = "ip_bitops", since = "1.75.0")] -impl Not for &'_ Ipv6Addr { +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const Not for &'_ Ipv6Addr { type Output = Ipv6Addr; #[inline] @@ -2400,23 +2407,25 @@ macro_rules! bitop_impls { )*) => { $( $(#[$attr])* - impl $BitOpAssign for $ty { + impl const $BitOpAssign for $ty { fn $bitop_assign(&mut self, rhs: $ty) { - for (lhs, rhs) in iter::zip(&mut self.octets, rhs.octets) { - lhs.$bitop_assign(rhs); + let mut idx = 0; + while idx < self.octets.len() { + self.octets[idx].$bitop_assign(rhs.octets[idx]); + idx += 1; } } } $(#[$attr])* - impl $BitOpAssign<&'_ $ty> for $ty { + impl const $BitOpAssign<&'_ $ty> for $ty { fn $bitop_assign(&mut self, rhs: &'_ $ty) { self.$bitop_assign(*rhs); } } $(#[$attr])* - impl $BitOp for $ty { + impl const $BitOp for $ty { type Output = $ty; #[inline] @@ -2427,7 +2436,7 @@ macro_rules! bitop_impls { } $(#[$attr])* - impl $BitOp<&'_ $ty> for $ty { + impl const $BitOp<&'_ $ty> for $ty { type Output = $ty; #[inline] @@ -2438,7 +2447,7 @@ macro_rules! bitop_impls { } $(#[$attr])* - impl $BitOp<$ty> for &'_ $ty { + impl const $BitOp<$ty> for &'_ $ty { type Output = $ty; #[inline] @@ -2450,7 +2459,7 @@ macro_rules! bitop_impls { } $(#[$attr])* - impl $BitOp<&'_ $ty> for &'_ $ty { + impl const $BitOp<&'_ $ty> for &'_ $ty { type Output = $ty; #[inline] @@ -2466,12 +2475,16 @@ macro_rules! bitop_impls { bitop_impls! { #[stable(feature = "ip_bitops", since = "1.75.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] impl (BitAnd, BitAndAssign) for Ipv4Addr = (bitand, bitand_assign); #[stable(feature = "ip_bitops", since = "1.75.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] impl (BitOr, BitOrAssign) for Ipv4Addr = (bitor, bitor_assign); #[stable(feature = "ip_bitops", since = "1.75.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] impl (BitAnd, BitAndAssign) for Ipv6Addr = (bitand, bitand_assign); #[stable(feature = "ip_bitops", since = "1.75.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] impl (BitOr, BitOrAssign) for Ipv6Addr = (bitor, bitor_assign); } diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 08a66361e6f4d..308d722f5d564 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -310,9 +310,10 @@ where } #[stable(feature = "nonzero_bitor", since = "1.45.0")] -impl BitOr for NonZero +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const BitOr for NonZero where - T: ZeroablePrimitive + BitOr, + T: ZeroablePrimitive + [const] BitOr, { type Output = Self; @@ -324,9 +325,10 @@ where } #[stable(feature = "nonzero_bitor", since = "1.45.0")] -impl BitOr for NonZero +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const BitOr for NonZero where - T: ZeroablePrimitive + BitOr, + T: ZeroablePrimitive + [const] BitOr, { type Output = Self; @@ -338,9 +340,10 @@ where } #[stable(feature = "nonzero_bitor", since = "1.45.0")] -impl BitOr> for T +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const BitOr> for T where - T: ZeroablePrimitive + BitOr, + T: ZeroablePrimitive + [const] BitOr, { type Output = NonZero; @@ -352,10 +355,11 @@ where } #[stable(feature = "nonzero_bitor", since = "1.45.0")] -impl BitOrAssign for NonZero +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const BitOrAssign for NonZero where T: ZeroablePrimitive, - Self: BitOr, + Self: [const] BitOr, { #[inline] fn bitor_assign(&mut self, rhs: Self) { @@ -364,10 +368,11 @@ where } #[stable(feature = "nonzero_bitor", since = "1.45.0")] -impl BitOrAssign for NonZero +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const BitOrAssign for NonZero where T: ZeroablePrimitive, - Self: BitOr, + Self: [const] BitOr, { #[inline] fn bitor_assign(&mut self, rhs: T) { @@ -1239,7 +1244,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls { // Impls for unsigned nonzero types only. (unsigned $Int:ty) => { #[stable(feature = "nonzero_div", since = "1.51.0")] - impl Div> for $Int { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Div> for $Int { type Output = $Int; /// Same as `self / other.get()`, but because `other` is a `NonZero<_>`, @@ -1257,7 +1263,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls { } #[stable(feature = "nonzero_div_assign", since = "1.79.0")] - impl DivAssign> for $Int { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const DivAssign> for $Int { /// Same as `self /= other.get()`, but because `other` is a `NonZero<_>`, /// there's never a runtime check for division-by-zero. /// @@ -1270,7 +1277,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls { } #[stable(feature = "nonzero_div", since = "1.51.0")] - impl Rem> for $Int { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Rem> for $Int { type Output = $Int; /// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic. @@ -1283,7 +1291,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls { } #[stable(feature = "nonzero_div_assign", since = "1.79.0")] - impl RemAssign> for $Int { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const RemAssign> for $Int { /// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic. #[inline] fn rem_assign(&mut self, other: NonZero<$Int>) { @@ -1323,7 +1332,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls { // Impls for signed nonzero types only. (signed $Int:ty) => { #[stable(feature = "signed_nonzero_neg", since = "1.71.0")] - impl Neg for NonZero<$Int> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Neg for NonZero<$Int> { type Output = Self; #[inline] @@ -1334,7 +1344,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls { } forward_ref_unop! { impl Neg, neg for NonZero<$Int>, - #[stable(feature = "signed_nonzero_neg", since = "1.71.0")] } + #[stable(feature = "signed_nonzero_neg", since = "1.71.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } }; } diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 25c2bcc03698c..c7040721b9353 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -213,7 +213,8 @@ impl fmt::UpperHex for Saturating { macro_rules! saturating_impl { ($($t:ty)*) => ($( #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl Add for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Add for Saturating<$t> { type Output = Saturating<$t>; #[inline] @@ -222,30 +223,36 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Add, add for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl AddAssign for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const AddAssign for Saturating<$t> { #[inline] fn add_assign(&mut self, other: Saturating<$t>) { *self = *self + other; } } forward_ref_op_assign! { impl AddAssign, add_assign for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] - impl AddAssign<$t> for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const AddAssign<$t> for Saturating<$t> { #[inline] fn add_assign(&mut self, other: $t) { *self = *self + Saturating(other); } } forward_ref_op_assign! { impl AddAssign, add_assign for Saturating<$t>, $t, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl Sub for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Sub for Saturating<$t> { type Output = Saturating<$t>; #[inline] @@ -254,30 +261,36 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Sub, sub for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl SubAssign for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const SubAssign for Saturating<$t> { #[inline] fn sub_assign(&mut self, other: Saturating<$t>) { *self = *self - other; } } forward_ref_op_assign! { impl SubAssign, sub_assign for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] - impl SubAssign<$t> for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const SubAssign<$t> for Saturating<$t> { #[inline] fn sub_assign(&mut self, other: $t) { *self = *self - Saturating(other); } } forward_ref_op_assign! { impl SubAssign, sub_assign for Saturating<$t>, $t, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl Mul for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Mul for Saturating<$t> { type Output = Saturating<$t>; #[inline] @@ -286,27 +299,32 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Mul, mul for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl MulAssign for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const MulAssign for Saturating<$t> { #[inline] fn mul_assign(&mut self, other: Saturating<$t>) { *self = *self * other; } } forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] - impl MulAssign<$t> for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const MulAssign<$t> for Saturating<$t> { #[inline] fn mul_assign(&mut self, other: $t) { *self = *self * Saturating(other); } } forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, $t, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } /// # Examples /// @@ -324,7 +342,8 @@ macro_rules! saturating_impl { #[doc = concat!("let _ = Saturating(0", stringify!($t), ") / Saturating(0);")] /// ``` #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl Div for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Div for Saturating<$t> { type Output = Saturating<$t>; #[inline] @@ -333,30 +352,36 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Div, div for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl DivAssign for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const DivAssign for Saturating<$t> { #[inline] fn div_assign(&mut self, other: Saturating<$t>) { *self = *self / other; } } forward_ref_op_assign! { impl DivAssign, div_assign for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] - impl DivAssign<$t> for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const DivAssign<$t> for Saturating<$t> { #[inline] fn div_assign(&mut self, other: $t) { *self = *self / Saturating(other); } } forward_ref_op_assign! { impl DivAssign, div_assign for Saturating<$t>, $t, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl Rem for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Rem for Saturating<$t> { type Output = Saturating<$t>; #[inline] @@ -365,30 +390,36 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Rem, rem for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl RemAssign for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const RemAssign for Saturating<$t> { #[inline] fn rem_assign(&mut self, other: Saturating<$t>) { *self = *self % other; } } forward_ref_op_assign! { impl RemAssign, rem_assign for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] - impl RemAssign<$t> for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const RemAssign<$t> for Saturating<$t> { #[inline] fn rem_assign(&mut self, other: $t) { *self = *self % Saturating(other); } } forward_ref_op_assign! { impl RemAssign, rem_assign for Saturating<$t>, $t, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl Not for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Not for Saturating<$t> { type Output = Saturating<$t>; #[inline] @@ -397,10 +428,12 @@ macro_rules! saturating_impl { } } forward_ref_unop! { impl Not, not for Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl BitXor for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitXor for Saturating<$t> { type Output = Saturating<$t>; #[inline] @@ -409,30 +442,36 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl BitXor, bitxor for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl BitXorAssign for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitXorAssign for Saturating<$t> { #[inline] fn bitxor_assign(&mut self, other: Saturating<$t>) { *self = *self ^ other; } } forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] - impl BitXorAssign<$t> for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitXorAssign<$t> for Saturating<$t> { #[inline] fn bitxor_assign(&mut self, other: $t) { *self = *self ^ Saturating(other); } } forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Saturating<$t>, $t, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl BitOr for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitOr for Saturating<$t> { type Output = Saturating<$t>; #[inline] @@ -441,30 +480,36 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl BitOr, bitor for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl BitOrAssign for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitOrAssign for Saturating<$t> { #[inline] fn bitor_assign(&mut self, other: Saturating<$t>) { *self = *self | other; } } forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] - impl BitOrAssign<$t> for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitOrAssign<$t> for Saturating<$t> { #[inline] fn bitor_assign(&mut self, other: $t) { *self = *self | Saturating(other); } } forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Saturating<$t>, $t, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl BitAnd for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitAnd for Saturating<$t> { type Output = Saturating<$t>; #[inline] @@ -473,27 +518,32 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl BitAnd, bitand for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl BitAndAssign for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitAndAssign for Saturating<$t> { #[inline] fn bitand_assign(&mut self, other: Saturating<$t>) { *self = *self & other; } } forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "saturating_int_assign_impl", since = "1.74.0")] - impl BitAndAssign<$t> for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitAndAssign<$t> for Saturating<$t> { #[inline] fn bitand_assign(&mut self, other: $t) { *self = *self & Saturating(other); } } forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Saturating<$t>, $t, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*) } @@ -950,7 +1000,8 @@ macro_rules! saturating_int_impl_signed { } #[stable(feature = "saturating_int_impl", since = "1.74.0")] - impl Neg for Saturating<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Neg for Saturating<$t> { type Output = Self; #[inline] fn neg(self) -> Self { @@ -958,7 +1009,8 @@ macro_rules! saturating_int_impl_signed { } } forward_ref_unop! { impl Neg, neg for Saturating<$t>, - #[stable(feature = "saturating_int_impl", since = "1.74.0")] } + #[stable(feature = "saturating_int_impl", since = "1.74.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*) } diff --git a/library/core/src/num/wrapping.rs b/library/core/src/num/wrapping.rs index d12c4a0506fcd..9ccad4b6459cd 100644 --- a/library/core/src/num/wrapping.rs +++ b/library/core/src/num/wrapping.rs @@ -88,7 +88,8 @@ impl fmt::UpperHex for Wrapping { macro_rules! sh_impl_signed { ($t:ident, $f:ident) => { #[stable(feature = "rust1", since = "1.0.0")] - impl Shl<$f> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Shl<$f> for Wrapping<$t> { type Output = Wrapping<$t>; #[inline] @@ -101,20 +102,24 @@ macro_rules! sh_impl_signed { } } forward_ref_binop! { impl Shl, shl for Wrapping<$t>, $f, - #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] } + #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl ShlAssign<$f> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const ShlAssign<$f> for Wrapping<$t> { #[inline] fn shl_assign(&mut self, other: $f) { *self = *self << other; } } forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "rust1", since = "1.0.0")] - impl Shr<$f> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Shr<$f> for Wrapping<$t> { type Output = Wrapping<$t>; #[inline] @@ -127,24 +132,28 @@ macro_rules! sh_impl_signed { } } forward_ref_binop! { impl Shr, shr for Wrapping<$t>, $f, - #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] } + #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl ShrAssign<$f> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const ShrAssign<$f> for Wrapping<$t> { #[inline] fn shr_assign(&mut self, other: $f) { *self = *self >> other; } } forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } }; } macro_rules! sh_impl_unsigned { ($t:ident, $f:ident) => { #[stable(feature = "rust1", since = "1.0.0")] - impl Shl<$f> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Shl<$f> for Wrapping<$t> { type Output = Wrapping<$t>; #[inline] @@ -153,20 +162,24 @@ macro_rules! sh_impl_unsigned { } } forward_ref_binop! { impl Shl, shl for Wrapping<$t>, $f, - #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] } + #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl ShlAssign<$f> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const ShlAssign<$f> for Wrapping<$t> { #[inline] fn shl_assign(&mut self, other: $f) { *self = *self << other; } } forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "rust1", since = "1.0.0")] - impl Shr<$f> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Shr<$f> for Wrapping<$t> { type Output = Wrapping<$t>; #[inline] @@ -175,17 +188,20 @@ macro_rules! sh_impl_unsigned { } } forward_ref_binop! { impl Shr, shr for Wrapping<$t>, $f, - #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] } + #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl ShrAssign<$f> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const ShrAssign<$f> for Wrapping<$t> { #[inline] fn shr_assign(&mut self, other: $f) { *self = *self >> other; } } forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } }; } @@ -214,7 +230,8 @@ sh_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize } macro_rules! wrapping_impl { ($($t:ty)*) => ($( #[stable(feature = "rust1", since = "1.0.0")] - impl Add for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Add for Wrapping<$t> { type Output = Wrapping<$t>; #[inline] @@ -223,30 +240,36 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl Add, add for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl AddAssign for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const AddAssign for Wrapping<$t> { #[inline] fn add_assign(&mut self, other: Wrapping<$t>) { *self = *self + other; } } forward_ref_op_assign! { impl AddAssign, add_assign for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] - impl AddAssign<$t> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const AddAssign<$t> for Wrapping<$t> { #[inline] fn add_assign(&mut self, other: $t) { *self = *self + Wrapping(other); } } forward_ref_op_assign! { impl AddAssign, add_assign for Wrapping<$t>, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "rust1", since = "1.0.0")] - impl Sub for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Sub for Wrapping<$t> { type Output = Wrapping<$t>; #[inline] @@ -255,30 +278,36 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl Sub, sub for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl SubAssign for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const SubAssign for Wrapping<$t> { #[inline] fn sub_assign(&mut self, other: Wrapping<$t>) { *self = *self - other; } } forward_ref_op_assign! { impl SubAssign, sub_assign for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] - impl SubAssign<$t> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const SubAssign<$t> for Wrapping<$t> { #[inline] fn sub_assign(&mut self, other: $t) { *self = *self - Wrapping(other); } } forward_ref_op_assign! { impl SubAssign, sub_assign for Wrapping<$t>, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "rust1", since = "1.0.0")] - impl Mul for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Mul for Wrapping<$t> { type Output = Wrapping<$t>; #[inline] @@ -287,30 +316,36 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl Mul, mul for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl MulAssign for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const MulAssign for Wrapping<$t> { #[inline] fn mul_assign(&mut self, other: Wrapping<$t>) { *self = *self * other; } } forward_ref_op_assign! { impl MulAssign, mul_assign for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] - impl MulAssign<$t> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const MulAssign<$t> for Wrapping<$t> { #[inline] fn mul_assign(&mut self, other: $t) { *self = *self * Wrapping(other); } } forward_ref_op_assign! { impl MulAssign, mul_assign for Wrapping<$t>, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "wrapping_div", since = "1.3.0")] - impl Div for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Div for Wrapping<$t> { type Output = Wrapping<$t>; #[inline] @@ -319,30 +354,36 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl Div, div for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl DivAssign for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const DivAssign for Wrapping<$t> { #[inline] fn div_assign(&mut self, other: Wrapping<$t>) { *self = *self / other; } } forward_ref_op_assign! { impl DivAssign, div_assign for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] - impl DivAssign<$t> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const DivAssign<$t> for Wrapping<$t> { #[inline] fn div_assign(&mut self, other: $t) { *self = *self / Wrapping(other); } } forward_ref_op_assign! { impl DivAssign, div_assign for Wrapping<$t>, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "wrapping_impls", since = "1.7.0")] - impl Rem for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Rem for Wrapping<$t> { type Output = Wrapping<$t>; #[inline] @@ -351,30 +392,36 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl Rem, rem for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl RemAssign for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const RemAssign for Wrapping<$t> { #[inline] fn rem_assign(&mut self, other: Wrapping<$t>) { *self = *self % other; } } forward_ref_op_assign! { impl RemAssign, rem_assign for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] - impl RemAssign<$t> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const RemAssign<$t> for Wrapping<$t> { #[inline] fn rem_assign(&mut self, other: $t) { *self = *self % Wrapping(other); } } forward_ref_op_assign! { impl RemAssign, rem_assign for Wrapping<$t>, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "rust1", since = "1.0.0")] - impl Not for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Not for Wrapping<$t> { type Output = Wrapping<$t>; #[inline] @@ -383,10 +430,12 @@ macro_rules! wrapping_impl { } } forward_ref_unop! { impl Not, not for Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "rust1", since = "1.0.0")] - impl BitXor for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitXor for Wrapping<$t> { type Output = Wrapping<$t>; #[inline] @@ -395,30 +444,36 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl BitXor, bitxor for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl BitXorAssign for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitXorAssign for Wrapping<$t> { #[inline] fn bitxor_assign(&mut self, other: Wrapping<$t>) { *self = *self ^ other; } } forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] - impl BitXorAssign<$t> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitXorAssign<$t> for Wrapping<$t> { #[inline] fn bitxor_assign(&mut self, other: $t) { *self = *self ^ Wrapping(other); } } forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Wrapping<$t>, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "rust1", since = "1.0.0")] - impl BitOr for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitOr for Wrapping<$t> { type Output = Wrapping<$t>; #[inline] @@ -427,30 +482,36 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl BitOr, bitor for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl BitOrAssign for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitOrAssign for Wrapping<$t> { #[inline] fn bitor_assign(&mut self, other: Wrapping<$t>) { *self = *self | other; } } forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] - impl BitOrAssign<$t> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitOrAssign<$t> for Wrapping<$t> { #[inline] fn bitor_assign(&mut self, other: $t) { *self = *self | Wrapping(other); } } forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Wrapping<$t>, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "rust1", since = "1.0.0")] - impl BitAnd for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitAnd for Wrapping<$t> { type Output = Wrapping<$t>; #[inline] @@ -459,30 +520,36 @@ macro_rules! wrapping_impl { } } forward_ref_binop! { impl BitAnd, bitand for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl BitAndAssign for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitAndAssign for Wrapping<$t> { #[inline] fn bitand_assign(&mut self, other: Wrapping<$t>) { *self = *self & other; } } forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Wrapping<$t>, Wrapping<$t>, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")] - impl BitAndAssign<$t> for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitAndAssign<$t> for Wrapping<$t> { #[inline] fn bitand_assign(&mut self, other: $t) { *self = *self & Wrapping(other); } } forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Wrapping<$t>, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } #[stable(feature = "wrapping_neg", since = "1.10.0")] - impl Neg for Wrapping<$t> { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Neg for Wrapping<$t> { type Output = Self; #[inline] fn neg(self) -> Self { @@ -490,7 +557,8 @@ macro_rules! wrapping_impl { } } forward_ref_unop! { impl Neg, neg for Wrapping<$t>, - #[stable(feature = "wrapping_ref", since = "1.14.0")] } + #[stable(feature = "wrapping_ref", since = "1.14.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*) } diff --git a/library/core/src/ops/arith.rs b/library/core/src/ops/arith.rs index d3c65d503de6d..16c719b0c3946 100644 --- a/library/core/src/ops/arith.rs +++ b/library/core/src/ops/arith.rs @@ -107,7 +107,8 @@ macro_rules! add_impl { } forward_ref_binop! { impl Add, add for $t, $t, - #[stable(feature = "rust1", since = "1.0.0")] } + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*) } @@ -220,7 +221,8 @@ macro_rules! sub_impl { } forward_ref_binop! { impl Sub, sub for $t, $t, - #[stable(feature = "rust1", since = "1.0.0")] } + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*) } @@ -354,7 +356,8 @@ macro_rules! mul_impl { } forward_ref_binop! { impl Mul, mul for $t, $t, - #[stable(feature = "rust1", since = "1.0.0")] } + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*) } @@ -497,7 +500,8 @@ macro_rules! div_impl_integer { } forward_ref_binop! { impl Div, div for $t, $t, - #[stable(feature = "rust1", since = "1.0.0")] } + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*)*) } @@ -518,7 +522,8 @@ macro_rules! div_impl_float { } forward_ref_binop! { impl Div, div for $t, $t, - #[stable(feature = "rust1", since = "1.0.0")] } + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*) } @@ -605,7 +610,8 @@ macro_rules! rem_impl_integer { } forward_ref_binop! { impl Rem, rem for $t, $t, - #[stable(feature = "rust1", since = "1.0.0")] } + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*)*) } @@ -641,7 +647,8 @@ macro_rules! rem_impl_float { } forward_ref_binop! { impl Rem, rem for $t, $t, - #[stable(feature = "rust1", since = "1.0.0")] } + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*) } @@ -685,7 +692,9 @@ rem_impl_float! { f16 f32 f64 f128 } /// ``` #[lang = "neg"] #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[doc(alias = "-")] +#[const_trait] pub trait Neg { /// The resulting type after applying the `-` operator. #[stable(feature = "rust1", since = "1.0.0")] @@ -708,7 +717,8 @@ pub trait Neg { macro_rules! neg_impl { ($($t:ty)*) => ($( #[stable(feature = "rust1", since = "1.0.0")] - impl Neg for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Neg for $t { type Output = $t; #[inline] @@ -717,7 +727,8 @@ macro_rules! neg_impl { } forward_ref_unop! { impl Neg, neg for $t, - #[stable(feature = "rust1", since = "1.0.0")] } + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*) } @@ -754,12 +765,14 @@ neg_impl! { isize i8 i16 i32 i64 i128 f16 f32 f64 f128 } /// ``` #[lang = "add_assign"] #[stable(feature = "op_assign_traits", since = "1.8.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "cannot add-assign `{Rhs}` to `{Self}`", label = "no implementation for `{Self} += {Rhs}`" )] #[doc(alias = "+")] #[doc(alias = "+=")] +#[const_trait] pub trait AddAssign { /// Performs the `+=` operation. /// @@ -777,7 +790,8 @@ pub trait AddAssign { macro_rules! add_assign_impl { ($($t:ty)+) => ($( #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl AddAssign for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const AddAssign for $t { #[inline] #[track_caller] #[rustc_inherit_overflow_checks] @@ -785,7 +799,8 @@ macro_rules! add_assign_impl { } forward_ref_op_assign! { impl AddAssign, add_assign for $t, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )+) } @@ -822,12 +837,14 @@ add_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f /// ``` #[lang = "sub_assign"] #[stable(feature = "op_assign_traits", since = "1.8.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "cannot subtract-assign `{Rhs}` from `{Self}`", label = "no implementation for `{Self} -= {Rhs}`" )] #[doc(alias = "-")] #[doc(alias = "-=")] +#[const_trait] pub trait SubAssign { /// Performs the `-=` operation. /// @@ -845,7 +862,8 @@ pub trait SubAssign { macro_rules! sub_assign_impl { ($($t:ty)+) => ($( #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl SubAssign for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const SubAssign for $t { #[inline] #[track_caller] #[rustc_inherit_overflow_checks] @@ -853,7 +871,8 @@ macro_rules! sub_assign_impl { } forward_ref_op_assign! { impl SubAssign, sub_assign for $t, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )+) } @@ -881,12 +900,14 @@ sub_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f /// ``` #[lang = "mul_assign"] #[stable(feature = "op_assign_traits", since = "1.8.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "cannot multiply-assign `{Self}` by `{Rhs}`", label = "no implementation for `{Self} *= {Rhs}`" )] #[doc(alias = "*")] #[doc(alias = "*=")] +#[const_trait] pub trait MulAssign { /// Performs the `*=` operation. /// @@ -904,7 +925,8 @@ pub trait MulAssign { macro_rules! mul_assign_impl { ($($t:ty)+) => ($( #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl MulAssign for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const MulAssign for $t { #[inline] #[track_caller] #[rustc_inherit_overflow_checks] @@ -912,7 +934,8 @@ macro_rules! mul_assign_impl { } forward_ref_op_assign! { impl MulAssign, mul_assign for $t, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )+) } @@ -940,12 +963,14 @@ mul_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f /// ``` #[lang = "div_assign"] #[stable(feature = "op_assign_traits", since = "1.8.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "cannot divide-assign `{Self}` by `{Rhs}`", label = "no implementation for `{Self} /= {Rhs}`" )] #[doc(alias = "/")] #[doc(alias = "/=")] +#[const_trait] pub trait DivAssign { /// Performs the `/=` operation. /// @@ -963,14 +988,16 @@ pub trait DivAssign { macro_rules! div_assign_impl { ($($t:ty)+) => ($( #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl DivAssign for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const DivAssign for $t { #[inline] #[track_caller] fn div_assign(&mut self, other: $t) { *self /= other } } forward_ref_op_assign! { impl DivAssign, div_assign for $t, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )+) } @@ -1002,12 +1029,14 @@ div_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f /// ``` #[lang = "rem_assign"] #[stable(feature = "op_assign_traits", since = "1.8.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "cannot calculate and assign the remainder of `{Self}` divided by `{Rhs}`", label = "no implementation for `{Self} %= {Rhs}`" )] #[doc(alias = "%")] #[doc(alias = "%=")] +#[const_trait] pub trait RemAssign { /// Performs the `%=` operation. /// @@ -1025,14 +1054,16 @@ pub trait RemAssign { macro_rules! rem_assign_impl { ($($t:ty)+) => ($( #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl RemAssign for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const RemAssign for $t { #[inline] #[track_caller] fn rem_assign(&mut self, other: $t) { *self %= other } } forward_ref_op_assign! { impl RemAssign, rem_assign for $t, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )+) } diff --git a/library/core/src/ops/bit.rs b/library/core/src/ops/bit.rs index 1a44243e0dc40..001967282190f 100644 --- a/library/core/src/ops/bit.rs +++ b/library/core/src/ops/bit.rs @@ -30,7 +30,9 @@ /// ``` #[lang = "not"] #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[doc(alias = "!")] +#[const_trait] pub trait Not { /// The resulting type after applying the `!` operator. #[stable(feature = "rust1", since = "1.0.0")] @@ -54,7 +56,8 @@ pub trait Not { macro_rules! not_impl { ($($t:ty)*) => ($( #[stable(feature = "rust1", since = "1.0.0")] - impl Not for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Not for $t { type Output = $t; #[inline] @@ -62,14 +65,16 @@ macro_rules! not_impl { } forward_ref_unop! { impl Not, not for $t, - #[stable(feature = "rust1", since = "1.0.0")] } + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*) } not_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 } #[stable(feature = "not_never", since = "1.60.0")] -impl Not for ! { +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const Not for ! { type Output = !; #[inline] @@ -138,10 +143,12 @@ impl Not for ! { #[lang = "bitand"] #[doc(alias = "&")] #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "no implementation for `{Self} & {Rhs}`", label = "no implementation for `{Self} & {Rhs}`" )] +#[const_trait] pub trait BitAnd { /// The resulting type after applying the `&` operator. #[stable(feature = "rust1", since = "1.0.0")] @@ -165,7 +172,8 @@ pub trait BitAnd { macro_rules! bitand_impl { ($($t:ty)*) => ($( #[stable(feature = "rust1", since = "1.0.0")] - impl BitAnd for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitAnd for $t { type Output = $t; #[inline] @@ -173,7 +181,8 @@ macro_rules! bitand_impl { } forward_ref_binop! { impl BitAnd, bitand for $t, $t, - #[stable(feature = "rust1", since = "1.0.0")] } + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*) } @@ -239,10 +248,12 @@ bitand_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 } #[lang = "bitor"] #[doc(alias = "|")] #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "no implementation for `{Self} | {Rhs}`", label = "no implementation for `{Self} | {Rhs}`" )] +#[const_trait] pub trait BitOr { /// The resulting type after applying the `|` operator. #[stable(feature = "rust1", since = "1.0.0")] @@ -266,7 +277,8 @@ pub trait BitOr { macro_rules! bitor_impl { ($($t:ty)*) => ($( #[stable(feature = "rust1", since = "1.0.0")] - impl BitOr for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitOr for $t { type Output = $t; #[inline] @@ -274,7 +286,8 @@ macro_rules! bitor_impl { } forward_ref_binop! { impl BitOr, bitor for $t, $t, - #[stable(feature = "rust1", since = "1.0.0")] } + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*) } @@ -340,10 +353,12 @@ bitor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 } #[lang = "bitxor"] #[doc(alias = "^")] #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "no implementation for `{Self} ^ {Rhs}`", label = "no implementation for `{Self} ^ {Rhs}`" )] +#[const_trait] pub trait BitXor { /// The resulting type after applying the `^` operator. #[stable(feature = "rust1", since = "1.0.0")] @@ -367,7 +382,8 @@ pub trait BitXor { macro_rules! bitxor_impl { ($($t:ty)*) => ($( #[stable(feature = "rust1", since = "1.0.0")] - impl BitXor for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitXor for $t { type Output = $t; #[inline] @@ -375,7 +391,8 @@ macro_rules! bitxor_impl { } forward_ref_binop! { impl BitXor, bitxor for $t, $t, - #[stable(feature = "rust1", since = "1.0.0")] } + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )*) } @@ -440,10 +457,12 @@ bitxor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 } #[lang = "shl"] #[doc(alias = "<<")] #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "no implementation for `{Self} << {Rhs}`", label = "no implementation for `{Self} << {Rhs}`" )] +#[const_trait] pub trait Shl { /// The resulting type after applying the `<<` operator. #[stable(feature = "rust1", since = "1.0.0")] @@ -465,7 +484,8 @@ pub trait Shl { macro_rules! shl_impl { ($t:ty, $f:ty) => { #[stable(feature = "rust1", since = "1.0.0")] - impl Shl<$f> for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Shl<$f> for $t { type Output = $t; #[inline] @@ -476,7 +496,8 @@ macro_rules! shl_impl { } forward_ref_binop! { impl Shl, shl for $t, $f, - #[stable(feature = "rust1", since = "1.0.0")] } + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } }; } @@ -559,10 +580,12 @@ shl_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize } #[lang = "shr"] #[doc(alias = ">>")] #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "no implementation for `{Self} >> {Rhs}`", label = "no implementation for `{Self} >> {Rhs}`" )] +#[const_trait] pub trait Shr { /// The resulting type after applying the `>>` operator. #[stable(feature = "rust1", since = "1.0.0")] @@ -584,7 +607,8 @@ pub trait Shr { macro_rules! shr_impl { ($t:ty, $f:ty) => { #[stable(feature = "rust1", since = "1.0.0")] - impl Shr<$f> for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const Shr<$f> for $t { type Output = $t; #[inline] @@ -595,7 +619,8 @@ macro_rules! shr_impl { } forward_ref_binop! { impl Shr, shr for $t, $f, - #[stable(feature = "rust1", since = "1.0.0")] } + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } }; } @@ -687,10 +712,12 @@ shr_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize } #[lang = "bitand_assign"] #[doc(alias = "&=")] #[stable(feature = "op_assign_traits", since = "1.8.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "no implementation for `{Self} &= {Rhs}`", label = "no implementation for `{Self} &= {Rhs}`" )] +#[const_trait] pub trait BitAndAssign { /// Performs the `&=` operation. /// @@ -720,13 +747,15 @@ pub trait BitAndAssign { macro_rules! bitand_assign_impl { ($($t:ty)+) => ($( #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl BitAndAssign for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitAndAssign for $t { #[inline] fn bitand_assign(&mut self, other: $t) { *self &= other } } forward_ref_op_assign! { impl BitAndAssign, bitand_assign for $t, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )+) } @@ -759,10 +788,12 @@ bitand_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 } #[lang = "bitor_assign"] #[doc(alias = "|=")] #[stable(feature = "op_assign_traits", since = "1.8.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "no implementation for `{Self} |= {Rhs}`", label = "no implementation for `{Self} |= {Rhs}`" )] +#[const_trait] pub trait BitOrAssign { /// Performs the `|=` operation. /// @@ -792,13 +823,15 @@ pub trait BitOrAssign { macro_rules! bitor_assign_impl { ($($t:ty)+) => ($( #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl BitOrAssign for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitOrAssign for $t { #[inline] fn bitor_assign(&mut self, other: $t) { *self |= other } } forward_ref_op_assign! { impl BitOrAssign, bitor_assign for $t, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )+) } @@ -831,10 +864,12 @@ bitor_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 } #[lang = "bitxor_assign"] #[doc(alias = "^=")] #[stable(feature = "op_assign_traits", since = "1.8.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "no implementation for `{Self} ^= {Rhs}`", label = "no implementation for `{Self} ^= {Rhs}`" )] +#[const_trait] pub trait BitXorAssign { /// Performs the `^=` operation. /// @@ -864,13 +899,15 @@ pub trait BitXorAssign { macro_rules! bitxor_assign_impl { ($($t:ty)+) => ($( #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl BitXorAssign for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const BitXorAssign for $t { #[inline] fn bitxor_assign(&mut self, other: $t) { *self ^= other } } forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for $t, $t, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } )+) } @@ -901,10 +938,12 @@ bitxor_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 } #[lang = "shl_assign"] #[doc(alias = "<<=")] #[stable(feature = "op_assign_traits", since = "1.8.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "no implementation for `{Self} <<= {Rhs}`", label = "no implementation for `{Self} <<= {Rhs}`" )] +#[const_trait] pub trait ShlAssign { /// Performs the `<<=` operation. /// @@ -926,7 +965,8 @@ pub trait ShlAssign { macro_rules! shl_assign_impl { ($t:ty, $f:ty) => { #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl ShlAssign<$f> for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const ShlAssign<$f> for $t { #[inline] #[rustc_inherit_overflow_checks] fn shl_assign(&mut self, other: $f) { @@ -935,7 +975,8 @@ macro_rules! shl_assign_impl { } forward_ref_op_assign! { impl ShlAssign, shl_assign for $t, $f, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } }; } @@ -984,10 +1025,12 @@ shl_assign_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize } #[lang = "shr_assign"] #[doc(alias = ">>=")] #[stable(feature = "op_assign_traits", since = "1.8.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] #[diagnostic::on_unimplemented( message = "no implementation for `{Self} >>= {Rhs}`", label = "no implementation for `{Self} >>= {Rhs}`" )] +#[const_trait] pub trait ShrAssign { /// Performs the `>>=` operation. /// @@ -1009,7 +1052,8 @@ pub trait ShrAssign { macro_rules! shr_assign_impl { ($t:ty, $f:ty) => { #[stable(feature = "op_assign_traits", since = "1.8.0")] - impl ShrAssign<$f> for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] + impl const ShrAssign<$f> for $t { #[inline] #[rustc_inherit_overflow_checks] fn shr_assign(&mut self, other: $f) { @@ -1018,7 +1062,8 @@ macro_rules! shr_assign_impl { } forward_ref_op_assign! { impl ShrAssign, shr_assign for $t, $f, - #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] } + #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")] + #[rustc_const_unstable(feature = "const_ops", issue = "143802")] } }; } diff --git a/library/core/src/time.rs b/library/core/src/time.rs index 0fb5c0bac7562..90e102c04cda2 100644 --- a/library/core/src/time.rs +++ b/library/core/src/time.rs @@ -1100,7 +1100,8 @@ impl Duration { } #[stable(feature = "duration", since = "1.3.0")] -impl Add for Duration { +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const Add for Duration { type Output = Duration; #[inline] @@ -1110,7 +1111,8 @@ impl Add for Duration { } #[stable(feature = "time_augmented_assignment", since = "1.9.0")] -impl AddAssign for Duration { +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const AddAssign for Duration { #[inline] fn add_assign(&mut self, rhs: Duration) { *self = *self + rhs; @@ -1118,7 +1120,8 @@ impl AddAssign for Duration { } #[stable(feature = "duration", since = "1.3.0")] -impl Sub for Duration { +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const Sub for Duration { type Output = Duration; #[inline] @@ -1128,7 +1131,8 @@ impl Sub for Duration { } #[stable(feature = "time_augmented_assignment", since = "1.9.0")] -impl SubAssign for Duration { +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const SubAssign for Duration { #[inline] fn sub_assign(&mut self, rhs: Duration) { *self = *self - rhs; @@ -1136,7 +1140,8 @@ impl SubAssign for Duration { } #[stable(feature = "duration", since = "1.3.0")] -impl Mul for Duration { +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const Mul for Duration { type Output = Duration; #[inline] @@ -1146,7 +1151,8 @@ impl Mul for Duration { } #[stable(feature = "symmetric_u32_duration_mul", since = "1.31.0")] -impl Mul for u32 { +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const Mul for u32 { type Output = Duration; #[inline] @@ -1156,7 +1162,8 @@ impl Mul for u32 { } #[stable(feature = "time_augmented_assignment", since = "1.9.0")] -impl MulAssign for Duration { +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const MulAssign for Duration { #[inline] fn mul_assign(&mut self, rhs: u32) { *self = *self * rhs; @@ -1164,7 +1171,8 @@ impl MulAssign for Duration { } #[stable(feature = "duration", since = "1.3.0")] -impl Div for Duration { +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const Div for Duration { type Output = Duration; #[inline] @@ -1175,7 +1183,8 @@ impl Div for Duration { } #[stable(feature = "time_augmented_assignment", since = "1.9.0")] -impl DivAssign for Duration { +#[rustc_const_unstable(feature = "const_ops", issue = "143802")] +impl const DivAssign for Duration { #[inline] #[track_caller] fn div_assign(&mut self, rhs: u32) { diff --git a/src/tools/clippy/clippy_lints/src/operators/assign_op_pattern.rs b/src/tools/clippy/clippy_lints/src/operators/assign_op_pattern.rs index 9c6141d822263..7317c62df7fe8 100644 --- a/src/tools/clippy/clippy_lints/src/operators/assign_op_pattern.rs +++ b/src/tools/clippy/clippy_lints/src/operators/assign_op_pattern.rs @@ -1,6 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::msrvs::Msrv; -use clippy_utils::qualify_min_const_fn::is_stable_const_fn; use clippy_utils::source::SpanRangeExt; use clippy_utils::ty::implements_trait; use clippy_utils::visitors::for_each_expr_without_closures; @@ -21,7 +20,7 @@ pub(super) fn check<'tcx>( expr: &'tcx hir::Expr<'_>, assignee: &'tcx hir::Expr<'_>, e: &'tcx hir::Expr<'_>, - msrv: Msrv, + _msrv: Msrv, ) { if let hir::ExprKind::Binary(op, l, r) = &e.kind { let lint = |assignee: &hir::Expr<'_>, rhs: &hir::Expr<'_>| { @@ -45,10 +44,8 @@ pub(super) fn check<'tcx>( } // Skip if the trait is not stable in const contexts - if is_in_const_context(cx) - && let Some(binop_id) = cx.tcx.associated_item_def_ids(trait_id).first() - && !is_stable_const_fn(cx, *binop_id, msrv) - { + // FIXME: reintroduce a better check after this is merged back into Clippy + if is_in_const_context(cx) { return; } From 1aa5668d20d53976860d141a66aa727e425e1079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 28 Jul 2025 02:01:46 +0000 Subject: [PATCH 17/24] Point at the `Fn()` or `FnMut()` bound that coerced a closure, which caused a move error When encountering a move error involving a closure because the captured value isn't `Copy`, and the obligation comes from a bound on a type parameter that requires `Fn` or `FnMut`, we point at it and explain that an `FnOnce` wouldn't cause the move error. ``` error[E0507]: cannot move out of `foo`, a captured variable in an `Fn` closure --> f111.rs:15:25 | 14 | fn do_stuff(foo: Option) { | --- ----------- move occurs because `foo` has type `Option`, which does not implement the `Copy` trait | | | captured outer variable 15 | require_fn_trait(|| async { | -- ^^^^^ `foo` is moved here | | | captured by this `Fn` closure 16 | if foo.map_or(false, |f| f.foo()) { | --- variable moved due to use in coroutine | help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once --> f111.rs:12:53 | 12 | fn require_fn_trait>(_: impl Fn() -> F) {} | ^^^^^^^^^ help: consider cloning the value if the performance cost is acceptable | 16 | if foo.clone().map_or(false, |f| f.foo()) { | ++++++++ ``` --- .../src/diagnostics/move_errors.rs | 40 ++++- compiler/rustc_errors/src/diagnostic.rs | 7 +- tests/ui/borrowck/borrowck-in-static.stderr | 1 + .../borrowck/borrowck-move-by-capture.stderr | 5 + tests/ui/borrowck/issue-103624.stderr | 5 + .../issue-87456-point-to-closure.stderr | 5 + ...ove-upvar-from-non-once-ref-closure.stderr | 5 + tests/ui/issues/issue-4335.stderr | 1 + ...-move-out-of-closure-env-issue-1965.stderr | 5 + ...e-52663-span-decl-captured-variable.stderr | 5 + ...borrowck-call-is-borrow-issue-12224.stderr | 1 + .../dont-suggest-ref/move-into-closure.rs | 21 +++ .../dont-suggest-ref/move-into-closure.stderr | 147 +++++++++++++++--- .../suggestions/option-content-move2.stderr | 11 ++ .../suggestions/option-content-move3.stderr | 12 ++ .../unboxed-closure-illegal-move.stderr | 22 +++ 16 files changed, 268 insertions(+), 25 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs index 1067f1e40ef66..b24cb012d2b82 100644 --- a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs @@ -9,7 +9,7 @@ use rustc_middle::bug; use rustc_middle::mir::*; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_mir_dataflow::move_paths::{LookupResult, MovePathIndex}; -use rustc_span::{BytePos, ExpnKind, MacroKind, Span}; +use rustc_span::{BytePos, DUMMY_SP, ExpnKind, MacroKind, Span}; use rustc_trait_selection::error_reporting::traits::FindExprBySpan; use rustc_trait_selection::infer::InferCtxtExt; use tracing::debug; @@ -507,12 +507,50 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { ); let closure_span = tcx.def_span(def_id); + let mut clause_span = DUMMY_SP; + let typck_result = self.infcx.tcx.typeck(self.mir_def_id()); + if let Some(closure_def_id) = def_id.as_local() + && let hir::Node::Expr(expr) = tcx.hir_node_by_def_id(closure_def_id) + && let hir::Node::Expr(parent) = tcx.parent_hir_node(expr.hir_id) + && let hir::ExprKind::Call(callee, _) = parent.kind + && let Some(ty) = typck_result.node_type_opt(callee.hir_id) + && let ty::FnDef(fn_def_id, args) = ty.kind() + && let predicates = tcx.predicates_of(fn_def_id).instantiate(tcx, args) + && let Some((_, span)) = + predicates.predicates.iter().zip(predicates.spans.iter()).find( + |(pred, _)| match pred.as_trait_clause() { + Some(clause) + if let ty::Closure(clause_closure_def_id, _) = + clause.self_ty().skip_binder().kind() + && clause_closure_def_id == def_id + && (tcx.lang_items().fn_mut_trait() + == Some(clause.def_id()) + || tcx.lang_items().fn_trait() + == Some(clause.def_id())) => + { + // Found `` + true + } + _ => false, + }, + ) + { + // We point at the `Fn()` or `FnMut()` bound that coerced the closure, which + // could be changed to `FnOnce()` to avoid the move error. + clause_span = *span; + } + self.cannot_move_out_of(span, &place_description) .with_span_label(upvar_span, "captured outer variable") .with_span_label( closure_span, format!("captured by this `{closure_kind}` closure"), ) + .with_span_help( + clause_span, + "`Fn` and `FnMut` closures require captured values to be able to be \ + consumed multiple times, but an `FnOnce` consume them only once", + ) } _ => { let source = self.borrowed_content_source(deref_base); diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 5a5563c7bb2c8..98be37fd84b59 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -847,17 +847,18 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> { self } + with_fn! { with_span_help, /// Prints the span with some help above it. /// This is like [`Diag::help()`], but it gets its own span. #[rustc_lint_diagnostics] - pub fn span_help>( + pub fn span_help( &mut self, - sp: S, + sp: impl Into, msg: impl Into, ) -> &mut Self { self.sub(Level::Help, msg, sp.into()); self - } + } } /// Disallow attaching suggestions to this diagnostic. /// Any suggestions attached e.g. with the `span_suggestion_*` methods diff --git a/tests/ui/borrowck/borrowck-in-static.stderr b/tests/ui/borrowck/borrowck-in-static.stderr index 9bcf64dd62e2a..d85f6f5fdd5c7 100644 --- a/tests/ui/borrowck/borrowck-in-static.stderr +++ b/tests/ui/borrowck/borrowck-in-static.stderr @@ -10,6 +10,7 @@ LL | Box::new(|| x) | | | captured by this `Fn` closure | + = help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once help: consider cloning the value if the performance cost is acceptable | LL | Box::new(|| x.clone()) diff --git a/tests/ui/borrowck/borrowck-move-by-capture.stderr b/tests/ui/borrowck/borrowck-move-by-capture.stderr index 732af1593d606..e9e054407662f 100644 --- a/tests/ui/borrowck/borrowck-move-by-capture.stderr +++ b/tests/ui/borrowck/borrowck-move-by-capture.stderr @@ -12,6 +12,11 @@ LL | let _h = to_fn_once(move || -> isize { *bar }); | | | `bar` is moved here | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/borrowck-move-by-capture.rs:3:37 + | +LL | fn to_fn_mut>(f: F) -> F { f } + | ^^^^^^^^ help: consider cloning the value before moving it into the closure | LL ~ let value = bar.clone(); diff --git a/tests/ui/borrowck/issue-103624.stderr b/tests/ui/borrowck/issue-103624.stderr index af65deb16dcf8..ef02280888671 100644 --- a/tests/ui/borrowck/issue-103624.stderr +++ b/tests/ui/borrowck/issue-103624.stderr @@ -13,6 +13,11 @@ LL | LL | self.b; | ^^^^^^ `self.b` is moved here | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/issue-103624.rs:7:36 + | +LL | async fn spawn_blocking(f: impl (Fn() -> T) + Send + Sync + 'static) -> T { + | ^^^^^^^^^^^ note: if `StructB` implemented `Clone`, you could clone the value --> $DIR/issue-103624.rs:23:1 | diff --git a/tests/ui/borrowck/issue-87456-point-to-closure.stderr b/tests/ui/borrowck/issue-87456-point-to-closure.stderr index a0c7cac2addd0..043e336cd86df 100644 --- a/tests/ui/borrowck/issue-87456-point-to-closure.stderr +++ b/tests/ui/borrowck/issue-87456-point-to-closure.stderr @@ -10,6 +10,11 @@ LL | LL | let _foo: String = val; | ^^^ move occurs because `val` has type `String`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/issue-87456-point-to-closure.rs:3:24 + | +LL | fn take_mut(_val: impl FnMut()) {} + | ^^^^^^^ help: consider borrowing here | LL | let _foo: String = &val; diff --git a/tests/ui/borrowck/unboxed-closures-move-upvar-from-non-once-ref-closure.stderr b/tests/ui/borrowck/unboxed-closures-move-upvar-from-non-once-ref-closure.stderr index 177e9c8d2487e..d33330413103f 100644 --- a/tests/ui/borrowck/unboxed-closures-move-upvar-from-non-once-ref-closure.stderr +++ b/tests/ui/borrowck/unboxed-closures-move-upvar-from-non-once-ref-closure.stderr @@ -10,6 +10,11 @@ LL | y.into_iter(); | | | move occurs because `y` has type `Vec`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/unboxed-closures-move-upvar-from-non-once-ref-closure.rs:5:28 + | +LL | fn call(f: F) where F : Fn() { + | ^^^^ note: `into_iter` takes ownership of the receiver `self`, which moves `y` --> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL help: you can `clone` the value and consume it, but this might not be your desired behavior diff --git a/tests/ui/issues/issue-4335.stderr b/tests/ui/issues/issue-4335.stderr index 42ac632256401..b6d8f08616383 100644 --- a/tests/ui/issues/issue-4335.stderr +++ b/tests/ui/issues/issue-4335.stderr @@ -10,6 +10,7 @@ LL | id(Box::new(|| *v)) | | | captured by this `FnMut` closure | + = help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once help: if `T` implemented `Clone`, you could clone the value --> $DIR/issue-4335.rs:5:10 | diff --git a/tests/ui/moves/moves-based-on-type-move-out-of-closure-env-issue-1965.stderr b/tests/ui/moves/moves-based-on-type-move-out-of-closure-env-issue-1965.stderr index 51d0f85c031f5..dfc983bf48744 100644 --- a/tests/ui/moves/moves-based-on-type-move-out-of-closure-env-issue-1965.stderr +++ b/tests/ui/moves/moves-based-on-type-move-out-of-closure-env-issue-1965.stderr @@ -10,6 +10,11 @@ LL | let _f = to_fn(|| test(i)); | | | captured by this `Fn` closure | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/moves-based-on-type-move-out-of-closure-env-issue-1965.rs:3:33 + | +LL | fn to_fn>(f: F) -> F { f } + | ^^^^^ help: consider cloning the value if the performance cost is acceptable | LL | let _f = to_fn(|| test(i.clone())); diff --git a/tests/ui/nll/issue-52663-span-decl-captured-variable.stderr b/tests/ui/nll/issue-52663-span-decl-captured-variable.stderr index 5754603700653..7f9a8e50dae66 100644 --- a/tests/ui/nll/issue-52663-span-decl-captured-variable.stderr +++ b/tests/ui/nll/issue-52663-span-decl-captured-variable.stderr @@ -10,6 +10,11 @@ LL | expect_fn(|| drop(x.0)); | | | captured by this `Fn` closure | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/issue-52663-span-decl-captured-variable.rs:1:33 + | +LL | fn expect_fn(f: F) where F : Fn() { + | ^^^^ help: consider cloning the value if the performance cost is acceptable | LL | expect_fn(|| drop(x.0.clone())); diff --git a/tests/ui/span/borrowck-call-is-borrow-issue-12224.stderr b/tests/ui/span/borrowck-call-is-borrow-issue-12224.stderr index f37dc320fa315..8081f7b3a8b0f 100644 --- a/tests/ui/span/borrowck-call-is-borrow-issue-12224.stderr +++ b/tests/ui/span/borrowck-call-is-borrow-issue-12224.stderr @@ -44,6 +44,7 @@ LL | LL | foo(f); | ^ move occurs because `f` has type `{closure@$DIR/borrowck-call-is-borrow-issue-12224.rs:52:17: 52:58}`, which does not implement the `Copy` trait | + = help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once help: consider cloning the value if the performance cost is acceptable | LL | foo(f.clone()); diff --git a/tests/ui/suggestions/dont-suggest-ref/move-into-closure.rs b/tests/ui/suggestions/dont-suggest-ref/move-into-closure.rs index 44eac3691a3be..0154810f56439 100644 --- a/tests/ui/suggestions/dont-suggest-ref/move-into-closure.rs +++ b/tests/ui/suggestions/dont-suggest-ref/move-into-closure.rs @@ -11,8 +11,29 @@ struct X(Y); struct Y; fn consume_fn(_f: F) { } +//~^ HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures fn consume_fnmut(_f: F) { } +//~^ HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures +//~| HELP `Fn` and `FnMut` closures pub fn main() { } diff --git a/tests/ui/suggestions/dont-suggest-ref/move-into-closure.stderr b/tests/ui/suggestions/dont-suggest-ref/move-into-closure.stderr index edda2cbc735a2..d9ed25983d66d 100644 --- a/tests/ui/suggestions/dont-suggest-ref/move-into-closure.stderr +++ b/tests/ui/suggestions/dont-suggest-ref/move-into-closure.stderr @@ -1,5 +1,5 @@ error[E0507]: cannot move out of `x.0`, as `x` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:28:21 + --> $DIR/move-into-closure.rs:49:21 | LL | let x = X(Y); | - captured outer variable @@ -12,13 +12,18 @@ LL | let X(_t) = x; | data moved here | move occurs because `_t` has type `Y`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:13:18 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ help: consider borrowing here | LL | let X(_t) = &x; | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:31:34 + --> $DIR/move-into-closure.rs:52:34 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -32,13 +37,18 @@ LL | if let Either::One(_t) = e { } | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:13:18 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ help: consider borrowing here | LL | if let Either::One(_t) = &e { } | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:34:37 + --> $DIR/move-into-closure.rs:55:37 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -52,13 +62,18 @@ LL | while let Either::One(_t) = e { } | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:13:18 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ help: consider borrowing here | LL | while let Either::One(_t) = &e { } | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:37:15 + --> $DIR/move-into-closure.rs:58:15 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -75,13 +90,18 @@ LL | Either::One(_t) | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:13:18 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ help: consider borrowing here | LL | match &e { | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:43:15 + --> $DIR/move-into-closure.rs:64:15 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -98,13 +118,18 @@ LL | Either::One(_t) => (), | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:13:18 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ help: consider borrowing here | LL | match &e { | + error[E0507]: cannot move out of `x.0`, as `x` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:51:25 + --> $DIR/move-into-closure.rs:72:25 | LL | let x = X(Y); | - captured outer variable @@ -118,13 +143,18 @@ LL | let X(mut _t) = x; | data moved here | move occurs because `_t` has type `Y`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:13:18 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ help: consider borrowing here | LL | let X(mut _t) = &x; | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:54:38 + --> $DIR/move-into-closure.rs:75:38 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -138,13 +168,18 @@ LL | if let Either::One(mut _t) = em { } | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:13:18 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ help: consider borrowing here | LL | if let Either::One(mut _t) = &em { } | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:57:41 + --> $DIR/move-into-closure.rs:78:41 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -158,13 +193,18 @@ LL | while let Either::One(mut _t) = em { } | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:13:18 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ help: consider borrowing here | LL | while let Either::One(mut _t) = &em { } | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:60:15 + --> $DIR/move-into-closure.rs:81:15 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -181,13 +221,18 @@ LL | Either::One(mut _t) | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:13:18 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ help: consider borrowing here | LL | match &em { | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:66:15 + --> $DIR/move-into-closure.rs:87:15 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -204,13 +249,18 @@ LL | Either::One(mut _t) => (), | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:13:18 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ help: consider borrowing here | LL | match &em { | + error[E0507]: cannot move out of `x.0`, as `x` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:85:21 + --> $DIR/move-into-closure.rs:106:21 | LL | let x = X(Y); | - captured outer variable @@ -223,13 +273,18 @@ LL | let X(_t) = x; | data moved here | move occurs because `_t` has type `Y`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:25:21 + | +LL | fn consume_fnmut(_f: F) { } + | ^^^^^^^ help: consider borrowing here | LL | let X(_t) = &x; | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:88:34 + --> $DIR/move-into-closure.rs:109:34 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -243,13 +298,18 @@ LL | if let Either::One(_t) = e { } | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:25:21 + | +LL | fn consume_fnmut(_f: F) { } + | ^^^^^^^ help: consider borrowing here | LL | if let Either::One(_t) = &e { } | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:91:37 + --> $DIR/move-into-closure.rs:112:37 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -263,13 +323,18 @@ LL | while let Either::One(_t) = e { } | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:25:21 + | +LL | fn consume_fnmut(_f: F) { } + | ^^^^^^^ help: consider borrowing here | LL | while let Either::One(_t) = &e { } | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:94:15 + --> $DIR/move-into-closure.rs:115:15 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -286,13 +351,18 @@ LL | Either::One(_t) | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:25:21 + | +LL | fn consume_fnmut(_f: F) { } + | ^^^^^^^ help: consider borrowing here | LL | match &e { | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:100:15 + --> $DIR/move-into-closure.rs:121:15 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -309,13 +379,18 @@ LL | Either::One(_t) => (), | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:25:21 + | +LL | fn consume_fnmut(_f: F) { } + | ^^^^^^^ help: consider borrowing here | LL | match &e { | + error[E0507]: cannot move out of `x.0`, as `x` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:108:25 + --> $DIR/move-into-closure.rs:129:25 | LL | let x = X(Y); | - captured outer variable @@ -329,13 +404,18 @@ LL | let X(mut _t) = x; | data moved here | move occurs because `_t` has type `Y`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:25:21 + | +LL | fn consume_fnmut(_f: F) { } + | ^^^^^^^ help: consider borrowing here | LL | let X(mut _t) = &x; | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:111:38 + --> $DIR/move-into-closure.rs:132:38 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -349,13 +429,18 @@ LL | if let Either::One(mut _t) = em { } | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:25:21 + | +LL | fn consume_fnmut(_f: F) { } + | ^^^^^^^ help: consider borrowing here | LL | if let Either::One(mut _t) = &em { } | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:114:41 + --> $DIR/move-into-closure.rs:135:41 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -369,13 +454,18 @@ LL | while let Either::One(mut _t) = em { } | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:25:21 + | +LL | fn consume_fnmut(_f: F) { } + | ^^^^^^^ help: consider borrowing here | LL | while let Either::One(mut _t) = &em { } | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:117:15 + --> $DIR/move-into-closure.rs:138:15 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -392,13 +482,18 @@ LL | Either::One(mut _t) | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:25:21 + | +LL | fn consume_fnmut(_f: F) { } + | ^^^^^^^ help: consider borrowing here | LL | match &em { | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:123:15 + --> $DIR/move-into-closure.rs:144:15 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -415,13 +510,18 @@ LL | Either::One(mut _t) => (), | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:25:21 + | +LL | fn consume_fnmut(_f: F) { } + | ^^^^^^^ help: consider borrowing here | LL | match &em { | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:130:15 + --> $DIR/move-into-closure.rs:151:15 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -438,6 +538,11 @@ LL | Either::One(mut _t) => (), | data moved here | move occurs because `_t` has type `X`, which does not implement the `Copy` trait | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:25:21 + | +LL | fn consume_fnmut(_f: F) { } + | ^^^^^^^ help: consider borrowing here | LL | match &em { diff --git a/tests/ui/suggestions/option-content-move2.stderr b/tests/ui/suggestions/option-content-move2.stderr index c73e874b40368..c8aa6667b583f 100644 --- a/tests/ui/suggestions/option-content-move2.stderr +++ b/tests/ui/suggestions/option-content-move2.stderr @@ -14,6 +14,11 @@ LL | LL | var = Some(NotCopyable); | --- variable moved due to use in closure | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/option-content-move2.rs:5:12 + | +LL | fn func H, H: FnMut()>(_: F) {} + | ^^^^^^^^^^^^ note: if `NotCopyable` implemented `Clone`, you could clone the value --> $DIR/option-content-move2.rs:1:1 | @@ -38,6 +43,12 @@ LL | move || { LL | LL | var = Some(NotCopyableButCloneable); | --- variable moved due to use in closure + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/option-content-move2.rs:5:12 + | +LL | fn func H, H: FnMut()>(_: F) {} + | ^^^^^^^^^^^^ error: aborting due to 2 previous errors diff --git a/tests/ui/suggestions/option-content-move3.stderr b/tests/ui/suggestions/option-content-move3.stderr index 68c52352a6512..2c9a86c036be4 100644 --- a/tests/ui/suggestions/option-content-move3.stderr +++ b/tests/ui/suggestions/option-content-move3.stderr @@ -9,6 +9,7 @@ LL | move || { LL | let x = var; | ^^^ move occurs because `var` has type `NotCopyable`, which does not implement the `Copy` trait | + = help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once note: if `NotCopyable` implemented `Clone`, you could clone the value --> $DIR/option-content-move3.rs:2:1 | @@ -37,6 +38,11 @@ LL | move || { LL | let x = var; | --- variable moved due to use in closure | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/option-content-move3.rs:6:12 + | +LL | fn func H, H: FnMut()>(_: F) {} + | ^^^^^^^^^^^^ note: if `NotCopyable` implemented `Clone`, you could clone the value --> $DIR/option-content-move3.rs:2:1 | @@ -57,6 +63,7 @@ LL | move || { LL | let x = var; | ^^^ move occurs because `var` has type `NotCopyableButCloneable`, which does not implement the `Copy` trait | + = help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once help: consider borrowing here | LL | let x = &var; @@ -77,6 +84,11 @@ LL | move || { LL | let x = var; | --- variable moved due to use in closure | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/option-content-move3.rs:6:12 + | +LL | fn func H, H: FnMut()>(_: F) {} + | ^^^^^^^^^^^^ help: consider cloning the value before moving it into the closure | LL ~ { diff --git a/tests/ui/unboxed-closures/unboxed-closure-illegal-move.stderr b/tests/ui/unboxed-closures/unboxed-closure-illegal-move.stderr index 8d9a61cb68126..9d87402a15bff 100644 --- a/tests/ui/unboxed-closures/unboxed-closure-illegal-move.stderr +++ b/tests/ui/unboxed-closures/unboxed-closure-illegal-move.stderr @@ -10,6 +10,11 @@ LL | let f = to_fn(|| drop(x)); | | | captured by this `Fn` closure | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/unboxed-closure-illegal-move.rs:7:33 + | +LL | fn to_fn>(f: F) -> F { f } + | ^^^^^ help: consider cloning the value if the performance cost is acceptable | LL | let f = to_fn(|| drop(x.clone())); @@ -27,6 +32,11 @@ LL | let f = to_fn_mut(|| drop(x)); | | | captured by this `FnMut` closure | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/unboxed-closure-illegal-move.rs:8:37 + | +LL | fn to_fn_mut>(f: F) -> F { f } + | ^^^^^^^^ help: consider cloning the value if the performance cost is acceptable | LL | let f = to_fn_mut(|| drop(x.clone())); @@ -43,6 +53,12 @@ LL | let f = to_fn(move || drop(x)); | ------- ^ `x` is moved here | | | captured by this `Fn` closure + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/unboxed-closure-illegal-move.rs:7:33 + | +LL | fn to_fn>(f: F) -> F { f } + | ^^^^^ error[E0507]: cannot move out of `x`, a captured variable in an `FnMut` closure --> $DIR/unboxed-closure-illegal-move.rs:32:40 @@ -55,6 +71,12 @@ LL | let f = to_fn_mut(move || drop(x)); | ------- ^ `x` is moved here | | | captured by this `FnMut` closure + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/unboxed-closure-illegal-move.rs:8:37 + | +LL | fn to_fn_mut>(f: F) -> F { f } + | ^^^^^^^^ error: aborting due to 4 previous errors From 2484e189bfbc3decc2aa8480ce249dd84f6d2f9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 7 Aug 2025 21:09:42 +0000 Subject: [PATCH 18/24] Add support for method calls --- .../src/diagnostics/move_errors.rs | 74 ++- .../dont-suggest-ref/move-into-closure.rs | 140 +++++ .../dont-suggest-ref/move-into-closure.stderr | 566 +++++++++++++++++- 3 files changed, 725 insertions(+), 55 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs index b24cb012d2b82..5adbaca7e1135 100644 --- a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs @@ -9,6 +9,7 @@ use rustc_middle::bug; use rustc_middle::mir::*; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_mir_dataflow::move_paths::{LookupResult, MovePathIndex}; +use rustc_span::def_id::DefId; use rustc_span::{BytePos, DUMMY_SP, ExpnKind, MacroKind, Span}; use rustc_trait_selection::error_reporting::traits::FindExprBySpan; use rustc_trait_selection::infer::InferCtxtExt; @@ -507,38 +508,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { ); let closure_span = tcx.def_span(def_id); - let mut clause_span = DUMMY_SP; - let typck_result = self.infcx.tcx.typeck(self.mir_def_id()); - if let Some(closure_def_id) = def_id.as_local() - && let hir::Node::Expr(expr) = tcx.hir_node_by_def_id(closure_def_id) - && let hir::Node::Expr(parent) = tcx.parent_hir_node(expr.hir_id) - && let hir::ExprKind::Call(callee, _) = parent.kind - && let Some(ty) = typck_result.node_type_opt(callee.hir_id) - && let ty::FnDef(fn_def_id, args) = ty.kind() - && let predicates = tcx.predicates_of(fn_def_id).instantiate(tcx, args) - && let Some((_, span)) = - predicates.predicates.iter().zip(predicates.spans.iter()).find( - |(pred, _)| match pred.as_trait_clause() { - Some(clause) - if let ty::Closure(clause_closure_def_id, _) = - clause.self_ty().skip_binder().kind() - && clause_closure_def_id == def_id - && (tcx.lang_items().fn_mut_trait() - == Some(clause.def_id()) - || tcx.lang_items().fn_trait() - == Some(clause.def_id())) => - { - // Found `` - true - } - _ => false, - }, - ) - { - // We point at the `Fn()` or `FnMut()` bound that coerced the closure, which - // could be changed to `FnOnce()` to avoid the move error. - clause_span = *span; - } self.cannot_move_out_of(span, &place_description) .with_span_label(upvar_span, "captured outer variable") @@ -547,7 +516,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { format!("captured by this `{closure_kind}` closure"), ) .with_span_help( - clause_span, + self.get_closure_bound_clause_span(*def_id), "`Fn` and `FnMut` closures require captured values to be able to be \ consumed multiple times, but an `FnOnce` consume them only once", ) @@ -599,6 +568,45 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { err } + fn get_closure_bound_clause_span(&self, def_id: DefId) -> Span { + let tcx = self.infcx.tcx; + let typeck_result = tcx.typeck(self.mir_def_id()); + let Some(closure_def_id) = def_id.as_local() else { return DUMMY_SP }; + let hir::Node::Expr(expr) = tcx.hir_node_by_def_id(closure_def_id) else { return DUMMY_SP }; + let hir::Node::Expr(parent) = tcx.parent_hir_node(expr.hir_id) else { return DUMMY_SP }; + let predicates = match parent.kind { + hir::ExprKind::Call(callee, _) => { + let Some(ty) = typeck_result.node_type_opt(callee.hir_id) else { return DUMMY_SP }; + let ty::FnDef(fn_def_id, args) = ty.kind() else { return DUMMY_SP }; + tcx.predicates_of(fn_def_id).instantiate(tcx, args) + } + hir::ExprKind::MethodCall(..) => { + let Some((_, method)) = typeck_result.type_dependent_def(parent.hir_id) else { + return DUMMY_SP; + }; + let args = typeck_result.node_args(parent.hir_id); + tcx.predicates_of(method).instantiate(tcx, args) + } + _ => return DUMMY_SP, + }; + for (pred, span) in predicates.predicates.iter().zip(predicates.spans.iter()) { + tracing::info!(?pred); + tracing::info!(?span); + if let Some(clause) = pred.as_trait_clause() + && let ty::Closure(clause_closure_def_id, _) = clause.self_ty().skip_binder().kind() + && *clause_closure_def_id == def_id + && (tcx.lang_items().fn_mut_trait() == Some(clause.def_id()) + || tcx.lang_items().fn_trait() == Some(clause.def_id())) + { + // Found `` + // We point at the `Fn()` or `FnMut()` bound that coerced the closure, which + // could be changed to `FnOnce()` to avoid the move error. + return *span; + } + } + DUMMY_SP + } + fn add_move_hints(&self, error: GroupedMoveError<'tcx>, err: &mut Diag<'_>, span: Span) { match error { GroupedMoveError::MovesFromPlace { mut binds_to, move_from, .. } => { diff --git a/tests/ui/suggestions/dont-suggest-ref/move-into-closure.rs b/tests/ui/suggestions/dont-suggest-ref/move-into-closure.rs index 0154810f56439..34088f219b2b8 100644 --- a/tests/ui/suggestions/dont-suggest-ref/move-into-closure.rs +++ b/tests/ui/suggestions/dont-suggest-ref/move-into-closure.rs @@ -35,6 +35,32 @@ fn consume_fnmut(_f: F) { } //~| HELP `Fn` and `FnMut` closures //~| HELP `Fn` and `FnMut` closures +trait T { + fn consume_fn(_f: F) { } + //~^ HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + fn method_consume_fn(&self, _f: F) { } + //~^ HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures + //~| HELP `Fn` and `FnMut` closures +} +impl T for () {} + pub fn main() { } fn move_into_fn() { @@ -94,6 +120,120 @@ fn move_into_fn() { }); } +fn move_into_assoc_fn() { + let e = Either::One(X(Y)); + let mut em = Either::One(X(Y)); + + let x = X(Y); + + // move into Fn + + <() as T>::consume_fn(|| { + let X(_t) = x; + //~^ ERROR cannot move + //~| HELP consider borrowing here + if let Either::One(_t) = e { } + //~^ ERROR cannot move + //~| HELP consider borrowing here + while let Either::One(_t) = e { } + //~^ ERROR cannot move + //~| HELP consider borrowing here + match e { + //~^ ERROR cannot move + //~| HELP consider borrowing here + Either::One(_t) + | Either::Two(_t) => (), + } + match e { + //~^ ERROR cannot move + //~| HELP consider borrowing here + Either::One(_t) => (), + Either::Two(ref _t) => (), + // FIXME: should suggest removing `ref` too + } + + let X(mut _t) = x; + //~^ ERROR cannot move + //~| HELP consider borrowing here + if let Either::One(mut _t) = em { } + //~^ ERROR cannot move + //~| HELP consider borrowing here + while let Either::One(mut _t) = em { } + //~^ ERROR cannot move + //~| HELP consider borrowing here + match em { + //~^ ERROR cannot move + //~| HELP consider borrowing here + Either::One(mut _t) + | Either::Two(mut _t) => (), + } + match em { + //~^ ERROR cannot move + //~| HELP consider borrowing here + Either::One(mut _t) => (), + Either::Two(ref _t) => (), + // FIXME: should suggest removing `ref` too + } + }); +} + +fn move_into_method() { + let e = Either::One(X(Y)); + let mut em = Either::One(X(Y)); + + let x = X(Y); + + // move into Fn + + ().method_consume_fn(|| { + let X(_t) = x; + //~^ ERROR cannot move + //~| HELP consider borrowing here + if let Either::One(_t) = e { } + //~^ ERROR cannot move + //~| HELP consider borrowing here + while let Either::One(_t) = e { } + //~^ ERROR cannot move + //~| HELP consider borrowing here + match e { + //~^ ERROR cannot move + //~| HELP consider borrowing here + Either::One(_t) + | Either::Two(_t) => (), + } + match e { + //~^ ERROR cannot move + //~| HELP consider borrowing here + Either::One(_t) => (), + Either::Two(ref _t) => (), + // FIXME: should suggest removing `ref` too + } + + let X(mut _t) = x; + //~^ ERROR cannot move + //~| HELP consider borrowing here + if let Either::One(mut _t) = em { } + //~^ ERROR cannot move + //~| HELP consider borrowing here + while let Either::One(mut _t) = em { } + //~^ ERROR cannot move + //~| HELP consider borrowing here + match em { + //~^ ERROR cannot move + //~| HELP consider borrowing here + Either::One(mut _t) + | Either::Two(mut _t) => (), + } + match em { + //~^ ERROR cannot move + //~| HELP consider borrowing here + Either::One(mut _t) => (), + Either::Two(ref _t) => (), + // FIXME: should suggest removing `ref` too + } + }); +} + fn move_into_fnmut() { let e = Either::One(X(Y)); let mut em = Either::One(X(Y)); diff --git a/tests/ui/suggestions/dont-suggest-ref/move-into-closure.stderr b/tests/ui/suggestions/dont-suggest-ref/move-into-closure.stderr index d9ed25983d66d..132a31c8f7ceb 100644 --- a/tests/ui/suggestions/dont-suggest-ref/move-into-closure.stderr +++ b/tests/ui/suggestions/dont-suggest-ref/move-into-closure.stderr @@ -1,5 +1,5 @@ error[E0507]: cannot move out of `x.0`, as `x` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:49:21 + --> $DIR/move-into-closure.rs:75:21 | LL | let x = X(Y); | - captured outer variable @@ -23,7 +23,7 @@ LL | let X(_t) = &x; | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:52:34 + --> $DIR/move-into-closure.rs:78:34 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -48,7 +48,7 @@ LL | if let Either::One(_t) = &e { } | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:55:37 + --> $DIR/move-into-closure.rs:81:37 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -73,7 +73,7 @@ LL | while let Either::One(_t) = &e { } | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:58:15 + --> $DIR/move-into-closure.rs:84:15 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -101,7 +101,7 @@ LL | match &e { | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:64:15 + --> $DIR/move-into-closure.rs:90:15 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -129,7 +129,7 @@ LL | match &e { | + error[E0507]: cannot move out of `x.0`, as `x` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:72:25 + --> $DIR/move-into-closure.rs:98:25 | LL | let x = X(Y); | - captured outer variable @@ -154,7 +154,7 @@ LL | let X(mut _t) = &x; | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:75:38 + --> $DIR/move-into-closure.rs:101:38 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -179,7 +179,7 @@ LL | if let Either::One(mut _t) = &em { } | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:78:41 + --> $DIR/move-into-closure.rs:104:41 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -204,7 +204,7 @@ LL | while let Either::One(mut _t) = &em { } | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:81:15 + --> $DIR/move-into-closure.rs:107:15 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -232,7 +232,7 @@ LL | match &em { | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure - --> $DIR/move-into-closure.rs:87:15 + --> $DIR/move-into-closure.rs:113:15 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -259,8 +259,530 @@ help: consider borrowing here LL | match &em { | + +error[E0507]: cannot move out of `x.0`, as `x` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:132:21 + | +LL | let x = X(Y); + | - captured outer variable +... +LL | <() as T>::consume_fn(|| { + | -- captured by this `Fn` closure +LL | let X(_t) = x; + | -- ^ + | | + | data moved here + | move occurs because `_t` has type `Y`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:39:22 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | let X(_t) = &x; + | + + +error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:135:34 + | +LL | let e = Either::One(X(Y)); + | - captured outer variable +... +LL | <() as T>::consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | if let Either::One(_t) = e { } + | -- ^ + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:39:22 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | if let Either::One(_t) = &e { } + | + + +error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:138:37 + | +LL | let e = Either::One(X(Y)); + | - captured outer variable +... +LL | <() as T>::consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | while let Either::One(_t) = e { } + | -- ^ + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:39:22 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | while let Either::One(_t) = &e { } + | + + +error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:141:15 + | +LL | let e = Either::One(X(Y)); + | - captured outer variable +... +LL | <() as T>::consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | match e { + | ^ +... +LL | Either::One(_t) + | -- + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:39:22 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | match &e { + | + + +error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:147:15 + | +LL | let e = Either::One(X(Y)); + | - captured outer variable +... +LL | <() as T>::consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | match e { + | ^ +... +LL | Either::One(_t) => (), + | -- + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:39:22 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | match &e { + | + + +error[E0507]: cannot move out of `x.0`, as `x` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:155:25 + | +LL | let x = X(Y); + | - captured outer variable +... +LL | <() as T>::consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | let X(mut _t) = x; + | ------ ^ + | | + | data moved here + | move occurs because `_t` has type `Y`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:39:22 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | let X(mut _t) = &x; + | + + +error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:158:38 + | +LL | let mut em = Either::One(X(Y)); + | ------ captured outer variable +... +LL | <() as T>::consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | if let Either::One(mut _t) = em { } + | ------ ^^ + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:39:22 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | if let Either::One(mut _t) = &em { } + | + + +error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:161:41 + | +LL | let mut em = Either::One(X(Y)); + | ------ captured outer variable +... +LL | <() as T>::consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | while let Either::One(mut _t) = em { } + | ------ ^^ + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:39:22 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | while let Either::One(mut _t) = &em { } + | + + +error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:164:15 + | +LL | let mut em = Either::One(X(Y)); + | ------ captured outer variable +... +LL | <() as T>::consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | match em { + | ^^ +... +LL | Either::One(mut _t) + | ------ + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:39:22 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | match &em { + | + + +error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:170:15 + | +LL | let mut em = Either::One(X(Y)); + | ------ captured outer variable +... +LL | <() as T>::consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | match em { + | ^^ +... +LL | Either::One(mut _t) => (), + | ------ + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:39:22 + | +LL | fn consume_fn(_f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | match &em { + | + + +error[E0507]: cannot move out of `x.0`, as `x` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:189:21 + | +LL | let x = X(Y); + | - captured outer variable +... +LL | ().method_consume_fn(|| { + | -- captured by this `Fn` closure +LL | let X(_t) = x; + | -- ^ + | | + | data moved here + | move occurs because `_t` has type `Y`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:50:29 + | +LL | fn method_consume_fn(&self, _f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | let X(_t) = &x; + | + + +error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:192:34 + | +LL | let e = Either::One(X(Y)); + | - captured outer variable +... +LL | ().method_consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | if let Either::One(_t) = e { } + | -- ^ + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:50:29 + | +LL | fn method_consume_fn(&self, _f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | if let Either::One(_t) = &e { } + | + + +error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:195:37 + | +LL | let e = Either::One(X(Y)); + | - captured outer variable +... +LL | ().method_consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | while let Either::One(_t) = e { } + | -- ^ + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:50:29 + | +LL | fn method_consume_fn(&self, _f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | while let Either::One(_t) = &e { } + | + + +error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:198:15 + | +LL | let e = Either::One(X(Y)); + | - captured outer variable +... +LL | ().method_consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | match e { + | ^ +... +LL | Either::One(_t) + | -- + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:50:29 + | +LL | fn method_consume_fn(&self, _f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | match &e { + | + + +error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:204:15 + | +LL | let e = Either::One(X(Y)); + | - captured outer variable +... +LL | ().method_consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | match e { + | ^ +... +LL | Either::One(_t) => (), + | -- + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:50:29 + | +LL | fn method_consume_fn(&self, _f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | match &e { + | + + +error[E0507]: cannot move out of `x.0`, as `x` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:212:25 + | +LL | let x = X(Y); + | - captured outer variable +... +LL | ().method_consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | let X(mut _t) = x; + | ------ ^ + | | + | data moved here + | move occurs because `_t` has type `Y`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:50:29 + | +LL | fn method_consume_fn(&self, _f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | let X(mut _t) = &x; + | + + +error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:215:38 + | +LL | let mut em = Either::One(X(Y)); + | ------ captured outer variable +... +LL | ().method_consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | if let Either::One(mut _t) = em { } + | ------ ^^ + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:50:29 + | +LL | fn method_consume_fn(&self, _f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | if let Either::One(mut _t) = &em { } + | + + +error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:218:41 + | +LL | let mut em = Either::One(X(Y)); + | ------ captured outer variable +... +LL | ().method_consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | while let Either::One(mut _t) = em { } + | ------ ^^ + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:50:29 + | +LL | fn method_consume_fn(&self, _f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | while let Either::One(mut _t) = &em { } + | + + +error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:221:15 + | +LL | let mut em = Either::One(X(Y)); + | ------ captured outer variable +... +LL | ().method_consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | match em { + | ^^ +... +LL | Either::One(mut _t) + | ------ + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:50:29 + | +LL | fn method_consume_fn(&self, _f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | match &em { + | + + +error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `Fn` closure + --> $DIR/move-into-closure.rs:227:15 + | +LL | let mut em = Either::One(X(Y)); + | ------ captured outer variable +... +LL | ().method_consume_fn(|| { + | -- captured by this `Fn` closure +... +LL | match em { + | ^^ +... +LL | Either::One(mut _t) => (), + | ------ + | | + | data moved here + | move occurs because `_t` has type `X`, which does not implement the `Copy` trait + | +help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once + --> $DIR/move-into-closure.rs:50:29 + | +LL | fn method_consume_fn(&self, _f: F) { } + | ^^^^ +help: consider borrowing here + | +LL | match &em { + | + + error[E0507]: cannot move out of `x.0`, as `x` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:106:21 + --> $DIR/move-into-closure.rs:246:21 | LL | let x = X(Y); | - captured outer variable @@ -284,7 +806,7 @@ LL | let X(_t) = &x; | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:109:34 + --> $DIR/move-into-closure.rs:249:34 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -309,7 +831,7 @@ LL | if let Either::One(_t) = &e { } | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:112:37 + --> $DIR/move-into-closure.rs:252:37 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -334,7 +856,7 @@ LL | while let Either::One(_t) = &e { } | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:115:15 + --> $DIR/move-into-closure.rs:255:15 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -362,7 +884,7 @@ LL | match &e { | + error[E0507]: cannot move out of `e.0`, as `e` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:121:15 + --> $DIR/move-into-closure.rs:261:15 | LL | let e = Either::One(X(Y)); | - captured outer variable @@ -390,7 +912,7 @@ LL | match &e { | + error[E0507]: cannot move out of `x.0`, as `x` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:129:25 + --> $DIR/move-into-closure.rs:269:25 | LL | let x = X(Y); | - captured outer variable @@ -415,7 +937,7 @@ LL | let X(mut _t) = &x; | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:132:38 + --> $DIR/move-into-closure.rs:272:38 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -440,7 +962,7 @@ LL | if let Either::One(mut _t) = &em { } | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:135:41 + --> $DIR/move-into-closure.rs:275:41 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -465,7 +987,7 @@ LL | while let Either::One(mut _t) = &em { } | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:138:15 + --> $DIR/move-into-closure.rs:278:15 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -493,7 +1015,7 @@ LL | match &em { | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:144:15 + --> $DIR/move-into-closure.rs:284:15 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -521,7 +1043,7 @@ LL | match &em { | + error[E0507]: cannot move out of `em.0`, as `em` is a captured variable in an `FnMut` closure - --> $DIR/move-into-closure.rs:151:15 + --> $DIR/move-into-closure.rs:291:15 | LL | let mut em = Either::One(X(Y)); | ------ captured outer variable @@ -548,6 +1070,6 @@ help: consider borrowing here LL | match &em { | + -error: aborting due to 21 previous errors +error: aborting due to 41 previous errors For more information about this error, try `rustc --explain E0507`. From 74496bc4d9f64ea01ae3714b9680cc22c1535cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 10 Aug 2025 19:26:03 +0000 Subject: [PATCH 19/24] review comments --- .../rustc_borrowck/src/diagnostics/move_errors.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs index 5adbaca7e1135..af71db6948329 100644 --- a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs @@ -571,9 +571,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { fn get_closure_bound_clause_span(&self, def_id: DefId) -> Span { let tcx = self.infcx.tcx; let typeck_result = tcx.typeck(self.mir_def_id()); - let Some(closure_def_id) = def_id.as_local() else { return DUMMY_SP }; - let hir::Node::Expr(expr) = tcx.hir_node_by_def_id(closure_def_id) else { return DUMMY_SP }; - let hir::Node::Expr(parent) = tcx.parent_hir_node(expr.hir_id) else { return DUMMY_SP }; + // Check whether the closure is an argument to a call, if so, + // get the instantiated where-bounds of that call. + let closure_hir_id = tcx.local_def_id_to_hir_id(def_id.expect_local()); + let hir::Node::Expr(parent) = tcx.parent_hir_node(closure_hir_id) else { return DUMMY_SP }; + let predicates = match parent.kind { hir::ExprKind::Call(callee, _) => { let Some(ty) = typeck_result.node_type_opt(callee.hir_id) else { return DUMMY_SP }; @@ -589,9 +591,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { } _ => return DUMMY_SP, }; + + // Check whether one of the where-bounds requires the closure to impl `Fn[Mut]`. for (pred, span) in predicates.predicates.iter().zip(predicates.spans.iter()) { - tracing::info!(?pred); - tracing::info!(?span); if let Some(clause) = pred.as_trait_clause() && let ty::Closure(clause_closure_def_id, _) = clause.self_ty().skip_binder().kind() && *clause_closure_def_id == def_id From e9609abda42e8d88cf1f07946362cd81516792da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 10 Aug 2025 21:47:52 +0000 Subject: [PATCH 20/24] Account for macros when trying to point at inference cause Do not point at macro invocation which expands to an inference error. Avoid the following: ``` error[E0308]: mismatched types --> $DIR/does-not-have-iter-interpolated.rs:12:5 | LL | quote!($($nonrep)*); | ^^^^^^^^^^^^^^^^^^^ | | | expected `HasIterator`, found `ThereIsNoIteratorInRepetition` | expected due to this | here the type of `has_iter` is inferred to be `ThereIsNoIteratorInRepetition` ``` --- compiler/rustc_hir_typeck/src/demand.rs | 2 +- .../quote/does-not-have-iter-interpolated-dup.stderr | 1 - .../proc-macro/quote/does-not-have-iter-interpolated.stderr | 1 - .../ui/proc-macro/quote/does-not-have-iter-separated.stderr | 5 +---- tests/ui/proc-macro/quote/does-not-have-iter.stderr | 5 +---- 5 files changed, 3 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index e5684f8cbe669..fb6ebe066a8f2 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -698,7 +698,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) { match (self.tcx.parent_hir_node(expr.hir_id), error) { (hir::Node::LetStmt(hir::LetStmt { ty: Some(ty), init: Some(init), .. }), _) - if init.hir_id == expr.hir_id => + if init.hir_id == expr.hir_id && !ty.span.source_equal(init.span) => { // Point at `let` assignment type. err.span_label(ty.span, "expected due to this"); diff --git a/tests/ui/proc-macro/quote/does-not-have-iter-interpolated-dup.stderr b/tests/ui/proc-macro/quote/does-not-have-iter-interpolated-dup.stderr index ecb12c1df3b6a..0bcea9b85f470 100644 --- a/tests/ui/proc-macro/quote/does-not-have-iter-interpolated-dup.stderr +++ b/tests/ui/proc-macro/quote/does-not-have-iter-interpolated-dup.stderr @@ -5,7 +5,6 @@ LL | quote!($($nonrep $nonrep)*); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | | expected `HasIterator`, found `ThereIsNoIteratorInRepetition` - | expected due to this | here the type of `has_iter` is inferred to be `ThereIsNoIteratorInRepetition` error: aborting due to 1 previous error diff --git a/tests/ui/proc-macro/quote/does-not-have-iter-interpolated.stderr b/tests/ui/proc-macro/quote/does-not-have-iter-interpolated.stderr index 093e2ebc09858..d945ab41a12e9 100644 --- a/tests/ui/proc-macro/quote/does-not-have-iter-interpolated.stderr +++ b/tests/ui/proc-macro/quote/does-not-have-iter-interpolated.stderr @@ -5,7 +5,6 @@ LL | quote!($($nonrep)*); | ^^^^^^^^^^^^^^^^^^^ | | | expected `HasIterator`, found `ThereIsNoIteratorInRepetition` - | expected due to this | here the type of `has_iter` is inferred to be `ThereIsNoIteratorInRepetition` error: aborting due to 1 previous error diff --git a/tests/ui/proc-macro/quote/does-not-have-iter-separated.stderr b/tests/ui/proc-macro/quote/does-not-have-iter-separated.stderr index 937209e675ec8..2d715f293d431 100644 --- a/tests/ui/proc-macro/quote/does-not-have-iter-separated.stderr +++ b/tests/ui/proc-macro/quote/does-not-have-iter-separated.stderr @@ -2,10 +2,7 @@ error[E0308]: mismatched types --> $DIR/does-not-have-iter-separated.rs:8:5 | LL | quote!($(a b),*); - | ^^^^^^^^^^^^^^^^ - | | - | expected `HasIterator`, found `ThereIsNoIteratorInRepetition` - | expected due to this + | ^^^^^^^^^^^^^^^^ expected `HasIterator`, found `ThereIsNoIteratorInRepetition` error: aborting due to 1 previous error diff --git a/tests/ui/proc-macro/quote/does-not-have-iter.stderr b/tests/ui/proc-macro/quote/does-not-have-iter.stderr index e74ea3348992f..ac8e725038a31 100644 --- a/tests/ui/proc-macro/quote/does-not-have-iter.stderr +++ b/tests/ui/proc-macro/quote/does-not-have-iter.stderr @@ -2,10 +2,7 @@ error[E0308]: mismatched types --> $DIR/does-not-have-iter.rs:8:5 | LL | quote!($(a b)*); - | ^^^^^^^^^^^^^^^ - | | - | expected `HasIterator`, found `ThereIsNoIteratorInRepetition` - | expected due to this + | ^^^^^^^^^^^^^^^ expected `HasIterator`, found `ThereIsNoIteratorInRepetition` error: aborting due to 1 previous error From d9bd24d331919299975f36c64ddf6febc4f41fd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 10 Aug 2025 21:52:23 +0000 Subject: [PATCH 21/24] Add test showing innecessary inference span --- tests/ui/proc-macro/auxiliary/match-expander.rs | 15 +++++++++++++++ tests/ui/proc-macro/match-expander.rs | 13 +++++++++++++ tests/ui/proc-macro/match-expander.stderr | 14 ++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 tests/ui/proc-macro/auxiliary/match-expander.rs create mode 100644 tests/ui/proc-macro/match-expander.rs create mode 100644 tests/ui/proc-macro/match-expander.stderr diff --git a/tests/ui/proc-macro/auxiliary/match-expander.rs b/tests/ui/proc-macro/auxiliary/match-expander.rs new file mode 100644 index 0000000000000..bf78df2addf31 --- /dev/null +++ b/tests/ui/proc-macro/auxiliary/match-expander.rs @@ -0,0 +1,15 @@ +extern crate proc_macro; + +use proc_macro::TokenStream; + +#[proc_macro] +pub fn matcher(input: TokenStream) -> TokenStream { +" +struct S(()); +let s = S(()); +match s { + true => {} + _ => {} +} +".parse().unwrap() +} diff --git a/tests/ui/proc-macro/match-expander.rs b/tests/ui/proc-macro/match-expander.rs new file mode 100644 index 0000000000000..34e9a876153a7 --- /dev/null +++ b/tests/ui/proc-macro/match-expander.rs @@ -0,0 +1,13 @@ +//@ proc-macro: match-expander.rs +// Ensure that we don't point at macro invocation when providing inference contexts. + +#[macro_use] +extern crate match_expander; + +fn main() { + match_expander::matcher!(); + //~^ ERROR: mismatched types + //~| NOTE: expected `S`, found `bool` + //~| NOTE: this expression has type `S` + //~| NOTE: in this expansion of match_expander::matcher! +} diff --git a/tests/ui/proc-macro/match-expander.stderr b/tests/ui/proc-macro/match-expander.stderr new file mode 100644 index 0000000000000..38a0acc4941e0 --- /dev/null +++ b/tests/ui/proc-macro/match-expander.stderr @@ -0,0 +1,14 @@ +error[E0308]: mismatched types + --> $DIR/match-expander.rs:8:5 + | +LL | match_expander::matcher!(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | expected `S`, found `bool` + | this expression has type `S` + | + = note: this error originates in the macro `match_expander::matcher` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`. From 48816e74930cb8769fea5acb8a706878b4d79d5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 10 Aug 2025 21:55:02 +0000 Subject: [PATCH 22/24] Do not point at macro invocation when providing inference context --- .../rustc_trait_selection/src/error_reporting/infer/mod.rs | 2 +- tests/ui/proc-macro/match-expander.rs | 1 - tests/ui/proc-macro/match-expander.stderr | 5 +---- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs index 1c890821b1d0a..8551780bcd5f4 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs @@ -459,7 +459,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { span, format!("this is an iterator with items of type `{}`", args.type_at(0)), ); - } else { + } else if !span.overlaps(cause.span) { let expected_ty = self.tcx.short_string(expected_ty, err.long_ty_path()); err.span_label(span, format!("this expression has type `{expected_ty}`")); } diff --git a/tests/ui/proc-macro/match-expander.rs b/tests/ui/proc-macro/match-expander.rs index 34e9a876153a7..23e5746c540f7 100644 --- a/tests/ui/proc-macro/match-expander.rs +++ b/tests/ui/proc-macro/match-expander.rs @@ -8,6 +8,5 @@ fn main() { match_expander::matcher!(); //~^ ERROR: mismatched types //~| NOTE: expected `S`, found `bool` - //~| NOTE: this expression has type `S` //~| NOTE: in this expansion of match_expander::matcher! } diff --git a/tests/ui/proc-macro/match-expander.stderr b/tests/ui/proc-macro/match-expander.stderr index 38a0acc4941e0..b77468ec60a5e 100644 --- a/tests/ui/proc-macro/match-expander.stderr +++ b/tests/ui/proc-macro/match-expander.stderr @@ -2,10 +2,7 @@ error[E0308]: mismatched types --> $DIR/match-expander.rs:8:5 | LL | match_expander::matcher!(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | expected `S`, found `bool` - | this expression has type `S` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `S`, found `bool` | = note: this error originates in the macro `match_expander::matcher` (in Nightly builds, run with -Z macro-backtrace for more info) From 74c3727b1fbb274d5b5f841302310461ac54ca5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 10 Aug 2025 21:59:04 +0000 Subject: [PATCH 23/24] Remove unnecessary parentheses in `assert!`s --- library/coretests/tests/tuple.rs | 2 +- library/std/tests/env.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/coretests/tests/tuple.rs b/library/coretests/tests/tuple.rs index ea1e281425c89..5d680d1047237 100644 --- a/library/coretests/tests/tuple.rs +++ b/library/coretests/tests/tuple.rs @@ -37,7 +37,7 @@ fn test_partial_ord() { assert!(!((1.0f64, 2.0f64) <= (f64::NAN, 3.0))); assert!(!((1.0f64, 2.0f64) > (f64::NAN, 3.0))); assert!(!((1.0f64, 2.0f64) >= (f64::NAN, 3.0))); - assert!(((1.0f64, 2.0f64) < (2.0, f64::NAN))); + assert!((1.0f64, 2.0f64) < (2.0, f64::NAN)); assert!(!((2.0f64, 2.0f64) < (2.0, f64::NAN))); } diff --git a/library/std/tests/env.rs b/library/std/tests/env.rs index e754cf8263b0f..b53fd69b7070b 100644 --- a/library/std/tests/env.rs +++ b/library/std/tests/env.rs @@ -16,7 +16,7 @@ fn test_self_exe_path() { #[test] fn test() { - assert!((!Path::new("test-path").is_absolute())); + assert!(!Path::new("test-path").is_absolute()); #[cfg(not(target_env = "sgx"))] current_dir().unwrap(); From fadc961d99f221fd720f97dc9eec61c2332cd451 Mon Sep 17 00:00:00 2001 From: gewitternacht <60887951+gewitternacht@users.noreply.github.com> Date: Mon, 11 Aug 2025 00:05:47 +0200 Subject: [PATCH 24/24] mention `Hash` and `Ord`; refine description of `derive` --- library/core/src/clone.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/library/core/src/clone.rs b/library/core/src/clone.rs index 6fcba298a4324..b5daa3d41fa01 100644 --- a/library/core/src/clone.rs +++ b/library/core/src/clone.rs @@ -153,10 +153,14 @@ mod uninit; /// Standard library collections such as /// [`HashMap`], [`HashSet`], [`BTreeMap`], [`BTreeSet`] and [`BinaryHeap`] /// rely on their keys respecting this property for correct behavior. -/// -/// This property is automatically satisfied when deriving both `Clone` and [`PartialEq`] -/// using `#[derive(Clone, PartialEq)]` or when additionally deriving [`Eq`] -/// using `#[derive(Clone, PartialEq, Eq)]`. +/// Furthermore, these collections require that cloning a key preserves the outcome of the +/// [`Hash`] and [`Ord`] methods. Thankfully, this follows automatically from `x.clone() == x` +/// if `Hash` and `Ord` are correctly implemented according to their own requirements. +/// +/// When deriving both `Clone` and [`PartialEq`] using `#[derive(Clone, PartialEq)]` +/// or when additionally deriving [`Eq`] using `#[derive(Clone, PartialEq, Eq)]`, +/// then this property is automatically upheld – provided that it is satisfied by +/// the underlying types. /// /// Violating this property is a logic error. The behavior resulting from a logic error is not /// specified, but users of the trait must ensure that such logic errors do *not* result in