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

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

Closed
Carrotman42 opened this issue Apr 17, 2024 · 6 comments
Assignees
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@Carrotman42
Copy link

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

i3d commented Apr 18, 2024

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

@randall77
Copy link
Contributor

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

go101 commented Apr 18, 2024

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)
}

@Carrotman42
Copy link
Author

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.)

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

Minimal reproducer:

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

@gopherbot
Copy link

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

@seankhliao seankhliao changed the title internal compiler error: panic: cmd/compile/internal/types2/check.go:595: assertion failed go/types,types: internal compiler error: panic: cmd/compile/internal/types2/check.go:595: assertion failed Apr 18, 2024
@seankhliao seankhliao changed the title go/types,types: internal compiler error: panic: cmd/compile/internal/types2/check.go:595: assertion failed go/types,types2: internal compiler error: panic: cmd/compile/internal/types2/check.go:595: assertion failed Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants