Skip to content

cmd/compile: ICE: "runtime error: index out of range [0] with length 0" due to generic code #52162

Closed
@vogtp

Description

@vogtp

What version of Go are you using (go version)?

$ go version
go version go1.18 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\vogtp\AppData\Local\go-build
set GOENV=C:\Users\vogtp\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\vogtp\go\pkg\mod
set GONOPROXY=gitlab-int.its.unibas.ch
set GONOSUMDB=gitlab-int.its.unibas.ch
set GOOS=windows
set GOPATH=C:\Users\vogtp\go
set GOPRIVATE=gitlab-int.its.unibas.ch
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Users\vogtp\sdk\go1.18
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Users\vogtp\sdk\go1.18\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.18
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\vogtp\go\src\gitlab-int.its.unibas.ch\vogtp\murx\go.mod
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\vogtp\AppData\Local\Temp\go-build1961016563=/tmp/go-build -gno-record-gcc-switches

What did you do?

https://go.dev/play/p/TgQFW3CWM12

What did you expect to see?

A error with a file and a line number, I can goto and fix.

What did you see instead?

:1: internal compiler error: panic: runtime error: index out of range [0] with length 0

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

Go build failed.

I really did not expect a request for a bug report, but here it is! ;-)

Activity

changed the title [-]affected/package: [/-] [+]runtime/generics: [/+] on Apr 5, 2022
vogtp

vogtp commented on Apr 5, 2022

@vogtp
Author

I just realised that changing
func (h *msgHandler[M]) Handle(msgHandler func(e *M)) error
to
func (h *msgHandler[M]) Handle(f func(e *M)) error
fixes the problem in my demo code, but not in my production code.
https://go.dev/play/p/UnIXXd3PS4Z

earthboundkid

earthboundkid commented on Apr 5, 2022

@earthboundkid
Contributor

Here is a shorter reproducer:

package main

func main() {
	_ = &G[struct{}]{}
}

type G[T any] struct{}

func (_ *G[T]) m(G func(*T)) {}

https://go.dev/play/p/pn99Ydje_nL

I think part of the problem is that the callback is named the same as the generic type.

earthboundkid

earthboundkid commented on Apr 5, 2022

@earthboundkid
Contributor

Even shorter:

package main

func main() {}

var _ = G[int]{}

type G[T any] struct{}

func (_ G[T]) m(G T) {}
ianlancetaylor

ianlancetaylor commented on Apr 5, 2022

@ianlancetaylor
Contributor

CC @randall77 @mdempsky

<autogenerated>:1: internal compiler error: panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
runtime/debug.Stack()
	/home/iant/go/src/runtime/debug/stack.go:24 +0x65
cmd/compile/internal/base.FatalfAt({0x50?, 0x0?}, {0xd0a192, 0x9}, {0xc00068cf10, 0x1, 0x1})
	/home/iant/go/src/cmd/compile/internal/base/print.go:227 +0x1d7
cmd/compile/internal/base.Fatalf(...)
	/home/iant/go/src/cmd/compile/internal/base/print.go:196
cmd/compile/internal/gc.handlePanic()
	/home/iant/go/src/cmd/compile/internal/gc/main.go:48 +0x85
panic({0xcd2aa0, 0xc0000a86a8})
	/home/iant/go/src/runtime/panic.go:838 +0x207
cmd/compile/internal/reflectdata.methodWrapper(0xc0000c8620, 0xc00040a370, 0x95?)
	/home/iant/go/src/cmd/compile/internal/reflectdata/reflect.go:1925 +0x1bf8
cmd/compile/internal/reflectdata.methods(0xc0000c8620)
	/home/iant/go/src/cmd/compile/internal/reflectdata/reflect.go:351 +0x3c5
cmd/compile/internal/reflectdata.uncommonSize(...)
	/home/iant/go/src/cmd/compile/internal/reflectdata/reflect.go:79
cmd/compile/internal/reflectdata.dcommontype(0x4a?, 0xc0000c8620)
	/home/iant/go/src/cmd/compile/internal/reflectdata/reflect.go:725 +0x1cc
cmd/compile/internal/reflectdata.writeType(0xc0003df420?)
	/home/iant/go/src/cmd/compile/internal/reflectdata/reflect.go:1150 +0x307
cmd/compile/internal/reflectdata.dcommontype(0x400?, 0xc0003df420)
	/home/iant/go/src/cmd/compile/internal/reflectdata/reflect.go:698 +0xbc
cmd/compile/internal/reflectdata.writeType(0xe7ad78?)
	/home/iant/go/src/cmd/compile/internal/reflectdata/reflect.go:1175 +0x10aa
cmd/compile/internal/reflectdata.WriteRuntimeTypes()
	/home/iant/go/src/cmd/compile/internal/reflectdata/reflect.go:1266 +0x3d8
cmd/compile/internal/gc.dumpdata()
	/home/iant/go/src/cmd/compile/internal/gc/obj.go:118 +0x6e
cmd/compile/internal/gc.Main(0xd39918)
	/home/iant/go/src/cmd/compile/internal/gc/main.go:316 +0x10f1
main.main()
	/home/iant/go/src/cmd/compile/main.go:55 +0xdd
added this to the Go1.19 milestone on Apr 5, 2022
ianlancetaylor

ianlancetaylor commented on Apr 5, 2022

@ianlancetaylor
Contributor

This works in 1.18.

changed the title [-]runtime/generics: [/-] [+]cmd/compile: compiler crash on generic code[/+] on Apr 5, 2022
changed the title [-]cmd/compile: compiler crash on generic code[/-] [+]cmd/compile: ICE: "runtime error: index out of range [0] with length 0" due to generic code[/+] on Apr 5, 2022
mdempsky

mdempsky commented on Apr 5, 2022

@mdempsky
Contributor

@ianlancetaylor

This works in 1.18.

Do you mean this works at tip? The playground links are for Go 1.18, which are reproducing the issue.

I'm not able to reproduce at tip. I am able to reproduce with "go1.18-pre13".

ianlancetaylor

ianlancetaylor commented on Apr 5, 2022

@ianlancetaylor
Contributor

Interesting. I can reproduce the crash with

go version devel go1.19-09117c7d86 Sat Mar 12 15:31:41 2022 -0800 linux/amd64

but I can't reproduce it with

go version go1.18 linux/amd64 (at revision f92bfa440facc9f31ef70105e3d957a6ca79efb7)
ianlancetaylor

ianlancetaylor commented on Apr 5, 2022

@ianlancetaylor
Contributor

And I can't reproduce it on tip.

Sounds like this has been fixed.

locked and limited conversation to collaborators on Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mdempsky@earthboundkid@vogtp@ianlancetaylor@gopherbot

        Issue actions

          cmd/compile: ICE: "runtime error: index out of range [0] with length 0" due to generic code · Issue #52162 · golang/go