rust-fuzz / arbitrary

Generating structured data from arbitrary, unstructured input.

Home Page:https://docs.rs/arbitrary/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deriving `Arbitrary` on a field containing a `Cow<'static, str>` fails.

teymour-aldridge opened this issue · comments

For example, the following code (using version 1.0.1) fails to compile.

use std::borrow::Cow;

#[derive(arbitrary::Arbitrary)]
pub struct WithACow {
    cow: Cow<'static, str>,
}

fn main() {
    println!("Hello, world!");
}

With the error message

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
 --> src/main.rs:3:10
  |
3 | #[derive(arbitrary::Arbitrary)]
  |          ^^^^^^^^^^^^^^^^^^^^
  |
note: first, the lifetime cannot outlive the lifetime `'arbitrary` as defined on the impl at 3:10...
 --> src/main.rs:3:10
  |
3 | #[derive(arbitrary::Arbitrary)]
  |          ^^^^^^^^^^^^^^^^^^^^
note: ...so that the expression is assignable
 --> src/main.rs:3:10
  |
3 | #[derive(arbitrary::Arbitrary)]
  |          ^^^^^^^^^^^^^^^^^^^^
  = note: expected `&mut Unstructured<'_>`
             found `&mut Unstructured<'arbitrary>`
  = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the expression is assignable
 --> src/main.rs:3:10
  |
3 | #[derive(arbitrary::Arbitrary)]
  |          ^^^^^^^^^^^^^^^^^^^^
  = note: expected `Cow<'static, _>`
             found `Cow<'_, _>`
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

But something like this does compile:

use std::borrow::Cow;

use arbitrary::Arbitrary;

pub struct WithACow {
    cow: Cow<'static, str>,
}

impl<'a> Arbitrary<'a> for WithACow {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        Ok(WithACow {
            cow: Arbitrary::arbitrary(u).map(Cow::Owned)?,
        })
    }
}

fn main() {
    println!("Hello, world!");
}

@teymour-aldridge

        Ok(WithACow {
            cow: Arbitrary::arbitrary(u).map(Cow::Owned)?,
        })

works, because you're always constructing an owned data.

To generate the Borrowed variant, Arbitrary would need data with 'static lifetime, but there is no place where it can get from. So the error makes sense, it's the case where one would need to construct Cow manually.
Once this #129 gets merged, you'll be able to implement custom arbitrary for single fields, which may help to workaround this problem.