Commit 6b5bf7e37d for golang

commit 6b5bf7e37dc1e7b0b390729be3b2e80a439fb897
Author: Jake Huneau <jakehuneau@yahoo.com>
Date:   Wed Dec 2 01:15:30 2026 +0000

    cmd/compile: don't append in place when an element can hold the slice

    For x = append(x, ...) with a non-SSAable x, the in-place path writes
    the new length to x and then stores the new elements. When x is one of
    the slice's own elements, as in

            type S []S
            s[1] = s[:1]
            s[1] = append(s[1], nil)

    the element store lands on x and overwrites the header that was just
    written, so s[1] ends up with the element's value instead of append's
    result.

    x can only lie in the slice's backing array if the element type holds
    a slice header in its own memory, directly or through struct fields
    and array elements, that x can alias. That slice type need not be
    identical to x's type: a pointer to a field of type []E can be
    converted to a pointer to a named type with underlying type []E. So
    check for any slice type with the same element type as x, and use the
    general path for those element types.

    Fixes #83501

    Change-Id: Ibf62567cc16b6c894104a848fdba6d27815cc49c
    GitHub-Last-Rev: e8e319502c7d99f4ba99641c97494f20ea51cf6d
    GitHub-Pull-Request: golang/go#83507
    Reviewed-on: https://go-review.googlesource.com/c/go/+/868361
    Reviewed-by: Keith Randall <khr@golang.org>
    Reviewed-by: Keith Randall <khr@google.com>
    LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
    Reviewed-by: David Chase <drchase@google.com>
    Auto-Submit: Keith Randall <khr@golang.org>
    Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>

diff --git a/src/cmd/compile/internal/ssagen/ssa.go b/src/cmd/compile/internal/ssagen/ssa.go
index 93d1afd78c..8fb6b6e448 100644
--- a/src/cmd/compile/internal/ssagen/ssa.go
+++ b/src/cmd/compile/internal/ssagen/ssa.go
@@ -1882,6 +1882,13 @@ func (s *state) stmt(n ir.Node) {
 				if !ir.SameSafeExpr(n.X, rhs.Args[0]) || base.Flag.N != 0 {
 					break
 				}
+				// The in-place path writes the new length to n.X before
+				// storing the new elements. If an element can hold n.X
+				// itself, the element store can overwrite that header,
+				// so use the general path (issue 83501).
+				if containsSliceType(n.X.Type().Elem(), n.X.Type()) {
+					break
+				}
 				// If the slice can be SSA'd, it'll be on the stack,
 				// so there will be no write barriers,
 				// so there's no need to attempt to prevent them.
@@ -8255,3 +8262,25 @@ func isStructNotSIMD(t *types.Type) bool {
 }

 var BoundsCheckFunc [ssa.BoundsKindCount]*obj.LSym
+
+// containsSliceType reports whether a value of type t can hold a slice
+// header that a value of the slice type st can alias, that is, whether t
+// is, or has a field or array element (directly or in nested structs and
+// arrays) that is, a slice type with the same element type as st. The
+// slice types need not be identical: a pointer to a field of type []E can
+// be converted to a pointer to a named type with underlying type []E.
+func containsSliceType(t, st *types.Type) bool {
+	switch {
+	case t.IsSlice():
+		return types.Identical(t.Elem(), st.Elem())
+	case t.IsStruct():
+		for _, f := range t.Fields() {
+			if containsSliceType(f.Type, st) {
+				return true
+			}
+		}
+	case t.IsArray():
+		return t.NumElem() > 0 && containsSliceType(t.Elem(), st)
+	}
+	return false
+}
diff --git a/test/fixedbugs/issue83501.go b/test/fixedbugs/issue83501.go
new file mode 100644
index 0000000000..7ccc430aa3
--- /dev/null
+++ b/test/fixedbugs/issue83501.go
@@ -0,0 +1,71 @@
+// run
+
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Issue 83501: an in-place append wrote the new length to the
+// destination before storing the new element, and the element store
+// could overwrite the destination when the destination is one of the
+// slice's own elements.
+
+package main
+
+type S []S
+
+type T struct {
+	n int
+	s []T
+}
+
+type A [1][]A
+
+type E struct {
+	n int
+	s []E
+}
+
+type L []E
+
+//go:noinline
+func check(what string, gotLen, gotCap, wantLen, wantCap int) {
+	if gotLen != wantLen || gotCap != wantCap {
+		panic(what + ": wrong len or cap")
+	}
+}
+
+func main() {
+	s := make(S, 2)
+	s[1] = s[:1]
+	s[1] = append(s[1], nil)
+	check("S nil element", len(s[1]), cap(s[1]), 2, 2)
+
+	s = make(S, 2)
+	s[0] = s[:1]
+	s[1] = s[:1]
+	s[1] = append(s[1], s[0])
+	check("S non-nil element", len(s[1]), cap(s[1]), 2, 2)
+
+	s = make(S, 2)
+	s[1] = s[:1]
+	p := &s[1]
+	*p = append(*p, nil)
+	check("S through pointer", len(s[1]), cap(s[1]), 2, 2)
+
+	t := make([]T, 2)
+	t[1].s = t[:1]
+	t[1].s = append(t[1].s, T{})
+	check("struct field", len(t[1].s), cap(t[1].s), 2, 2)
+
+	a := make([]A, 2)
+	a[1][0] = a[:1]
+	a[1][0] = append(a[1][0], A{})
+	check("array element", len(a[1][0]), cap(a[1][0]), 2, 2)
+
+	// A slice of a named type stored in a field of an unnamed slice type.
+	e := make([]E, 2)
+	e[1].s = e[:1]
+	l := (*L)(&e[1].s)
+	*l = append(*l, E{})
+	check("named slice type", len(e[1].s), cap(e[1].s), 2, 2)
+}