rust-lang / rust-lang/rust

Using `as` to get `todo!` to work with `impl` return gives false unreachable code warning

Open
#123,871 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
pub fn f() -> impl Iterator<Item = ()> {
    todo!() as core::iter::Empty<()>
}
Current output
warning: unreachable expression
 --> src/lib.rs:2:5
  |
2 |     todo!() as core::iter::Empty<()>
  |     -------^^^^^^^^^^^^^^^^^^^^^^^^^
  |     |
  |     unreachable expression
  |     any code following this expression is unreachable
  |
  = note: `#[warn(unreachable_code)]` on by default

warning: `playground` (lib) generated 1 warning
Desired output
No warning
Rationale and extra context

Using todo!() with a return type that is impl currently causes a type error. Using as to give it a specific type fixes that but brings this incorrect warning message.

as T by itself shouldn't really be considered an expression.

Other cases

No response

Rust Version
rustc 1.77.2 (25ef9e3d8 2024-04-09)
binary: rustc
commit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04
commit-date: 2024-04-09
host: aarch64-apple-darwin
release: 1.77.2
LLVM version: 17.0.6

rustc 1.78.0-beta.6 (27011d5dc 2024-04-09)
binary: rustc
commit-hash: 27011d5dc09a8d1ac2289d024f54ecac9fc70d47
commit-date: 2024-04-09
host: aarch64-apple-darwin
release: 1.78.0-beta.6
LLVM version: 18.1.2

rustc 1.79.0-nightly (aa067fb98 2024-04-10)
binary: rustc
commit-hash: aa067fb984d36462548bb785da221bfaf38253f0
commit-date: 2024-04-10
host: aarch64-apple-darwin
release: 1.79.0-nightly
LLVM version: 18.1.3
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example in src/lib.rs with the listed rustc versions and confirm the unreachable_code warning. Trace the compiler's unreachable-code handling for the todo!() as core::iter::Empty<()> expression, then add a regression test showing that this case produces no warning and run the relevant compiler tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.