Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use const array repeat expressions for uninit_array #62799

Merged
merged 4 commits into from Jul 22, 2019

Conversation

@RalfJung
Copy link
Member

commented Jul 19, 2019

With a first implementation of #49147 having landed, we can make this macro nicer and phase it out with the next bootstrap bump.

However, to make this work, we have to mark MaybeUninit::uninit() as promotable. I do feel uneasy about promoting stuff involving uninitialized memory, but OTOH no operation on MaybeUninit is promotable, so maybe this is okay?

r? @oli-obk @eddyb

@@ -248,6 +248,7 @@ impl<T> MaybeUninit<T> {
/// [type]: union.MaybeUninit.html
#[stable(feature = "maybe_uninit", since = "1.36.0")]
#[inline(always)]
#[rustc_promotable]

This comment has been minimized.

Copy link
@bjorn3

bjorn3 Jul 19, 2019

Contributor

Does this have support for feature gating?

This comment has been minimized.

Copy link
@RalfJung

RalfJung Jul 19, 2019

Author Member

I don't think so. @oli-obk ?

This comment has been minimized.

Copy link
@bjorn3

bjorn3 Jul 19, 2019

Contributor

Explicitly puting the MaybeUninit::uninit() seems to work. That would make this unnecessary.

https://play.integer32.com/?version=stable&mode=debug&edition=2018&gist=71886586e61fc9a677ce120c825c4aae

This comment has been minimized.

Copy link
@RalfJung

RalfJung Jul 20, 2019

Author Member

That playground does not load here? I just get a blank page. The one at https://play.rust-lang.org/ works.

Can you paste the code you are suggesting here in GitHub?

This comment has been minimized.

Copy link
@bjorn3

bjorn3 Jul 20, 2019

Contributor

Yeah the playground at integer32 broken for me too.

The code was:

use std::mem::MaybeUninit;

fn main() {
    const A: MaybeUninit<u8> = MaybeUninit::uninit();
    let a_arr = [A; 2];
}

But looking at the optimized llvm ir, it seems that llvm emits a memset with zero, instead of keeping it uninitialized in case of:

use std::mem::MaybeUninit;

pub fn abc() -> [MaybeUninit<u8>; 16] {
    const A: MaybeUninit<u8> = MaybeUninit::uninit();
    let a_arr = [A; 16]; //unsafe { std::mem::uninitialized() };
    a_arr
}

This comment has been minimized.

Copy link
@RalfJung

RalfJung Jul 20, 2019

Author Member

MaybeUninit<u8> is Copy, so this does not really test the new code paths anyway.

But yes, you can probably "trigger" promotion as usual with a manual const.

@Centril

This comment has been minimized.

Copy link
Member

commented Jul 21, 2019

However, to make this work, we have to mark MaybeUninit::uninit() as promotable. I do feel uneasy about promoting stuff involving uninitialized memory, but OTOH no operation on MaybeUninit is promotable, so maybe this is okay?

This should not be necessary. You can use a perma-unstable associated constant instead:

impl<T> MaybeUninit<T> {
    #[unstable(
        feature = "internal_uninit_const",
        issue = "0",
        reason = "hack to work around promotability",
    )]
    pub const UNINIT: Self = Self::uninit();
}

fn foo<T>() {
    let x = [MaybeUninit::<T>::UNINIT; 5]; // OK.
}

(I checked that this idea works on master as of the time of writing this comment.)

@eddyb

This comment has been minimized.

Copy link
Member

commented Jul 21, 2019

@Centril The goal is to remove the macro ASAP.

On Zulip I suggested we treat zero-argument const fns like consts and always promote them.

@RalfJung

This comment has been minimized.

Copy link
Member Author

commented Jul 21, 2019

Ah yes an explicit constant should do it, true. That won't help users outside libstd but that is a separate concern.

On Zulip I suggested we treat zero-argument const fns like consts and always promote them.

I don't think I am a fan of expanding the scope of promotion in any way. const and nullary const fn are not equivalent in every way. For example, we do a validity check on the value of a const, but we don't do anything like that for nullary const fn. The rules for what code is allowed are also not the same for both.

@RalfJung

This comment has been minimized.

Copy link
Member Author

commented Jul 21, 2019

Okay, this no longer makes MaybeUninit::uninit promotable.

@rust-highfive

This comment was marked as resolved.

Copy link
Collaborator

commented Jul 21, 2019

The job x86_64-gnu-llvm-6.0 of your PR failed (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
2019-07-21T10:30:07.7350600Z ##[command]git remote add origin https://github.com/rust-lang/rust
2019-07-21T10:30:08.3137296Z ##[command]git config gc.auto 0
2019-07-21T10:30:08.3143749Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2019-07-21T10:30:08.3149746Z ##[command]git config --get-all http.proxy
2019-07-21T10:30:08.3152904Z ##[command]git -c http.extraheader="AUTHORIZATION: basic ***" fetch --force --tags --prune --progress --no-recurse-submodules --depth=2 origin +refs/heads/*:refs/remotes/origin/* +refs/pull/62799/merge:refs/remotes/pull/62799/merge
---
2019-07-21T10:30:42.8072851Z do so (now or later) by using -b with the checkout command again. Example:
2019-07-21T10:30:42.8072883Z 
2019-07-21T10:30:42.8073334Z   git checkout -b <new-branch-name>
2019-07-21T10:30:42.8073365Z 
2019-07-21T10:30:42.8073441Z HEAD is now at 1b45b5e3c Merge 4b47e78a16524ed4878bfffaaf60c32bb18d88ae into 1301422a6c2e8916560b8cc2f0564f38d8858a75
2019-07-21T10:30:42.8207521Z ##[section]Starting: Collect CPU-usage statistics in the background
2019-07-21T10:30:42.8209720Z ==============================================================================
2019-07-21T10:30:42.8209764Z Task         : Bash
2019-07-21T10:30:42.8209814Z Description  : Run a Bash script on macOS, Linux, or Windows
---
2019-07-21T10:36:59.3365617Z     Finished release [optimized] target(s) in 1m 29s
2019-07-21T10:36:59.3438336Z tidy check
2019-07-21T10:37:00.2715485Z * 577 error codes
2019-07-21T10:37:00.2715595Z * highest error code: E0732
2019-07-21T10:37:00.3128062Z tidy error: /checkout/src/libcore/mem/maybe_uninit.rs:256: malformed stability attribute: missing `feature` key
2019-07-21T10:37:01.1369702Z some tidy checks failed
2019-07-21T10:37:01.1371237Z 
2019-07-21T10:37:01.1371237Z 
2019-07-21T10:37:01.1372279Z command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/tidy" "/checkout/src" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "--no-vendor"
2019-07-21T10:37:01.1372860Z 
2019-07-21T10:37:01.1373042Z 
2019-07-21T10:37:01.1376215Z failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test src/tools/tidy
2019-07-21T10:37:01.1376733Z Build completed unsuccessfully in 0:01:32
2019-07-21T10:37:01.1376733Z Build completed unsuccessfully in 0:01:32
2019-07-21T10:37:02.7082063Z ##[error]Bash exited with code '1'.
2019-07-21T10:37:02.7118602Z ##[section]Starting: Checkout
2019-07-21T10:37:02.7120699Z ==============================================================================
2019-07-21T10:37:02.7120757Z Task         : Get sources
2019-07-21T10:37:02.7120805Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@RalfJung

This comment has been minimized.

Copy link
Member Author

commented Jul 21, 2019

tidy error: /checkout/src/libcore/mem/maybe_uninit.rs:256: malformed stability attribute: missing feature key

But there is a feature key. Is tidy's parser too short-sighted to handle multi-line attributes?

@eddyb

This comment has been minimized.

Copy link
Member

commented Jul 21, 2019

For example, we do a validity check on the value of a const, but we don't do anything like that for nullary const fn. The rules for what code is allowed are also not the same for both.

No reason we couldn't! Or even for any sub-computation that doesn't depend on arguments.
EDIT: would be interesting to do some crater run to see if anyone wrote some nullary const fns that wouldn't pass const validation.

@Centril

This comment has been minimized.

Copy link
Member

commented Jul 21, 2019

That won't help users outside libstd but that is a separate concern.

To work around the lack of {-# LANGUAGE ScopedTypeVariables #-} when in a generic setting, other crates can use a slightly modified version of the hack which involves an extension trait that contains the associated constant. It's not exactly pretty, but it will get the job done 😝.

@Centril

This comment has been minimized.

Copy link
Member

commented Jul 21, 2019

I'm happy with this so r=me rollup if you like or wait for Oliver's review otherwise, up to you. ;)

@RalfJung

This comment has been minimized.

Copy link
Member Author

commented Jul 21, 2019

@bors r=Centril rollup

@bors

This comment has been minimized.

Copy link
Contributor

commented Jul 21, 2019

📌 Commit f3abbf7 has been approved by Centril

@bors

This comment has been minimized.

Copy link
Contributor

commented Jul 21, 2019

🌲 The tree is currently closed for pull requests below priority 1500, this pull request will be tested once the tree is reopened

Centril added a commit to Centril/rust that referenced this pull request Jul 22, 2019

Rollup merge of rust-lang#62799 - RalfJung:uninit-array, r=Centril
use const array repeat expressions for uninit_array

With a first implementation of rust-lang#49147 having landed, we can make this macro nicer and phase it out with the next bootstrap bump.

However, to make this work, we have to mark `MaybeUninit::uninit()` as promotable. I do feel uneasy about promoting stuff involving uninitialized memory, but OTOH no *operation* on `MaybeUninit` is promotable, so maybe this is okay?

r? @oli-obk @eddyb

bors added a commit that referenced this pull request Jul 22, 2019

Auto merge of #62873 - Centril:rollup-ncnuelj, r=Centril
Rollup of 14 pull requests

Successful merges:

 - #62709 (Test that maplike FromIter satisfies uniqueness)
 - #62713 (Stabilize <*mut _>::cast and <*const _>::cast)
 - #62746 ( do not use assume_init in std::io)
 - #62787 (Fix typo in src/libstd/net/udp.rs doc comment)
 - #62788 (normalize use of backticks in compiler messages for libcore/ptr)
 - #62799 (use const array repeat expressions for uninit_array)
 - #62810 (normalize use of backticks in compiler messages for librustc_lint)
 - #62812 (normalize use of backticks in compiler messages for librustc_metadata)
 - #62832 (normalize use of backticks in compiler messages for librustc_incremental)
 - #62845 (read: fix doc comment)
 - #62853 (normalize use of backticks in compiler messages for librustc/hir)
 - #62854 (Fix typo in Unicode character name)
 - #62858 (Change wrong variable name.)
 - #62870 (fix lexing of comments with many \r)

Failed merges:

r? @ghost

@bors bors merged commit f3abbf7 into rust-lang:master Jul 22, 2019

4 checks passed

pr Build #20190721.18 succeeded
Details
pr (Linux mingw-check) Linux mingw-check succeeded
Details
pr (Linux x86_64-gnu-llvm-6.0) Linux x86_64-gnu-llvm-6.0 succeeded
Details
pr (LinuxTools) LinuxTools succeeded
Details

@RalfJung RalfJung deleted the RalfJung:uninit-array branch Jul 23, 2019

@Centril Centril referenced this pull request Jul 27, 2019
2 of 4 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.