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

Unexpected todo!/panic! behavior resulting in compilation error #85341

Closed
WalterSmuts opened this issue May 15, 2021 · 1 comment
Closed

Unexpected todo!/panic! behavior resulting in compilation error #85341

WalterSmuts opened this issue May 15, 2021 · 1 comment
Labels
C-bug Category: This is a bug.

Comments

@WalterSmuts
Copy link

WalterSmuts commented May 15, 2021

I am going through the Category Theory For Programmers book and am writing quite obscure code. I defined a memorize function but, as usual, didn't yet know how I'd like to implement it and opted to insert a todo! for the time being. This was the first time I came across the todo! macro in the body of a function, which I thought was a valid return type for all functions,
which produced a compilation error.

I tried this code:

fn memoize<V, R>(f: impl Fn(V) -> R) -> impl Fn(V) -> R {
    todo!()
}

and even:

fn memoize<V, R>(f: impl Fn(V) -> R) -> impl Fn(V) -> R {
    panic!()
}

I expected the program to successfully compile, but instead I received the following compilation error:

error[E0277]: expected a `Fn<(V,)>` closure, found `()`
 --> src/lib.rs:1:41
  |
1 | fn memoize<V, R>(f: impl Fn(V) -> R) -> impl Fn(V) -> R {
  |                                         ^^^^^^^^^^^^^^^ expected an `Fn<(V,)>` closure, found `()`
  |
  = help: the trait `Fn<(V,)>` is not implemented for `()`

error: aborting due to previous error

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

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

The more interesting part is that the function does compile successfully when you return it's input argument:

fn memoize<V, R>(f: impl Fn(V) -> R) -> impl Fn(V) -> R {
    f
}

This makes me think that there is some weird interaction between the panic! macro and the type checker or something.

Meta

Tried on both the latest stable and nightly versions. Both produce the same output.

rustc --version --verbose:

> rustc -V
rustc 1.52.1 (9bc8c42bb 2021-05-09)
@WalterSmuts WalterSmuts added the C-bug Category: This is a bug. label May 15, 2021
@jonas-schievink
Copy link
Contributor

duplicate of #69882

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants