You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/divergence.md
+20Lines changed: 20 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,26 @@ Divergence is the state where a particular section of code could never be encoun
6
6
7
7
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<!>`).
8
8
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
+
9
29
r[divergence.fallback]
10
30
## Fallback
11
31
If a type to be inferred is only unified with diverging expressions, then that type will be inferred to be `!`.
0 commit comments