Skip to content

fix(sol-macro): derive Default for dynamic arrays of non-Default elements - #1129

Merged
DaniPopes merged 3 commits into
alloy-rs:mainfrom
satyakwok:fix/default-dynamic-array-non-default-elem
Jul 3, 2026
Merged

DaniPopes merged 3 commits into
alloy-rs:mainfrom
satyakwok:fix/default-dynamic-array-non-default-elem

Conversation

@satyakwok

Copy link
Copy Markdown
Contributor

A dynamic Solidity array maps to Vec<T>, whose Default impl holds for any T. can_derive_default still required the element type to be Default for dynamic arrays, so a struct/error with a field like MyEnum[] lost its Default derive under #[sol(all_derives)] even though Vec<MyEnum>: Default.

This mirrors how the field's Rust type is generated (expand_rust_type_to): Some(size) → [T; N], None → Vec<T>. Only fixed-size arrays need T: Default (and N <= 32); dynamic arrays are always Default.

Added a regression test (test_default_dynamic_array_of_non_default_elem).

…ents

A dynamic Solidity array maps to Vec<T>, whose Default impl holds for any T.
can_derive_default still required the element type to be Default for dynamic
arrays, so a struct/error with a field like MyEnum[] lost its Default derive
under #[sol(all_derives)] even though Vec<MyEnum>: Default.

Only require the element to be Default (and length <= 32) for fixed-size arrays
([T; N]); dynamic arrays are always Default.
@satyakwok
satyakwok force-pushed the fix/default-dynamic-array-non-default-elem branch from afa7bd4 to 066b214 Compare June 14, 2026 14:16
@satyakwok

satyakwok commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor Author

@DaniPopes, @mattsse small follow-up near the same area as #1124 — can_derive_default required the element to be Default even for dynamic arrays, but T[] is Vec<T> which is always Default (only fixed [T; N] needs T: Default). Mirrors the eval_array_size predicate from expand_rust_type_to. Mind taking a look when you get a chance?

@github-project-automation github-project-automation Bot moved this to Reviewed in Alloy Jun 30, 2026
@DaniPopes
DaniPopes enabled auto-merge (squash) June 30, 2026 12:12
DaniPopes added 2 commits July 3, 2026 15:44
…rray-non-default-elem

# Conflicts:
#	crates/sol-types/tests/derives.rs
@DaniPopes
DaniPopes merged commit ea33d7a into alloy-rs:main Jul 3, 2026
31 checks passed
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in Alloy Jul 3, 2026
@satyakwok
satyakwok deleted the fix/default-dynamic-array-non-default-elem branch July 4, 2026 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants