Skip to content

Commit fa9825d

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

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/divergence.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@ 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+
r[divergence.uninhabited]
10+
Though `!` is considered an uninhabited type, a type being uninhabited is not sufficient for it to diverge.
11+
12+
> [!EXAMPLE]
13+
> ```rust,compile_fail,E0308
14+
> # #![ feature(never_type) ]
15+
> # fn make<T>() -> T { loop {} }
16+
> enum Empty {}
17+
> fn diverging() -> ! {
18+
> // This has a type of `!`.
19+
> // So, the entire function is considered diverging
20+
> make::<!>();
21+
> }
22+
> fn not_diverging() -> ! {
23+
> // This type is uninhabited.
24+
> // However, the entire function is not considered diverging
25+
> make::<Empty>();
26+
> }
27+
> ```
28+
929
r[divergence.fallback]
1030
## Fallback
1131
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)