Commit 9711dd2a09 for golang

commit 9711dd2a09532c03db000c6f9d0528ff954fa951
Author: Anand T <anandprince100@gmail.com>
Date:   Thu Nov 19 18:22:45 2026 +0000

    encoding/gob: assign ids to recursive map element types

    When a slice of pointers was encoded first, a recursive struct could
    construct a map referring back to the slice before the slice had a type
    ID. The map recorded element ID zero and the decoder rejected the field.

    Assign an ID to an unfinished map element before recording it, following
    the existing slice and struct handling. Add a round-trip regression with
    nested children, decoding the pointer slice into a slice of values.

    Fixes #47542

    Change-Id: I42718caa64a3d2d75bd9f0025542bdf2e9009247
    GitHub-Last-Rev: 20f75907fcdf4a27676a744278feb25c1c9b2eab
    GitHub-Pull-Request: golang/go#83218
    Reviewed-on: https://go-review.googlesource.com/c/go/+/865211
    Reviewed-by: Sean Liao <sean@liao.dev>
    Reviewed-by: David Chase <drchase@google.com>
    Auto-Submit: Sean Liao <sean@liao.dev>
    LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
    Reviewed-by: Rob Pike <r@golang.org>
    Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>

diff --git a/src/encoding/gob/codec_test.go b/src/encoding/gob/codec_test.go
index ec56ad50b2..41c70779d4 100644
--- a/src/encoding/gob/codec_test.go
+++ b/src/encoding/gob/codec_test.go
@@ -1019,6 +1019,31 @@ type Direct struct {
 	M map[string]int
 }

+type recursiveMapSlice struct {
+	ID       string
+	Children map[string][]*recursiveMapSlice
+}
+
+func TestRecursiveMapSlice(t *testing.T) {
+	want := []*recursiveMapSlice{{
+		ID: "parent",
+		Children: map[string][]*recursiveMapSlice{
+			"children": {{ID: "child"}},
+		},
+	}}
+	var buffer bytes.Buffer
+	if err := NewEncoder(&buffer).Encode(want); err != nil {
+		t.Fatal(err)
+	}
+	var got []recursiveMapSlice
+	if err := NewDecoder(&buffer).Decode(&got); err != nil {
+		t.Fatal(err)
+	}
+	if len(got) != 1 || !reflect.DeepEqual(got[0], *want[0]) {
+		t.Errorf("round trip = %#v, want %#v", got, want)
+	}
+}
+
 func TestIndirectSliceMapArray(t *testing.T) {
 	// Marshal indirect, unmarshal to direct.
 	i := new(Indirect)
diff --git a/src/encoding/gob/type.go b/src/encoding/gob/type.go
index b7a2c9c0e6..6ba62dc6ff 100644
--- a/src/encoding/gob/type.go
+++ b/src/encoding/gob/type.go
@@ -360,6 +360,11 @@ func newMapType(name string) *mapType {
 func (m *mapType) init(key, elem gobType) {
 	// Set our type id before evaluating the element's, in case it's our own.
 	setTypeId(m)
+	// The element can be a slice still being constructed through a recursive
+	// struct field. Give it an id before recording the map's element type.
+	if elem.id() == 0 {
+		setTypeId(elem)
+	}
 	m.Key = key.id()
 	m.Elem = elem.id()
 }