Skip to content

Commit

Permalink
Made it so an error occurs when an unsupported attribute is specified…
Browse files Browse the repository at this point in the history
… for enum variants. (fix #12)
  • Loading branch information
frozenlib committed May 21, 2024
1 parent 6d66057 commit 637f29e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,21 @@ fn expr_for_enum(input: &DeriveInput, data: &DataEnum, bounds: &mut Bounds) -> R
let args: ArbitraryArgsForFieldOrVariant = parse_from_attrs(&variant.attrs, "arbitrary")?;
let mut weight = None;
for attr in &variant.attrs {
if attr.path().is_ident("weight") {
let Some(ident) = attr.path().get_ident() else {
continue;
};
if ident == "weight" {
if weight.is_some() {
bail!(attr.span(), "`#[weight]` can specify only once.");
}
weight = Some(attr.parse_args::<WeightArg>()?);
}
if ident == "any" || ident == "strategy" || ident == "map" || ident == "by_ref" {
bail!(
attr.span(),
"`#[{ident}]` cannot be specified for a variant. Consider specifying it for a field instead."
);
}
}
let weight = if let Some(arg) = weight {
if arg.is_zero() {
Expand Down
12 changes: 6 additions & 6 deletions src/syn_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ use syn::{
};

macro_rules! bail {
($span:expr, $message:literal $(,)?) => {
return std::result::Result::Err(syn::Error::new($span, $message))
(_, $($arg:tt)*) => {
bail!(::proc_macro2::Span::call_site(), $($arg)*)
};
($span:expr, $err:expr $(,)?) => {
return std::result::Result::Err(syn::Error::new($span, $err))
($span:expr, $fmt:literal $(,)?) => {
return ::std::result::Result::Err(::syn::Error::new($span, ::std::format!($fmt)))
};
($span:expr, $fmt:expr, $($arg:tt)*) => {
return std::result::Result::Err(syn::Error::new($span, std::format!($fmt, $($arg)*)))
($span:expr, $fmt:literal, $($arg:tt)*) => {
return ::std::result::Result::Err(::syn::Error::new($span, ::std::format!($fmt, $($arg)*)))
};
}

Expand Down
8 changes: 8 additions & 0 deletions tests/compile_fail/variant_any_attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use test_strategy::Arbitrary;
#[derive(Arbitrary, Debug)]
enum X {
#[any]
A,
}

fn main() {}
5 changes: 5 additions & 0 deletions tests/compile_fail/variant_any_attr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: `#[any]` cannot be specified for a variant. Consider specifying it for a field instead.
--> tests/compile_fail/variant_any_attr.rs:4:5
|
4 | #[any]
| ^
8 changes: 8 additions & 0 deletions tests/compile_fail/variant_strategy_attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use test_strategy::Arbitrary;
#[derive(Arbitrary, Debug)]
enum X {
#[strategy(0..10usize)]
A(usize),
}

fn main() {}
5 changes: 5 additions & 0 deletions tests/compile_fail/variant_strategy_attr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: `#[strategy]` cannot be specified for a variant. Consider specifying it for a field instead.
--> tests/compile_fail/variant_strategy_attr.rs:4:5
|
4 | #[strategy(0..10usize)]
| ^

0 comments on commit 637f29e

Please sign in to comment.