proptest-rs / proptest

Hypothesis-like property testing for Rust
Apache License 2.0
1.63k stars 152 forks source link

Using `prop_oneof` with a `cfg` guard results in compile errors #430

Open bruceg opened 4 months ago

bruceg commented 4 months ago

I have an enum in which some of the fields are only present when a non-default feature is enabled:

#[derive(Clone, Debug)]
enum Foo {
    A,
    #[cfg(feature = "dev")]
    B,
}

Trying to implement Arbitrary manually for this enum results in a compiler error:

impl Arbitrary for Foo {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;
    fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
        prop_oneof![
            Just(Self::A),
            #[cfg(feature = "dev")]
            Just(Self::B),
        ]
        .boxed()
    }
}
    Checking testproptest v0.1.0 (/tmp/testproptest)
error[E0061]: this function takes 1 argument but 0 arguments were supplied
  --> src/lib.rs:14:9
   |
14 | /         prop_oneof![
15 | |             Just(Self::A),
16 | |             #[cfg(feature = "dev")]
17 | |             Just(Self::B),
18 | |         ]
   | |_________^ an argument is missing
   |
note: associated function defined here
  --> /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/alloc/src/sync.rs:386:12
   = note: this error originates in the macro `$crate::prop_oneof` which comes from the expansion of the macro `prop_oneof` (in Nightly builds, run with -Z macro-backtrace for more info)
help: provide the argument
  --> /home/bruce.guenter/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proptest-1.4.0/src/sugar.rs:348:53
   |
348|              ($weight1, $crate::std_facade::Arc::new(/* data */))))
   |                                                     ~~~~~~~~~~~~

Is there any way to use prop_oneof with cfg guards, or do the guards have to be outside of the macro?