Skip to content

Conversation

@ernestognw
Copy link
Member

@ernestognw ernestognw commented Oct 3, 2025

This PR implements JavaScript-like slice and splice functions for value type arrays (uint256[], bytes32[], address[]) in the OpenZeppelin Arrays library.

Key features:

  • slice: Creates a new array containing elements from a specified range (non-mutating)
  • splice: Modifies the original array in-place, keeping elements from a specified range (mutating)
  • Both functions support start-only and start/end variants with proper bounds sanitization
  • Uses memory-safe mcopy instruction for efficient array operations
  • Includes comprehensive test coverage with both JavaScript and Solidity fuzz tests

Breaking change: Requires pragma bump to ^0.8.24 for Arrays and all dependent contracts to support the mcopy instruction.

This addresses issue #5723 and provides developers with familiar array manipulation utilities similar to JavaScript's Array methods.

PR Checklist

  • Tests
  • Documentation
  • Changeset entry (run npx changeset add)

@ernestognw ernestognw requested a review from a team as a code owner October 3, 2025 20:17
@changeset-bot
Copy link

changeset-bot bot commented Oct 3, 2025

🦋 Changeset detected

Latest commit: e1fbbc9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openzeppelin-solidity Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ernestognw ernestognw changed the title Add Array.slice and Array.splice for value type arrays Add Array.slice and Array.splice for value type arrays Oct 3, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 3, 2025

Walkthrough

  • Adds a minor changeset noting new Arrays slice/splice utilities for value-type arrays.
  • Implements in-memory slice and in-place splice for uint256[], bytes32[], and address[] in contracts/utils/Arrays.sol, with start/end sanitization and memory-safe copies.
  • Updates contracts/utils/Bytes.sol comments to state splice mirrors JavaScript Array.splice.
  • Extends scripts/generate/templates/Arrays.js to generate slice/splice helpers for value types.
  • Introduces Solidity tests (test/utils/Arrays.t.sol) validating slice/splice across types and ranges with assertion helpers.
  • Refactors JavaScript tests to chai-as-promised “eventually” style and adds comprehensive slice/splice suites (test/utils/Arrays.test.js).

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title clearly and concisely summarizes the main change of the pull request by stating that Array.slice and Array.splice are being added for value type arrays, which matches the core of the implemented functionality.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
Description Check ✅ Passed The provided pull request description clearly outlines the addition of JavaScript-like slice and splice functions for value-type arrays, detailing both start-only and start/end variants, memory-safe implementations, comprehensive test coverage, and the required pragma bump, all of which directly correspond to the changes introduced across the Arrays library, templates, and tests.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ernestognw
Copy link
Member Author

ernestognw commented Oct 3, 2025

The only downside of this PR is that we'd need to bump pragma of Arrays.sol to 0.8.24 to support mcopy (and its dependants)
I would say it's fine since we're bumping it in other places as well. If we decide to get this in 5.5 then it would go along all the other pragma bumps

@ernestognw ernestognw added this to the 5.5-final milestone Oct 8, 2025
Amxx
Amxx previously approved these changes Oct 8, 2025
james-toussaint
james-toussaint previously approved these changes Oct 9, 2025
Comment on lines 396 to 397
uint256 length = array.length;
end = Math.min(end, length);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint256 length = array.length;
end = Math.min(end, length);
end = Math.min(end, array.length);

@Amxx Amxx dismissed stale reviews from james-toussaint and themself via 041b35a October 9, 2025 08:05
@Amxx Amxx enabled auto-merge (squash) October 9, 2025 15:23
Amxx
Amxx previously approved these changes Oct 9, 2025
auto-merge was automatically disabled October 9, 2025 15:35

Pull Request is not mergeable

@Amxx Amxx merged commit 9cc4d59 into OpenZeppelin:master Oct 9, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants