Skip to content

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

Closed

Description

@adonovan

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

Activity

  1. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on May 6, 2022
  2. added this to the Go1.19 milestone on May 6, 2022
  3. findleyr commented on May 7, 2022

    @findleyr
    Contributor

    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?

  4. findleyr commented on May 7, 2022

    @findleyr
    Contributor

    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
    
  5. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    and removed
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on May 7, 2022
  6. griesemer commented on May 7, 2022

    @griesemer
    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.

  7. gopherbot commented on May 7, 2022

    @gopherbot
    Contributor

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

  8. locked and limited conversation to collaborators on Jun 22, 2023
  9. added a commit that references this issue on Sep 19, 2024
    3391517
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

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions