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

Purely-diverging functions cannot return impl Trait. #44923

Closed
kennytm opened this issue Sep 29, 2017 · 6 comments
Closed

Purely-diverging functions cannot return impl Trait. #44923

kennytm opened this issue Sep 29, 2017 · 6 comments

Comments

@kennytm
Copy link
Member

@kennytm kennytm commented Sep 29, 2017

Originally reported in #34511 (comment), separated out to make it easier to track. #43869 and #44731 are examples caused by this issue.

If a function is purely diverging, any part of the return type cannot contain impl Trait, e.g.

fn do_it_later_but_cannot() -> impl Iterator<Item=u8> { //~ ERROR E0227
    unimplemented!()
}

this fails because ! does not implement Iterator. But I think this should be allowed even without the explicit impl, since ! can be coerced to any concrete type.

@malbarbo
Copy link
Contributor

@malbarbo malbarbo commented Sep 29, 2017

@kennytm Do you know where the issue of general diverging functions cannot return impl Trait are being tracked?

@kennytm
Copy link
Member Author

@kennytm kennytm commented Sep 29, 2017

@malbarbo there’s no problem returning “general diverging functions”, if you mean something like this:

fn foo() -> impl Bar {
    if bar() {
        Baz::new()
    } else {
        panic!(“diverge”)
    }
}
@malbarbo
Copy link
Contributor

@malbarbo malbarbo commented Sep 29, 2017

@kennytm Yes, I mean that. But I asked because I remembered vaguely some limitations that I had encountered in the past. Looking at my old code, the limitation was not that... sorry. The limitation is this:

fn g<T>(t: T) -> Result<impl Iterator<Item=u32>, T> {
    Err(t)
}

fails to compile with error:

fn g<T>(t: T) -> Result<impl Iterator<Item=u32>, T>
                        ^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for `_`

If we change the type to impl Clone, the error changes to:

fn g<T>(t: T) -> Result<impl Clone, T>
                        ^^^^^^^^^^
note: the return type of a function must have a statically known size

It this a know issue? Is this expected?

@Aaron1011
Copy link
Member

@Aaron1011 Aaron1011 commented Oct 9, 2017

I'd like to work on this.

@hanna-kruppe
Copy link
Contributor

@hanna-kruppe hanna-kruppe commented Apr 1, 2018

this and #36375 are duplicates of each other

@ishitatsuyuki
Copy link
Contributor

@ishitatsuyuki ishitatsuyuki commented Apr 9, 2018

I think we can close this as duplicate of #36375?

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
7 participants