From ff12204a42f343fc62ebbd330343d2fac16dd0af Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Sat, 11 Jul 2020 22:34:57 +0300 Subject: [PATCH 01/12] Default for arrays via const generics --- library/core/src/array/mod.rs | 68 +++++++++++++++++++++++++---------- library/core/src/lib.rs | 2 ++ library/core/tests/array.rs | 2 +- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 966272ca11549..3bffafd324467 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -353,28 +353,60 @@ impl Ord for [T; N] { } } -// The Default impls cannot be generated using the array_impls! macro because -// they require array literals. - -macro_rules! array_impl_default { - {$n:expr, $t:ident $($ts:ident)*} => { - #[stable(since = "1.4.0", feature = "array_default")] - impl Default for [T; $n] where T: Default { - fn default() -> [T; $n] { - [$t::default(), $($ts::default()),*] - } +/// This module implements `Default` for arrays. +mod default_impls { + // A trait implemented by all arrays which are either empty or contain a type implementing `Default`. + #[unstable( + feature = "array_default_internals", + reason = "implementation detail", + issue = "none" + )] + #[marker] + pub trait ArrayDefault {} + + #[unstable( + feature = "array_default_internals", + reason = "implementation detail", + issue = "none" + )] + impl ArrayDefault for [T; 0] {} + + #[unstable( + feature = "array_default_internals", + reason = "implementation detail", + issue = "none" + )] + impl ArrayDefault for [T; N] {} + + trait DefaultHack { + fn default_hack() -> Self; + } + + impl DefaultHack for T { + default fn default_hack() -> Self { + unreachable!(); } - array_impl_default!{($n - 1), $($ts)*} - }; - {$n:expr,} => { - #[stable(since = "1.4.0", feature = "array_default")] - impl Default for [T; $n] { - fn default() -> [T; $n] { [] } + } + + impl DefaultHack for T { + fn default_hack() -> Self { + Default::default() } - }; + } + #[stable(since = "1.4.0", feature = "array_default")] + impl Default for [T; N] + where + [T; N]: ArrayDefault, + { + fn default() -> [T; N] { + assert_eq!(std::mem::size_of::<[(); N]>(), 0); + // SAFETY: it is always valid to use `ceroed` for zero-sized value. + let arr: [(); N] = unsafe { std::mem::zeroed() }; + arr.map(DefaultHack::default_hack) + } + } } -array_impl_default! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T} #[lang = "array"] impl [T; N] { diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index c9a80b5bc7722..c4d92440067be 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -64,6 +64,7 @@ #![allow(explicit_outlives_requirements)] #![allow(incomplete_features)] #![feature(allow_internal_unstable)] +#![feature(array_default_internals)] #![feature(arbitrary_self_types)] #![feature(asm)] #![feature(cfg_target_has_atomic)] @@ -111,6 +112,7 @@ #![feature(lang_items)] #![feature(link_llvm_intrinsics)] #![feature(llvm_asm)] +#![feature(marker_trait_attr)] #![feature(negative_impls)] #![feature(never_type)] #![feature(nll)] diff --git a/library/core/tests/array.rs b/library/core/tests/array.rs index 89c2a969c28bb..31f385a95b281 100644 --- a/library/core/tests/array.rs +++ b/library/core/tests/array.rs @@ -288,7 +288,7 @@ fn array_default_impl_avoids_leaks_on_panic() { } } - let res = std::panic::catch_unwind(|| <[Bomb; 5]>::default()); + let res = std::panic::catch_unwind(|| <[Bomb; 35]>::default()); let panic_msg = match res { Ok(_) => unreachable!(), Err(p) => p.downcast::<&'static str>().unwrap(), From 9fe51df88db530b50a3903598fb8b682cc0560ca Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Thu, 20 Aug 2020 16:33:14 +0300 Subject: [PATCH 02/12] typo --- library/core/src/array/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 3bffafd324467..c04d1f1e76332 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -400,7 +400,7 @@ mod default_impls { { fn default() -> [T; N] { assert_eq!(std::mem::size_of::<[(); N]>(), 0); - // SAFETY: it is always valid to use `ceroed` for zero-sized value. + // SAFETY: it is always valid to use `zeroed` for zero-sized value. let arr: [(); N] = unsafe { std::mem::zeroed() }; arr.map(DefaultHack::default_hack) } From 53533018eaf4ad6c3e7a7ccd9fdfb8d9ad8a81f9 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Thu, 20 Aug 2020 16:33:39 +0300 Subject: [PATCH 03/12] fix error --- library/core/src/array/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index c04d1f1e76332..981c681018b71 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -402,7 +402,7 @@ mod default_impls { assert_eq!(std::mem::size_of::<[(); N]>(), 0); // SAFETY: it is always valid to use `zeroed` for zero-sized value. let arr: [(); N] = unsafe { std::mem::zeroed() }; - arr.map(DefaultHack::default_hack) + arr.map(|_unit| DefaultHack::default_hack()) } } } From bfa075c24983be3007055f6d399003d21b87b5f9 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Thu, 20 Aug 2020 16:43:22 +0300 Subject: [PATCH 04/12] not bootstrap --- library/core/src/array/mod.rs | 28 ++++++++++++++++++++++++++-- library/core/src/lib.rs | 2 +- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 981c681018b71..2d57a8d660c0e 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -354,6 +354,7 @@ impl Ord for [T; N] { } /// This module implements `Default` for arrays. +#[cfg(not(bootstrap))] mod default_impls { // A trait implemented by all arrays which are either empty or contain a type implementing `Default`. #[unstable( @@ -399,14 +400,37 @@ mod default_impls { [T; N]: ArrayDefault, { fn default() -> [T; N] { - assert_eq!(std::mem::size_of::<[(); N]>(), 0); + assert_eq!(crate::mem::size_of::<[(); N]>(), 0); // SAFETY: it is always valid to use `zeroed` for zero-sized value. - let arr: [(); N] = unsafe { std::mem::zeroed() }; + let arr: [(); N] = unsafe { crate::mem::zeroed() }; arr.map(|_unit| DefaultHack::default_hack()) } } } +#[cfg(bootstrap)] +mod default_impls { + macro_rules! array_impl_default { + {$n:expr, $t:ident $($ts:ident)*} => { + #[stable(since = "1.4.0", feature = "array_default")] + impl Default for [T; $n] where T: Default { + fn default() -> [T; $n] { + [$t::default(), $($ts::default()),*] + } + } + array_impl_default!{($n - 1), $($ts)*} + }; + {$n:expr,} => { + #[stable(since = "1.4.0", feature = "array_default")] + impl Default for [T; $n] { + fn default() -> [T; $n] { [] } + } + }; + } + + array_impl_default! {16, T T T T T T T T T T T T T T T T} +} + #[lang = "array"] impl [T; N] { diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index c4d92440067be..94b628abb169f 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -64,7 +64,7 @@ #![allow(explicit_outlives_requirements)] #![allow(incomplete_features)] #![feature(allow_internal_unstable)] -#![feature(array_default_internals)] +#![cfg_attr(not(bootstrap), feature(array_default_internals))] #![feature(arbitrary_self_types)] #![feature(asm)] #![feature(cfg_target_has_atomic)] From b73556bf4563ec90b6cf85be23a6e573aad2bba4 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Thu, 20 Aug 2020 16:44:53 +0300 Subject: [PATCH 05/12] unopt --- library/core/src/array/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 2d57a8d660c0e..c5a59b1e925d9 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -428,7 +428,7 @@ mod default_impls { }; } - array_impl_default! {16, T T T T T T T T T T T T T T T T} + array_impl_default! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T} } From e37314417ac3df0a33091570059a3ac10c375314 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Thu, 20 Aug 2020 17:05:13 +0300 Subject: [PATCH 06/12] fix tidy --- library/core/src/array/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index c5a59b1e925d9..1e0ebd02777be 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -427,7 +427,7 @@ mod default_impls { } }; } - + array_impl_default! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T} } From 69421114a982e75f543480fa1be74a58dd348d52 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Thu, 20 Aug 2020 17:13:28 +0300 Subject: [PATCH 07/12] no need to go unsafe lol --- library/core/src/array/mod.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 1e0ebd02777be..c115153ed78f0 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -400,10 +400,7 @@ mod default_impls { [T; N]: ArrayDefault, { fn default() -> [T; N] { - assert_eq!(crate::mem::size_of::<[(); N]>(), 0); - // SAFETY: it is always valid to use `zeroed` for zero-sized value. - let arr: [(); N] = unsafe { crate::mem::zeroed() }; - arr.map(|_unit| DefaultHack::default_hack()) + [(); N].map(|_unit| DefaultHack::default_hack()) } } } From 6ddaadd642549921f18632fa761f101119443476 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Thu, 20 Aug 2020 17:27:58 +0300 Subject: [PATCH 08/12] fmt --- library/core/src/array/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index c115153ed78f0..16f6ab1451c49 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -428,7 +428,6 @@ mod default_impls { array_impl_default! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T} } - #[lang = "array"] impl [T; N] { /// Returns an array of the same size as `self`, with function `f` applied to each element From 55b208f6e957e8327f1a837bf0a0a97ac13148e0 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Sun, 11 Oct 2020 00:58:41 +0300 Subject: [PATCH 09/12] experiment --- library/core/src/array/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 16f6ab1451c49..8ed8b57fc30f2 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -354,7 +354,7 @@ impl Ord for [T; N] { } /// This module implements `Default` for arrays. -#[cfg(not(bootstrap))] +//#[cfg(not(bootstrap))] mod default_impls { // A trait implemented by all arrays which are either empty or contain a type implementing `Default`. #[unstable( @@ -405,7 +405,7 @@ mod default_impls { } } -#[cfg(bootstrap)] +/*#[cfg(bootstrap)] mod default_impls { macro_rules! array_impl_default { {$n:expr, $t:ident $($ts:ident)*} => { @@ -426,7 +426,7 @@ mod default_impls { } array_impl_default! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T} -} +}*/ #[lang = "array"] impl [T; N] { From d2ca70bc14424534e19154f4e7d6cbc783635f3e Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Sun, 11 Oct 2020 01:03:29 +0300 Subject: [PATCH 10/12] unfortunate --- library/core/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 94b628abb169f..90dde3ec01525 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -124,7 +124,7 @@ #![feature(repr_simd, platform_intrinsics)] #![feature(rustc_attrs)] #![feature(simd_ffi)] -#![feature(min_specialization)] +#![feature(specialization)] #![feature(staged_api)] #![feature(std_internals)] #![feature(stmt_expr_attributes)] From fd23a86aed70cd569143eb9dea7316cfe2392240 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Sun, 11 Oct 2020 01:16:18 +0300 Subject: [PATCH 11/12] rm garbage --- library/core/src/array/mod.rs | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 8ed8b57fc30f2..026b22d38609e 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -354,7 +354,6 @@ impl Ord for [T; N] { } /// This module implements `Default` for arrays. -//#[cfg(not(bootstrap))] mod default_impls { // A trait implemented by all arrays which are either empty or contain a type implementing `Default`. #[unstable( @@ -405,29 +404,6 @@ mod default_impls { } } -/*#[cfg(bootstrap)] -mod default_impls { - macro_rules! array_impl_default { - {$n:expr, $t:ident $($ts:ident)*} => { - #[stable(since = "1.4.0", feature = "array_default")] - impl Default for [T; $n] where T: Default { - fn default() -> [T; $n] { - [$t::default(), $($ts::default()),*] - } - } - array_impl_default!{($n - 1), $($ts)*} - }; - {$n:expr,} => { - #[stable(since = "1.4.0", feature = "array_default")] - impl Default for [T; $n] { - fn default() -> [T; $n] { [] } - } - }; - } - - array_impl_default! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T} -}*/ - #[lang = "array"] impl [T; N] { /// Returns an array of the same size as `self`, with function `f` applied to each element From f786fa0e59fb5c90fc4d6876cde5593c7ba034cb Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Sun, 11 Oct 2020 02:24:41 +0300 Subject: [PATCH 12/12] min spec --- library/core/src/default.rs | 2 ++ library/core/src/lib.rs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/library/core/src/default.rs b/library/core/src/default.rs index 9a8d65cd4e06b..9240c9764996a 100644 --- a/library/core/src/default.rs +++ b/library/core/src/default.rs @@ -81,6 +81,8 @@ /// } /// ``` #[stable(feature = "rust1", since = "1.0.0")] +// specialization is used in array impls +#[rustc_specialization_trait] pub trait Default: Sized { /// Returns the "default value" for a type. /// diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 90dde3ec01525..94b628abb169f 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -124,7 +124,7 @@ #![feature(repr_simd, platform_intrinsics)] #![feature(rustc_attrs)] #![feature(simd_ffi)] -#![feature(specialization)] +#![feature(min_specialization)] #![feature(staged_api)] #![feature(std_internals)] #![feature(stmt_expr_attributes)]