Skip to content

Preserve nil map zero values and write panics - #174

Merged
tylerlaprade merged 5 commits into
masterfrom
codex/dev128-nil-map-zero
Nov 24, 2026
Merged

tylerlaprade merged 5 commits into
masterfrom
codex/dev128-nil-map-zero

Conversation

@tylerlaprade

@tylerlaprade tylerlaprade commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Reproduction

var values map[string]int
value, ok := values["missing"]
fmt.Println(values == nil, len(values), value, ok)
defer func() { fmt.Println(recover()) }()
values["new"] = 7

Go prints true 0 0 false, then assignment to entry in nil map. Master allocated an empty BTreeMap, printed false 0 0 false, accepted the write, and recovered <nil>.

A zero-valued struct map field exposed the related read boundary: plain lookup, comma-ok lookup, and range unwrapped None instead of producing Go's zero/empty behavior.

Change

Unnamed local map declarations without an initializer now keep the Go nil zero value. Their plain and compound writes still evaluate keys and values first, then raise Go's runtime message if the map remains nil. Named maps retain their existing wrapper representation.

Map-field plain lookup, comma-ok lookup, and range now use the nil-tolerant handle path. Initialized map fields continue to use the same value-copy behavior.

Differential evidence

The fixture compares actual Go 1.24 and translated Rust 1.100 output and exit behavior for:

  • an initialized make(map...) control;
  • nil local and struct-field equality, lookup, comma-ok lookup, length, and range;
  • key/value side effects before a plain assignment panic;
  • compound assignment panic; and
  • recovered runtime text.

Eleven selected differential programs pass, covering nil and initialized map selectors, named-map fields, promoted pointer map fields, ordinary maps, source-translated path/filepath, hostname, and exec.ExitError. This is representative coverage, not a percentile.

The Go unit-test ratchet is unchanged at its 33 documented failures. Focused map/evaluation-order/value-copy tests, go vet ./go, and git diff --check pass.

The initial CI run found two new unit-ratchet entries. Commit 0985f66 fixes the named-map initialization regression and updates the selector value-copy assertion to the new nil-tolerant path; both reproduce and pass locally.

Known boundary

Go recovers the nil-map assignment payload as an error; translated Rust currently recovers a string. That pre-existing dynamic-type transport gap is tracked separately in DEV-73 / PR #97.

Linear: DEV-68 (DEV-128 was closed as a duplicate)

— Codex

Summary by Sourcery

Preserve Go nil-map semantics across local declarations, field access, reads, ranges, and writes.

New Features:

  • Preserve nil zero values for unnamed local maps and expose Go-compatible nil behavior for map-field reads and ranges.

Bug Fixes:

  • Make nil map writes and compound assignments evaluate keys and values before panicking with Go's nil-map assignment message.
  • Return Go-compatible zero values and empty results for nil map lookups, comma-ok lookups, length checks, and ranges.
  • Retain existing named-map representations and initialized-map value-copy behavior.

Tests:

  • Add differential coverage for nil and initialized maps, evaluation order, compound assignments, field access, ranges, and recovered panic output.

@sourcery-ai

sourcery-ai Bot commented Nov 23, 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 2 days and 5 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@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-23T18:34:12.700050Z
We couldn’t complete this review. Comment @codex review to try again.
fa152d4 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.

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 21 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e99009f2-86e3-4c4e-9865-fbdf7fe38d72
Commits

Reviewing files that changed from the base of the PR and between 8929444 and 5472f5d.

Files selected for processing: 23

  • go/context.go
  • go/expr.go
  • go/stmt.go
  • go/stmt_test.go
  • go/transpile.go
  • go/value_copy_test.go
  • tests.bats
  • tests/anonymous_structs_nested/main.rs
  • tests/blank_assignment_of_nil_values/main.rs
  • tests/blank_assignment_of_nil_values_goroutines/main.rs
  • tests/defer_named_return_map_capture/main.rs
  • tests/goroutine_panic_recover/main.rs
  • tests/map_comma_ok_value_copy/main.rs
  • tests/map_comma_ok_value_copy_goroutines/main.rs
  • tests/map_nil_assignment/Cargo.toml
  • tests/map_nil_assignment/expected_output.txt
  • tests/map_nil_assignment/main.go
  • tests/map_nil_assignment/main.rs
  • tests/map_range_key_value_reuse/main.rs
  • tests/method_closure_receiver_capture/main.rs
  • tests/pointer_receiver_deref_assignment/main.rs
  • tests/range_loops/main.rs
  • tests/slice_elem_struct_ptr_index/main.rs
  • 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

@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.

@sourcery-ai

sourcery-ai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Data structures

Add per-file and per-function tracking for uninitialized local map variables so later write translation can distinguish nil zero values from initialized maps.

  • go/context.go L108-712 (5 small edits)
  • go/transpile.go L22

Implementation

Preserve nil zero values for local maps and generate nil-safe reads, ranges, and Go-compatible panics for writes to tracked nil maps.

  • go/expr.go L15205
  • go/stmt.go L2163-2175, L2719-2747 (+12 small edits)

Tests

Add focused assertions for nil-map declarations, write evaluation order and panics, and nil-tolerant field reads and ranges.

  • go/stmt_test.go L6576-6627
  • go/value_copy_test.go L314
  • tests.bats L2066-2069
  • tests/map_nil_assignment/Cargo.toml L1-11
  • tests/map_nil_assignment/expected_output.txt L1-8
  • tests/map_nil_assignment/main.go L1-54
  • tests/map_nil_assignment/main.rs L1-927

Regenerate Rust fixture snapshots affected by the nil-tolerant map access and zero-value translation changes.

  • tests/anonymous_structs_nested/main.rs L1096
  • tests/blank_assignment_of_nil_values/main.rs L812
  • tests/blank_assignment_of_nil_values_goroutines/main.rs L974
  • tests/defer_named_return_map_capture/main.rs L746
  • tests/goroutine_panic_recover/main.rs L1124
  • tests/map_comma_ok_value_copy/main.rs L861-870, L913-915
  • tests/map_comma_ok_value_copy_goroutines/main.rs L1095-1097
  • tests/map_range_key_value_reuse/main.rs L762
  • tests/method_closure_receiver_capture/main.rs L816
  • tests/pointer_receiver_deref_assignment/main.rs L753
  • tests/range_loops/main.rs L894
  • tests/slice_elem_struct_ptr_index/main.rs L1352, L1362, L1370

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@tylerlaprade tylerlaprade changed the title Preserve nil zero values and write panics for local maps Preserve nil map zero values and write panics Nov 23, 2026
Fixtures whose generated code the merged translator changes: anonymous_structs_nested blank_assignment_of_nil_values blank_assignment_of_nil_values_goroutines defer_named_return_map_capture goroutine_panic_recover map_comma_ok_value_copy map_comma_ok_value_copy_goroutines map_nil_assignment map_range_key_value_reuse method_closure_receiver_capture pointer_receiver_deref_assignment range_loops slice_elem_struct_ptr_index

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 50746c3 into master Nov 24, 2026
0 of 2 checks passed
@tylerlaprade
tylerlaprade deleted the codex/dev128-nil-map-zero branch November 24, 2026 11:19
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.

2 participants