-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Document never type fallback in !
's docs
#124419
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
Changes from 2 commits
23b67de
b73bfd2
3a40e83
e2eb053
e18b6e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -268,6 +268,52 @@ mod prim_bool {} | |
/// [`Debug`]: fmt::Debug | ||
/// [`default()`]: Default::default | ||
/// | ||
/// # Never type fallback | ||
/// | ||
/// When the compiler sees a value of type `!` it implicitly inserts a coercion (if possible) | ||
/// to allow type checker to infer any type: | ||
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// ```rust,ignore (illustrative-and-has-placeholders) | ||
/// // this | ||
/// let x: u8 = panic!(); | ||
/// | ||
/// // is (essentially) turned by the compiler into | ||
/// let x: u8 = absurd(panic!()); | ||
/// | ||
/// // where absurd is a function with the following signature | ||
/// // (it's sound, because `!` always marks unreachable code): | ||
/// fn absurd<T>(_: !) -> T { ... } | ||
// FIXME: use `core::convert::absurd` here instead, once it's merged | ||
/// ``` | ||
/// | ||
/// While it's convenient to be able to use non-diverging code in one of the branches (like | ||
/// `if a { b } else { return }`) this could lead to compilation errors: | ||
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// ```compile_fail | ||
/// // this | ||
/// { panic!() }; | ||
/// | ||
/// // gets turned into this | ||
/// { absurd(panic!()) }; // error: can't infer the type of `absurd` | ||
/// ``` | ||
/// | ||
/// To prevent such errors, the compiler remembers where it inserted `absurd` calls, and | ||
/// if it can't infer their type, it sets the type to the fallback type: | ||
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// ```rust, ignore | ||
/// type Fallback = /* An arbitrarily selected type! */; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Arbitrarily selected" here sounds like "the compiler picks from some set", not "we made a choice and taught it to the compiler". I'm not sure what would be better, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's nothing stopping us from having arbitrarily complicated fallback logic, aside from a reluctance to implement anything more complex, which is why I suggested it that way. We do pick from a set. The set has one member. :^) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hoping to grow the set to two :p I'm also not a fan of "arbitrary" here because we did purposefully choose the fallback type... But I'm not sure how to phrase this properly :/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whenever I use arbitrary I strongly mean the "the reason for why is because I said so", because often there were multiple reasonable choices we could have made, but we could only make one (one set, with any number of members). |
||
/// { absurd::<Fallback>(panic!()) } | ||
/// ``` | ||
/// | ||
/// This is what is known as "never type fallback". | ||
/// | ||
/// Historically fallback was [`()`], causing confusing behavior where `!` spontaneously coerced | ||
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// to `()`, even though `()` was never mentioned (because of the fallback). There are plans to | ||
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// change it in 2024 edition (and possibly in all editions on a later date), see | ||
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// [Tracking Issue for making `!` fall back to `!`][fallback-ti]. | ||
/// | ||
/// [`()`]: prim@unit | ||
/// [fallback-ti]: https://github.com/rust-lang/rust/issues/123748 | ||
/// | ||
#[unstable(feature = "never_type", issue = "35121")] | ||
mod prim_never {} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.