Emit range-based arity check throwing ArgumentsRangeMismatch#19
Merged
Merged
Conversation
730a91f to
58bddda
Compare
58bddda to
217c5a9
Compare
…ismatch The synthesized `@JS` function binding validated arity with an exact `arguments.count == <total>` guard and threw an ad-hoc `Exception(name: "InvalidArgumentCount", ...)`, over-rejecting omitted trailing optionals and ignoring author-written default values. Compute `required` (total minus the trailing run of params that have a default value or an optional type) and `max` (total). Emit an exact guard when `required == max`, otherwise `guard arguments.count >= required && arguments.count <= max`, both throwing core's new `Exceptions.ArgumentsRangeMismatch`. When the trailing run is non-empty, decode the required prefix once and `switch` on `arguments.count`: an omitted defaulted param drops its label (Swift applies the default), an omitted optional param is passed `nil`. Share `isOptionalType`, `isOmittable`, and `hasDefaultValue` via `MacroHelpers` (removing the private copy of `isOptionalType` in `RecordMacro`).
217c5a9 to
c820879
Compare
Kudo
approved these changes
Jun 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The synthesized
@JSfunction binding validated arity with an exactarguments.count == <total>guard and threw an ad-hocException(name: "InvalidArgumentCount", ...). That over-rejects an omitted trailing optional, ignores author-written default values, and isn't a typed exception. A function with trailing defaulted or optional parameters accepts a range of arities, not one exact count.Paired with expo/expo#46901, which adds the public
Exceptions.ArgumentsRangeMismatchexception this generated code throws. The two must land together: the code emitted here references that core exception.How
In
DecorateModuleBuilder, computerequired(total minus the trailing run of parameters that have a default value or an optional type) andmax(total). Emit an exact guard whenrequired == max, otherwiseguard arguments.count >= required && arguments.count <= max, both throwingExceptions.ArgumentsRangeMismatch.When the trailing run is non-empty, decode the required prefix once and
switchonarguments.count:nil;arguments[i]traps pastcount, so a slot the caller didn't pass is never indexed).Also shared
isOptionalTypeviaMacroHelpers(removing the private copy inRecordMacro).Example
A function with a trailing defaulted parameter:
now generates a range guard and a per-arity call:
When every parameter is required the guard collapses to an exact
arguments.count == <total>check and the body is a flat decode-then-call (noswitch).Test Plan
swift test, 86 passing, including new expansion cases:switch);nilwhen omitted, Void return path);required == 0).Existing exact-arity expectations were updated to the new exception form.