Skip to content

! cannot be used as impl Trait when ! implements Trait #105284

Open
@zachs18

Description

@zachs18

I tried this code (playground link):

#![feature(never_type)]
#![allow(unreachable_code)]

pub trait Trait {}
impl Trait for ! {}

pub fn test() -> impl Trait {
    let x: ! = panic!();
    x
}

I expected to see this happen: The code should compile. Because ! implements Trait, ! should be a valid return type for fn test (and no fallback should occur).

Instead, this happened: The code does not compile. x "falls back" to (), (which does not implement Trait), so the function does not typecheck, giving the following error message:

error[E0277]: the trait bound `(): Trait` is not satisfied
 --> src/lib.rs:7:18
  |
7 | pub fn test() -> impl Trait {
  |                  ^^^^^^^^^^ the trait `Trait` is not implemented for `()`
  |
  = help: the trait `Trait` is implemented for `!`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error

See also:

Meta

rustc +nightly --version --verbose:

rustc 1.67.0-nightly (32e613bba 2022-12-02)
binary: rustc
commit-hash: 32e613bbaafee1bcabba48a2257b838f8d1c03d3
commit-date: 2022-12-02
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-never_type`#![feature(never_type)]`T-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