From 62722735fb62c2f93656a012c29310155de35467 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 31 Jan 2020 00:30:55 -0500 Subject: [PATCH 1/9] impl From<[T; N]> for Vec --- src/liballoc/vec.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index f661b830428..c8ff4a41536 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2397,6 +2397,13 @@ impl From<&mut [T]> for Vec { } } +#[stable(feature = "vec_from_array", since = "1.42.0")] +impl From<[T; N]> for Vec { + fn from(arr: [T; N]) -> Vec { + <[T]>::into_vec(box arr) + } +} + #[stable(feature = "vec_from_cow_slice", since = "1.14.0")] impl<'a, T> From> for Vec where From daeb8ece8c930174baa874aa3f2f278fbec9637d Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 31 Jan 2020 10:46:08 -0500 Subject: [PATCH 2/9] fix error compiling stage2 Co-Authored-By: lzutao --- src/liballoc/vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index c8ff4a41536..f557d056dc8 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2399,7 +2399,7 @@ impl From<&mut [T]> for Vec { #[stable(feature = "vec_from_array", since = "1.42.0")] impl From<[T; N]> for Vec { - fn from(arr: [T; N]) -> Vec { + fn from(arr: [T; N]) -> Self { <[T]>::into_vec(box arr) } } From f267d9dc191debfef74cc211dbca3d286b51f761 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 31 Jan 2020 11:41:19 -0500 Subject: [PATCH 3/9] limit From impl to LengthAtMost32 Co-Authored-By: Mazdak Farrokhzad --- src/liballoc/vec.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index f557d056dc8..8fede31847c 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2398,7 +2398,10 @@ impl From<&mut [T]> for Vec { } #[stable(feature = "vec_from_array", since = "1.42.0")] -impl From<[T; N]> for Vec { +impl From<[T; N]> for Vec +where + [T; N]: LengthAtMost32, +{ fn from(arr: [T; N]) -> Self { <[T]>::into_vec(box arr) } From e3d5eaf2bba89db8359393ac7656db19cb421fb5 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 31 Jan 2020 11:44:58 -0500 Subject: [PATCH 4/9] add ui-tests --- .../array-impls/alloc-traits-impls-length-32.rs | 4 ++++ .../array-impls/alloc-types-no-impls-length-33.rs | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/test/ui/const-generics/array-impls/alloc-traits-impls-length-32.rs b/src/test/ui/const-generics/array-impls/alloc-traits-impls-length-32.rs index db941a440e1..0d0765e971d 100644 --- a/src/test/ui/const-generics/array-impls/alloc-traits-impls-length-32.rs +++ b/src/test/ui/const-generics/array-impls/alloc-traits-impls-length-32.rs @@ -14,6 +14,10 @@ where Vec::::new() } +pub fn yes_array_into_vec() -> Vec { + [].into() +} + use std::collections::VecDeque; pub fn yes_vecdeque_partial_eq_array() -> impl PartialEq<[B; 32]> diff --git a/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.rs b/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.rs index 3a23b9b5832..92dff7e0792 100644 --- a/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.rs +++ b/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.rs @@ -2,6 +2,11 @@ use std::{convert::TryFrom, rc::Rc, sync::Arc}; +pub fn no_vec() { + let v: Vec<_> = [0; 33].into(); + //~^ ERROR the trait bound `std::vec::Vec: std::convert::From<[u8; 33]>` is not satisfied +} + pub fn no_box() { let boxed_slice = Box::new([0; 33]) as Box<[i32]>; let boxed_array = >::try_from(boxed_slice); From ba46b61bbcd0d2860105c429cf73254c388e9118 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 31 Jan 2020 13:59:57 -0500 Subject: [PATCH 5/9] bless UI tests --- .../alloc-types-no-impls-length-33.rs | 2 +- .../alloc-types-no-impls-length-33.stderr | 23 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.rs b/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.rs index 92dff7e0792..4b195f3a06e 100644 --- a/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.rs +++ b/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.rs @@ -4,7 +4,7 @@ use std::{convert::TryFrom, rc::Rc, sync::Arc}; pub fn no_vec() { let v: Vec<_> = [0; 33].into(); - //~^ ERROR the trait bound `std::vec::Vec: std::convert::From<[u8; 33]>` is not satisfied + //~^ ERROR arrays only have std trait implementations for lengths 0..=32 } pub fn no_box() { diff --git a/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.stderr b/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.stderr index 193fb4c4374..d795840551c 100644 --- a/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.stderr +++ b/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.stderr @@ -1,5 +1,14 @@ +error[E0277]: arrays only have std trait implementations for lengths 0..=32 + --> $DIR/alloc-types-no-impls-length-33.rs:6:29 + | +LL | let v: Vec<_> = [0; 33].into(); + | ^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[{integer}; 33]` + | + = note: required because of the requirements on the impl of `std::convert::From<[{integer}; 33]>` for `std::vec::Vec<{integer}>` + = note: required because of the requirements on the impl of `std::convert::Into>` for `[{integer}; 33]` + error[E0277]: the trait bound `std::boxed::Box<[i32; 33]>: std::convert::From>` is not satisfied - --> $DIR/alloc-types-no-impls-length-33.rs:7:23 + --> $DIR/alloc-types-no-impls-length-33.rs:12:23 | LL | let boxed_array = >::try_from(boxed_slice); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From>` is not implemented for `std::boxed::Box<[i32; 33]>` @@ -14,7 +23,7 @@ LL | let boxed_array = >::try_from(boxed_slice); = note: required because of the requirements on the impl of `std::convert::TryFrom>` for `std::boxed::Box<[i32; 33]>` error[E0277]: the trait bound `std::boxed::Box<[i32; 33]>: std::convert::TryFrom>` is not satisfied - --> $DIR/alloc-types-no-impls-length-33.rs:7:23 + --> $DIR/alloc-types-no-impls-length-33.rs:12:23 | LL | let boxed_array = >::try_from(boxed_slice); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::TryFrom>` is not implemented for `std::boxed::Box<[i32; 33]>` @@ -23,7 +32,7 @@ LL | let boxed_array = >::try_from(boxed_slice); as std::convert::TryFrom>> error[E0277]: the trait bound `std::rc::Rc<[i32; 33]>: std::convert::From>` is not satisfied - --> $DIR/alloc-types-no-impls-length-33.rs:14:23 + --> $DIR/alloc-types-no-impls-length-33.rs:19:23 | LL | let boxed_array = >::try_from(boxed_slice); | ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From>` is not implemented for `std::rc::Rc<[i32; 33]>` @@ -38,7 +47,7 @@ LL | let boxed_array = >::try_from(boxed_slice); = note: required because of the requirements on the impl of `std::convert::TryFrom>` for `std::rc::Rc<[i32; 33]>` error[E0277]: the trait bound `std::rc::Rc<[i32; 33]>: std::convert::TryFrom>` is not satisfied - --> $DIR/alloc-types-no-impls-length-33.rs:14:23 + --> $DIR/alloc-types-no-impls-length-33.rs:19:23 | LL | let boxed_array = >::try_from(boxed_slice); | ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::TryFrom>` is not implemented for `std::rc::Rc<[i32; 33]>` @@ -47,7 +56,7 @@ LL | let boxed_array = >::try_from(boxed_slice); as std::convert::TryFrom>> error[E0277]: the trait bound `std::sync::Arc<[i32; 33]>: std::convert::From>` is not satisfied - --> $DIR/alloc-types-no-impls-length-33.rs:21:23 + --> $DIR/alloc-types-no-impls-length-33.rs:26:23 | LL | let boxed_array = >::try_from(boxed_slice); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From>` is not implemented for `std::sync::Arc<[i32; 33]>` @@ -62,7 +71,7 @@ LL | let boxed_array = >::try_from(boxed_slice); = note: required because of the requirements on the impl of `std::convert::TryFrom>` for `std::sync::Arc<[i32; 33]>` error[E0277]: the trait bound `std::sync::Arc<[i32; 33]>: std::convert::TryFrom>` is not satisfied - --> $DIR/alloc-types-no-impls-length-33.rs:21:23 + --> $DIR/alloc-types-no-impls-length-33.rs:26:23 | LL | let boxed_array = >::try_from(boxed_slice); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::TryFrom>` is not implemented for `std::sync::Arc<[i32; 33]>` @@ -70,6 +79,6 @@ LL | let boxed_array = >::try_from(boxed_slice); = help: the following implementations were found: as std::convert::TryFrom>> -error: aborting due to 6 previous errors +error: aborting due to 7 previous errors For more information about this error, try `rustc --explain E0277`. From 96794d86f127dd409760765a64a36c07d9ed585f Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 1 Feb 2020 13:15:50 +0000 Subject: [PATCH 6/9] fix test failure --- src/liballoc/vec.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 8fede31847c..d5339453f81 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2402,8 +2402,13 @@ impl From<[T; N]> for Vec where [T; N]: LengthAtMost32, { - fn from(arr: [T; N]) -> Self { - <[T]>::into_vec(box arr) + #[cfg(not(test))] + fn from(s: [T; N]) -> Vec { + (box s as Box<[T]>).into_vec() + } + #[cfg(test)] + fn from(s: [T; N]) -> Vec { + crate::slice::into_vec(box s) } } From 1ac4a461425b7a2e29aeb3c2b2cae944f8cbcc77 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 1 Feb 2020 13:35:26 +0000 Subject: [PATCH 7/9] make the impl a little prettier --- src/liballoc/vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index d5339453f81..921c0427959 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2404,7 +2404,7 @@ where { #[cfg(not(test))] fn from(s: [T; N]) -> Vec { - (box s as Box<[T]>).into_vec() + <[T]>::into_vec(box s) } #[cfg(test)] fn from(s: [T; N]) -> Vec { From 8212584c9ecdbd91736fc7a480fe3b7409b73477 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Tue, 10 Mar 2020 23:13:56 +0000 Subject: [PATCH 8/9] Bump release cutoff --- src/liballoc/vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 921c0427959..9f30ca29c03 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2397,7 +2397,7 @@ impl From<&mut [T]> for Vec { } } -#[stable(feature = "vec_from_array", since = "1.42.0")] +#[stable(feature = "vec_from_array", since = "1.44.0")] impl From<[T; N]> for Vec where [T; N]: LengthAtMost32, From 3477e67a92878adae48b975915cb7a5c21026cd4 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Tue, 10 Mar 2020 23:49:45 +0000 Subject: [PATCH 9/9] Allow vec.rs to be over 3000 lines :( --- src/liballoc/vec.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 9f30ca29c03..b9bd0dd8a1e 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -1,3 +1,4 @@ +// ignore-tidy-filelength //! A contiguous growable array type with heap-allocated contents, written //! `Vec`. //!