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

automock fails for method taking Option<&u32> #571

Open
qwandor opened this issue Apr 24, 2024 · 5 comments
Open

automock fails for method taking Option<&u32> #571

qwandor opened this issue Apr 24, 2024 · 5 comments

Comments

@qwandor
Copy link

qwandor commented Apr 24, 2024

Trying to mock the following trait fails:

#[mockall::automock]
trait Foo {
    fn bar(&self, x: Option<&u32>);
}

with a confusing error message:

error[E0106]: missing lifetime specifier
 --> src/main.rs:3:29
  |
3 |     fn bar(&self, x: Option<&u32>);
  |                             ^ expected named lifetime parameter
  |
  = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider making the bound lifetime-generic with a new `'a` lifetime
  |
1 ~ for<'a> #[mockall::automock]
2 | trait Foo {
3 ~     fn bar(&self, x: Option<&'a u32>);
  |
help: consider introducing a named lifetime parameter
  |
1 ~ #[mockall::automock]<'a>
2 | trait Foo {
3 ~     fn bar(&self, x: Option<&'a u32>);
  |

error[E0637]: `&` without an explicit lifetime name cannot be used here
 --> src/main.rs:3:29
  |
3 |     fn bar(&self, x: Option<&u32>);
  |                             ^ explicit lifetime name needed here
  |
help: consider introducing a higher-ranked lifetime here
  |
1 ~ for<'a> #[mockall::automock]
2 | trait Foo {
3 ~     fn bar(&self, x: Option<&'a u32>);
  |

Some errors have detailed explanations: E0106, E0637.
For more information about an error, try `rustc --explain E0106`.

If there is some reason this can't be supported, the error message should at least be more helpful in explaining why.

@asomers
Copy link
Owner

asomers commented Apr 24, 2024

Have you tried adding a lifetime parameter, like this?

fn bar<'a>(&'a self, x: Option<&'a u32>);

@t-moe
Copy link

t-moe commented Jun 20, 2024

I can confirm this can be solved by desugaring the lifetimes as mentioned above. It would be nice if the automock macro can do this automatically.

@sophokles73
Copy link

What if I want to mock a trait from another crate that I cannot simply add the lifetimes to?

@asomers
Copy link
Owner

asomers commented Jun 21, 2024

What if I want to mock a trait from another crate that I cannot simply add the lifetimes to?

Then you'll have to manually write a mock function. You can wrap a Mockall function if you want, in order to get access to Mockall's expectations.

@metatoaster
Copy link

metatoaster commented Jun 27, 2024

Yes as noted, this can be manually solved even if the trait is from another crate, and that the failure appears to be general for Option<&T> (or possibly all generic types where the type parameter is a reference). I ran into this exact issue with trying to mock a trait that looks something like from a different crate:

pub trait SomeTrait {
    fn insert(&self, description: Option<&str>);
}

The natural solution using the mock! macro will result in a similar error as the one that was reported:

mock! { 
    pub Platform {}

    impl SomeTrait for Platform {
        fn insert(&self, description: Option<&str>);
    }
}

The workaround will require creating the mocked fn manually inside the definition, and then impl SomeTrait for the mock outside the mock! macro and have the impl for the mock point to that mocked fn, like so:

mock! { 
    pub Platform {
        pub fn some_trait_insert<'a>(&self, description: Option<&'a str>);
    }
}

impl SomeTrait for MockPlatform {
    fn insert(&self, description: Option<&str>) {
        self.some_trait_insert(description)
    }   
}   

But yes, it would be great if this workaround isn't needed, or an error message be generated to recommend a workaround like so.

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

5 participants