Nonsensical error message about mismatched types

With the code

fn foo(env: &mut String)
    -> impl FnMut(&str) -> nom::IResult<&str, &str> {

on rustc 1.55.0 and nom 7 I get the error

error[E0308]: mismatched types
  --> src/
2  |     -> impl FnMut(&str) -> nom::IResult<&str, &str> {
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
  ::: /Users/dan/.cargo/registry/src/
35 | ) -> impl Fn(Input) -> IResult<Input, Input, Error>
   |      ----------------------------------------------
   |      |
   |      the expected opaque type
   |      the found opaque type
   = note: expected associated type `<impl Fn<(&str,)> as FnOnce<(&str,)>>::Output`
              found associated type `<impl Fn<(&str,)> as FnOnce<(&str,)>>::Output`

Why are the expected and found types the same? This seems like a bug to me. (Unfortunately I couldn't find an example for the error that didn't involve nom.)

1 Like

When you encounter bizarre errors like this one it's usually due to for<'a> lifetimes. I have a minimal repro and I think that if you tie the lifetime of env: &mut String with the lifetime of the &str the closure takes, your code will work (although you'll likely want to make the result of the closure be tied to its input as well, otherwise you'll likely get other errors).

Can you file a ticket asking to improve the diagnostic?

1 Like

Thanks! I pared down your code a bit and filed


I think the best way to fix this is by using |s| nom::bytes::complete::tag("foo")(s) instead of nom::bytes::complete::tag("foo").


That is indeed a common solution for these... I should turn it into a machine applicable suggestion :thinking:


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.