Skip to content

Commit 09d0f2b

Browse files
committed
Add section about uninhabited types in divergence.md
1 parent f9cf9f5 commit 09d0f2b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/divergence.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ Divergence is the state where a particular section of code could never be encoun
66

77
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(loop {})` produces a type of `Option<!>`).
88

9+
> [!NOTE]
10+
> Though `!` is considered an uninhabited type, a type being uninhabited is not sufficient for it to diverge.
11+
>
12+
> ```rust,compile_fail,E0308
13+
> # #![ feature(never_type) ]
14+
> # fn make<T>() -> T { loop {} }
15+
> enum Empty {}
16+
> fn diverging() -> ! {
17+
> // This has a type of `!`.
18+
> // So, the entire function is considered diverging
19+
> make::<!>();
20+
> }
21+
> fn not_diverging() -> ! {
22+
> // This type is uninhabited.
23+
> // However, the entire function is not considered diverging
24+
> make::<Empty>();
25+
> }
26+
> ```
27+
928
r[divergence.fallback]
1029
## Fallback
1130
If a type to be inferred is only unified with diverging expressions, then that type will be inferred to be `!`.

0 commit comments

Comments
 (0)