From f5735d1fbe2738019ae81dd20bcb6d411d68c396 Mon Sep 17 00:00:00 2001 From: SlightlyOutOfPhase Date: Wed, 20 Nov 2019 12:04:14 -0500 Subject: [PATCH 1/9] Make `MaybeUninit::assume_init()` a `const fn` It can be, because it simply calls an intrinsics and then another function which is already a `const fn` (`ManuallyDrop::into_inner` specifically.) Example use case for this: https://github.com/slightlyoutofphase/staticvec/blob/addf203c0ec11905c403d25faacfdd29f9191a28/src/lib.rs#L50 If `assume_init` were `const fn`, then the linked `new` function could be also. --- src/libcore/mem/maybe_uninit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/mem/maybe_uninit.rs b/src/libcore/mem/maybe_uninit.rs index e05b40052ee76..378794a946972 100644 --- a/src/libcore/mem/maybe_uninit.rs +++ b/src/libcore/mem/maybe_uninit.rs @@ -482,7 +482,7 @@ impl MaybeUninit { #[stable(feature = "maybe_uninit", since = "1.36.0")] #[inline(always)] #[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "assume_init")] - pub unsafe fn assume_init(self) -> T { + pub unsafe const fn assume_init(self) -> T { intrinsics::panic_if_uninhabited::(); ManuallyDrop::into_inner(self.value) } From bc764fd45267536486e2b4a63b51c4c5a7746f0e Mon Sep 17 00:00:00 2001 From: SlightlyOutOfPhase Date: Wed, 20 Nov 2019 12:59:15 -0500 Subject: [PATCH 2/9] Fix order of `unsafe` and `const` Did not know it mattered TBH! --- src/libcore/mem/maybe_uninit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/mem/maybe_uninit.rs b/src/libcore/mem/maybe_uninit.rs index 378794a946972..0251ac0b8d78b 100644 --- a/src/libcore/mem/maybe_uninit.rs +++ b/src/libcore/mem/maybe_uninit.rs @@ -482,7 +482,7 @@ impl MaybeUninit { #[stable(feature = "maybe_uninit", since = "1.36.0")] #[inline(always)] #[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "assume_init")] - pub unsafe const fn assume_init(self) -> T { + pub const unsafe fn assume_init(self) -> T { intrinsics::panic_if_uninhabited::(); ManuallyDrop::into_inner(self.value) } From 04e1681bd199a5a4548ffa16ae2f21ab7ef043aa Mon Sep 17 00:00:00 2001 From: SlightlyOutOfPhase Date: Wed, 20 Nov 2019 14:32:43 -0500 Subject: [PATCH 3/9] I guess it's a different "intrinsics"? Hopefully this works. Based on the following playground link, panic_if_uninhabited *is* already valid in `const fn`, to be clear: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=8cbb9b4778e8f93ee1863c64995d15a7 --- src/libcore/mem/maybe_uninit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/mem/maybe_uninit.rs b/src/libcore/mem/maybe_uninit.rs index 0251ac0b8d78b..f40122d36417c 100644 --- a/src/libcore/mem/maybe_uninit.rs +++ b/src/libcore/mem/maybe_uninit.rs @@ -483,7 +483,7 @@ impl MaybeUninit { #[inline(always)] #[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "assume_init")] pub const unsafe fn assume_init(self) -> T { - intrinsics::panic_if_uninhabited::(); + crate::intrinsics::panic_if_uninhabited::(); ManuallyDrop::into_inner(self.value) } From ad9fc611cc63c4f3f4602b2bd39faa33ac5b8d21 Mon Sep 17 00:00:00 2001 From: SlightlyOutOfPhase Date: Wed, 20 Nov 2019 14:43:56 -0500 Subject: [PATCH 4/9] Try something else... Definitely confused about why `libcore` can't already use fully itself though. --- src/libcore/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index ea5536eb50cae..322cadeba599c 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -81,6 +81,7 @@ #![feature(extern_types)] #![feature(fundamental)] #![feature(intrinsics)] +#![feature(core_intrinsics)] #![feature(is_sorted)] #![feature(iter_once_with)] #![feature(lang_items)] From 6b28ffe67c39a54f6efb3e3c6364e3d1f713fc61 Mon Sep 17 00:00:00 2001 From: SlightlyOutOfPhase Date: Wed, 20 Nov 2019 15:53:27 -0500 Subject: [PATCH 5/9] Move #![feature(const_fn)] down the list, to see if it helps --- src/libcore/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 322cadeba599c..aa8d047f050ef 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -71,7 +71,6 @@ #![feature(bound_cloned)] #![feature(cfg_target_has_atomic)] #![feature(concat_idents)] -#![feature(const_fn)] #![feature(const_fn_union)] #![feature(const_generics)] #![feature(custom_inner_attributes)] @@ -81,7 +80,6 @@ #![feature(extern_types)] #![feature(fundamental)] #![feature(intrinsics)] -#![feature(core_intrinsics)] #![feature(is_sorted)] #![feature(iter_once_with)] #![feature(lang_items)] @@ -128,6 +126,7 @@ #![feature(maybe_uninit_slice)] #![feature(external_doc)] #![feature(associated_type_bounds)] +#![feature(const_fn)] #[prelude_import] #[allow(unused)] From 70fde3270c60399bc88eeca254a0decb2e54c523 Mon Sep 17 00:00:00 2001 From: SlightlyOutOfPhase Date: Wed, 20 Nov 2019 15:56:11 -0500 Subject: [PATCH 6/9] Remove `crate::` prefix again as it can't possibly be necessary --- src/libcore/mem/maybe_uninit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/mem/maybe_uninit.rs b/src/libcore/mem/maybe_uninit.rs index f40122d36417c..0251ac0b8d78b 100644 --- a/src/libcore/mem/maybe_uninit.rs +++ b/src/libcore/mem/maybe_uninit.rs @@ -483,7 +483,7 @@ impl MaybeUninit { #[inline(always)] #[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "assume_init")] pub const unsafe fn assume_init(self) -> T { - crate::intrinsics::panic_if_uninhabited::(); + intrinsics::panic_if_uninhabited::(); ManuallyDrop::into_inner(self.value) } From 44f99a0a1e4f31911e135cd97be4d65cd62f013d Mon Sep 17 00:00:00 2001 From: SlightlyOutOfPhase Date: Wed, 20 Nov 2019 17:17:41 -0500 Subject: [PATCH 7/9] Put #![feature(const_fn)] back where it goes first --- src/libcore/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index aa8d047f050ef..18b046e758a5b 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -70,6 +70,7 @@ #![feature(asm)] #![feature(bound_cloned)] #![feature(cfg_target_has_atomic)] +#![feature(const_fn)] #![feature(concat_idents)] #![feature(const_fn_union)] #![feature(const_generics)] @@ -126,7 +127,6 @@ #![feature(maybe_uninit_slice)] #![feature(external_doc)] #![feature(associated_type_bounds)] -#![feature(const_fn)] #[prelude_import] #[allow(unused)] From 68e1e77bfc6ad37da161bc64f700842a3de63619 Mon Sep 17 00:00:00 2001 From: SlightlyOutOfPhase Date: Wed, 20 Nov 2019 17:18:53 -0500 Subject: [PATCH 8/9] Actually put it back properly. --- src/libcore/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 18b046e758a5b..ea5536eb50cae 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -70,8 +70,8 @@ #![feature(asm)] #![feature(bound_cloned)] #![feature(cfg_target_has_atomic)] -#![feature(const_fn)] #![feature(concat_idents)] +#![feature(const_fn)] #![feature(const_fn_union)] #![feature(const_generics)] #![feature(custom_inner_attributes)] From 726a95774569890a8a323cf633608433293a7caa Mon Sep 17 00:00:00 2001 From: SlightlyOutOfPhase Date: Wed, 20 Nov 2019 17:22:28 -0500 Subject: [PATCH 9/9] Add `rustc_const_unstable`, properly I hope. --- src/libcore/mem/maybe_uninit.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/mem/maybe_uninit.rs b/src/libcore/mem/maybe_uninit.rs index 0251ac0b8d78b..2a3be50ca0087 100644 --- a/src/libcore/mem/maybe_uninit.rs +++ b/src/libcore/mem/maybe_uninit.rs @@ -482,6 +482,7 @@ impl MaybeUninit { #[stable(feature = "maybe_uninit", since = "1.36.0")] #[inline(always)] #[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "assume_init")] + #[rustc_const_unstable(feature = "const_assume_init")] pub const unsafe fn assume_init(self) -> T { intrinsics::panic_if_uninhabited::(); ManuallyDrop::into_inner(self.value)