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: generics + constant cycle causes panic in type checker #52748

Closed
adonovan opened this issue May 6, 2022 · 4 comments
Closed

cmd/compile: generics + constant cycle causes panic in type checker #52748

adonovan opened this issue May 6, 2022 · 4 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented May 6, 2022

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

$ go version
go version go1.18.1 darwin/arm64

$ cat b.go 
package main

import "unsafe"

type S[T any] struct{}

const c = unsafe.Sizeof(S[[c]byte]{})

$ go tool compile b.go
<unknown line number>: internal compiler error: panic: unimplemented basic: invalid type (kind 0)

Please file a bug report including a short program that triggers the error.
https://go.dev/issue/new
@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 6, 2022
@ianlancetaylor ianlancetaylor added this to the Go1.19 milestone May 6, 2022
@findleyr
Copy link
Contributor

findleyr commented May 7, 2022

Huh, this reports an initialization cycle when type-checking. For example, after copying the above into cmd/compile/internal/types2/testdata/manual.go:

> go test -run=TestManual
--- FAIL: TestManual (0.00s)
    check_test.go:161: testdata/manual.go:14:7: initialization cycle for c
                testdata/manual.go:14:7: c refers to
                testdata/manual.go:14:7: c

How does it get past type-checking?

@findleyr
Copy link
Contributor

findleyr commented May 7, 2022

Aha, full stack is below. It's getting to noder.gcSizes.Sizeof, despite the cycle error. Probably this is actually a types2 bug.

> go tool compile cmd/compile/internal/types2/testdata/manual.go
<unknown line number>: internal compiler error: panic: unimplemented basic: invalid type (kind 0)

goroutine 1 [running]:
runtime/debug.Stack()
        /home/rfindley/src/go/src/runtime/debug/stack.go:24 +0x65
cmd/compile/internal/base.FatalfAt({0x1?, 0x0?}, {0xd039b2, 0x9}, {0xc0001383d8, 0x1, 0x1})
        /home/rfindley/src/go/src/cmd/compile/internal/base/print.go:227 +0x1d7
cmd/compile/internal/base.Fatalf(...)
        /home/rfindley/src/go/src/cmd/compile/internal/base/print.go:196
cmd/compile/internal/gc.handlePanic()
        /home/rfindley/src/go/src/cmd/compile/internal/gc/main.go:48 +0x85
panic({0xc727e0, 0xc00010ac60})
        /home/rfindley/src/go/src/runtime/panic.go:866 +0x212
cmd/compile/internal/types2.(*Checker).handleBailout(0xc000432000, 0xc000139848)
        /home/rfindley/src/go/src/cmd/compile/internal/types2/check.go:295 +0x8b
panic({0xc727e0, 0xc00010ac60})
        /home/rfindley/src/go/src/runtime/panic.go:866 +0x212
cmd/compile/internal/noder.(*gcSizes).Sizeof(0xc000432000?, {0xe6b678, 0x12edb00})
        /home/rfindley/src/go/src/cmd/compile/internal/noder/sizes.go:96 +0x3e7
cmd/compile/internal/types2.(*Config).sizeof(0xe6b678?, {0xe6b678?, 0x12edb00?})
        /home/rfindley/src/go/src/cmd/compile/internal/types2/sizes.go:261 +0x31
cmd/compile/internal/types2.(*Checker).builtin(0xc000432000, 0xc00041b7c0, 0xc00041b700, 0x12)
        /home/rfindley/src/go/src/cmd/compile/internal/types2/builtins.go:716 +0x2adb
cmd/compile/internal/types2.(*Checker).callExpr(0xc000432000, 0xc00041b7c0, 0xc00041b700)
        /home/rfindley/src/go/src/cmd/compile/internal/types2/call.go:155 +0xcc9
cmd/compile/internal/types2.(*Checker).exprInternal(0xc000432000, 0xc00041b7c0, {0xe6c560?, 0xc00041b700}, {0x0?, 0x0?})
        /home/rfindley/src/go/src/cmd/compile/internal/types2/expr.go:1636 +0xbf1
cmd/compile/internal/types2.(*Checker).rawExpr(0xc000432000, 0xc00041b7c0, {0xe6c560?, 0xc00041b700?}, {0x0?, 0x0?}, 0x0)
        /home/rfindley/src/go/src/cmd/compile/internal/types2/expr.go:1251 +0x1a5
cmd/compile/internal/types2.(*Checker).expr(0xb4a998?, 0xc000432000?, {0xe6c560?, 0xc00041b700?})
        /home/rfindley/src/go/src/cmd/compile/internal/types2/expr.go:1793 +0x35
cmd/compile/internal/types2.(*Checker).constDecl(0xc000432000, 0xc0004087e0, {0x0?, 0x0?}, {0xe6c560, 0xc00041b700}, 0x0)
        /home/rfindley/src/go/src/cmd/compile/internal/types2/decl.go:387 +0x2ee
cmd/compile/internal/types2.(*Checker).objDecl(0xc000432000, {0xe70878?, 0xc0004087e0?}, 0x0?)
        /home/rfindley/src/go/src/cmd/compile/internal/types2/decl.go:191 +0x9b9
cmd/compile/internal/types2.(*Checker).packageObjects(0xc000432000)
        /home/rfindley/src/go/src/cmd/compile/internal/types2/resolver.go:685 +0x285
cmd/compile/internal/types2.(*Checker).checkFiles(0xc000432000, {0xc00011c460, 0x1, 0x1})
        /home/rfindley/src/go/src/cmd/compile/internal/types2/check.go:325 +0x14e
cmd/compile/internal/types2.(*Checker).Files(...)
        /home/rfindley/src/go/src/cmd/compile/internal/types2/check.go:300
cmd/compile/internal/types2.(*Config).Check(0xc000117908?, {0xd0685a?, 0x1?}, {0xc00011c460, 0x1, 0x1}, 0x0?)
        /home/rfindley/src/go/src/cmd/compile/internal/types2/api.go:417 +0x70
cmd/compile/internal/noder.checkFiles({0xc00011c438, 0x1, 0x203000?})
        /home/rfindley/src/go/src/cmd/compile/internal/noder/irgen.go:64 +0x45c
cmd/compile/internal/noder.check2({0xc00011c438, 0x1, 0x1})
        /home/rfindley/src/go/src/cmd/compile/internal/noder/irgen.go:77 +0x46
cmd/compile/internal/noder.LoadPackage({0xc000124010, 0x1, 0x1})
        /home/rfindley/src/go/src/cmd/compile/internal/noder/noder.go:84 +0x49c
cmd/compile/internal/gc.Main(0xd338f0)
        /home/rfindley/src/go/src/cmd/compile/internal/gc/main.go:190 +0xb13
main.main()
        /home/rfindley/src/go/src/cmd/compile/main.go:55 +0xdd

@griesemer griesemer added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels May 7, 2022
@griesemer
Copy link
Contributor

This is a compiler bug: The compiler supplies its own size computation function to the type checker, and this size computation function assumes that a type cannot be invalid, which it is in this case. In contrast, the types2 size computation function has a catch-all for cases like these.

We just need to decide if a catch-all or adding the invalid case (adding an entry basicSizes in noder/sizes.go) to is the better solution.

@gopherbot
Copy link

Change https://go.dev/cl/404894 mentions this issue: cmd/compile: don't crash in size computation for invalid type

@golang golang locked and limited conversation to collaborators Jun 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants