Skip to content
Open
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
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
#![feature(const_convert)]
#![feature(const_default)]
#![feature(const_destruct)]
#![feature(const_drop_in_place)]
#![feature(const_eval_select)]
#![feature(const_heap)]
#![feature(const_index)]
Expand Down
6 changes: 5 additions & 1 deletion library/alloc/src/raw_vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ impl<T, A: Allocator> RawVec<T, A> {
}
}

unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> {
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
unsafe impl<#[may_dangle] T, A: [const] Allocator> const Drop for RawVec<T, A> {
/// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
fn drop(&mut self) {
// SAFETY: We are in a Drop impl, self.inner will not be used again.
Expand Down Expand Up @@ -861,7 +862,10 @@ impl<A: Allocator> RawVecInner<A> {
}
Ok(())
}
}

#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
const impl<A: [const] Allocator> RawVecInner<A> {
/// # Safety
///
/// This function deallocates the owned allocation, but does not update `ptr` or `cap` to
Expand Down
15 changes: 9 additions & 6 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ use core::cmp::Ordering;
use core::hash::{Hash, Hasher};
#[cfg(not(no_global_oom_handling))]
use core::iter;
#[cfg(not(no_global_oom_handling))]
use core::marker::Destruct;
use core::marker::{Freeze, PhantomData};
use core::marker::{Destruct, Freeze, PhantomData};
Comment thread
Lars-Schumann marked this conversation as resolved.
use core::mem::{self, Assume, ManuallyDrop, MaybeUninit, SizedTypeProperties, TransmuteFrom};
use core::ops::{self, Index, IndexMut, Range, RangeBounds};
use core::ptr::{self, NonNull};
Expand Down Expand Up @@ -2184,7 +2182,8 @@ impl<T, A: Allocator> Vec<T, A> {
/// [`spare_capacity_mut()`]: Vec::spare_capacity_mut
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn set_len(&mut self, new_len: usize) {
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
pub const unsafe fn set_len(&mut self, new_len: usize) {
ub_checks::assert_unsafe_precondition!(
check_library_ub,
"Vec::set_len requires that new_len <= capacity()",
Expand Down Expand Up @@ -4245,7 +4244,8 @@ impl<T: Ord, A: Allocator> Ord for Vec<T, A> {
}

#[stable(feature = "rust1", since = "1.0.0")]
unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec<T, A> {
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
unsafe impl<#[may_dangle] T: [const] Destruct, A: Allocator> const Drop for Vec<T, A> {
fn drop(&mut self) {
unsafe {
// use drop for [T]
Expand Down Expand Up @@ -4464,7 +4464,10 @@ impl From<&str> for Vec<u8> {
}

#[stable(feature = "array_try_from_vec", since = "1.48.0")]
impl<T, A: Allocator, const N: usize> TryFrom<Vec<T, A>> for [T; N] {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T: [const] Destruct, A: [const] Allocator + [const] Destruct, const N: usize> const
TryFrom<Vec<T, A>> for [T; N]
{
type Error = Vec<T, A>;

/// Gets the entire contents of the `Vec<T>` as an array,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,20 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {

bb4: {
StorageLive(_32);
StorageLive(_29);
StorageLive(_30);
StorageLive(_27);
StorageLive(_13);
StorageLive(_14);
StorageLive(_20);
StorageLive(_24);
StorageLive(_15);
StorageLive(_26);
StorageLive(_16);
StorageLive(_13);
_13 = copy _8;
StorageLive(_14);
_14 = copy _11;
switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb5, otherwise: bb8];
}

bb5: {
StorageLive(_18);
StorageLive(_15);
_15 = copy _14 as std::ptr::NonNull<T> (Transmute);
_16 = copy _13 as *mut T (Transmute);
StorageLive(_17);
Expand All @@ -207,6 +204,7 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
}

bb6: {
StorageDead(_15);
StorageDead(_18);
StorageLive(_19);
_19 = Offset(copy _16, const 1_usize);
Expand All @@ -216,27 +214,27 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
}

bb7: {
StorageDead(_15);
StorageDead(_18);
goto -> bb10;
}

bb8: {
StorageLive(_20);
_20 = copy _14 as usize (Transmute);
switchInt(copy _20) -> [0: bb9, otherwise: bb12];
}

bb9: {
StorageDead(_20);
goto -> bb10;
}

bb10: {
StorageDead(_16);
StorageDead(_26);
StorageDead(_15);
StorageDead(_24);
StorageDead(_20);
StorageDead(_14);
StorageDead(_13);
StorageDead(_16);
StorageDead(_24);
StorageDead(_27);
StorageLive(_21);
StorageLive(_23);
Expand All @@ -246,7 +244,6 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
StorageDead(_23);
StorageDead(_21);
StorageDead(_30);
StorageDead(_29);
StorageDead(_32);
StorageDead(_12);
drop(_2) -> [return: bb11, unwind unreachable];
Expand All @@ -259,24 +256,25 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
bb12: {
_24 = SubUnchecked(copy _20, const 1_usize);
_11 = copy _24 as *const T (Transmute);
StorageDead(_20);
goto -> bb13;
}

bb13: {
StorageLive(_26);
StorageLive(_25);
_25 = copy _13 as *const T (Transmute);
_26 = &(*_25);
StorageDead(_25);
_27 = Option::<&T>::Some(copy _26);
StorageDead(_16);
StorageDead(_26);
StorageDead(_15);
StorageDead(_24);
StorageDead(_20);
StorageDead(_14);
StorageDead(_13);
StorageDead(_16);
StorageDead(_24);
_28 = copy ((_27 as Some).0: &T);
StorageDead(_27);
StorageLive(_29);
_29 = copy _12;
_30 = AddWithOverflow(copy _12, const 1_usize);
assert(!move (_30.1: bool), "attempt to compute `{} + {}`, which would overflow", copy _12, const 1_usize) -> [success: bb14, unwind unreachable];
Expand All @@ -288,9 +286,11 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
_31 = (copy _29, copy _28);
_32 = Option::<(usize, &T)>::Some(move _31);
StorageDead(_31);
StorageDead(_30);
StorageDead(_29);
StorageDead(_30);
StorageLive(_33);
_33 = copy (((_32 as Some).0: (usize, &T)).0: usize);
StorageLive(_34);
_34 = copy (((_32 as Some).0: (usize, &T)).1: &T);
StorageLive(_35);
_35 = &_2;
Expand All @@ -302,6 +302,8 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
bb15: {
StorageDead(_36);
StorageDead(_35);
StorageDead(_34);
StorageDead(_33);
StorageDead(_32);
goto -> bb4;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,18 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {

bb4: {
StorageLive(_23);
StorageLive(_12);
StorageLive(_13);
StorageLive(_19);
StorageLive(_20);
StorageLive(_14);
StorageLive(_22);
StorageLive(_15);
StorageLive(_12);
_12 = copy _8;
StorageLive(_13);
_13 = copy _11;
switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb5, otherwise: bb8];
}

bb5: {
StorageLive(_17);
StorageLive(_14);
_14 = copy _13 as std::ptr::NonNull<T> (Transmute);
_15 = copy _12 as *mut T (Transmute);
StorageLive(_16);
Expand All @@ -166,6 +164,7 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
}

bb6: {
StorageDead(_14);
StorageDead(_17);
StorageLive(_18);
_18 = Offset(copy _15, const 1_usize);
Expand All @@ -175,27 +174,27 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
}

bb7: {
StorageDead(_14);
StorageDead(_17);
goto -> bb10;
}

bb8: {
StorageLive(_19);
_19 = copy _13 as usize (Transmute);
switchInt(copy _19) -> [0: bb9, otherwise: bb12];
}

bb9: {
StorageDead(_19);
goto -> bb10;
}

bb10: {
StorageDead(_15);
StorageDead(_22);
StorageDead(_14);
StorageDead(_20);
StorageDead(_19);
StorageDead(_13);
StorageDead(_12);
StorageDead(_15);
StorageDead(_20);
StorageDead(_23);
drop(_2) -> [return: bb11, unwind unreachable];
}
Expand All @@ -207,22 +206,23 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
bb12: {
_20 = SubUnchecked(copy _19, const 1_usize);
_11 = copy _20 as *const T (Transmute);
StorageDead(_19);
goto -> bb13;
}

bb13: {
StorageLive(_22);
StorageLive(_21);
_21 = copy _12 as *const T (Transmute);
_22 = &(*_21);
StorageDead(_21);
_23 = Option::<&T>::Some(copy _22);
StorageDead(_15);
StorageDead(_22);
StorageDead(_14);
StorageDead(_20);
StorageDead(_19);
StorageDead(_13);
StorageDead(_12);
StorageDead(_15);
StorageDead(_20);
StorageLive(_24);
_24 = copy ((_23 as Some).0: &T);
StorageLive(_25);
_25 = &_2;
Expand All @@ -234,6 +234,7 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
bb14: {
StorageDead(_26);
StorageDead(_25);
StorageDead(_24);
StorageDead(_23);
goto -> bb4;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ fn range_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
}

bb4: {
StorageLive(_7);
_7 = copy _4;
_4 = AddUnchecked(copy _7, const 1_usize);
_8 = Option::<usize>::Some(copy _7);
StorageDead(_7);
Comment on lines +75 to +79
Copy link
Copy Markdown
Contributor

@oli-obk oli-obk May 7, 2026

Choose a reason for hiding this comment

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

I mean, this is probably an improvement, but what causes it? Do we have mir-opts that are checking constness of functions out of some reason?

View changes since the review

StorageDead(_6);
StorageLive(_9);
_9 = copy ((_8 as Some).0: usize);
StorageLive(_11);
_10 = Lt(copy _9, copy _3);
assert(move _10, "index out of bounds: the length is {} but the index is {}", copy _3, copy _9) -> [success: bb5, unwind unreachable];
}
Expand All @@ -93,6 +97,8 @@ fn range_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
bb6: {
StorageDead(_13);
StorageDead(_12);
StorageDead(_11);
StorageDead(_9);
StorageDead(_8);
goto -> bb1;
}
Expand Down
Loading
Loading