golang/go

cmd/compile: ICE assertion failed with append(nil, "string"...)

Opened this issue · 6 comments

Go version

go version go1.25.4 darwin/arm64

Output of go env in your module/workspace:

-

What did you do?

This is nonsense code, obviously. But the panic happened while doing real work and just having my code in a temporarily broken state as I was rewriting parts of it, which had gopls crash. Minimal repro:

package main

func main() {
	s := "hello"
	msg := append(nil, s...)
	print(msg)
}

What did you see happen?

gopls crash consistently, so I also tried go run

go run main.go
# command-line-arguments
<unknown line number>: internal compiler error: panic: cmd/compile/internal/types2/builtins.go:1093: assertion failed

Please file a bug report including a short program that triggers the error.
https://go.dev/issue/new

What did you expect to see?

A compiler error without the panic. 1.24 seems to do the right thing:

go version
go version go1.24.2 darwin/arm64

go run main.go
# command-line-arguments
./main.go:5:16: first argument to append must be a slice; have untyped nil

cc @golang/compiler @mrkfrmn @griesemer

Thanks, @anderseknert, for reporting and especially for providing the minimal repro, really appreciated.
I will take care of this.

Quick follow-up: this code is not valid because the first argument to append must be a slice, it cannot be the predeclared identifier nil, but obviously this error should be reported rather than leading to a crash.
It appears that this is a regression from 1.24 where this used to work as expected (playground).

Thanks Robert! Indeed, I did not expect the code to compile. I only noticed because gopls crashed immediately on saving the file, and then again when I restarted it. I had done some work involving AppendText methods, which do accept nil... and so when moving things around later I think that's how a nil ended up in a regular append.

Change https://go.dev/cl/718860 mentions this issue: go/types, types2: first argument to append must never be be nil