-
Notifications
You must be signed in to change notification settings - Fork 59
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 undeclared lifetime
error
#299
Comments
I guess this may be related to #293. I've been seeing what's generated by using #[mockall::automock]
pub trait MyTrait {
fn do_something<'a>(&'a self, report: &'a String) -> Result<(), String>;
} This works perfectly. On the other hand, if we introduce a new generic parameter, by changing #[mockall::automock]
pub trait MyTrait {
fn do_something<'a, T>(&'a self, report: &'a T) -> Result<(), String>;
} We get an error. And the code being generated for generics doesn't take the lifetimes into account: impl GenericExpectations {
/// Simulating calling the real method.
pub fn call<T>(&self, report: &'a T) -> Option<Result<(), String>> { |
There are some limitations when mocking generic methods. First, Mockall doesn't currently doesn't support generic methods that have both generic type parameters and generic lifetime parameters. That could probably be fixed. However, your bigger problem is that Mockall doesn't support generic methods with non- I suggest two work arounds:
https://docs.rs/mockall/0.10.1/mockall/#methods-with-generic-lifetimes |
Is there a ticket or rough estimate of effort to enable this? Curious because I've started integrating mockall into a proof of concept and have a feeling I may run into this. |
No ticket for it. And no effort estimate. Sorry. |
I'm not sure if this is helpful or not, but another small example that reproduces this issue: struct Ref<'a>(&'a ());
#[mockall::automock]
trait Foo {
fn bar(&self, x: Ref<'_>) -> Ref<'_>;
// ~~ error: `'_` cannot be used here
fn baz<'a>(&'a self, x: Ref<'a>) -> Ref<'a>;
// ~~ error: use of undeclared lifetime name `'a`
fn baw<'a>(&'a self, x: Ref<'a>); // ok.
} Note that this does not involve any generic types, only generic lifetimes. |
I also ran into this problem mocking a production service, specifically a trait that returned a boxed |
I have not been able to compile the following mock (neither with
mock!
norautomock
):The error I see is:
I've tested both versions 0.9.1 and 0.10.0 (using rustc 1.53.0)
It seems like the macro misses to generate the lifetime
'a
, and then the compiler complains about it being used in the where clause, but it doesn't complain about its use in other parts of the signature.I'm pretty new to this library and I don't know where else to look at.
Please let me know if you need further information to help with the identification of the problem.
Thanks a lot ;-)
The text was updated successfully, but these errors were encountered: