Apply spread operator when delegating variadic static funcs to companion - #261
Merged
Merged
Conversation
A public/internal static function is emitted as an `override` on the type's
`companion object` plus a delegating `open fun` on `CompanionClass` that calls
back into the concrete implementation. For a variadic parameter the delegating
member is declared `vararg`, but `appendCompanionClassDelegatingMember` forwarded
the argument by bare name, so inside the delegate the parameter has its Array
type and Kotlin rejects the call:
Argument type mismatch: actual type is 'kotlin.Array<...>',
but 'kotlin.String' was expected.
Prefix the forwarded argument with the spread ("*") operator when the parameter
is variadic, for both the labeled and unlabeled argument forms.
Fixes skiptools#64.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Motivation
When a
public/internalstatic function has a variadic parameter, the generated companion delegation drops the spread operator, producing uncompilable Kotlin (issue #64):Root cause
A static function is transpiled to an
overrideon the type'scompanion objectplus a delegatingopen funon the innerCompanionClassthat calls back into the concrete implementation. Both are declared withvararg, butappendCompanionClassDelegatingMember(KotlinStatementTypes.swift) forwarded each argument by bare name. Inside the delegate avarargparameter has itsArraytype, so the call needs the spread (*) operator.Before:
After:
Fix
Prefix the forwarded argument with
*when the parameter is variadic — for both the unlabeled (*libraryNames) and labeled (names = *names) argument forms.Testing
StaticTests.testStaticVariadicSpreadasserts the spread is emitted for both the unlabeled and labeled variadic forms (byte-for-byte against the generated Kotlin).swift testsuite green (923 tests, 0 failures).