Skip to content

Commit

Permalink
try to fixup code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed Jun 28, 2024
1 parent 509ac7d commit 88a1e81
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/rust-2024/never-type-fallback.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
When the compiler sees a value of type ! in a [coercion site],
it implicitly inserts a coercion to allow the type checker to infer any type:

```rust
```rust,ignore (has placeholders)
// this
let x: u8 = panic!();
Expand All @@ -25,7 +25,7 @@ fn absurd<T>(_: !) -> T { ... }

This can lead to compilation errors if the type cannot be inferred:

```rust
```rust,ignore (uses code from previous example)
// this
{ panic!() };
Expand All @@ -36,7 +36,7 @@ This can lead to compilation errors if the type cannot be inferred:
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:

```rust
```rust,ignore (has placeholders, uses code from previous example)
type Fallback = /* An arbitrarily selected type! */;
{ absurd::<Fallback>(panic!()) }
```
Expand Down Expand Up @@ -67,7 +67,7 @@ The complication is that it might not be trivial to see which type needs to be s
One of the most common patterns which are broken by this change is using `f()?;` where `f` is
generic over the ok-part of the return type:

```rust
```rust,ignore (can't compile outside of a result-returning function)
fn f<T: Default>() -> Result<T, ()> {
Ok(T::default())
}
Expand All @@ -80,19 +80,19 @@ desugaring of `?` operator it used to be inferred to `()`, but it will be inferr

To fix the issue you need to specify the `T` type explicitly:

```rust
```rust,ignore (can't compile outside of a result-returning function, mentions function from previous example)
f::<()>()?;
// OR
() = f()?;
```

Another relatively common case is `panic`king in a closure:

```rust
```rust,edition2015
trait Unit {}
impl Unit for () {}
fn run(f: impl FnOnce() -> impl Unit) {
fn run<R: Unit>(f: impl FnOnce() -> R) {
f();
}
Expand Down

0 comments on commit 88a1e81

Please sign in to comment.