Skip to content

Issue with impl Trait and panic!() #55022

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

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
Labels
A-associated-items Area: Associated items (types, constants & functions) A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. F-never_type `#![feature(never_type)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Comments

@futile
Copy link
Contributor

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

FYI: This also means that

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

doesn't compile!

@futile
Copy link
Contributor Author

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.

@memoryruins
Copy link
Contributor

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)

@varkor
Copy link
Member

varkor commented Oct 13, 2018

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

@estebank estebank added A-associated-items Area: Associated items (types, constants & functions) A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. F-never_type `#![feature(never_type)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team labels Aug 5, 2019
@Centril
Copy link
Contributor

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.

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

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!

@Centril
Copy link
Contributor

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).

@futile
Copy link
Contributor Author

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! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. F-never_type `#![feature(never_type)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team
Projects
None yet
Development

No branches or pull requests

6 participants