Skip to content

impl Trait in return type does not compile if function returns panic!() / todo!() / unimplemented!() #102410

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
UditDey opened this issue Sep 28, 2022 · 3 comments
Labels
C-bug Category: This is a bug.

Comments

@UditDey
Copy link

UditDey commented Sep 28, 2022

The following code fails to compile:

trait Trait {}

struct Struct;

impl Trait for Struct {}

fn example() -> impl Trait {
    panic!()
}
error[E0277]: the trait bound `(): Trait` is not satisfied
 --> src/main.rs:7:17
  |
7 | fn example() -> impl Trait {
  |                 ^^^^^^^^^^ the trait `Trait` is not implemented for `()`
  |
  = help: the trait `Trait` is implemented for `Struct`

However, replacing the impl Trait with the regular generics syntax compiles fine:

fn example<T: Trait>() -> T {
    panic!()
}

This happens only if there is a single path in the function terminating in a panic!(). The following compiles fine too:

fn example(b: bool) -> impl Trait {
    if b {
        Struct {}
    }
    else {
        panic!()
    }
}

This also happens with todo!() and unimplemented!() since those macros are also basically just panic!()s.

If my understanding is correct, impl Trait and <T: Trait> should behave identically, but clearly they don't and the typecheck fails in the impl Trait version. This means that, for example, using todo!() in place of unfinished code is not possible in a function returning impl Trait.

This occurs on 1.64.0 stable as well as nightly

@UditDey UditDey added the C-bug Category: This is a bug. label Sep 28, 2022
@UditDey
Copy link
Author

UditDey commented Sep 28, 2022

The same behavior can be seen with !. With impl Trait this does not compile, but replacing it with <T: Trait> works fine:

trait Trait {}

struct Struct;

impl Trait for Struct {}

fn never_return() -> ! {
    loop {}
}

fn example() -> impl Trait {
    never_return()
}

In fact, the error message seems to be incorrect as well:

error[E0277]: the trait bound `(): Trait` is not satisfied
  --> src/main.rs:11:17
   |
11 | fn example() -> impl Trait {
   |                 ^^^^^^^^^^ the trait `Trait` is not implemented for `()`
   |
   = help: the trait `Trait` is implemented for `Struct`

Shouldn't it be trait bound '!: Trait' is not satisfied? If the return type of panic!() is taken as ! then this is where the problem stems from

@memoryruins
Copy link
Contributor

Duplicate of #36375

@thomcc
Copy link
Member

thomcc commented Dec 14, 2022

Closed as a duplicate.

@thomcc thomcc closed this as completed Dec 14, 2022
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

3 participants