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

Unable to use generic argument/return type with lifetime bound #216

Closed
lilymara-onesignal opened this issue Sep 29, 2020 · 4 comments
Closed

Comments

@lilymara-onesignal
Copy link

When trying to mock a method that has a generic argument or return type that is bound by a generic lifetime, automock can't seem to generate the correct code:

use mockall::automock;

trait Foo<'a> {}

#[automock]
trait Bar {
    fn bar<'a, F>(&self, foo: F)
    where
        F: Foo<'a>;
}

This fails to compile with these errors:

    Checking mock-test v0.1.0 (/Users/nate/projects/hacks/mock-test)
error[E0601]: `main` function not found in crate `mock_test`
  --> src/main.rs:1:1
   |
1  | / use mockall::automock;
2  | |
3  | | trait Foo<'a> {}
4  | |
...  |
9  | |         F: Foo<'a>;
10 | | }
   | |_^ consider adding a `main` function to `src/main.rs`

error[E0261]: use of undeclared lifetime name `'a`
 --> src/main.rs:9:16
  |
7 |     fn bar<'a, F>(&self, foo: F)
  |                - help: consider introducing lifetime `'a` here: `'a,`
8 |     where
9 |         F: Foo<'a>;
  |                ^^ undeclared lifetime

error[E0261]: use of undeclared lifetime name `'a`
 --> src/main.rs:9:16
  |
9 |         F: Foo<'a>;
  |                ^^ undeclared lifetime
  |
help: consider introducing lifetime `'a` here
  |
5 | #[automock]<'a>
  |            ^^^^
help: consider introducing lifetime `'a` here
  |
7 |     fn bar<'a, 'a, F>(&self, foo: F)
  |                ^^^

error[E0310]: the parameter type `F` may not live long enough
 --> src/main.rs:5:1
  |
5 | #[automock]
  | ^^^^^^^^^^^ ...so that the type `__mock_MockBar_Bar::__bar::Expectations<F>` will meet its required lifetime bounds
6 | trait Bar {
7 |     fn bar<'a, F>(&self, foo: F)
  |                - help: consider adding an explicit lifetime bound...: `F: 'static`
  |
  = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0310]: the parameter type `F` may not live long enough
 --> src/main.rs:5:1
  |
5 | #[automock]
  | ^^^^^^^^^^^ ...so that the type `F` will meet its required lifetime bounds
6 | trait Bar {
7 |     fn bar<'a, F>(&self, foo: F)
  |                - help: consider adding an explicit lifetime bound...: `F: 'static`
  |
  = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 5 previous errors

Some errors have detailed explanations: E0261, E0310, E0601.
For more information about an error, try `rustc --explain E0261`.
error: could not compile `mock-test`.
@asomers-ax
Copy link

Unsurprising. Lifetime arguments are a real viper's nest. What happens if you write it like this?

use mockall::automock;

trait Foo<'a> {}

#[automock]
trait Bar {
    fn bar<'a, F: Foo<'a>>(&self, foo: F);
}

@lilymara-onesignal
Copy link
Author

That code appears to fail with the same errors

@asomers
Copy link
Owner

asomers commented Oct 1, 2020

This is harder than I expected, thanks to that Generic. Mockall uses downcasting to handle generic methods, and Rust's Any type requires 'static. That means that Mockall can't handle any non-'static type generics. I don't think I'm going to be able to implement this. The best workaround I can suggest would be to manually implement your function in terms of a mock function that has no non-'static type generics, like this:

trait Bar {
    fn bar<'a, F: Foo<'a>>(&self, foo: F);
}
mock! {
    Boo { }
    Bar {
        pub fn bean<'a>(&self, foo: &dyn Foo<'a>);
    }
}
impl Bar for MockBoo {
    fn bar<'a, F: Foo<'a>>(&self, foo: F) {
        self.bean(&foo)
    }
}

@asomers
Copy link
Owner

asomers commented Oct 2, 2020

Closing this issue in favor of the more general #217 .

@asomers asomers closed this as completed Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants