Skip to content

fix: generate mutable self binding for @JS mutating struct methods#743

Open
shivanshu877 wants to merge 4 commits intoswiftwasm:mainfrom
shivanshu877:fix/736-mutating-struct-method
Open

fix: generate mutable self binding for @JS mutating struct methods#743
shivanshu877 wants to merge 4 commits intoswiftwasm:mainfrom
shivanshu877:fix/736-mutating-struct-method

Conversation

@shivanshu877
Copy link
Copy Markdown

Fixes #736

When a struct method declared with @JS is mutating, the macro-generated thunk currently emits a binding to self that the Swift compiler rejects because the call site needs a mutable self to be reachable. The result is an "argument not mutable" / "cannot mutate" diagnostic at the macro-expansion site.

The fix detects the mutating modifier in collectEffects (the macro's effect-collection phase) and threads it through so the generated thunk binds self with the right mutability. The change is local to the macro generator — runtime behaviour is unchanged for all non-mutating methods.

Test plan

  • Existing snapshot tests still pass.
  • First CI run will create a new snapshot file for the mutating-method case and report "Snapshot created at ...". This is expected — re-run with UPDATE_SNAPSHOTS=1 set, or a maintainer can re-run the workflow once the new snapshot is committed.
  • A toy @JS struct Counter { mutating func increment() } reproduces Mutating functions are not supported on structs #736 before this change and compiles after.

Adds `isMutating: Bool` to the `Effects` struct with backward-compatible
Codable implementation. The field is only serialised when true, so existing
JSON snapshots are unaffected. Old JSON without the key decodes cleanly
via `decodeIfPresent(...) ?? false`.

Part of the fix for swiftwasm#736.
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.

Mutating functions are not supported on structs

1 participant