Skip to content

Commit 45a465e

Browse files
Rollup merge of rust-lang#155616 - Lars-Schumann:const-array-try-from-vec, r=oli-obk
constify `TryFrom<Vec>` for array Related issues rust-lang#79597, rust-lang#143773, rust-lang#133214 this makes `Vec` `const Destruct` along the way.
2 parents be57c2a + 123d0d1 commit 45a465e

23 files changed

Lines changed: 196 additions & 116 deletions

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
#![feature(const_convert)]
106106
#![feature(const_default)]
107107
#![feature(const_destruct)]
108+
#![feature(const_drop_in_place)]
108109
#![feature(const_eval_select)]
109110
#![feature(const_heap)]
110111
#![feature(const_index)]

library/alloc/src/raw_vec/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,8 @@ impl<T, A: Allocator> RawVec<T, A> {
417417
}
418418
}
419419

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

867+
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
868+
const impl<A: [const] Allocator> RawVecInner<A> {
865869
/// # Safety
866870
///
867871
/// This function deallocates the owned allocation, but does not update `ptr` or `cap` to

library/alloc/src/vec/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ use core::cmp::Ordering;
7979
use core::hash::{Hash, Hasher};
8080
#[cfg(not(no_global_oom_handling))]
8181
use core::iter;
82-
#[cfg(not(no_global_oom_handling))]
83-
use core::marker::Destruct;
84-
use core::marker::{Freeze, PhantomData};
82+
use core::marker::{Destruct, Freeze, PhantomData};
8583
use core::mem::{self, Assume, ManuallyDrop, MaybeUninit, SizedTypeProperties, TransmuteFrom};
8684
use core::ops::{self, Index, IndexMut, Range, RangeBounds};
8785
use core::ptr::{self, NonNull};
@@ -2218,7 +2216,8 @@ impl<T, A: Allocator> Vec<T, A> {
22182216
/// [`spare_capacity_mut()`]: Vec::spare_capacity_mut
22192217
#[inline]
22202218
#[stable(feature = "rust1", since = "1.0.0")]
2221-
pub unsafe fn set_len(&mut self, new_len: usize) {
2219+
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2220+
pub const unsafe fn set_len(&mut self, new_len: usize) {
22222221
ub_checks::assert_unsafe_precondition!(
22232222
check_library_ub,
22242223
"Vec::set_len requires that new_len <= capacity()",
@@ -4288,7 +4287,10 @@ impl<T: Ord, A: Allocator> Ord for Vec<T, A> {
42884287
}
42894288

42904289
#[stable(feature = "rust1", since = "1.0.0")]
4291-
unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec<T, A> {
4290+
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
4291+
const unsafe impl<#[may_dangle] T: [const] Destruct, A: [const] Allocator + [const] Destruct> Drop
4292+
for Vec<T, A>
4293+
{
42924294
fn drop(&mut self) {
42934295
unsafe {
42944296
// use drop for [T]
@@ -4507,7 +4509,10 @@ impl From<&str> for Vec<u8> {
45074509
}
45084510

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

45134518
/// Gets the entire contents of the `Vec<T>` as an array,

tests/ui/consts/bad-array-size-in-type-err.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ LL | pub const fn i(_: Wrap) {}
5757
| ^ - value is dropped here
5858
| |
5959
| the destructor for this type cannot be evaluated in constant functions
60+
|
61+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
62+
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
63+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
6064

6165
error: aborting due to 7 previous errors
6266

tests/ui/consts/const-eval/issue-65394.stock.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ LL | let mut x = Vec::<i32>::new();
66
...
77
LL | };
88
| - value is dropped here
9+
|
10+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
11+
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
12+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
913

1014
error: aborting due to 1 previous error
1115

tests/ui/consts/const-eval/livedrop.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ LL | let mut always_returned = None;
66
...
77
LL | always_returned = never_returned;
88
| --------------- value is dropped here
9+
|
10+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
11+
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
12+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
913

1014
error: aborting due to 1 previous error
1115

tests/ui/consts/control-flow/drop-fail.precise.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
2-
--> $DIR/drop-fail.rs:9:9
1+
error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
2+
--> $DIR/drop-fail.rs:16:9
33
|
4-
LL | let x = Some(Vec::new());
4+
LL | let x = Some(NotConstDestruct);
55
| ^ the destructor for this type cannot be evaluated in constants
66
...
77
LL | };
88
| - value is dropped here
99

10-
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
11-
--> $DIR/drop-fail.rs:40:9
10+
error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
11+
--> $DIR/drop-fail.rs:47:9
1212
|
1313
LL | let mut tmp = None;
1414
| ^^^^^^^ the destructor for this type cannot be evaluated in constants

tests/ui/consts/control-flow/drop-fail.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33
#![feature(const_destruct)]
44
#![cfg_attr(precise, feature(const_precise_live_drops))]
55

6+
7+
struct NotConstDestruct;
8+
9+
impl Drop for NotConstDestruct {
10+
fn drop(&mut self) {}
11+
}
12+
613
// `x` is *not* always moved into the final value and may be dropped inside the initializer.
7-
const _: Option<Vec<i32>> = {
8-
let y: Option<Vec<i32>> = None;
9-
let x = Some(Vec::new());
14+
const _: Option<NotConstDestruct> = {
15+
let y: Option<NotConstDestruct> = None;
16+
let x = Some(NotConstDestruct);
1017
//[stock,precise]~^ ERROR destructor of
1118

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

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

2532
vec_tuple.0
2633
};
2734

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

3340
match x {
3441
Ok(x) | Err(x) => x,
3542
}
3643
};
3744

38-
const _: Option<Vec<i32>> = {
39-
let mut some = Some(Vec::new());
45+
const _: Option<NotConstDestruct> = {
46+
let mut some = Some(NotConstDestruct);
4047
let mut tmp = None;
4148
//[stock,precise]~^ ERROR destructor of
4249

tests/ui/consts/control-flow/drop-fail.stock.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
2-
--> $DIR/drop-fail.rs:9:9
1+
error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
2+
--> $DIR/drop-fail.rs:16:9
33
|
4-
LL | let x = Some(Vec::new());
4+
LL | let x = Some(NotConstDestruct);
55
| ^ the destructor for this type cannot be evaluated in constants
66
...
77
LL | };
88
| - value is dropped here
99

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

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

28-
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
29-
--> $DIR/drop-fail.rs:40:9
28+
error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
29+
--> $DIR/drop-fail.rs:47:9
3030
|
3131
LL | let mut tmp = None;
3232
| ^^^^^^^ the destructor for this type cannot be evaluated in constants

tests/ui/consts/miri_unleashed/assoc_const.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
// a test demonstrating why we do need to run static const qualification on associated constants
55
// instead of just checking the final constant
66

7+
8+
struct NotConstDestruct;
9+
10+
impl Drop for NotConstDestruct {
11+
fn drop(&mut self) {}
12+
}
13+
714
trait Foo<T> {
815
const X: T;
916
}
@@ -15,18 +22,18 @@ trait Bar<T, U: Foo<T>> {
1522
impl Foo<u32> for () {
1623
const X: u32 = 42;
1724
}
18-
impl Foo<Vec<u32>> for String {
19-
const X: Vec<u32> = Vec::new();
25+
impl Foo<NotConstDestruct> for NotConstDestruct {
26+
const X: NotConstDestruct = NotConstDestruct;
2027
}
2128

2229
impl Bar<u32, ()> for () {}
23-
impl Bar<Vec<u32>, String> for String {}
30+
impl Bar<NotConstDestruct, NotConstDestruct> for NotConstDestruct {}
2431

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

3239
//~? WARN skipping const checks

0 commit comments

Comments
 (0)