From 0aa623e108b65e9361e0c85e3eb73bd9381e96e9 Mon Sep 17 00:00:00 2001 From: Thalia Archibald Date: Wed, 5 Mar 2025 00:21:02 -0800 Subject: [PATCH 1/2] minicore: Add size_of to prelude and add rust_2024 prelude --- crates/ide-completion/src/tests/expression.rs | 1 + crates/test-utils/src/minicore.rs | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/crates/ide-completion/src/tests/expression.rs b/crates/ide-completion/src/tests/expression.rs index 375575128377..68218ae03fd3 100644 --- a/crates/ide-completion/src/tests/expression.rs +++ b/crates/ide-completion/src/tests/expression.rs @@ -1920,6 +1920,7 @@ fn bar() { md rust_2015 (use core::prelude::rust_2015) md rust_2018 (use core::prelude::rust_2018) md rust_2021 (use core::prelude::rust_2021) + md rust_2024 (use core::prelude::rust_2024) tt Clone tt Copy tt IntoIterator diff --git a/crates/test-utils/src/minicore.rs b/crates/test-utils/src/minicore.rs index 202afebde70f..d01db2d04cb7 100644 --- a/crates/test-utils/src/minicore.rs +++ b/crates/test-utils/src/minicore.rs @@ -1874,6 +1874,7 @@ pub mod prelude { marker::Sized, // :sized marker::Sync, // :sync mem::drop, // :drop + mem::size_of, // :size_of ops::Drop, // :drop ops::{AsyncFn, AsyncFnMut, AsyncFnOnce}, // :async_fn ops::{Fn, FnMut, FnOnce}, // :fn @@ -1895,6 +1896,10 @@ pub mod prelude { pub mod rust_2021 { pub use super::v1::*; } + + pub mod rust_2024 { + pub use super::v1::*; + } } #[prelude_import] From 0811ca0a611f7f203a55e104a25e562288bb0696 Mon Sep 17 00:00:00 2001 From: Thalia Archibald Date: Tue, 4 Mar 2025 20:28:38 -0800 Subject: [PATCH 2/2] Use size_of from the prelude instead of imported Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the prelude instead of importing or qualifying them. These functions were added to all preludes in Rust 1.80. --- crates/hir-def/src/hir/type_ref.rs | 2 +- crates/hir-ty/src/consteval/tests.rs | 3 +-- crates/hir-ty/src/display.rs | 2 +- crates/hir-ty/src/mir/eval.rs | 4 ++-- crates/hir-ty/src/mir/lower.rs | 2 +- crates/ide-db/src/generated/lints.rs | 4 ++-- crates/ide-db/src/symbol_index.rs | 3 +-- crates/ide/src/status.rs | 3 +-- crates/intern/src/symbol.rs | 31 +++++++++++----------------- crates/profile/src/memory_usage.rs | 2 +- crates/test-utils/src/minicore.rs | 2 +- 11 files changed, 24 insertions(+), 34 deletions(-) diff --git a/crates/hir-def/src/hir/type_ref.rs b/crates/hir-def/src/hir/type_ref.rs index 6d4d519cd2b7..6de4026dff75 100644 --- a/crates/hir-def/src/hir/type_ref.rs +++ b/crates/hir-def/src/hir/type_ref.rs @@ -260,7 +260,7 @@ pub enum TypeBound { } #[cfg(target_pointer_width = "64")] -const _: [(); 24] = [(); ::std::mem::size_of::()]; +const _: [(); 24] = [(); size_of::()]; #[derive(Clone, PartialEq, Eq, Hash, Debug)] pub enum UseArgRef { diff --git a/crates/hir-ty/src/consteval/tests.rs b/crates/hir-ty/src/consteval/tests.rs index 2d7d4cacd2c5..249170c4f3ee 100644 --- a/crates/hir-ty/src/consteval/tests.rs +++ b/crates/hir-ty/src/consteval/tests.rs @@ -2713,12 +2713,11 @@ fn const_trait_assoc() { r#" //- minicore: size_of, fn //- /a/lib.rs crate:a - use core::mem::size_of; pub struct S(T); impl S { pub const X: usize = { let k: T; - let f = || core::mem::size_of::(); + let f = || size_of::(); f() }; } diff --git a/crates/hir-ty/src/display.rs b/crates/hir-ty/src/display.rs index 49f061813d10..6ee4780d206f 100644 --- a/crates/hir-ty/src/display.rs +++ b/crates/hir-ty/src/display.rs @@ -4,7 +4,7 @@ use std::{ fmt::{self, Debug}, - mem::{self, size_of}, + mem, }; use base_db::CrateId; diff --git a/crates/hir-ty/src/mir/eval.rs b/crates/hir-ty/src/mir/eval.rs index 6b20522cf34c..d0f9f4519caa 100644 --- a/crates/hir-ty/src/mir/eval.rs +++ b/crates/hir-ty/src/mir/eval.rs @@ -302,7 +302,7 @@ impl Address { } } - fn to_bytes(&self) -> [u8; mem::size_of::()] { + fn to_bytes(&self) -> [u8; size_of::()] { usize::to_le_bytes(self.to_usize()) } @@ -589,7 +589,7 @@ pub fn interpret_mir( let ty = body.locals[return_slot()].ty.clone(); let mut evaluator = Evaluator::new(db, body.owner, assert_placeholder_ty_is_unused, trait_env)?; let it: Result = (|| { - if evaluator.ptr_size() != std::mem::size_of::() { + if evaluator.ptr_size() != size_of::() { not_supported!("targets with different pointer size from host"); } let interval = evaluator.interpret_mir(body.clone(), None.into_iter())?; diff --git a/crates/hir-ty/src/mir/lower.rs b/crates/hir-ty/src/mir/lower.rs index f88696692e6a..2fe4ce8319e2 100644 --- a/crates/hir-ty/src/mir/lower.rs +++ b/crates/hir-ty/src/mir/lower.rs @@ -1394,7 +1394,7 @@ impl<'ctx> MirLowerCtx<'ctx> { .layout_of_ty(ty.clone(), self.db.trait_environment_for_body(self.owner)) .map(|it| it.size.bytes_usize()) }; - const USIZE_SIZE: usize = mem::size_of::(); + const USIZE_SIZE: usize = size_of::(); let bytes: Box<[_]> = match l { hir_def::hir::Literal::String(b) => { let b = b.as_str(); diff --git a/crates/ide-db/src/generated/lints.rs b/crates/ide-db/src/generated/lints.rs index ed9d6c67501e..333dbe33059c 100644 --- a/crates/ide-db/src/generated/lints.rs +++ b/crates/ide-db/src/generated/lints.rs @@ -15086,7 +15086,7 @@ cannot be represented as the underlying type without loss."##, }, Lint { label: "clippy::manual_bits", - description: r##"Checks for usage of `std::mem::size_of::() * 8` when + description: r##"Checks for usage of `size_of::() * 8` when `T::BITS` is available."##, default_severity: Severity::Allow, warn_since: None, @@ -17428,7 +17428,7 @@ count of elements of type `T`"##, }, Lint { label: "clippy::size_of_ref", - description: r##"Checks for calls to `std::mem::size_of_val()` where the argument is + description: r##"Checks for calls to `size_of_val()` where the argument is a reference to a reference."##, default_severity: Severity::Allow, warn_since: None, diff --git a/crates/ide-db/src/symbol_index.rs b/crates/ide-db/src/symbol_index.rs index bb4c289c9084..2737436993de 100644 --- a/crates/ide-db/src/symbol_index.rs +++ b/crates/ide-db/src/symbol_index.rs @@ -24,7 +24,6 @@ use std::{ cmp::Ordering, fmt, hash::{Hash, Hasher}, - mem, ops::ControlFlow, }; @@ -299,7 +298,7 @@ impl SymbolIndex { } pub fn memory_size(&self) -> usize { - self.map.as_fst().size() + self.symbols.len() * mem::size_of::() + self.map.as_fst().size() + self.symbols.len() * size_of::() } fn range_to_map_value(start: usize, end: usize) -> u64 { diff --git a/crates/ide/src/status.rs b/crates/ide/src/status.rs index f8ecaa8fdf25..a44be67668ce 100644 --- a/crates/ide/src/status.rs +++ b/crates/ide/src/status.rs @@ -268,8 +268,7 @@ struct AttrsStats { impl fmt::Display for AttrsStats { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let size = - self.entries * std::mem::size_of::() + self.total * std::mem::size_of::(); + let size = self.entries * size_of::() + self.total * size_of::(); let size = Bytes::new(size as _); write!( fmt, diff --git a/crates/intern/src/symbol.rs b/crates/intern/src/symbol.rs index b3bf285edfb1..0fa6701ca3fa 100644 --- a/crates/intern/src/symbol.rs +++ b/crates/intern/src/symbol.rs @@ -18,20 +18,17 @@ use triomphe::Arc; pub mod symbols; // some asserts for layout compatibility -const _: () = assert!(std::mem::size_of::>() == std::mem::size_of::<&str>()); -const _: () = assert!(std::mem::align_of::>() == std::mem::align_of::<&str>()); +const _: () = assert!(size_of::>() == size_of::<&str>()); +const _: () = assert!(align_of::>() == align_of::<&str>()); -const _: () = assert!(std::mem::size_of::>>() == std::mem::size_of::<&&str>()); -const _: () = assert!(std::mem::align_of::>>() == std::mem::align_of::<&&str>()); +const _: () = assert!(size_of::>>() == size_of::<&&str>()); +const _: () = assert!(align_of::>>() == align_of::<&&str>()); -const _: () = - assert!(std::mem::size_of::<*const *const str>() == std::mem::size_of::()); -const _: () = - assert!(std::mem::align_of::<*const *const str>() == std::mem::align_of::()); +const _: () = assert!(size_of::<*const *const str>() == size_of::()); +const _: () = assert!(align_of::<*const *const str>() == align_of::()); -const _: () = assert!(std::mem::size_of::>>() == std::mem::size_of::()); -const _: () = - assert!(std::mem::align_of::>>() == std::mem::align_of::()); +const _: () = assert!(size_of::>>() == size_of::()); +const _: () = assert!(align_of::>>() == align_of::()); /// A pointer that points to a pointer to a `str`, it may be backed as a `&'static &'static str` or /// `Arc>` but its size is that of a thin pointer. The active variant is encoded as a tag @@ -49,9 +46,7 @@ impl TaggedArcPtr { const BOOL_BITS: usize = true as usize; const fn non_arc(r: &'static &'static str) -> Self { - assert!( - mem::align_of::<&'static &'static str>().trailing_zeros() as usize > Self::BOOL_BITS - ); + assert!(align_of::<&'static &'static str>().trailing_zeros() as usize > Self::BOOL_BITS); // SAFETY: The pointer is non-null as it is derived from a reference // Ideally we would call out to `pack_arc` but for a `false` tag, unfortunately the // packing stuff requires reading out the pointer to an integer which is not supported @@ -64,9 +59,7 @@ impl TaggedArcPtr { } fn arc(arc: Arc>) -> Self { - assert!( - mem::align_of::<&'static &'static str>().trailing_zeros() as usize > Self::BOOL_BITS - ); + assert!(align_of::<&'static &'static str>().trailing_zeros() as usize > Self::BOOL_BITS); Self { packed: Self::pack_arc( // Safety: `Arc::into_raw` always returns a non null pointer @@ -131,8 +124,8 @@ impl fmt::Debug for Symbol { } } -const _: () = assert!(std::mem::size_of::() == std::mem::size_of::>()); -const _: () = assert!(std::mem::align_of::() == std::mem::align_of::>()); +const _: () = assert!(size_of::() == size_of::>()); +const _: () = assert!(align_of::() == align_of::>()); static MAP: OnceLock>> = OnceLock::new(); diff --git a/crates/profile/src/memory_usage.rs b/crates/profile/src/memory_usage.rs index 006748ddb08e..f5b8eca060f9 100644 --- a/crates/profile/src/memory_usage.rs +++ b/crates/profile/src/memory_usage.rs @@ -38,7 +38,7 @@ impl MemoryUsage { // approximate that by using the Commit Charge value. use windows_sys::Win32::System::{Threading::*, ProcessStatus::*}; - use std::mem::{MaybeUninit, size_of}; + use std::mem::MaybeUninit; let proc = unsafe { GetCurrentProcess() }; let mut mem_counters = MaybeUninit::uninit(); diff --git a/crates/test-utils/src/minicore.rs b/crates/test-utils/src/minicore.rs index d01db2d04cb7..bf53e58d70ce 100644 --- a/crates/test-utils/src/minicore.rs +++ b/crates/test-utils/src/minicore.rs @@ -1825,7 +1825,7 @@ macro_rules! impl_int { ($($t:ty)*) => { $( impl $t { - pub const fn from_ne_bytes(bytes: [u8; mem::size_of::()]) -> Self { + pub const fn from_ne_bytes(bytes: [u8; size_of::()]) -> Self { unsafe { mem::transmute(bytes) } } }