Skip to content

Error when trying use trait alias for Fn() in impl #60755

Closed

Description

This code produce compiling error:

#![feature(trait_alias)]

struct MyStruct {}
trait MyFn = Fn(&MyStruct);

fn foo (_: impl MyFn) {}

fn main () {
    foo(|_| {});
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0631]: type mismatch in closure arguments
 --> src/main.rs:9:5
  |
9 |     foo(|_| {});
  |     ^^^ --- found signature of `fn(_) -> _`
  |     |
  |     expected signature of `for<'r> fn(&'r MyStruct) -> _`
  |
note: required by `foo`
 --> src/main.rs:6:1
  |
6 | fn foo (_: impl MyFn) {}
  | ^^^^^^^^^^^^^^^^^^^^^

error[E0271]: type mismatch resolving `for<'r> <[closure@src/main.rs:9:9: 9:15] as std::ops::FnOnce<(&'r MyStruct,)>>::Output == ()`
 --> src/main.rs:9:5
  |
9 |     foo(|_| {});
  |     ^^^ expected bound lifetime parameter, found concrete lifetime
  |
note: required by `foo`
 --> src/main.rs:6:1
  |
6 | fn foo (_: impl MyFn) {}
  | ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0271`.
error: Could not compile `playground`.

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

But when non aliased trait used, then no error occured. That is when we replace
fn foo (_: impl MyFn) {}
with
fn foo (_: impl Fn(&MyStruct)) {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-traitsArea: Trait systemC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.F-trait_alias`#![feature(trait_alias)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions