Skip to content

Give a struct copy's slice field its own header - #218

Merged
tylerlaprade merged 3 commits into
masterfrom
claude/struct-copy-slice-field
Nov 24, 2026
Merged

tylerlaprade merged 3 commits into
masterfrom
claude/struct-copy-slice-field

Conversation

@tylerlaprade

@tylerlaprade tylerlaprade commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Covers the slice-field case of DEV-55.

A copy of a struct kept the original's cell for a slice field. So the copy and the original shared the slice header as well as the array:

a := Order{Items: []string{"apple"}}
b := a
b.Items = append(b.Items, "pear")
b.Items[0] = "APPLE"
fmt.Println(a.Items, b.Items) // Go: [apple] [APPLE pear]   Rust: [APPLE pear] [APPLE pear]

c := a
c.Items = nil
fmt.Println(a.Items == nil)   // Go: false   Rust: true

Go copies the header. The two slices share the array, so an element write shows in both while they share it, but each has its own length. Now the Go copy of a struct (__go_value_clone) gives an unnamed slice field a cell of its own, holding a copy of the GoSlice header that shares the array. Everything that is a Go copy is fixed this way:

  • assignment
  • value receivers that append to a slice field
  • range values
  • values read out of maps and slices

Map, pointer and defined-slice fields keep their handles, as before.

Also: map[K]T{k: v} for a struct variable v did not compile (E0507, a move out of v's cell). The map now stores Go's copy of v.

Fixtures. struct_copy_slice_field and struct_copy_slice_field_goroutines cover:

  • assignment, nil and append on the copy
  • shared element writes
  • appends within capacity from both sides
  • range values
  • a value receiver that appends
  • map values
  • copies made in goroutines

Both match Go. 73 other snapshots change, each a struct with a slice field. Each was built and run, and all match Go. The ratchet is unchanged. TestGoValueCopyCallForType and TestStructValueCloneBuildsLargeStructWithStatements pinned the shared cell and now expect the new one.

Not covered. An interface or func field of the copy still shares the original's cell. Those are the rest of DEV-55.

— agent: claude-chat


Generated by Claude Code

Summary by Sourcery

Match Go’s struct-copy semantics by giving unnamed slice fields their own headers while retaining shared backing arrays.

New Features:

  • Give unnamed slice fields in copied structs independent slice headers while preserving shared backing arrays.

Bug Fixes:

  • Make struct copies behave like Go for slice reassignment, append, element updates, range values, receiver copies, map values, and goroutine copies.
  • Copy struct variables when storing them in map literals instead of moving them out of their cells.

Enhancements:

  • Preserve existing handle-sharing behavior for map, pointer, and defined-slice fields.

Tests:

  • Add plain and goroutine regression fixtures covering struct copies with slice fields.
  • Update generated snapshots and clone-generation tests for independent slice-field cells.

A copy of a struct kept the original's cell for a slice field, so the copy
and the original shared the slice header as well as the array:

    b := a
    b.Items = nil        // set a.Items to nil too
    b.Items = append(b.Items, x)  // grew a.Items too

Go copies the header: the two share the array, so an element write shows
in both, but each has its own length. The Go copy of a struct
(__go_value_clone) now gives an unnamed slice field a cell of its own
holding a copy of the GoSlice header, which shares the array. This also
fixes value receivers that append to a slice field, range values, and
values read out of maps and slices, which are all Go copies. Map, pointer
and defined slice fields keep their handles, as before.

Also: map[K]T{k: v} for a struct variable v did not compile (E0507, a
move out of v's cell). The map now stores Go's copy of v.

Fixtures: struct_copy_slice_field and its goroutine variant cover
assignment, nil and append on the copy, shared element writes, appends
within capacity from both sides, range values, a value receiver that
appends, map values and copies made in goroutines. Both match Go.
71 other snapshots change, each a struct with a slice field. Each was
built and run, and all match Go. The ratchet is unchanged;
TestGoValueCopyCallForType and
TestStructValueCloneBuildsLargeStructWithStatements now expect the new
cell.

Not covered: an interface field or a func field of the copy still shares
the original's cell (DEV-55).

Agent: claude-chat
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TkCXVghVg3khqYkpQSWyEM
@tylerlaprade tylerlaprade added the agent:claude-chat Work by the Claude chat agent (claude.ai session), acting as the repo owner label Nov 24, 2026 — with Claude
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-24T12:20:46.230010Z
We couldn’t complete this review. Comment @codex review to try again.
c14191b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@sourcery-ai

sourcery-ai Bot commented Nov 24, 2026

Copy link
Copy Markdown

Sorry @tylerlaprade, this account has used its review budget of 1,500,000 diff characters for the last 7 days.

You can request another review in 1 day and 13 hours by commenting @sourcery-ai review. Upgrade to get a review now.

…ce-field

Fixtures whose generated code the merged translator changes: fprintln_writers fprintln_writers_plain

Agent: claude-chat
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TkCXVghVg3khqYkpQSWyEM
@tylerlaprade
tylerlaprade merged commit 217549d into master Nov 24, 2026
1 of 2 checks passed
@tylerlaprade
tylerlaprade deleted the claude/struct-copy-slice-field branch November 24, 2026 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent:claude-chat Work by the Claude chat agent (claude.ai session), acting as the repo owner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants