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

Make mem::uninitialized() and mem::zeroed() const fn #1633

Closed
wants to merge 1 commit into from

Conversation

Amanieu
Copy link
Member

@Amanieu Amanieu commented May 27, 2016

@nagisa
Copy link
Member

nagisa commented May 27, 2016

I’m inclined to not do anything like this before we get const fn implemented proper, not hacked onto the ye olde const_eval.

@Amanieu
Copy link
Member Author

Amanieu commented May 29, 2016

Actually I think there is a better way of doing this by using unions:

union MyUnion<T> {
    data: T,
    no_data: (),
}

static X: MyUnion<T> = MyUnion { no_data: () };

So maybe this RFC isn't needed after all...

@nrc nrc added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label May 30, 2016
@Ericson2314
Copy link
Contributor

@nagisa I'll agree based on how close miri is :).
@Amanieu I rather only use unions for FFI. They add zero safety here over the original RFC. And practically, I think unions are a longer way off.

@Amanieu
Copy link
Member Author

Amanieu commented Jun 2, 2016

The advantage of using unions is that it will cleanly handle Some(uninitialized) since the no_data in the union inhibits enum layout optimizations.

@Ericson2314
Copy link
Contributor

Hmm? Aren't we getting rid of the option for the proposed usecase? What layout optimizations are there without a tag? I am really confused by that comment.

@rkjnsn
Copy link
Contributor

rkjnsn commented Jun 16, 2016

@Ericson2314 My understanding is that with this RFC as proposed, it would be possible to do something like this:

static X: Option<&T> = unsafe { Some(std::mem::zeroed()) };

Because of the enum layout optimization, the final value of X at runtime would be None. The compiler would have to deal with this during compile-time constant evaluation.

Without this RFC, but with unions, one would instead write

static X: Option<MyUnion<&T>> = Some(MyUnion { no_data: () });

The union would inhibit the layout optimization, so the Option would have a tag, avoiding the corner case.

(Note that I don't think anyone would write either of these in actual code. The point is to demonstrate that the former introduces a compile-time corner case that the latter does not.)

@alexcrichton
Copy link
Member

Thanks for the RFC @Amanieu! The libs team discussed this today and the conclusion was that this likely doesn't need an RFC, just a compiler implementation. That is, from the libs perspective, we're fine doing this any time. In light of that I'm going to close, but PRs are of course always welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants