Skip to content

cmd/compile: incorrect evaluation order of make length and capacity arguments #81696

Closed

Description

@0xkohe

Go version

go version go1.27.1 linux/amd64
go version go1.26.8 linux/amd64
go version go1.28-devel_12c76d44 Tue Sep 22 02:16:50 2026 -0700 linux/amd64

All three gc versions reproduce the problem. The development version was built from unmodified commit 12c76d447f576e6c14c566c8524c13a0c0d476b9, the master HEAD fetched on 2026-09-22.

Output of go env in your module/workspace

Relevant go env -json output from the fresh Go 1.27.1 container:

{
  "CGO_ENABLED": "0",
  "GOARCH": "amd64",
  "GOCACHE": "/work/cache",
  "GOENV": "",
  "GOEXPERIMENT": "",
  "GOFLAGS": "",
  "GOHOSTARCH": "amd64",
  "GOHOSTOS": "linux",
  "GOMODCACHE": "/work/modcache",
  "GOOS": "linux",
  "GOPATH": "/work/gopath",
  "GOPROXY": "off",
  "GOROOT": "/usr/local/go",
  "GOSUMDB": "off",
  "GOTOOLCHAIN": "local",
  "GOTOOLDIR": "/usr/local/go/pkg/tool/linux_amd64",
  "GOVERSION": "go1.27.1"
}

The other two versions used /opt/go as GOROOT, /opt/go/pkg/tool/linux_amd64 as GOTOOLDIR, and their corresponding GOVERSION values. The checks used fresh Debian 12 containers on the same Linux/amd64 host, with gc, networking disabled, and an explicitly cleared environment. GOENV=off was set; go env reported the empty value shown above. Build caches started empty for each version and were shared only among that version's cases.

What did you do?

Save this as make-trace-assert.go:

package main

var trace int

func f(x int) int { trace = trace*10 + x; return x }

func main() {
    s := make([]int, f(1), f(3))
    if trace != 13 { panic(trace) }
    if len(s) != 1 || cap(s) != 3 { panic("shape") }
}

Build and run with the stock gc toolchain:

GOTOOLCHAIN=local GOENV=off GOFLAGS= GOEXPERIMENT= CGO_ENABLED=0 go build -p 2 -o program make-trace-assert.go
./program

The assertion records call order in an integer and checks the resulting slice length and capacity.

What did you see happen?

On all three versions, the default executable panics with panic: 31 and exits with status 2. This indicates that f(3) ran before f(1). Builds succeed with exit 0. Stdout is empty. The exact stderr from the Go 1.27.1 container is:

panic: 31

goroutine 1 [running]:
main.main()
	/work/sources/make-trace-assert.go:9 +0x46

The result matrix below is identical for Go 1.26.8, Go 1.27.1, and the development commit:

Build setting Assertion result
default panic: 31, executable exit 2
-gcflags=-N panic: 31, executable exit 2
-gcflags=-l normal exit 0
-gcflags='-N -l' normal exit 0

Adding //go:noinline to f, or evaluating f(1) and f(3) in separate preceding statements, also made the program exit normally on all three versions.

What did you expect to see?

Normal exit: the calls to f(1) and f(3) should occur in that order, leaving trace == 13 and a slice with length 1 and capacity 3.

The order-of-evaluation rule specifies lexical left-to-right order for function calls. Both size operands here are function calls; this is not a question about the unspecified ordering of a plain variable read relative to a call. The make specification does not provide an exception for these calls.

Related report

#74379 is related history concerning missing side effects. Its make regression test uses one explicit size argument. In the example here, both calls occur, but their order is reversed. I did not find an exact matching report in the issue search; please link a duplicate if one exists.

Discovery context

I encountered this behavior while developing executable Go semantics in the K Framework and comparing specification-derived test cases against the Go compiler. The reproducer above uses only the Go toolchain.

A snapshot of the semantics implementation is available at ba97042.

Activity

  1. thepudds commented on Sep 23, 2026

    @thepudds
    Member

    Hi @0xkohe, thank you for the report. I'll take a look.

  2. self-assigned this
    on Sep 23, 2026
  3. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Sep 23, 2026
  4. zigo101 commented on Sep 26, 2026

    @zigo101
    Contributor

    This doesn't look like an evaluation order problem. It looks like the f function is called one time more than expected.
    [Edit] Aha, not really.

  5. randall77 commented on Sep 26, 2026

    @randall77
    Contributor

    This looks like an evaluation order bug in escape analysis. Both calls to f are inlined, and the compiler notices that the capacity is constant. It rewrites the make to have a literal constant capacity so it can be stack allocated. It also makes sure the previous capacity expression is evaluated, for its side effects. But it doesn't consider that the length expression might have side effects, so the capacity expression ends up being evaluated first.

    Seems related to CL 684116. Maybe just that that CL is incomplete. @thepudds

  6. gopherbot commented on Sep 27, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/839725 mentions this issue: cmd/compile: keep make's evaluation order when rewriting with literals

  7. added a commit that references this issue on Sep 28, 2026
    d98516a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions