Skip to content

Cannot return never type as impl Trait (like unimplemented!() stub) #71715

Closed
@ilyavennik

Description

@ilyavennik

I tried this code:

trait Foo {
    fn foo(&self);
}

impl Foo for i32 {
    fn foo(&self) {
        println!("<Foo as i32>::foo")
    }
}

fn bar() -> impl Foo {
    42
}

fn baz() -> impl Foo {
    unimplemented!()
}

I expected to see this happen: successful compilation. Instead, this happened: compilation error:

error[E0277]: the trait bound `(): Foo` is not satisfied
  --> src/lib.rs:15:13
   |
15 | fn baz() -> impl Foo {
   |             ^^^^^^^^ the trait `Foo` is not implemented for `()`
16 |     unimplemented!()
   |     ---------------- this returned value is of type `!`
   |
   = note: the return type of a function must have a statically known size

Meta

rustc --version --verbose:

rustc 1.43.0 (4fb7144ed 2020-04-20)
binary: rustc
commit-hash: 4fb7144ed159f94491249e86d5bbd033b5d60550
commit-date: 2020-04-20
host: x86_64-unknown-linux-gnu
release: 1.43.0
LLVM version: 9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions