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] Destruct> 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
17 changes: 11 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 @@ -2190,7 +2188,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 @@ -4255,7 +4254,10 @@ 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: [const] Allocator + [const] Destruct> const Drop
for Vec<T, A>
{
fn drop(&mut self) {
unsafe {
// use drop for [T]
Expand Down Expand Up @@ -4474,7 +4476,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
4 changes: 4 additions & 0 deletions tests/ui/consts/bad-array-size-in-type-err.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ LL | pub const fn i(_: Wrap) {}
| ^ - value is dropped here
| |
| the destructor for this type cannot be evaluated in constant functions
|
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 7 previous errors

Expand Down
4 changes: 4 additions & 0 deletions tests/ui/consts/const-eval/issue-65394.stock.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ LL | let mut x = Vec::<i32>::new();
...
LL | };
| - value is dropped here
|
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 1 previous error

Expand Down
4 changes: 4 additions & 0 deletions tests/ui/consts/const-eval/livedrop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ LL | let mut always_returned = None;
...
LL | always_returned = never_returned;
| --------------- value is dropped here
|
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 1 previous error

Expand Down
10 changes: 5 additions & 5 deletions tests/ui/consts/control-flow/drop-fail.precise.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:9:9
error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:16:9
|
LL | let x = Some(Vec::new());
LL | let x = Some(NotConstDestruct);
| ^ the destructor for this type cannot be evaluated in constants
...
LL | };
| - value is dropped here

error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:40:9
error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:47:9
|
LL | let mut tmp = None;
| ^^^^^^^ the destructor for this type cannot be evaluated in constants
Expand Down
25 changes: 16 additions & 9 deletions tests/ui/consts/control-flow/drop-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
#![feature(const_destruct)]
#![cfg_attr(precise, feature(const_precise_live_drops))]


struct NotConstDestruct;

impl Drop for NotConstDestruct {
fn drop(&mut self) {}
}

// `x` is *not* always moved into the final value and may be dropped inside the initializer.
const _: Option<Vec<i32>> = {
let y: Option<Vec<i32>> = None;
let x = Some(Vec::new());
const _: Option<NotConstDestruct> = {
let y: Option<NotConstDestruct> = None;
let x = Some(NotConstDestruct);
//[stock,precise]~^ ERROR destructor of

if true {
Expand All @@ -18,25 +25,25 @@ const _: Option<Vec<i32>> = {

// We only clear `NeedsDrop` if a local is moved from in entirely. This is a shortcoming of the
// existing analysis.
const _: Vec<i32> = {
let vec_tuple = (Vec::new(),);
const _: NotConstDestruct = {
let vec_tuple = (NotConstDestruct,);
//[stock]~^ ERROR destructor of

vec_tuple.0
};

// This applies to single-field enum variants as well.
const _: Vec<i32> = {
let x: Result<_, Vec<i32>> = Ok(Vec::new());
const _: NotConstDestruct = {
let x: Result<_, NotConstDestruct> = Ok(NotConstDestruct);
//[stock]~^ ERROR destructor of

match x {
Ok(x) | Err(x) => x,
}
};

const _: Option<Vec<i32>> = {
let mut some = Some(Vec::new());
const _: Option<NotConstDestruct> = {
let mut some = Some(NotConstDestruct);
let mut tmp = None;
//[stock,precise]~^ ERROR destructor of

Expand Down
22 changes: 11 additions & 11 deletions tests/ui/consts/control-flow/drop-fail.stock.stderr
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:9:9
error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:16:9
|
LL | let x = Some(Vec::new());
LL | let x = Some(NotConstDestruct);
| ^ the destructor for this type cannot be evaluated in constants
...
LL | };
| - value is dropped here

error[E0493]: destructor of `(Vec<i32>,)` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:22:9
error[E0493]: destructor of `(NotConstDestruct,)` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:29:9
|
LL | let vec_tuple = (Vec::new(),);
LL | let vec_tuple = (NotConstDestruct,);
| ^^^^^^^^^ the destructor for this type cannot be evaluated in constants
...
LL | };
| - value is dropped here

error[E0493]: destructor of `Result<Vec<i32>, Vec<i32>>` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:30:9
error[E0493]: destructor of `Result<NotConstDestruct, NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:37:9
|
LL | let x: Result<_, Vec<i32>> = Ok(Vec::new());
LL | let x: Result<_, NotConstDestruct> = Ok(NotConstDestruct);
| ^ the destructor for this type cannot be evaluated in constants
...
LL | };
| - value is dropped here

error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:40:9
error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:47:9
|
LL | let mut tmp = None;
| ^^^^^^^ the destructor for this type cannot be evaluated in constants
Expand Down
15 changes: 11 additions & 4 deletions tests/ui/consts/miri_unleashed/assoc_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
// a test demonstrating why we do need to run static const qualification on associated constants
// instead of just checking the final constant


struct NotConstDestruct;

impl Drop for NotConstDestruct {
fn drop(&mut self) {}
}

trait Foo<T> {
const X: T;
}
Expand All @@ -15,18 +22,18 @@ trait Bar<T, U: Foo<T>> {
impl Foo<u32> for () {
const X: u32 = 42;
}
impl Foo<Vec<u32>> for String {
const X: Vec<u32> = Vec::new();
impl Foo<NotConstDestruct> for NotConstDestruct {
const X: NotConstDestruct = NotConstDestruct;
}

impl Bar<u32, ()> for () {}
impl Bar<Vec<u32>, String> for String {}
impl Bar<NotConstDestruct, NotConstDestruct> for NotConstDestruct {}

fn main() {
// this is fine, but would have been forbidden by the static checks on `F`
let x = <() as Bar<u32, ()>>::F;
// this test only causes errors due to the line below, so post-monomorphization
let y = <String as Bar<Vec<u32>, String>>::F;
let y = <NotConstDestruct as Bar<NotConstDestruct, NotConstDestruct>>::F;
}

//~? WARN skipping const checks
24 changes: 12 additions & 12 deletions tests/ui/consts/miri_unleashed/assoc_const.stderr
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
error[E0080]: calling non-const function `<Vec<u32> as Drop>::drop`
--> $DIR/assoc_const.rs:12:31
error[E0080]: calling non-const function `<NotConstDestruct as Drop>::drop`
--> $DIR/assoc_const.rs:19:31
|
LL | const F: u32 = (U::X, 42).1;
| ^ evaluation of `<std::string::String as Bar<std::vec::Vec<u32>, std::string::String>>::F` failed inside this call
| ^ evaluation of `<NotConstDestruct as Bar<NotConstDestruct, NotConstDestruct>>::F` failed inside this call
|
note: inside `std::ptr::drop_glue::<(Vec<u32>, u32)> - shim(Some((Vec<u32>, u32)))`
note: inside `std::ptr::drop_glue::<(NotConstDestruct, u32)> - shim(Some((NotConstDestruct, u32)))`
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
note: inside `std::ptr::drop_glue::<Vec<u32>> - shim(Some(Vec<u32>))`
note: inside `std::ptr::drop_glue::<NotConstDestruct> - shim(Some(NotConstDestruct))`
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL

note: erroneous constant encountered
--> $DIR/assoc_const.rs:29:13
--> $DIR/assoc_const.rs:36:13
|
LL | let y = <String as Bar<Vec<u32>, String>>::F;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | let y = <NotConstDestruct as Bar<NotConstDestruct, NotConstDestruct>>::F;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

note: erroneous constant encountered
--> $DIR/assoc_const.rs:29:13
--> $DIR/assoc_const.rs:36:13
|
LL | let y = <String as Bar<Vec<u32>, String>>::F;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | let y = <NotConstDestruct as Bar<NotConstDestruct, NotConstDestruct>>::F;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

warning: skipping const checks
|
help: skipping check that does not even have a feature gate
--> $DIR/assoc_const.rs:12:20
--> $DIR/assoc_const.rs:19:20
|
LL | const F: u32 = (U::X, 42).1;
| ^^^^^^^^^^
Expand Down
18 changes: 12 additions & 6 deletions tests/ui/consts/miri_unleashed/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@

use std::mem::ManuallyDrop;


struct NotConstDestruct;

impl Drop for NotConstDestruct {
fn drop(&mut self) {}
}

fn main() {}

static TEST_OK: () = {
let v: Vec<i32> = Vec::new();
let v: NotConstDestruct = NotConstDestruct;
let _v = ManuallyDrop::new(v);
};

// Make sure we catch executing bad drop functions.
// The actual error is tested by the error-pattern above.
static TEST_BAD: () = {
let _v: Vec<i32> = Vec::new();
};
//~^ NOTE failed inside this call
//~| ERROR calling non-const function `<Vec<i32> as Drop>::drop`
//~| NOTE inside `std::ptr::drop_glue::<Vec<i32>> - shim(Some(Vec<i32>))`
let _v: NotConstDestruct = NotConstDestruct;
}; //~ NOTE failed inside this call
//~| ERROR calling non-const function `<NotConstDestruct as Drop>::drop`
//~| NOTE inside `std::ptr::drop_glue::<NotConstDestruct> - shim(Some(NotConstDestruct))`

//~? WARN skipping const checks
10 changes: 5 additions & 5 deletions tests/ui/consts/miri_unleashed/drop.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error[E0080]: calling non-const function `<Vec<i32> as Drop>::drop`
--> $DIR/drop.rs:16:1
error[E0080]: calling non-const function `<NotConstDestruct as Drop>::drop`
--> $DIR/drop.rs:23:1
|
LL | };
| ^ evaluation of `TEST_BAD` failed inside this call
|
note: inside `std::ptr::drop_glue::<Vec<i32>> - shim(Some(Vec<i32>))`
note: inside `std::ptr::drop_glue::<NotConstDestruct> - shim(Some(NotConstDestruct))`
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL

warning: skipping const checks
|
help: skipping check that does not even have a feature gate
--> $DIR/drop.rs:15:9
--> $DIR/drop.rs:22:9
|
LL | let _v: Vec<i32> = Vec::new();
LL | let _v: NotConstDestruct = NotConstDestruct;
| ^^

error: aborting due to 1 previous error; 1 warning emitted
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/consts/promote-not.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ LL | let x = &String::new();
...
LL | };
| - value is dropped here
|
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0716]: temporary value dropped while borrowed
--> $DIR/promote-not.rs:60:33
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/consts/promoted_const_call3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ LL | let _: &'static _ = &String::new();
LL |
LL | };
| - value is dropped here
|
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0493]: destructor of `String` cannot be evaluated at compile-time
--> $DIR/promoted_const_call3.rs:8:30
Expand All @@ -14,6 +18,10 @@ LL | let _: &'static _ = &id(&String::new());
| ^^^^^^^^^^^^^ - value is dropped here
| |
| the destructor for this type cannot be evaluated in constants
|
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0716]: temporary value dropped while borrowed
--> $DIR/promoted_const_call3.rs:13:26
Expand Down
Loading
Loading