-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
wishlist: Way to express Fn
trait like (FnOnce() -> !)
#1120
Comments
It is possible to fake this using a "void" enum like |
You don't need any unsafe code there, |
@sfackler interesting, does that have the same optimization results (i.e. does LLVM know that this unreachable is actually unreachable)? |
Yep: http://is.gd/JVCrHf enum Void {}
impl Void {
fn unreachable(&self) -> ! {
match *self {}
}
} O0 IR: %Void = type {}
; Function Attrs: noreturn uwtable
define internal void @_ZN4Void11unreachable20h428ace37d3624b8ehaaE(%Void* noalias readonly) unnamed_addr #0 {
entry-block:
%self = alloca %Void*
store %Void* %0, %Void** %self
%1 = load %Void** %self, !nonnull !0
unreachable
join: ; No predecessors!
unreachable
} |
Cool, thanks for bringing it up further. I'm not familiar with the empty enum type, and I'm quite surprised it's allowed by the Rust language at all. As I understand it, the trick is that it's impossible to create a
Also I'm not sure why the unreachable function is needed at all - this works fine too:
|
For a very good reason. |
@diwic writes:
The fact that such a transmute works today is an implementation artifact that, in an ideal world, would be statically rejected; see also #1076 and rust-lang/rust#4499 In particular, the memory layout for |
Relevant: #1001 Is there a reason not to make |
@canndrew I woudn't want For example, it does not make sense to use it in many contexts where you should be able to put "normal types", such as argument positions for functions. As I said on another ticket, if we were to add |
Doesn't make sense in what way? Merely not useful to do so, or nonsensical (cannot work) at some deeper level?
For what reason? (Sorry, you may have already answered this somewhere - but not on the ticket you linked.) At a technical level, let me put forth the following proposal as a straw man: (1) |
Sure it makes sense. What's wrong with writing
True, you could never call this function so there's no point in writing it. But it has a meaning so there's no point in disallowing it either. There are times where it would be useful for
What's the reason for this?
What's the value in not having it? It just makes the language more complicated. I mean, imagine if we took some other type (eg. |
Sorry, I hadn't seen @glaebhoerl's very similar comment when I posted mine. |
In case it's not obvious what I'm saying here: Rust fixes C's mistake of not treating empty structs like plain ol' types and instead giving them their own weird syntax and semantics that breaks generic code. But then rust goes and makes the exact same mistake with empty enums. If we can fix this, we should. |
@glaebhoerl wrote:
I will concede that, in theory, many mental models of what "types" denote can be made to work with the My problem is that on the implementation side of Rust, I worry about e.g. code-gen for a function that takes an argument of type
From skimming over the PR where this was removed: PR #17603. I would like to infer that the impact on the compiler would be minimal (and thus I would withdraw the above misgivings).
|
@glaebhoerl wrote:
Because I believe the real-world usage of Just my opinion; i don't have any data to support it. |
I think that would be misleading because it implies that a |
@Diggsey yes okay, the choice of name was probably not ideal. (That's sort of a separate topic as to whether using such default bound is a good idea at all; I am willing to concede that both the name and the idea are potentially bad.) |
Ideally, any code that handles a value of type
From the user's point of view, values of type However this all seems moot because in 1.0 empty types have size 0 and I don't see how that can be changed without breaking backwards compatibility.
No, because it's not new. We already have empty types. If you moved @reem's rust-void into libcore and made |
This is not quite true; you cannot tag empty enums with |
This code compiles and runs though:
Or are you saying you'd be okay with breaking this behavior? |
If we guaranteed the stability of |
Ah yeah, of course. |
I just noticed this old RFC of @glaebhoerl's for anonymous enums. If we implemented that then |
Triage: Fixed by #1216 |
Spawned off of rust-lang/rust#25325
As written by @diwic originally:
Here's a minimal example that shows the problem:
which fails with:
...AFAICS, there's no good reason this syntax is not allowed.
The text was updated successfully, but these errors were encountered: