Skip to content

Casting or adding type ascription to panic!() triggers unreachable_code #67227

Open
@leo60228

Description

@leo60228
fn test() -> impl Iterator<Item = i32> {
    panic!()
}

doesn't compile, as () (nor !) implement Iterator<Item = i32>.

#![feature(never_type_fallback)] // required for `panic!() as _`
fn test() -> impl Iterator<Item = i32> {
    panic!() as std::iter::Empty<_>
}

compiles, but gives this warning:

warning: unreachable expression
 --> src/lib.rs:3:5
  |
3 |     panic!() as std::iter::Empty<_>
  |     --------^^^^^^^^^^^^^^^^^^^^^^^
  |     |
  |     unreachable expression
  |     any code following this expression is unreachable
  |
  = note: `#[warn(unreachable_code)]` on by default
  = note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
#![feature(type_ascription)]
fn test() -> impl Iterator<Item = i32> {
    panic!(): std::iter::Empty<_>
}

also compiles, but gives the same warning.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.F-never_type`#![feature(never_type)]`P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions