golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.69k stars 17.49k forks source link

cmd/compile: importing generic alias types is not supported in 1.23 #68526

Open splace opened 1 month ago

splace commented 1 month ago

Go version

go1.23rc1 (+ go1.23rc2)

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/var/home/simon/.cache/go-build'
GOENV='/var/home/simon/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/var/home/simon/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/var/home/simon/go'
GOPRIVATE=''
GOPROXY='direct'
GOROOT='/usr/lib/golang'
GOSUMDB='off'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/usr/lib/golang/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23rc1'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/var/home/simon/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build892698478=/tmp/go-build -gno-record-gcc-switches'

What did you do?

GOEXPERIMENT=aliastypeparams GO111MODULE=auto ~/go/bin/go1.23rc2 run .

example code files: has relative local import bug.zip

What did you see happen?

# _....
<unknown line number>: 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

What did you expect to see?

same as when inside package, that is, no error.

gabyhelp commented 1 month ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

ianlancetaylor commented 1 month ago

CC @griesemer @findleyr

seankhliao commented 1 month ago

In playground instead of a zip file: https://go.dev/play/p/MwWuoC7fDFg?v=gotip

splace commented 1 month ago

In playground instead of a zip file: https://go.dev/play/p/MwWuoC7fDFg?v=gotip

neat (inlining files)

splace commented 1 month ago

same only without import (works as)...

play.ground/foo

cherrymui commented 1 month ago

The failure stacktrace is

<unknown line number>: internal compiler error: panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
runtime/debug.Stack()
    /usr/local/go/src/runtime/debug/stack.go:26 +0x5e
cmd/compile/internal/base.FatalfAt({0x43c330?, 0xc0?}, {0xe2d472, 0x9}, {0xc00043c360, 0x1, 0x1})
    /usr/local/go/src/cmd/compile/internal/base/print.go:230 +0x1ea
cmd/compile/internal/base.Fatalf(...)
    /usr/local/go/src/cmd/compile/internal/base/print.go:195
cmd/compile/internal/gc.handlePanic()
    /usr/local/go/src/cmd/compile/internal/gc/main.go:53 +0x8a
panic({0xdf0300?, 0xc000028318?})
    /usr/local/go/src/runtime/panic.go:785 +0x132
cmd/compile/internal/types2.(*Checker).handleBailout(0xc000007c00, 0xc00043f120)
    /usr/local/go/src/cmd/compile/internal/types2/check.go:390 +0x88
panic({0xdf0300?, 0xc000028318?})
    /usr/local/go/src/runtime/panic.go:785 +0x132
cmd/compile/internal/importer.(*reader).doTyp(0xc00043c6b8)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:260 +0x43a
cmd/compile/internal/importer.(*pkgReader).typIdx(...)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:227
cmd/compile/internal/importer.(*reader).typ(0xc0001109b0)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:197 +0x1b9
cmd/compile/internal/importer.(*reader).param(...)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:361
cmd/compile/internal/importer.(*reader).params(0xc00043c9b0)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:351 +0xc5
cmd/compile/internal/importer.(*reader).signature(0xc00043c9b0, 0x0, {0x0, 0x0, 0x0}, {0x0, 0x0, 0x0})
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:340 +0x65
cmd/compile/internal/importer.(*reader).doTyp(0xc0001109b0)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:273 +0x385
cmd/compile/internal/importer.(*pkgReader).typIdx(...)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:227
cmd/compile/internal/importer.(*reader).typ(0xc0004343c0)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:197 +0x1b9
cmd/compile/internal/importer.(*pkgReader).objIdx.func1()
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:415 +0x332
cmd/compile/internal/types2.(*Scope).Lookup.resolve.func1()
    /usr/local/go/src/cmd/compile/internal/types2/scope.go:266 +0x39
sync.(*Once).doSlow(0xda5080?, 0xc0001fa4e0?)
    /usr/local/go/src/sync/once.go:76 +0xb4
sync.(*Once).Do(...)
    /usr/local/go/src/sync/once.go:67
cmd/compile/internal/types2.resolve(...)
    /usr/local/go/src/cmd/compile/internal/types2/scope.go:265
cmd/compile/internal/types2.(*Scope).Lookup(0xc0004360e0?, {0x78d02cb07218, 0x4})
    /usr/local/go/src/cmd/compile/internal/types2/scope.go:71 +0x98
cmd/compile/internal/importer.(*reader).obj(0xc00043ce78)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:374 +0x9a
cmd/compile/internal/importer.(*reader).doTyp(0xc00043ce78)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:251 +0x85
cmd/compile/internal/importer.(*pkgReader).typIdx(...)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:227
cmd/compile/internal/importer.(*reader).typ(0xc000111170)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:197 +0x1b9
cmd/compile/internal/importer.(*reader).param(...)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:361
cmd/compile/internal/importer.(*reader).params(0xc00043d170)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:351 +0xc5
cmd/compile/internal/importer.(*reader).signature(0xc00043d170, 0x0, {0x0, 0x0, 0x0}, {0x0, 0x0, 0x0})
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:340 +0x65
cmd/compile/internal/importer.(*reader).doTyp(0xc000111170)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:273 +0x385
cmd/compile/internal/importer.(*pkgReader).typIdx(...)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:227
cmd/compile/internal/importer.(*reader).typ(0xc0004342a0)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:197 +0x1b9
cmd/compile/internal/importer.(*reader).param(...)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:361
cmd/compile/internal/importer.(*reader).params(0xc0004342a0)
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:351 +0xc5
cmd/compile/internal/importer.(*reader).signature(0xc0004342a0, 0x0, {0x0, 0x0, 0x0}, {0xc00005e660, 0x1, 0x1})
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:341 +0x74
cmd/compile/internal/importer.(*pkgReader).objIdx.func1()
    /usr/local/go/src/cmd/compile/internal/importer/ureader.go:427 +0x507
cmd/compile/internal/types2.(*Scope).Lookup.resolve.func1()
    /usr/local/go/src/cmd/compile/internal/types2/scope.go:266 +0x39
sync.(*Once).doSlow(0xda5080?, 0xc0001fa4e0?)
    /usr/local/go/src/sync/once.go:76 +0xb4
sync.(*Once).Do(...)
    /usr/local/go/src/sync/once.go:67
cmd/compile/internal/types2.resolve(...)
    /usr/local/go/src/cmd/compile/internal/types2/scope.go:265
cmd/compile/internal/types2.(*Scope).Lookup(0xc000007c00?, {0x14c93f8, 0x1})
    /usr/local/go/src/cmd/compile/internal/types2/scope.go:71 +0x98
cmd/compile/internal/types2.(*Checker).selector(0xc000007c00, 0xc00042ea00, 0xc000430a00, 0x0, 0x0)
    /usr/local/go/src/cmd/compile/internal/types2/call.go:714 +0x205
cmd/compile/internal/types2.(*Checker).exprInternal(0xc000007c00, 0x0, 0xc00042ea00, {0xf9fed8, 0xc000430a00}, {0x0, 0x0})
    /usr/local/go/src/cmd/compile/internal/types2/expr.go:1379 +0xb2c
cmd/compile/internal/types2.(*Checker).rawExpr(0xc000007c00, 0x0, 0xc00042ea00, {0xf9fed8?, 0xc000430a00?}, {0x0?, 0x0?}, 0x1)
    /usr/local/go/src/cmd/compile/internal/types2/expr.go:996 +0x1a9
cmd/compile/internal/types2.(*Checker).exprOrType(0xc000007c00, 0xc00042ea00, {0xf9fed8?, 0xc000430a00?}, 0xcd?)
    /usr/local/go/src/cmd/compile/internal/types2/expr.go:1662 +0x3b
cmd/compile/internal/types2.(*Checker).indexExpr(...)
    /usr/local/go/src/cmd/compile/internal/types2/index.go:19
cmd/compile/internal/types2.(*Checker).exprInternal(0xc000007c00, 0x0, 0xc00042ea00, {0xf9fe90, 0xc000081f20}, {0x0, 0x0})
    /usr/local/go/src/cmd/compile/internal/types2/expr.go:1382 +0x1c5
cmd/compile/internal/types2.(*Checker).rawExpr(0xc000007c00, 0x0, 0xc00042ea00, {0xf9fe90?, 0xc000081f20?}, {0x0?, 0x0?}, 0x0)
    /usr/local/go/src/cmd/compile/internal/types2/expr.go:996 +0x1a9
cmd/compile/internal/types2.(*Checker).stmt(0xc000007c00, 0x0, {0xf9ddc8, 0xc00007eaa0})
    /usr/local/go/src/cmd/compile/internal/types2/stmt.go:404 +0x119
cmd/compile/internal/types2.(*Checker).stmtList(0xc000007c00, 0x0, {0xc00002d270?, 0xc4f022?, 0xc00042c930?})
    /usr/local/go/src/cmd/compile/internal/types2/stmt.go:120 +0x85
cmd/compile/internal/types2.(*Checker).funcBody(0xc000007c00, 0x1f5de65000000000?, {0xc000013694?, 0x0?}, 0xc00042e9c0, 0xc00042e840, {0x0?, 0x0?})
    /usr/local/go/src/cmd/compile/internal/types2/stmt.go:40 +0x310
cmd/compile/internal/types2.(*Checker).funcDecl.func1()
    /usr/local/go/src/cmd/compile/internal/types2/decl.go:789 +0x3a
cmd/compile/internal/types2.(*Checker).processDelayed(0xc000007c00, 0x0)
    /usr/local/go/src/cmd/compile/internal/types2/check.go:502 +0x19c
cmd/compile/internal/types2.(*Checker).checkFiles(0xc000007c00, {0xc00005e600, 0x1, 0x1})
    /usr/local/go/src/cmd/compile/internal/types2/check.go:448 +0x685
cmd/compile/internal/types2.(*Checker).Files(0x7fff52ac6c70?, {0xc00005e600?, 0x0?, 0x0?})
    /usr/local/go/src/cmd/compile/internal/types2/check.go:408 +0x75
cmd/compile/internal/types2.(*Config).Check(0xc00042c700, {0x7fff52ac6c70?, 0xc000012027?}, {0xc00005e600, 0x1, 0x1}, 0xc000434000)
    /usr/local/go/src/cmd/compile/internal/types2/api.go:480 +0x66
cmd/compile/internal/noder.checkFiles({0x0, {0x0, 0x0}}, {0xc00005e5e8, 0x1, 0x1?})
    /usr/local/go/src/cmd/compile/internal/noder/irgen.go:95 +0x5c9
cmd/compile/internal/noder.writePkgStub({0x0?, {0x0?, 0x0?}}, {0xc00005e5e8, 0x1, 0x1})
    /usr/local/go/src/cmd/compile/internal/noder/unified.go:317 +0x6a
cmd/compile/internal/noder.unified({0x0?, {0x0?, 0x0?}}, {0xc00005e5e8?, 0xd6d740?, 0x0?})
    /usr/local/go/src/cmd/compile/internal/noder/unified.go:193 +0xb3
cmd/compile/internal/noder.LoadPackage({0xc000022220, 0x1, 0x2})
    /usr/local/go/src/cmd/compile/internal/noder/noder.go:77 +0x43a
cmd/compile/internal/gc.Main(0xe68280)
    /usr/local/go/src/cmd/compile/internal/gc/main.go:200 +0xbed
main.main()
    /usr/local/go/src/cmd/compile/main.go:57 +0xf9

cc @golang/compiler @griesemer

timothy-king commented 1 month ago

While digging in I found another bug. I'll file a new bug in a bit.

https://go.dev/play/p/arcCVWSA8AL?v=gotip

// GOEXPERIMENT=aliastypeparams
package main

import "play.ground/foo"

func main() {
    type _ foo.A[int]
}
-- go.mod --
module play.ground
-- foo/foo.go --
package foo

type A[_ any] struct{} // generic named type
// type A[_ any] = struct{} // generic type alias // Fails with "./prog.go:7:9: invalid operation: foo.A[int] (struct{} is not a generic type)"
timothy-king commented 1 month ago

Here is a simplified reproducer https://go.dev/play/p/aNAwYF7zDZ-?v=gotip

cuonglm commented 1 month ago

It seems to me this is the problem with lazy importing, go/types ureader works well.

gopherbot commented 1 month ago

Change https://go.dev/cl/599915 mentions this issue: DO NOT SUBMIT

timothy-king commented 1 month ago

AFAICT export data does not yet have support for type parameterized aliases. That would mean no support for multiple Go package with GOEXPERIMENT=aliastypeparams yet. (Apologies if I just missed it.)

I have a WiP CL that fixes the crash https://go.dev/cl/599915. I know I don't yet have reflection working yet. Put together these changes seem fairly risky to me given how late in the release cycle we are for 1.23.

Three options I am seeing for addressing this are: 1) implement support for exportdata for type parameterized aliases last minute for 1.23, 1) no GOEXPERIMENT=aliastypeparams support in 1.23 (then land everything early in 1.24), or 1) accept that multiple packages are not supported in the goexperiment and have a better error message here.

findleyr commented 1 month ago

AFAICT export data does not yet have support for type parameterized aliases. That would mean no support for multiple Go package with GOEXPERIMENT=aliastypeparams yet. (Apologies if I just missed it.)

Yes, that's right. This was a known limitation, but in that case I think it should be clarified in the release notes. A better error message (option 3) sounds like the safest bet at this point. I'm not sure there's any reason to back out the experiment (option 2), since it's an experiment, but it is unfortunate that importing doesn't work.

timothy-king commented 1 month ago

AFAICT the current patch set for https://go.dev/cl/599915 fixes the issue. It still needs a much more thorough review and a roll out plan. Ideally changes like this would have a couple of months to soak before a release. I am still looking into whether/how we can do a better error message.

gopherbot commented 1 month ago

Change https://go.dev/cl/600615 mentions this issue: all: support type parameterized aliases

gopherbot commented 1 month ago

Change https://go.dev/cl/601035 mentions this issue: [release-branch.go1.23] cmd/compile: avoid compiler panic when instantiating imported generic type alias

gopherbot commented 1 month ago

Change https://go.dev/cl/600936 mentions this issue: _content/doc/go1.23: document known issue for generic alias types

griesemer commented 1 month ago

The CL addresses the issue for 1.23. This is now a tracking issue for the complete fix in 1.24.

gopherbot commented 3 weeks ago

Change https://go.dev/cl/604102 mentions this issue: cmd/internal/testdir: add a -gomodversion flag

gopherbot commented 1 week ago

Change https://go.dev/cl/608595 mentions this issue: cmd/compile/internal/noder: write V2 bitstream aliastypeparams=1

jfrech commented 1 week ago

Is support for cross-package generic type aliases planned? Or would resolution of this issue entail only emission of a non-iceing error?

timothy-king commented 1 week ago

Is support for cross-package generic type aliases planned?

Yes. Follow along here #68778.

Or would resolution of this issue entail only emission of a non-iceing error?

According to https://github.com/golang/go/issues/68526#issuecomment-2251551529 this will be resolved after cross package support is working.