Skip to content

todo!(), unimplemented!(), panic!(), … don't play well with impl Trait #74363

@robinmoussu

Description

@robinmoussu

I tried this code:

fn test() -> impl Iterator<Item=f32> {
    todo!()
}

I expected to see this happen: the code should compile (and fails at runtime).

Instead, this happened:

error[E0277]: `()` is not an iterator
 --> src/lib.rs:1:14
  |
1 | fn test() -> impl Iterator<Item=f32> {
  |              ^^^^^^^^^^^^^^^^^^^^^^^ `()` is not an iterator
2 |     todo!()
  |     ------- this returned value is of type `!`
  |
  = help: the trait `std::iter::Iterator` is not implemented for `()`
  = note: the return type of a function must have a statically known size

error: aborting due to previous error

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

Note: The issue exists for similar macros: unimplemented, panic, and most probably others that I'm not aware of.


I'm using the following workaround (which obviously works only for iterators, but can be adapted for any kind of object being iterated on):

fn test() -> impl Iterator<Item=f32> {
    const TODO: [f32; 0] = [];
    todo!();
    #[allow(unreachable_code)]
    TODO.iter().cloned()    
}

Note: I'm relatively sure that this issue is known (given how easy it is to reproduce it), but I didn't found it in the bug tracker.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-bugCategory: This is a bug.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions