Skip to content

Tracking issue for release notes of #144804: Don't warn on never to any as casts as unreachable #145498

@rustbot

Description

@rustbot

This issue tracks the release notes text for #144804.

cc @WaffleLapkin, @compiler-errors -- original issue/PR authors and assignees for drafting text

See the forge.rust-lang.org chapter about release notes for an overview of how the release team makes use of these tracking issues.

Release notes text

This section should be edited to specify the correct category(s) for the change, with succinct description(s) of what changed. Some things worth considering:

  • Does this need an additional compat notes section?
  • Was this a libs stabilization that should have additional headers to list new APIs under Stabilized APIs and Const Stabilized APIs?
# Language/Compiler/Libraries/Stabilized APIs/Const Stabilized APIs/Rustdoc/Compatibility Notes/Internal Changes/Other
- [Don't warn on never to any `as` casts as unreachable](https://github.com/rust-lang/rust/pull/144804)

Tip

Use the previous releases for inspiration on how to write the release notes text and which categories to pick.

Release blog section

If this change is notable enough for inclusion in the blog post then this section should be edited to contain a draft for the blog post. Otherwise leave it empty.

# Never type casts with RPIT

When prototyping functions which return RPIT (return position impl trait) it might be tempting to use `todo!()` before actually writing the implementation:

```rust
fn iterator() -> impl Iterator<Item = u8> {
    todo!()
}
```

However, this causes an error:
```
error[E0277]: `!` is not an iterator
 --> src/lib.rs:1:18
  |
1 | fn iterator() -> impl Iterator<Item = u8> {
  |                  ^^^^^^^^^^^^^^^^^^^^^^^^ `!` is not an iterator
2 |     todo!()
  |     ------- return type was inferred to be `!` here
  |
  = help: the trait `Iterator` is not implemented for `!`
```

While `!` (never type, type of `todo!()`) can be coerced to any type, this does not help in this example. RPIT needs some underlying type implementing the trait and `!` does not implement `Iterator`.

One solution to this problem is to add an explicit cast from `!` to a type which implements the trait:

```rust
fn iterator() -> impl Iterator<Item = u8> {
    todo!() as std::iter::Empty<_>
}
```

Previously this code caused warnings (because the `as` cast is technically unreachable). With this release, `rustc` no longer emits warnings on this code.

Note

If a blog post section is required the release-blog-post label should be added (@rustbot label +release-blog-post) to this issue as otherwise it may be missed by the release team.

Metadata

Metadata

Assignees

No one assigned

    Labels

    F-never_type`#![feature(never_type)]`L-unreachable_codeLint: unreachable_codeT-langRelevant to the language teamneeds-triageThis issue may need triage. Remove it if it has been sufficiently triaged.release-blog-postMarks issues tracking what text to put in the release blog post.relnotesMarks issues that should be documented in the release notes of the next release.relnotes-tracking-issueMarks issues tracking what text to put in release notes.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions