Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
splace opened this issue Jul 19, 2024 · 21 comments
Closed

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

splace opened this issue Jul 19, 2024 · 21 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@splace
Copy link

splace commented Jul 19, 2024

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.

@ianlancetaylor ianlancetaylor changed the title go1.23 import package with new iterator features -> "internal compiler error: panic: runtime error: index out of range [0] with length 0" cmd/compile: compile panics with generic type alias when not using modules Jul 19, 2024
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 19, 2024
@ianlancetaylor
Copy link
Contributor

CC @griesemer @findleyr

@seankhliao
Copy link
Member

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

@seankhliao seankhliao changed the title cmd/compile: compile panics with generic type alias when not using modules cmd/compile: compile panics with generic type alias Jul 19, 2024
@splace
Copy link
Author

splace commented Jul 19, 2024

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

neat (inlining files)

@splace
Copy link
Author

splace commented Jul 19, 2024

same only without import (works as)...

play.ground/foo

@cherrymui
Copy link
Member

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

@cherrymui cherrymui added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 19, 2024
@cherrymui cherrymui added this to the Go1.23 milestone Jul 19, 2024
@timothy-king timothy-king self-assigned this Jul 19, 2024
@timothy-king
Copy link
Contributor

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
Copy link
Contributor

timothy-king commented Jul 19, 2024

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

@cuonglm
Copy link
Member

cuonglm commented Jul 20, 2024

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

@gopherbot
Copy link
Contributor

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

@timothy-king
Copy link
Contributor

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,
  2. no GOEXPERIMENT=aliastypeparams support in 1.23 (then land everything early in 1.24), or
  3. accept that multiple packages are not supported in the goexperiment and have a better error message here.

@findleyr
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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

@gopherbot
Copy link
Contributor

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
Copy link
Contributor

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

gopherbot pushed a commit to golang/website that referenced this issue Jul 25, 2024
For golang/go#68526.
For golang/go#65614.

Change-Id: I260da94ffaae0d6583e6396b0a78b46dd298313c
Reviewed-on: https://go-review.googlesource.com/c/website/+/600936
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Tim King <taking@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Bypass: Robert Griesemer <gri@google.com>
@griesemer griesemer changed the title cmd/compile: compile panics with generic type alias cmd/compile: importing generic alias types is not supported in 1.23 Jul 25, 2024
@griesemer griesemer modified the milestones: Go1.23, Go1.24 Jul 25, 2024
@griesemer
Copy link
Contributor

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

gopherbot pushed a commit that referenced this issue Jul 26, 2024
…rting generic type alias

When GOEXPERIMENT=aliastypeparams is set, type aliases may have type
parameters. The compiler export data doesn't export that type parameter
information yet, which leads to an index-out-of-bounds panic when a
client package imports a package with a general type alias and then
refers to one of the missing type parameters.

This CL detects this specific case and panics with a more informative
panic message explaining the shortcoming. The change is only in effect
if the respective GOEXPERIMENT is enabled.

Manually tested. No test addded since this is just a temporary fix
(Go 1.24 will have a complete implementation), and because the existing
testing framework doesn't easily support testing that a compilation
panics.

Together with @taking and input from @rfindley.

For #68526.

Change-Id: I24737b153a7e2f9b705cd29a5b70b2b9e808dffc
Reviewed-on: https://go-review.googlesource.com/c/go/+/601035
Reviewed-by: Tim King <taking@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
@gopherbot
Copy link
Contributor

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

gopherbot pushed a commit that referenced this issue Aug 20, 2024
Adds a -gomodversion flag to testdir. This sets the go version
in generated go.mod files. This is just runindir tests at the moment.
This is a building block so that tests can be written for exported
type parameterized aliases (like reproducing #68526).

This also adds a test that uses this feature. A type parameterized
alias is used so aliastypeparams and gotypesalias must be enabled.
gotypesalias is enabled by the go module version. The alias is not
exported and will not appear in exportdata. The test shows the
package containing the alias can be imported. This encapsulates
the level of support of type parameterized aliases in 1.23.

Updates #68526
Updates #68778

Change-Id: I8e20df6baa178e1d427d0fff627a16714d9c3b18
Reviewed-on: https://go-review.googlesource.com/c/go/+/604102
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Austin Clements <austin@google.com>
@gopherbot
Copy link
Contributor

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

gopherbot pushed a commit that referenced this issue Aug 28, 2024
Enables V2 unified IR bitstreams when GOEXPERIMENT aliastypeparams
are enabled.

Allows pkgbits.NewPkgEncoder to set the output version.
Reenables support for writing V0 streams.

Updates #68778
Updates #68526

Change-Id: I590c494d81ab7db148232ceaba52229068d1e986
Reviewed-on: https://go-review.googlesource.com/c/go/+/608595
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
@jfrech
Copy link

jfrech commented Aug 30, 2024

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
Copy link
Contributor

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 #68526 (comment) this will be resolved after cross package support is working.

xiandaonancheng pushed a commit to xiandaonancheng/go that referenced this issue Sep 13, 2024
…rting generic type alias

When GOEXPERIMENT=aliastypeparams is set, type aliases may have type
parameters. The compiler export data doesn't export that type parameter
information yet, which leads to an index-out-of-bounds panic when a
client package imports a package with a general type alias and then
refers to one of the missing type parameters.

This CL detects this specific case and panics with a more informative
panic message explaining the shortcoming. The change is only in effect
if the respective GOEXPERIMENT is enabled.

Manually tested. No test addded since this is just a temporary fix
(Go 1.24 will have a complete implementation), and because the existing
testing framework doesn't easily support testing that a compilation
panics.

Together with @taking and input from @rfindley.

For golang#68526.

Change-Id: I24737b153a7e2f9b705cd29a5b70b2b9e808dffc
Reviewed-on: https://go-review.googlesource.com/c/go/+/601035
Reviewed-by: Tim King <taking@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
@github-project-automation github-project-automation bot moved this from Todo to Done in Go Compiler / Runtime Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Development

No branches or pull requests