1111When the compiler sees a value of type ! in a [ coercion site] ,
1212it implicitly inserts a coercion to allow the type checker to infer any type:
1313
14- ``` rust
14+ ``` rust,ignore (has placeholders)
1515// this
1616let x: u8 = panic!();
1717
@@ -25,7 +25,7 @@ fn absurd<T>(_: !) -> T { ... }
2525
2626This can lead to compilation errors if the type cannot be inferred:
2727
28- ``` rust
28+ ``` rust,ignore (uses code from previous example)
2929// this
3030{ panic!() };
3131
@@ -36,7 +36,7 @@ This can lead to compilation errors if the type cannot be inferred:
3636To prevent such errors, the compiler remembers where it inserted absurd calls,
3737and if it can’t infer the type, it uses the fallback type instead:
3838
39- ``` rust
39+ ``` rust,ignore (has placeholders, uses code from previous example)
4040type Fallback = /* An arbitrarily selected type! */;
4141{ absurd::<Fallback>(panic!()) }
4242```
@@ -67,7 +67,7 @@ The complication is that it might not be trivial to see which type needs to be s
6767One of the most common patterns which are broken by this change is using ` f()?; ` where ` f ` is
6868generic over the ok-part of the return type:
6969
70- ``` rust
70+ ``` rust,ignore (can't compile outside of a result-returning function)
7171fn f<T: Default>() -> Result<T, ()> {
7272 Ok(T::default())
7373}
@@ -80,19 +80,19 @@ desugaring of `?` operator it used to be inferred to `()`, but it will be inferr
8080
8181To fix the issue you need to specify the ` T ` type explicitly:
8282
83- ``` rust
83+ ``` rust,ignore (can't compile outside of a result-returning function, mentions function from previous example)
8484f::<()>()?;
8585// OR
8686() = f()?;
8787```
8888
8989Another relatively common case is ` panic ` king in a closure:
9090
91- ``` rust
91+ ``` rust,edition2015,should_panic
9292trait Unit {}
9393impl Unit for () {}
9494
95- fn run (f : impl FnOnce () -> impl Unit ) {
95+ fn run<R: Unit> (f: impl FnOnce() -> R ) {
9696 f();
9797}
9898
@@ -103,14 +103,14 @@ Previously `!` from the `panic!` coerced to `()` which implements `Unit`.
103103However now the ` ! ` is kept as ` ! ` so this code fails because ` ! ` does not implement ` Unit ` .
104104To fix this you can specify return type of the closure:
105105
106- ``` rust
106+ ``` rust,ignore (uses function from the previous example)
107107run(|| -> () { panic!() });
108108```
109109
110110A similar case to the ` f()? ` can be seen when using a ` ! ` -typed expression in a branch and a
111111function with unconstrained return in the other:
112112
113- ``` rust
113+ ``` rust,edition2015
114114if true {
115115 Default::default()
116116} else {
0 commit comments