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

Issue with impl Trait and panic!() #55022

Open
futile opened this issue Oct 12, 2018 · 4 comments

Comments

@futile
Copy link

commented Oct 12, 2018

The following code does not compile on nightly (and 1.29.1 stable) (playground):

fn foo() -> impl Iterator<Item = i32> {
    panic!()
}

I expected this code to compile, but instead I get the following error:

error[E0277]: the trait bound `(): std::iter::Iterator` is not satisfied
 --> src/lib.rs:1:13
  |
1 | fn foo() -> impl Iterator<Item = i32> {
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^ `()` is not an iterator; maybe try calling `.iter()` or a similar method
  |
  = 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`.

To learn more, run the command again with --verbose.

This seems like it should work to me.

@killercup

This comment has been minimized.

Copy link
Member

commented Oct 12, 2018

FYI: This also means that

fn foo() -> impl Iterator<Item = i32> {
    unimplemented!()
}

doesn't compile!

@futile

This comment has been minimized.

Copy link
Author

commented Oct 12, 2018

Yeah, I always use panic!() instead of unimplemented!() because it's easier to type, but that is exactly how I found it.

@memoryruins

This comment has been minimized.

Copy link
Contributor

commented Oct 12, 2018

Clicking through compiler explorer, the original example has never compiled on any stable release (since stabilizing impl trait on 1.26). The error also occurs on traits without associated types

trait Bar {}

fn foo() -> impl Bar {
    unimplemented!()
}
3 | fn foo() -> impl Bar {
  |             ^^^^^^^^ the trait `Bar` is not implemented for `()`

(It will compile when returning something like impl Eq due to () implementing Eq)

@varkor

This comment has been minimized.

Copy link
Member

commented Oct 13, 2018

This is a known design question concerning ! in general: #35121.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.