-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Should repr(transparent)
require *inhabited* 1-ZSTs?
#96921
Comments
It is certainly correct that every value of However, transmuting |
IMO it makes sense to keep it for weird shenanigans like in the hyper issue that Ralf just linked here. If you want to "disable" an enum variant (which you have control over, and has a specified repr), without changing the shape of the enum, nor its size, it's more or less the only way. I understand that we want to make uninhabited types with inhabited fields be zero-sized, but maybe we should specifically avoid that for |
Note that this issue is about structs. We don't do the layout optimization (of making an uninhabited struct have size 0) on structs because it is in conflict with partial initialization. For enums, last time this came up, since enums cannot be partially initialized, most people seemed to be in favor of allowing more aggressive layout optimizations there. We might even already do those, I am not sure. Also see #93032. |
I thought the size needed to be preserved because of stuff like |
That can be (and currently AFAIK is) compiled with temporaries. But anyway, that's a topic for #93032. I agree |
I don't understand what you mean by this. Just because something is a |
You can't publicly safely transmute, no. |
I see your point, and I don't mind either way, but people using |
https://lib.rs/crates/ref-cast comes to mind, here. But it looks like it's sound by essentially allowing only |
can someone clarify if there are any known cases of people using this? What is this hyper example? (cc @seanmonstar or @erickt -- is hyper using "uninhabitable" never types in combination with My take is that this should be a warning for sure, but possibly an error -- it doesn't seem a priori invalid to me, but it's definitely suspicious. If you have an uninhabited type, then the function ought never to return, but in that case, why do you care about the type being transparent, which only affects functions that return? Could be that it arises because of generic or macro-created code, of course, where sometimes the fn returns and sometimes it doesn't. |
It seems it is used in hyper currently, to create a type that can satisfy some trait bounds, but that is otherwise never actually constructed. It was linked further up, here's a PR that discussed it in hyper: hyperium/hyper#2831 |
We talked about this in today's @rust-lang/lang meeting. We probably don't want to break this pattern since it's used, but it may make sense to add a lint. |
I don't think anyone but me thought of this stuff, and Hyper is a very alive project so if you feel like we should just do otherwise, that's possible too. |
rust-lang/unsafe-code-guidelines#337 pointed out that the following code compiles:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=67b6fd93f899912b2aec43b4cf362f31
Does that make sense? Should we change the rule here, either retroactively or in a new edition?
The text was updated successfully, but these errors were encountered: