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: internal compiler error: panic: runtime error: invalid memory address or nil pointer dereference #54399

Closed
dmholtz opened this issue Aug 11, 2022 · 2 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge

Comments

@dmholtz
Copy link

dmholtz commented Aug 11, 2022

Input

package main

import "fmt"

type WeightType interface {
	float64 | int
}

type IEdge interface {
	To() int
}

type Edge struct {
	to int
}

func (e Edge) To() int {
	return e.to
}

type WeightedEdge[W WeightType] struct {
	Edge
	weight W
}

func (e WeightedEdge[W]) Weight() W {
	return e.weight
}

type Graph[N any, E IEdge] struct {
	nodes []N
	edges []E
}

func Accept[N any, E IEdge](gr Graph[N, E]) {
	for _, e := range gr.edges {
		to := e.To()
		fmt.Printf("To: %d\n", to)
	}
}

func main() {
	g := Graph[struct{}, WeightedEdge[int]]{}
	Accept(g)
}

Output:

When attempting to build this program using go1.19 or go1.18.5, an internal compiler error is triggered:

david@w520:~/repos/gobug$ go build main.go
# command-line-arguments
./main.go:38:13: internal compiler error: panic: runtime error: invalid memory address or nil pointer dereference

Please file a bug report including a short program that triggers the error.
https://go.dev/issue/new

In a previous version of go, namely go.1.18.2, this error does NOT occur, i.e. there a no build errors.

david@MacBook golang-compiler-error % go build main.go
david@MacBook golang-compiler-error %

Other Observations:

  • The bug is triggered by a combination of
    • generics
    • nested structs
    • interfaces
  • Exhaustive bug description and other observations in this GitHub Repository

What version of Go are you using (go version)?

$ go version
go version go1.19 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

After realizing that the template asks for a playground link, I c/p the code to playground: https://go.dev/play/p/toMBvIw6jmU

Interesting short-term fix for others who observe this bug

  • avoid generics
  • avoid nested structs

Please refer to my bug report repository for details.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Aug 11, 2022
@cuonglm
Copy link
Member

cuonglm commented Aug 11, 2022

Seems duplicated of #54348

@cuonglm
Copy link
Member

cuonglm commented Aug 11, 2022

Confirm that it's duplicated and fixed by CL https://go-review.googlesource.com/c/go/+/422274

@cuonglm cuonglm closed this as completed Aug 11, 2022
@golang golang locked and limited conversation to collaborators Aug 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

3 participants