Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,11 @@ impl<T, A: Allocator> Vec<T, A> {
// * We only construct `&mut` references to `self.buf` through `&mut self` methods; borrow-
// check ensures that it is not possible to mutably alias `self.buf` within the
// returned lifetime.
unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
unsafe {
// normally this would use `slice::from_raw_parts`, but it's
// instantiated often enough that avoiding the UB check is worth it
&*core::intrinsics::aggregate_raw_ptr::<*const [T], _, _>(self.as_ptr(), self.len)
}
}

/// Extracts a mutable slice of the entire vector.
Expand Down Expand Up @@ -1779,7 +1783,11 @@ impl<T, A: Allocator> Vec<T, A> {
// * We only construct references to `self.buf` through `&self` and `&mut self` methods;
// borrow-check ensures that it is not possible to construct a reference to `self.buf`
// within the returned lifetime.
unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
unsafe {
// normally this would use `slice::from_raw_parts_mut`, but it's
// instantiated often enough that avoiding the UB check is worth it
&mut *core::intrinsics::aggregate_raw_ptr::<*mut [T], _, _>(self.as_mut_ptr(), self.len)
}
}

/// Returns a raw pointer to the vector's buffer, or a dangling raw pointer
Expand Down
24 changes: 16 additions & 8 deletions library/core/src/slice/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ impl<T, U> const PartialEq<[U]> for [T]
where
T: [const] PartialEq<U>,
{
#[inline]
fn eq(&self, other: &[U]) -> bool {
SlicePartialEq::equal(self, other)
}

fn ne(&self, other: &[U]) -> bool {
SlicePartialEq::not_equal(self, other)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -99,10 +96,6 @@ impl<T: PartialOrd> PartialOrd for [T] {
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
const trait SlicePartialEq<B> {
fn equal(&self, other: &[B]) -> bool;

fn not_equal(&self, other: &[B]) -> bool {
!self.equal(other)
}
Comment on lines -103 to -105
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annot: nothing actually overrode this anywhere, so removed it in favour of the usual PartialEq::ne.

}

// Generic slice equality
Expand All @@ -111,6 +104,11 @@ impl<A, B> const SlicePartialEq<B> for [A]
where
A: [const] PartialEq<B>,
{
// It's not worth trying to inline the loops underneath here *in MIR*,
// and preventing it encourages more useful inlining upstream,
// such as in `<str as PartialEq>::eq`.
// The codegen backend can still inline it later if needed.
#[rustc_no_mir_inline]
default fn equal(&self, other: &[B]) -> bool {
if self.len() != other.len() {
return false;
Expand Down Expand Up @@ -140,6 +138,16 @@ impl<A, B> const SlicePartialEq<B> for [A]
where
A: [const] BytewiseEq<B>,
{
// This is usually a pretty good backend inlining candidate because the
// intrinsic tends to just be `memcmp`. However, as of 2025-12 letting
// MIR inline this makes reuse worse because it means that, for example,
// `String::eq` doesn't inline, whereas by keeping this from inling all
// the wrappers until the call to this disappear. If the heuristics have
// changed and this is no longer fruitful, though, please do remove it.
// In the mean time, it's fine to not inline it in MIR because the backend
// will still inline it if it things it's important to do so.
#[rustc_no_mir_inline]
#[inline]
fn equal(&self, other: &[B]) -> bool {
if self.len() != other.len() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
scope 4 {
debug _x => _8;
}
scope 18 (inlined foo) {
scope 19 (inlined foo) {
let mut _27: *const [()];
}
}
scope 16 (inlined slice_from_raw_parts::<()>) {
scope 17 (inlined std::ptr::from_raw_parts::<[()], ()>) {
scope 17 (inlined slice_from_raw_parts::<()>) {
scope 18 (inlined std::ptr::from_raw_parts::<[()], ()>) {
}
}
}
Expand All @@ -49,19 +49,21 @@
scope 7 {
let _21: std::ptr::NonNull<[u8]>;
scope 8 {
scope 11 (inlined NonNull::<[u8]>::as_mut_ptr) {
scope 12 (inlined NonNull::<[u8]>::as_non_null_ptr) {
scope 13 (inlined NonNull::<[u8]>::cast::<u8>) {
scope 12 (inlined NonNull::<[u8]>::as_mut_ptr) {
scope 13 (inlined NonNull::<[u8]>::as_non_null_ptr) {
scope 14 (inlined NonNull::<[u8]>::cast::<u8>) {
let mut _25: *mut [u8];
scope 14 (inlined NonNull::<[u8]>::as_ptr) {
scope 15 (inlined NonNull::<[u8]>::as_ptr) {
}
}
}
scope 15 (inlined NonNull::<u8>::as_ptr) {
scope 16 (inlined NonNull::<u8>::as_ptr) {
}
}
}
scope 10 (inlined <std::alloc::Global as Allocator>::allocate) {
scope 11 (inlined std::alloc::Global::alloc_impl) {
}
}
}
scope 9 (inlined #[track_caller] Layout::from_size_align_unchecked) {
Expand Down Expand Up @@ -192,8 +194,8 @@
+ _18 = const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }};
StorageDead(_24);
StorageLive(_19);
- _19 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], copy _18, const false) -> [return: bb7, unwind unreachable];
+ _19 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}, const false) -> [return: bb7, unwind unreachable];
- _19 = std::alloc::Global::alloc_impl_runtime(copy _18, const false) -> [return: bb7, unwind unreachable];
+ _19 = std::alloc::Global::alloc_impl_runtime(const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}, const false) -> [return: bb7, unwind unreachable];
}

bb7: {
Expand Down
Loading
Loading