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: wrong panic message for method call on nil of generic interface type #51521

Open
mdempsky opened this issue Mar 7, 2022 · 2 comments
Labels
NeedsFix
Milestone

Comments

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Mar 7, 2022

The program below currently prints FAIL interface conversion: interface is nil, not main.I and exits with failure when built with cmd/compile. I think this is incorrect, as there's no type assertion in the user code.

I believe the correct behavior is the method call p.M() should raise a "nil pointer dereference" panic, and so the test program should quietly exit with success.

This isn't a spec compliance issue per se, as the Go spec doesn't mandate any particular panic messages. But it is inconsistent with the documentation for package runtime.

package main

import (
	"fmt"
	"strings"
)

type I interface{ M() }

func F[P I](p P) { p.M() }

func main() {
	defer func() {
		err := recover()
		if err, ok := err.(error); ok && strings.Contains(err.Error(), "nil pointer dereference") {
			return
		}
		fmt.Println("FAIL", err)
	}()

	F[I](nil)
}

Marking for Go 1.19, because I doubt this affects any real users.

/cc @griesemer @ianlancetaylor @randall77

@mdempsky mdempsky added the NeedsFix label Mar 7, 2022
@mdempsky mdempsky added this to the Go1.19 milestone Mar 7, 2022
@mdempsky
Copy link
Member Author

@mdempsky mdempsky commented Mar 7, 2022

Another failure case, but this one currently fails to link due to main.main: relocation target go.interface { M() go.shape.int_0 }.M not defined:

package main

import (
	"fmt"
	"strings"
)

func F[T any]() {
	interface{ M() T }.M(nil)
}

func main() {
	defer func() {
		err := recover()
		if err, ok := err.(error); ok && strings.Contains(err.Error(), "nil pointer dereference") {
			return
		}
		fmt.Println("FAIL", err)
	}()

	F[int]()
}

@gopherbot
Copy link

@gopherbot gopherbot commented Mar 7, 2022

Change https://go.dev/cl/390356 mentions this issue: test: add test case for #51521

gopherbot pushed a commit that referenced this issue Mar 7, 2022
The test case is already working with unified IR, so add it to make
sure we don't regress while finishing unified IR's support for
dictionaries.

Updates #51521.

Change-Id: Ib7c8bf9612d30cd552e8e631fd0d487dcb177f14
Reviewed-on: https://go-review.googlesource.com/c/go/+/390356
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix
Projects
None yet
Development

No branches or pull requests

2 participants