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

Closed
futile opened this issue Oct 12, 2018 · 8 comments
Closed

Issue with impl Trait and panic!() #55022

futile opened this issue Oct 12, 2018 · 8 comments

Comments

@futile
Copy link

@futile futile 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
Copy link
Member

@killercup killercup commented Oct 12, 2018

FYI: This also means that

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

doesn't compile!

Loading

@futile
Copy link
Author

@futile futile 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.

Loading

@memoryruins
Copy link
Contributor

@memoryruins memoryruins 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)

Loading

@varkor
Copy link
Member

@varkor varkor commented Oct 13, 2018

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

Loading

@Centril
Copy link
Contributor

@Centril Centril commented Dec 12, 2019

I'm closing this as not-a-bug as we never decided that ! should implement any Trait and so long as it does not, -> impl Trait and unimplemented!() / friends won't unify.

Loading

@Centril Centril closed this Dec 12, 2019
@futile
Copy link
Author

@futile futile commented Dec 12, 2019

@Centril Yes, they currently won't, but I just assumed that it is uncontroversial that panic!/unreachable! should work in this case. So I thought it would rather be a matter of figuring out how to fix this instead of whether this should be fixed.

However, if the do-we-want-this-to-work comes first, can you refer me to the appropriate way to open an issue/discussion to figure that out? That would be awesome, thanks!

Loading

@Centril
Copy link
Contributor

@Centril Centril commented Dec 12, 2019

There's some discussion in the tracking issue, e.g. starting with #35121 (comment) and probably more in various places (e.g. rust-lang/rfcs#2619). It's far more subtle than "all traits" I think and needs some language design (assuming there's agreement which there might not be). If you want to tackle this I'd read all previous discussions first and then raise an issue on internals.rust-lang.org/. (However, I'm skeptical that this should be a priority for the language and compiler teams).

Loading

@futile
Copy link
Author

@futile futile commented Dec 12, 2019

Ah, I see the issue is already being discussed. If that's the case I'll just let it rest, seems there are enough people aware of it already. Thanks for the links! :)

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants