-
Notifications
You must be signed in to change notification settings - Fork 13.3k
It should be possible to get a mutable reference to a ZST literal #103821
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
Comments
I had a similar issue, it also works with shorter hack
that produces almost the same assembly as another accepted example
|
@rustbot label +A-zst +T-types |
This seems to work well for ZSTs. fn get_mut_ref<'a>() -> &'a mut () { Box::leak(Box::new(())) } Does not actually invoke the allocator. Instead returns a properly aligned dangling ref. |
That this works for empty arrays is maintained for backwards compatibility, since this was allowed in some very early version of promotion. I think we could plausibly allow this for more / all ZST. Cc @oli-obk |
yea, seems ok to add more |
Does anyone have a sense of what it would take to implement this (technically)? Also, from a process perspective, I assume this is simple enough that it could just be a PR + FCP (rather than a full RFC)? |
The check for this is here: rust/compiler/rustc_const_eval/src/transform/promote_consts.rs Lines 470 to 484 in 866710c
It needs to be adjusted to detect more zero-sized types. This is polymorphic code so we can't just compute the layout, we have to actually traverse the type. Not sure if such an analysis to predict whether a type is a ZST already exists. And yeah I assume FCP will suffice. |
Once the naive layout logic lands again, we could probably use it for this, too |
Naive layout was part of #113166 but the PR got reverted |
Awesome, thanks! Is re-landing naive layout tracked anywhere? |
I don't know what @moulins plan is, no one else is working on it. |
The following code is legal:
However, if we instead use
()
, it's no longer legal:We get this error:
Is there a reason that the former is legal while the latter is not? I would expect the latter to be legal - in the general case, it'd be great if it were legal for any ZST, although I'm sure that's more complicated (especially in a generic context).
On Fuchsia, we're currently using this hack to work around this limitation.
The text was updated successfully, but these errors were encountered: