Skip to content

Commit aa899f9

Browse files
committed
fix(derive): allow derive(Arbitrary) for struct Option
Use the full path to `core::option::Option` and `core::default::Default` in the generated code. This allows `derive(Arbitrary)` on structs named `Option` or `Default`.
1 parent 915848a commit aa899f9

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

derive/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ fn construct(
341341
fn construct_take_rest(fields: &Fields) -> Result<TokenStream> {
342342
construct(fields, |idx, field| {
343343
determine_field_constructor(field).map(|field_constructor| match field_constructor {
344-
FieldConstructor::Default => quote!(Default::default()),
344+
FieldConstructor::Default => quote!(::core::default::Default::default()),
345345
FieldConstructor::Arbitrary => {
346346
if idx + 1 == fields.len() {
347347
quote! { arbitrary::Arbitrary::arbitrary_take_rest(u)? }
@@ -392,7 +392,7 @@ fn gen_size_hint_method(input: &DeriveInput) -> Result<TokenStream> {
392392
size_hint_fields(fields).map(|hint| {
393393
quote! {
394394
#[inline]
395-
fn size_hint(depth: usize) -> (usize, Option<usize>) {
395+
fn size_hint(depth: usize) -> (usize, ::core::option::Option<usize>) {
396396
arbitrary::size_hint::recursion_guard(depth, |depth| #hint)
397397
}
398398
}
@@ -414,7 +414,7 @@ fn gen_size_hint_method(input: &DeriveInput) -> Result<TokenStream> {
414414
.map(|variants| {
415415
quote! {
416416
#[inline]
417-
fn size_hint(depth: usize) -> (usize, Option<usize>) {
417+
fn size_hint(depth: usize) -> (usize, ::core::option::Option<usize>) {
418418
arbitrary::size_hint::and(
419419
<u32 as arbitrary::Arbitrary>::size_hint(depth),
420420
arbitrary::size_hint::recursion_guard(depth, |depth| {
@@ -429,7 +429,7 @@ fn gen_size_hint_method(input: &DeriveInput) -> Result<TokenStream> {
429429

430430
fn gen_constructor_for_field(field: &Field) -> Result<TokenStream> {
431431
let ctor = match determine_field_constructor(field)? {
432-
FieldConstructor::Default => quote!(Default::default()),
432+
FieldConstructor::Default => quote!(::core::default::Default::default()),
433433
FieldConstructor::Arbitrary => quote!(arbitrary::Arbitrary::arbitrary(u)?),
434434
FieldConstructor::With(function_or_closure) => quote!((#function_or_closure)(u)?),
435435
FieldConstructor::Value(value) => quote!(#value),

tests/derive.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,20 @@ fn test_field_attributes() {
300300
// 17 is the 3rd byte used by arbitrary
301301
assert_eq!(parcel.price, 17);
302302
}
303+
304+
#[test]
305+
fn derive_structs_named_same_as_core() {
306+
#[derive(Debug, Arbitrary)]
307+
struct Option {
308+
f: core::option::Option<u32>,
309+
}
310+
311+
let _ = Option::arbitrary(&mut Unstructured::new(&[]));
312+
313+
#[derive(Debug, Default, Arbitrary)]
314+
struct Default {
315+
f: u32,
316+
}
317+
318+
let _ = Default::arbitrary(&mut Unstructured::new(&[]));
319+
}

0 commit comments

Comments
 (0)