Skip to content

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

Merged
merged 5 commits into from
May 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Kevin Reid <kpreid@switchb.org>
Co-authored-by: Herman Skogseth <herman.skogseth@me.com>
Co-authored-by: Jubilee <46493976+workingjubilee@users.noreply.github.com>
  • Loading branch information
4 people authored May 2, 2024
commit 3a40e838bfc22c6415e32fcd94fd48e83e1844b1
15 changes: 7 additions & 8 deletions library/core/src/primitive_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ mod prim_bool {}
///
/// # 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:
/// When the compiler sees a value of type `!` in a [coercion site](https://doc.rust-lang.org/reference/type-coercions.html#coercion-sites), it implicitly inserts a coercion
/// to allow the type checker to infer any type:
///
/// ```rust,ignore (illustrative-and-has-placeholders)
/// // this
Expand All @@ -286,8 +286,7 @@ mod prim_bool {}
// 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:
/// This can lead to compilation errors if the type cannot be inferred:
///
/// ```compile_fail
/// // this
Expand All @@ -298,17 +297,17 @@ mod prim_bool {}
/// ```
///
/// 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:
/// if it can't infer the type, it uses the fallback type instead:
/// ```rust, ignore
/// type Fallback = /* An arbitrarily selected type! */;
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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. :^)

Copy link
Member Author

Choose a reason for hiding this comment

The 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 :/

Copy link
Member

@workingjubilee workingjubilee Apr 27, 2024

Choose a reason for hiding this comment

The 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
/// to `()`, even though `()` was never mentioned (because of the fallback). There are plans to
/// change it in 2024 edition (and possibly in all editions on a later date), see
/// Historically, the fallback type was [`()`], causing confusing behavior where `!` spontaneously coerced
/// to `()`, even when it would not infer `()` without the fallback. There are plans to
/// change it in the [2024 edition](https://doc.rust-lang.org/nightly/edition-guide/rust-2024/index.html) (and possibly in all editions on a later date); see
/// [Tracking Issue for making `!` fall back to `!`][fallback-ti].
///
/// [`()`]: prim@unit
Expand Down