golang/go

go/types,types2: internal compiler error: panic: cmd/compile/internal/types2/check.go:595: assertion failed

Carrotman42 opened this issue · 6 comments

Go version

go dev branch (for playground)

Output of go env in your module/workspace:

go playground (sorry, not sure how to get the env from there)

What did you do?

This program crashes the compiler at the current dev branch in the playground: https://go.dev/play/p/3JI9h22Igk8?v=gotip

(sidenote: I was only trying to get this part of the spec to compile :])

What did you see happen?

Compiler crash

What did you expect to see?

Compiler success or standard error message

i3d commented

I'd also request to clarify whether this is indeed an intended syntax [or some sort of typo]

@griesemer

import "fmt"

func recv[T ~bool](ch <-chan T) {
	var x, ok T = <-ch
	fmt.Println(x, ok)
}

func main() {
	ch := make(chan bool, 1)
	ch <- true
	recv(ch)
}
# command-line-arguments
<unknown line number>: internal compiler error: panic: ../../../sandbox/tmp/src/cmd/compile/internal/types2/check.go:596: assertion failed

goroutine 1 [running]:
runtime/debug.Stack()
	../../../sandbox/tmp/src/runtime/debug/stack.go:26 +0x64
cmd/compile/internal/base.FatalfAt({0x300e8?, 0x140?}, {0x10182d7e2, 0x9}, {0x14000030118, 0x1, 0x1})
	../../../sandbox/tmp/src/cmd/compile/internal/base/print.go:225 +0x1fc
cmd/compile/internal/base.Fatalf(...)
	../../../sandbox/tmp/src/cmd/compile/internal/base/print.go:194
cmd/compile/internal/gc.handlePanic()
	../../../sandbox/tmp/src/cmd/compile/internal/gc/main.go:52 +0x90
panic({0x1019d2fa0?, 0x1400002cfa0?})
	../../../sandbox/tmp/src/runtime/panic.go:759 +0x124
cmd/compile/internal/types2.(*Checker).handleBailout(0x1400043c000, 0x14000031020)
	../../../sandbox/tmp/src/cmd/compile/internal/types2/check.go:364 +0x9c
panic({0x1019d2fa0?, 0x1400002cfa0?})
	../../../sandbox/tmp/src/runtime/panic.go:759 +0x124
cmd/compile/internal/types2.assert(0x10?)
	../../../sandbox/tmp/src/cmd/compile/internal/types2/errors.go:25 +0x60
cmd/compile/internal/types2.(*Checker).recordCommaOkTypes(0x1400043c000, {0x101a7d3c0, 0x14000438060}, {0x1400002cf70, 0x2, 0x0?})
	../../../sandbox/tmp/src/cmd/compile/internal/types2/check.go:596 +0x15c
cmd/compile/internal/types2.(*Checker).initVars(0x1400043c000, {0x1400002cf60, 0x2, 0x2}, {0x140000305f0, 0x1, 0x1}, {0x0, 0x0})
	../../../sandbox/tmp/src/cmd/compile/internal/types2/assignments.go:431 +0x430
cmd/compile/internal/types2.(*Checker).varDecl(0x1400043c000, 0x1400043f9d0, {0x1400002cf60, 0x2, 0x2}, {0x101a7d258, 0x14000432a50}, {0x101a7d3c0, 0x14000438060})
	../../../sandbox/tmp/src/cmd/compile/internal/types2/decl.go:466 +0x1f4

It is probably confused by the fact that the ok part of the assignment is ~bool.

I believe the syntax your are using is correct, although weird. Probably better (and solves the crash) to do x, ok := <-ch.
Converting the ok to the type parameter can always be done later.

go101 commented

Similar:

package main

import "fmt"

func foo[T ~bool](m map[int]T) {
	var x, ok T = m[0]
	fmt.Println(x, ok)
}

func main() {
	m := map[int]bool{}
	foo(m)
}

Thanks for taking a look, @randall77 . To be clear I don't actually need a workaround here - I filed an issue because the crash message told me to do so :)

(I'm not really writing this code, I was just trying to get this code example from the spec to compile and found this unrelated problem.)

Minimal reproducer:

func _[T bool](ch chan T) {
	var _, _ T = <-ch
}

Change https://go.dev/cl/580075 mentions this issue: go/types, types2: use correct predicate when asserting comma-ok types