golang / go

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

cmd/compile: panic when declaring method on generic alias #70417

Closed griesemer closed 1 day ago

griesemer commented 3 days ago

Compiling

package p

type T[P any] struct{}

type A[P any] = T[P]

func (A[P]) m() {}

panics:

$ go tool compile testdata/manual.go 
<unknown line number>: internal compiler error: panic: /Users/gri/goroot/src/cmd/compile/internal/types2/named.go:282: assertion failed

goroutine 1 [running]:
runtime/debug.Stack()
        /Users/gri/goroot/src/runtime/debug/stack.go:26 +0x5e
cmd/compile/internal/base.FatalfAt({0x38df0?, 0xc0?}, {0xb3fcb42, 0x9}, {0xc000038e20, 0x1, 0x1})
        /Users/gri/goroot/src/cmd/compile/internal/base/print.go:230 +0x1ea
cmd/compile/internal/base.Fatalf(...)
        /Users/gri/goroot/src/cmd/compile/internal/base/print.go:195
cmd/compile/internal/gc.handlePanic()
        /Users/gri/goroot/src/cmd/compile/internal/gc/main.go:54 +0x8a
panic({0xb5bbf60?, 0xc000034b70?})
        /Users/gri/goroot/src/runtime/panic.go:787 +0x132
cmd/compile/internal/types2.(*Checker).handleBailout(0xc000003a40, 0xc000039230)
        /Users/gri/goroot/src/cmd/compile/internal/types2/check.go:396 +0x88
panic({0xb5bbf60?, 0xc000034b70?})
        /Users/gri/goroot/src/runtime/panic.go:787 +0x132
cmd/compile/internal/types2.assert(0x58?)
        /Users/gri/goroot/src/cmd/compile/internal/types2/errors.go:25 +0x54
cmd/compile/internal/types2.(*Named).cleanup(0xc00013b1f0)
        /Users/gri/goroot/src/cmd/compile/internal/types2/named.go:282 +0x3e
cmd/compile/internal/types2.(*Checker).cleanup(0xc000003a40)
        /Users/gri/goroot/src/cmd/compile/internal/types2/check.go:523 +0x36
cmd/compile/internal/types2.(*Checker).checkFiles(0xc000003a40, {0xc0000666f0, 0x1, 0x1})
        /Users/gri/goroot/src/cmd/compile/internal/types2/check.go:457 +0x6e5
cmd/compile/internal/types2.(*Checker).Files(0xb65fd40?, {0xc0000666f0?, 0x0?, 0x0?})
        /Users/gri/goroot/src/cmd/compile/internal/types2/check.go:414 +0x75
cmd/compile/internal/types2.(*Config).Check(0xc00013acb0, {0xb401d8f?, 0xc0000100a7?}, {0xc0000666f0, 0x1, 0x1}, 0xc000414720)
        /Users/gri/goroot/src/cmd/compile/internal/types2/api.go:480 +0x14f
cmd/compile/internal/noder.checkFiles({0x0, {0x0, 0x0}}, {0xc0000666c0, 0x1, 0xc0000394a8?})
        /Users/gri/goroot/src/cmd/compile/internal/noder/irgen.go:95 +0x5c9
cmd/compile/internal/noder.writePkgStub({0x0?, {0x0?, 0x0?}}, {0xc0000666c0, 0x1, 0x1})
        /Users/gri/goroot/src/cmd/compile/internal/noder/unified.go:319 +0x6a
cmd/compile/internal/noder.unified({0x0?, {0x0?, 0x0?}}, {0xc0000666c0?, 0xb5a7380?, 0x0?})
        /Users/gri/goroot/src/cmd/compile/internal/noder/unified.go:195 +0xb3
cmd/compile/internal/noder.LoadPackage({0xc000020130, 0x1, 0x1})
        /Users/gri/goroot/src/cmd/compile/internal/noder/noder.go:77 +0x43a
cmd/compile/internal/gc.Main(0xb66cf70)
        /Users/gri/goroot/src/cmd/compile/internal/gc/main.go:208 +0xcc5
main.main()
        /Users/gri/goroot/src/cmd/compile/main.go:57 +0xf9

cc: @findleyr @timothy-king for visibility.

gabyhelp commented 3 days ago

Related Issues

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

timothy-king commented 3 days ago

Just to double check, the goal is to return an error and not panic right?

Renaming each independent binding for type parameters to a distinct name:

package p

type T[P any] struct{}

type A[Q any] = T[Q]

func (A[R]) m() {} // <- should not be accepted as there is no R to instantiate A[R].
griesemer commented 3 days ago

Just discussed with @ianlancetaylor . We concluded that we should not permit methods that have a generic alias as receiver type. We already do not allow methods that have a non-generic alias receiver but where the aliased type is an (instantiated) generic type.

gopherbot commented 3 days ago

Change https://go.dev/cl/629080 mentions this issue: go/types, types2: disallow new methods on generic alias types

gopherbot commented 3 days ago

Change https://go.dev/cl/629577 mentions this issue: spec: document restrictions for method receivers that are aliases