-
Notifications
You must be signed in to change notification settings - Fork 563
Add a chapter on divergence #2067
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,37 @@ | ||||||
| r[divergence] | ||||||
| # Divergence | ||||||
|
|
||||||
| r[divergence.intro] | ||||||
| Divergence is the state where a particular section of code could never be encountered at runtime. Importantly, while there are certain language constructs that immediately produce a _diverging expression_ of the type [`!`](./types/never.md), divergence can also propogate to the surrounding block. | ||||||
|
|
||||||
| Any expression of type [`!`](./types/never.md) is a _diverging expression_, but there are also diverging expressions which are not of type `!` (e.g. `Some(panic!())`). | ||||||
|
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. I got confused at first by the second half of this sentence. Can you expand on the
Suggested change
also: should this set of expressions be enumerated?
Member
Author
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.
I can expand on this, but I think this exact suggestion is not actually correct;
See #2067 (comment). I had this before but TC suggested to remove it. |
||||||
|
|
||||||
| r[divergence.fallback] | ||||||
| ## Fallback | ||||||
| If a type to be inferred is only unified with diverging expressions, then that type will be inferred to be `!`. | ||||||
|
|
||||||
| > [!EXAMPLE] | ||||||
| > ```rust,compile_fail,E0277 | ||||||
| > fn foo() -> i32 { 22 } | ||||||
| > match foo() { | ||||||
| > // ERROR: The trait bound `!: Default` is not satisfied. | ||||||
| > 4 => Default::default(), | ||||||
| > _ => return, | ||||||
| > }; | ||||||
| > ``` | ||||||
|
Comment on lines
+7
to
+21
Contributor
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. It feels that we might need to say more here to guard against too simple a reading. We say, 1) not all diverging expressions have type However, of course, this does not compile: trait Tr: Sized {
fn m() -> Self { loop {} }
}
impl<T> Tr for T {}
fn f() -> u8 { 0 }
fn g() -> ! {
match f() {
0 => Tr::m(),
// ^^^^^^^ There's a type to be inferred here.
_ => Some(panic!()),
// ^^^^^^^^^^^^^^ This is a diverging expression.
} // ERROR: Mismatched types.
}What might we be able to say to tease this apart?
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. is this addressed by the bit about type unification happening structurally below at https://github.com/rust-lang/reference/pull/2067/files#diff-11f5e85172f632195dd6a9a80daa852911fd3d66e788177a3bb39f6c5d7f21cfR27 ?
Member
Author
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. It's a good observation. I've tried to be a little nuanced in my wording - for example, in the list of expression that I removed from your review, I had the wording "the following language constructs unconditonally produce a diverging expression of the type |
||||||
| > [!EDITION-2024] | ||||||
| > Before the 2024 edition, the type was inferred to instead be `()`. | ||||||
| > [!NOTE] | ||||||
| > Importantly, type unification may happen *structurally*, so the fallback `!` may be part of a larger type. The > following compiles: | ||||||
| > ```rust | ||||||
| > fn foo() -> i32 { 22 } | ||||||
| > // This has the type `Option<!>`, not `!` | ||||||
| > match foo() { | ||||||
| > 4 => Default::default(), | ||||||
| > _ => Some(return), | ||||||
| > }; | ||||||
| > ``` | ||||||
| <!-- TODO: This last point should likely should be moved to a more general "type inference" section discussing generalization + unification. --> | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -44,7 +44,7 @@ r[expr.block.result] | |||||
| Then the final operand is executed, if given. | ||||||
|
|
||||||
| r[expr.block.type] | ||||||
| The type of a block is the type of the final operand, or `()` if the final operand is omitted. | ||||||
| Except in the case of divergence (see below), the type of a block is the type of the final operand, or `()` if the final operand is omitted. | ||||||
|
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. not positive this link is correct but I did my best
Suggested change
|
||||||
|
|
||||||
| ```rust | ||||||
| # fn fn_call() {} | ||||||
|
|
@@ -63,6 +63,54 @@ assert_eq!(5, five); | |||||
| > [!NOTE] | ||||||
| > As a control flow expression, if a block expression is the outer expression of an expression statement, the expected type is `()` unless it is followed immediately by a semicolon. | ||||||
|
|
||||||
| r[expr.block.type.diverging] | ||||||
| A block is itself considered to be [diverging](../divergence.md) if all reachable control flow paths contain a [diverging expression](../divergence.md#r-divergence.diverging-expressions), unless that expression is a place expression that is not read from. | ||||||
|
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. place expression could also use a link: expressions.html#r-expr.place-value.place-memory-location |
||||||
|
|
||||||
| ```rust,no_run | ||||||
| # fn make<T>() -> T { loop {} } | ||||||
| fn no_control_flow() -> ! { | ||||||
| // There are no conditional statements, so this entire block is diverging. | ||||||
| loop {} | ||||||
| } | ||||||
|
|
||||||
| fn control_flow_diverging() -> ! { | ||||||
| // All paths are diverging, so this entire block is diverging. | ||||||
| if true { | ||||||
| loop {} | ||||||
| } else { | ||||||
| loop {} | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn control_flow_not_diverging() -> () { | ||||||
| // Some paths are not diverging, so this entire block is not diverging. | ||||||
| if true { | ||||||
| () | ||||||
| } else { | ||||||
| loop {} | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| struct Foo { | ||||||
| x: !, | ||||||
| } | ||||||
|
|
||||||
| fn diverging_place_read() -> () { | ||||||
| let foo = Foo { x: make() }; | ||||||
| let _: ! = { | ||||||
| // A read of a place expression produces a diverging block | ||||||
| let _x = foo.x; | ||||||
| }; | ||||||
| } | ||||||
| fn diverging_place_not_read() -> () { | ||||||
| let foo = Foo { x: make() }; | ||||||
| let _: () = { | ||||||
| // Asssignment to `_` means the place is not read | ||||||
| let _ = foo.x; | ||||||
| }; | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| r[expr.block.value] | ||||||
| Blocks are always [value expressions] and evaluate the last operand in value expression context. | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,6 +96,27 @@ Every binding in each `|` separated pattern must appear in all of the patterns i | |
| r[expr.match.binding-restriction] | ||
| Every binding of the same name must have the same type, and have the same binding mode. | ||
|
|
||
| r[expr.match.type] | ||
| The type of the overall `match` expression is the [least upper bound](../type-coercions.md#r-coerce.least-upper-bound) of the individual match arms. | ||
|
Comment on lines
+99
to
+100
Contributor
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. If we're inlining the rule about determining the type based on the LUB for match, from https://doc.rust-lang.org/1.90.0/reference/type-coercions.html#r-coerce.least-upper-bound.intro, probably we'd need to do that for the other rules there also (and then either remove the list from there or convert it to an admonition or index).
Contributor
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. As an aside, looking into this rule is what prompted me to file: |
||
|
|
||
| r[expr.match.empty] | ||
| If there are no match arms, then the `match` expression is diverging and the type is [`!`](../types/never.md). | ||
|
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. does this require a diverging scrutinee expression to compile? probably worth adding an example either way
Member
Author
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. This is a good question! The answer is (subtly) no. Take the following: fn make<T>() -> T { loop {} }
enum Empty {}
fn diverging_match_no_arms() -> ! {
let e: Empty = make();
match e {}
}
Adding this as an example. |
||
|
|
||
| r[expr.match.conditional] | ||
| If either the scrutinee expression or all of the match arms diverge, then the entire `match` expression also diverges. | ||
|
|
||
| > [!NOTE] | ||
| > Even if the entire `match` expression diverges, its type may not be [`!`](../types/never.md). | ||
| > | ||
| >```rust,compile_fail,E0004 | ||
| > let a = match true { | ||
| > true => Some(panic!()), | ||
| > false => None, | ||
| > }; | ||
| > // Fails to compile because `a` has the type `Option<!>`. | ||
| > match a {} | ||
| >``` | ||
|
|
||
| r[expr.match.guard] | ||
| ## Match guards | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We try to keep the number of top-level chapters contained. Looking at it, perhaps most of what's here that can't be inlined on the pages for each expression would make sense appearing in the Expressions chapter (e.g., we talk about place and value expressions there -- the verbiage about when a place expression is diverging might make sense near that) and in the Never type chapter.