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: "interface is nil, not interface {}" panic in generic code #47925

Closed
mdempsky opened this issue Aug 23, 2021 · 3 comments
Closed

cmd/compile: "interface is nil, not interface {}" panic in generic code #47925

mdempsky opened this issue Aug 23, 2021 · 3 comments
Assignees
Labels
Milestone

Comments

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Aug 23, 2021

Reported on Discord, this programs fails with a runtime panic with -G=3:

package main

type myifacer[T any] interface{ do(T) error }

type stuff[T any] struct{}

func (s stuff[T]) run() interface{} {
	var i myifacer[T]
	return i
}

func main() {
	stuff[int]{}.run()
}

It works with go2go and GOEXPERIMENT=unified.

/cc @danscales @randall77

@mdempsky mdempsky added this to the Go1.18 milestone Aug 23, 2021
@danscales
Copy link

@danscales danscales commented Aug 24, 2021

@randall77 Looks like my change here: https://go-review.googlesource.com/c/go/+/342009/4/src/cmd/compile/internal/noder/stencil.go
doesn't quite work if the source interface is nil, since type assertions fail for nil values. Is there any simple/clean way to fix this approach, besides putting a nil check around the code?

If only adding a nil check will work, then I think I will just go back to my other implementation https://go-review.googlesource.com/c/go/+/342009/2/src/cmd/compile/internal/noder/stencil.go (which you also thought was fine) that explicitly gets the runtime type from the itab. That approach works with the above test case. I will make it so we share the itabType() code with walk.go, rather than duplicating.

Loading

@danscales danscales self-assigned this Aug 24, 2021
@randall77
Copy link
Contributor

@randall77 randall77 commented Aug 24, 2021

I have a fix for this, at least in part. It's more complicated than either of the above suggestions.
When converting from an interface, we need to use the runtime type from the type word of the interface, not from the dictionary. The dictionary can only provide the interface type for itabs, if the result is a nonempty interface.

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented Aug 24, 2021

Change https://golang.org/cl/344830 mentions this issue: cmd/compile: fix stenciling of conversions between interfaces

Loading

@danscales danscales assigned randall77 and unassigned danscales Aug 25, 2021
@gopherbot gopherbot closed this in 6a35e07 Aug 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants