Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ChaiTRex opened this issue Apr 12, 2024 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ChaiTRex
Copy link
Contributor

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

@ChaiTRex ChaiTRex added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 12, 2024
@cuviper
Copy link
Member

cuviper commented Apr 12, 2024

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

It's literally a type cast expression.

If we had type ascription, then I would agree that shouldn't look unreachable.

@ChaiTRex
Copy link
Contributor Author

OK, then perhaps the problem is that todo!() as T should not be considered unreachable as if it comes after a statement that panics or returns or whatever, as it doesn't come after such an expression. It is that expression.

@cuviper
Copy link
Member

cuviper commented Apr 12, 2024

I would say that the panicking todo!() is a subexpression of the todo!() as T expression, and it does render the rest of the expression unreachable, just like if you had todo!() + 1 or foo(todo!()).

Don't get me wrong though, I am sympathetic to the need for a type in todo!() with -> impl Trait. We just don't have a good way to write that yet.

@cuviper
Copy link
Member

cuviper commented Apr 12, 2024

For iterators, if you're willing to have a lazy panic until the iterator is actually used, then this isn't too bad:

pub fn f() -> impl Iterator<Item = ()> {
    core::iter::repeat_with(|| todo!())
}

@eggyal
Copy link
Contributor

eggyal commented Apr 12, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants