Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upPre-RFC: Add an `Empty` type to libcore. Use it to replace the `fn() -> !` syntax. #1001
Comments
This comment has been minimized.
This comment has been minimized.
|
One other alternative is to keep the |
This comment has been minimized.
This comment has been minimized.
The advantage of the current built-in support for If we do want to add a library type instead of or in addition to it, I think it should be called |
This comment has been minimized.
This comment has been minimized.
|
The bottom type was removed in rust-lang/rust#14973, because of maintenance difficulty? I don't think it's very important for 1.0 though, requiring the user to provide a must-be-never-ending closure |
This comment has been minimized.
This comment has been minimized.
How often do you call a diverging function from a non-diverging function without using
And similarly |
This comment has been minimized.
This comment has been minimized.
I quite like this. Then the name |
This comment has been minimized.
This comment has been minimized.
It's only important because it would be a backwards-incompatible change. |
This comment has been minimized.
This comment has been minimized.
reem
commented
Mar 23, 2015
|
From an optimization perspective, the implementation of Bikeshed: Note that there exists (disclaimer: I wrote it) a crate with a similar type and helpers called void, whose code is here: https://github.com/reem/rust-void |
This comment has been minimized.
This comment has been minimized.
reem
commented
Mar 23, 2015
|
Also, a similar empty type used to exist in |
This comment has been minimized.
This comment has been minimized.
|
@canndrew: Why would it not be backwards-compatible (provided we choose to keep |
canndrew
referenced this issue
May 20, 2015
Closed
wishlist: Way to express `Fn` trait like `(FnOnce() -> !)` #1120
This comment has been minimized.
This comment has been minimized.
|
@llogiq I meant if the |
This comment has been minimized.
This comment has been minimized.
Agreed. Well, I think the |
pnkfelix
referenced this issue
Jan 4, 2016
Closed
`unreachable_code` lint doesn't work with unboxed closures #20574
This comment has been minimized.
This comment has been minimized.
|
Triage: Superseded by #1216 |
canndrew commentedMar 22, 2015
Summary
Add an
Emptytype to libcore. Use it to replace thefn() -> !sytax.Motivation
See some previous discussion here: http://internals.rust-lang.org/t/should-be-a-type/1723 here: rust-lang/rust#20172 and here: rust-lang/rust#18414
Edit: @reem has already implemented a crate for this. See: https://github.com/reem/rust-void
A standard
Emptytype (ie. an enum with no variants) would be a useful addition to the standard library. One use case is if you need to implement a trait method that returnsResult<_, Err>whereErris an associated type but you know that your implementation cannot fail. Currently, library authors can easily define their own empty types, but this will result in multiple libraries defining the same thing.Making diverging functions ordinary functions also allows them to be used in generic code. For example, it would allow functions that diverge to be passed as arguments to higher-order functions.
Removing
!and replacing it with a plain ol' type will make Rust slightly simpler.Detailed design
Add this to libcore:
Add an
emptylang item so that intrinsics likeabortcan return it. Make all methods that are currently marked as diverging (eg.panic,abort) returnEmptyinstead. Make thepanic!macro expand tobegin_unwind(...).elim()instead of justbegin_unwind(...)to avoid users having to add the.elim()themselves.Add a convenience method to
Result<T, Empty>:Drawbacks
This change will require typing
.elim()where previously someone wouldn't have had to. But only when calling a diverging function from a non-diverging function without using one of thepanic!family macros. In the future, Rust's type checker could be improved to allow any type which is isomorphic toEmptyto unify with any other type.It might make things like reachability checking more difficult. But it would be nice anyway if the compiler could detect unreachable code after code that produces an empty value.
!used to be treated as a type though, but this was removed due to maintenance difficulties.Alternatives
Just add an
Emptytype tolibcorebut keep the seperate divergence syntax aswell.Name it
Neveras in "this value can never exist" or "this function can never return". Name itVoid. However this is likely to confuse programmers from a C-family language background asVoidis not(). Name itBottom. Name it something else.Unresolved questions
Is there time to make this change before 1.0?