rust-fuzz / rust-fuzz/arbitrary

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

Open
#86 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
880
Forks
93
PR merge metrics
No merged PRs in 30d

Description

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!");
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure using the arbitrary::Arbitrary derive on the WithACow example with Cow<'static, str>, then compare it with the manual Arbitrary implementation shown in the issue. Trace the derive macro entry point and make the example compile while preserving the expected Cow<'static, str> type.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.