Repository navigation
Description
I am looking at a behavior change introduced by CL 806600
(merged as commit 98ef86b, "cmd/compile/internal/walk: expand makeslicecopy optimization").
I am not sure whether this behavior is considered acceptable. This report
focuses on how to reproduce it with two toolchains built from source.
Toolchains
I built two toolchains from the same repository:
- before: the parent of
98ef86b779, commitab4ad11bad - after: commit
98ef86b779itself
$ GOROOT=/path/to/go-before /path/to/go-before/bin/go version
go version go1.28-devel_ab4ad11bad Fri Aug 14 19:53:41 2026 -0700 linux/amd64
$ GOROOT=/path/to/go-after /path/to/go-after/bin/go version
go version go1.28-devel_98ef86b779 Fri Aug 14 20:02:36 2026 -0700 linux/amd64
The toolchains were built with ./make.bash:
$ git clone https://go.googlesource.com/go
$ cd go
$ git worktree add ../go-before 98ef86b779^
$ git worktree add ../go-after 98ef86b779
$ cd ../go-before/src
$ GOROOT_BOOTSTRAP=/path/to/bootstrap ./make.bash
$ cd ../../go-after/src
$ GOROOT_BOOTSTRAP=/path/to/bootstrap ./make.bash
/path/to/bootstrap is an existing Go installation. Both toolchains are run
with an explicit GOROOT pointing at the corresponding build.
Main reproduction
package main
import "fmt"
var Calls int
var Sink []int
func src() []int {
Calls++
return []int{1, 2, 3}
}
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("recover, calls = %d\n", Calls)
}
}()
n := -1
m := make([]int, n)
copy(m, src())
Sink = m
}Running the same file with the two toolchains:
$ GOROOT=/path/to/go-before /path/to/go-before/bin/go run main.go
recover, calls = 0
$ GOROOT=/path/to/go-after /path/to/go-after/bin/go run main.go
recover, calls = 1
| Build | Output |
|---|---|
| parent ab4ad11 | recover, calls = 0 |
| CL 806600, commit 98ef86b | recover, calls = 1 |
The source file is identical in both runs. The only difference is the compiler
build.
Variant: src itself panics
The difference is not limited to the Calls counter. If src panics, the
panic value changes:
package main
import "fmt"
var Sink []int
func src() []int {
var p *int
return []int{*p}
}
func main() {
defer func() {
fmt.Printf("recover: %v\n", recover())
}()
n := -1
m := make([]int, n)
copy(m, src())
Sink = m
}$ GOROOT=/path/to/go-before /path/to/go-before/bin/go run main.go
recover: runtime error: makeslice: len out of range
$ GOROOT=/path/to/go-after /path/to/go-after/bin/go run main.go
recover: runtime error: invalid memory address or nil pointer dereference
Variant: the source is a slice expression
The same difference appears without a function call, using a source expression
that can panic:
package main
import "fmt"
var Sink []int
func main() {
defer func() {
fmt.Printf("recover: %v\n", recover())
}()
var s []int
n := -1
m := make([]int, n)
copy(m, s[10:])
Sink = m
}$ GOROOT=/path/to/go-before /path/to/go-before/bin/go run main.go
recover: runtime error: makeslice: len out of range
$ GOROOT=/path/to/go-after /path/to/go-after/bin/go run main.go
recover: runtime error: slice bounds out of range [10:0]
Summary
| Program | Parent ab4ad11 | CL 806600 98ef86b |
|---|---|---|
src() increments Calls |
calls = 0 |
calls = 1 |
src() dereferences a nil pointer |
makeslice: len out of range |
invalid memory address or nil pointer dereference |
copy(m, s[10:]) with a short s |
makeslice: len out of range |
slice bounds out of range [10:0] |
Is this behavior expected? If not, what would be the preferred way to limit the
optimization? I can send a follow-up CL.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
randall77 commentedon Oct 29, 2026
I agree that this is a bug. In your main reproducer,
srcshould never be called.Not sure what the right fix would be.
Ch1n-ch1nless commentedon Oct 29, 2026
I also don’t see a good solution to this problem. Perhaps, at this stage, we should revert this commit and add the tests from the issue to the compiler, so that in the future, as this optimization is further developed, we can immediately gauge how safe it is.
cuonglm commentedon Oct 29, 2026
Maybe just disable the optimization if either source or destination have side effects?
JunyangShao commentedon Oct 29, 2026
@golang/compiler
gopherbot commentedon Oct 29, 2026
Change https://go.dev/cl/856401 mentions this issue:
cmd/compile: don't apply make+copy optimization to side-effecting operandsthepudds commentedon Nov 3, 2026
Was just chatting with @JunyangShao about this and wanted to record here that I thought @cuonglm's suggestion of disabling the optimization based on side effects seems like a very reasonable approach, especially to fix the bug (and later work could try to expand the optimization if worthwhile).