Skip to content

Commit 941db8b

Browse files
committed
Merge PR #310: Add a section for the never type change
2 parents 713b114 + 42b054d commit 941db8b

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@
4949
- [Rustfmt: Combine all delimited exprs as last argument](rust-2024/rustfmt-overflow-delimited-expr.md)
5050
- [`gen` keyword](rust-2024/gen-keyword.md)
5151
- [Macro fragment specifiers](rust-2024/macro-fragment-specifiers.md)
52+
- [Never type fallback change](rust-2024/never-type-fallback.md)
5253
- [`unsafe extern` blocks](rust-2024/unsafe-extern.md)
5354
- [Unsafe attributes](rust-2024/unsafe-attributes.md)

src/rust-2024/never-type-fallback.md

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# Never type fallback change
2+
3+
🚧 The 2024 Edition has not yet been released and hence this section is still "under construction".
4+
5+
## Summary
6+
7+
- Never type (`!`) to any type ("never-to-any") coercions fall back to never type (`!`) rather than to unit type (`()`).
8+
9+
## Details
10+
11+
When the compiler sees a value of type `!` (never) in a [coercion site][], it implicitly inserts a coercion to allow the type checker to infer any type:
12+
13+
```rust,should_panic
14+
# #![feature(never_type)]
15+
// This:
16+
let x: u8 = panic!();
17+
18+
// ...is (essentially) turned by the compiler into:
19+
let x: u8 = absurd(panic!());
20+
21+
// ...where `absurd` is the following function
22+
// (it's sound because `!` always marks unreachable code):
23+
fn absurd<T>(x: !) -> T { x }
24+
```
25+
26+
This can lead to compilation errors if the type cannot be inferred:
27+
28+
```rust,compile_fail,E0282
29+
# #![feature(never_type)]
30+
# fn absurd<T>(x: !) -> T { x }
31+
// This:
32+
{ panic!() };
33+
34+
// ...gets turned into this:
35+
{ absurd(panic!()) }; //~ ERROR can't infer the type of `absurd`
36+
```
37+
38+
To prevent such errors, the compiler remembers where it inserted `absurd` calls, and if it can't infer the type, it uses the fallback type instead:
39+
40+
```rust,should_panic
41+
# #![feature(never_type)]
42+
# fn absurd<T>(x: !) -> T { x }
43+
type Fallback = /* An arbitrarily selected type! */ !;
44+
{ absurd::<Fallback>(panic!()) }
45+
```
46+
47+
This is what is known as "never type fallback".
48+
49+
Historically, the fallback type has been `()` (unit). This caused `!` to spontaneously coerce to `()` even when the compiler would not infer `()` without the fallback. That was confusing and has prevented the stabilization of the `!` type.
50+
51+
In the 2024 edition, the fallback type is now `!`. (We plan to make this change across all editions at a later date.) This makes things work more intuitively. Now when you pass `!` and there is no reason to coerce it to something else, it is kept as `!`.
52+
53+
In some cases your code might depend on the fallback type being `()`, so this can cause compilation errors or changes in behavior.
54+
55+
[coercion site]: ../../reference/type-coercions.html#coercion-sites
56+
57+
## Migration
58+
59+
There is no automatic fix, but there is automatic detection of code that will be broken by the edition change. While still on a previous edition you will see warnings if your code will be broken.
60+
61+
The fix is to specify the type explicitly so that the fallback type is not used. Unfortunately, it might not be trivial to see which type needs to be specified.
62+
63+
One of the most common patterns broken by this change is using `f()?;` where `f` is generic over the `Ok`-part of the return type:
64+
65+
```rust
66+
# #![allow(dependency_on_unit_never_type_fallback)]
67+
# fn outer<T>(x: T) -> Result<T, ()> {
68+
fn f<T: Default>() -> Result<T, ()> {
69+
Ok(T::default())
70+
}
71+
72+
f()?;
73+
# Ok(x)
74+
# }
75+
```
76+
77+
You might think that, in this example, type `T` can't be inferred. However, due to the current desugaring of the `?` operator, it was inferred as `()`, and it will now be inferred as `!`.
78+
79+
To fix the issue you need to specify the `T` type explicitly:
80+
81+
<!-- TODO: edition2024 -->
82+
```rust
83+
# #![deny(dependency_on_unit_never_type_fallback)]
84+
# fn outer<T>(x: T) -> Result<T, ()> {
85+
# fn f<T: Default>() -> Result<T, ()> {
86+
# Ok(T::default())
87+
# }
88+
f::<()>()?;
89+
// ...or:
90+
() = f()?;
91+
# Ok(x)
92+
# }
93+
```
94+
95+
Another relatively common case is panicking in a closure:
96+
97+
```rust,should_panic
98+
# #![allow(dependency_on_unit_never_type_fallback)]
99+
trait Unit {}
100+
impl Unit for () {}
101+
102+
fn run<R: Unit>(f: impl FnOnce() -> R) {
103+
f();
104+
}
105+
106+
run(|| panic!());
107+
```
108+
109+
Previously `!` from the `panic!` coerced to `()` which implements `Unit`. However now the `!` is kept as `!` so this code fails because `!` doesn't implement `Unit`. To fix this you can specify the return type of the closure:
110+
111+
<!-- TODO: edition2024 -->
112+
```rust,should_panic
113+
# #![deny(dependency_on_unit_never_type_fallback)]
114+
# trait Unit {}
115+
# impl Unit for () {}
116+
#
117+
# fn run<R: Unit>(f: impl FnOnce() -> R) {
118+
# f();
119+
# }
120+
run(|| -> () { panic!() });
121+
```
122+
123+
A similar case to that of `f()?` can be seen when using a `!`-typed expression in one branch and a function with an unconstrained return type in the other:
124+
125+
```rust
126+
# #![allow(dependency_on_unit_never_type_fallback)]
127+
if true {
128+
Default::default()
129+
} else {
130+
return
131+
};
132+
```
133+
134+
Previously `()` was inferred as the return type of `Default::default()` because `!` from `return` was spuriously coerced to `()`. Now, `!` will be inferred instead causing this code to not compile because `!` does not implement `Default`.
135+
136+
Again, this can be fixed by specifying the type explicitly:
137+
138+
<!-- TODO: edition2024 -->
139+
```rust
140+
# #![deny(dependency_on_unit_never_type_fallback)]
141+
() = if true {
142+
Default::default()
143+
} else {
144+
return
145+
};
146+
147+
// ...or:
148+
149+
if true {
150+
<() as Default>::default()
151+
} else {
152+
return
153+
};
154+
```

0 commit comments

Comments
 (0)