Passing safe references to empty enums
See original GitHub issueOn IRC, @mystor asked me whether it would be illegal to pass around &SafeType
or &mut SafeType
given this definition:
enum Impossible {}
#[repr(C)]
pub struct SafeType {
_prohibit_constructor: Impossible
}
In particular, is it ok to have an empty enum “by-value” in a struct in this fashion?
Issue Analytics
- State:
- Created 7 years ago
- Comments:24 (5 by maintainers)
Top Results From Across the Web
Is it better to pass a enum class as value or const reference ...
enum class holds an integral value just like a regular enum so you can safely pass it by value without any overhead.
Read more >Handbook - Enums - TypeScript
String enums allow you to give a meaningful and readable value when your code runs, independent of the name of the enum member...
Read more >Stop wasting time with enums in C# | Volare Software
Enums in C# can make you code easier to read: But enums don't cross in and out of C# easily. Have you ever...
Read more >Attaching Values to Java Enum - Baeldung
Let's start by adding the element names. ... First of all, we notice the special syntax in the declaration list. This is how...
Read more >Build Enumerations of Constants With Python's Enum
By doing this, you've lost the reference to the enum itself. ... from enum import Enum >>> class Empty(Enum): ... pass .
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Revisiting my comment from earlier…
I’m struck anew by what treacherous ground this question of “should it be legal, or should it be undefined behavior” really is. When other people express the sentiment that some particular
unsafe
hack is “obviously wrong” and shouldn’t be allowed, I’m usually quick to observe that preventing people from doing it is not a realistic possibility – that the only decision we have the power to make is whether to introduce potential security vulnerabilities into peoples’ code when (not if) they do it. In other words, that in these questions we should be attuned to practical consequences at least as much as theoretical elegance. And then here I am, expressing the sentiment that usingunsafe
code to get a reference to an uninhabited type is obviously wrong and shouldn’t be allowed.For what it’s worth, I think my feelings on the subject attach more strongly to uninhabited types than to reference types. A live value of an uninhabited type is the very definition of undefined behavior (like GCC’s __builtin_unreachable), what sound type systems everywhere exist to preclude, and the clarity of this really should not be muddied. If this isn’t undefined behavior, then nothing is.
On the other hand, if we say that in the presence of
unsafe
code, reference types should not always be taken at their word, a principle that just happens to apply uniformly to references to uninhabited types as well as to every other type, that seems like a separate matter. In a tootsie pop model, it seems appropriate that in “safe code”,&!
and&mut !
really would be tantamount to!
, as per the logical interpretation of reference types, while in “unsafe code”, they would perhaps not be taken so seriously.This is now basically being discussed at https://github.com/rust-lang/unsafe-code-guidelines/issues/77: does a reference always have to have a pointee that is valid for its type, or does it “just” have to be aligned and dereferencable?