audunhalland / unimock

A versatile and developer-friendly trait mocking library
MIT License
71 stars 3 forks source link

Can't mock impl Trait params #15

Closed TheDan64 closed 1 year ago

TheDan64 commented 1 year ago
trait Bar {}

#[unimock(api=MockFoo)]
trait Foo {
    fn foo(&self, t: impl Bar);
}
error[E0658]: `impl Trait` in type aliases is unstable
  --> src/main.rs:54:22
   |
54 |     fn foo(&self, t: impl Bar);
   |                      ^^^^^^^^
   |
   = note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
error: unconstrained opaque type
  --> src/main.rs:54:22
   |
54 |     fn foo(&self, t: impl Bar);
   |                      ^^^^^^^^
   |
   = note: `Inputs` must be used in combination with a concrete type within the same module

error[E0308]: mismatched types
   --> src/main.rs:54:19
    |
52  | #[unimock(api=MockFoo)]
    | ----------------------- arguments to this function are incorrect
53  | trait Foo {
54  |     fn foo(&self, t: impl Bar);
    |                   ^  --------
    |                   |  |
    |                   |  the expected opaque type
    |                   |  this type parameter
    |                   expected opaque type, found type parameter `impl Bar`
    |
    = note: expected opaque type `_::<impl MockFn for foo>::Inputs<'_>`
            found type parameter `impl Bar`
    = help: type parameters must be constrained to match other types
    = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
note: function defined here
   --> /home/dkolsoi/.cargo/registry/src/github.com-1ecc6299db9ec823/unimock-0.4.8/src/macro_api.rs:209:8
    |
209 | pub fn eval<'u, 'i, F>(unimock: &'u Unimock, inputs: F::Inputs<'i>) -> Evaluation<'u, 'i, F>
    |        ^^^^

Even with the nightly feature gate enabled, this still fails to compile. So not sure if it's possible

audunhalland commented 1 year ago

This should be pretty similar to https://github.com/audunhalland/unimock/issues/14. Hopefully just need to detect where it says impl $Trait and make a new generic param with an auto-generated name for use in MockFn.

audunhalland commented 1 year ago

0.4.9 has been released with support for this.