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

Closed
mdempsky opened this issue Mar 7, 2022 · 4 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@mdempsky
Copy link
Member

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 The path to resolution is known, but the work has not been done. label Mar 7, 2022
@mdempsky mdempsky added this to the Go1.19 milestone Mar 7, 2022
@mdempsky
Copy link
Member Author

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

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

Rolling forward to 1.20 as it should be fixed by unified IR. Please comment if you disagree. Thanks.

@ianlancetaylor ianlancetaylor modified the milestones: Go1.19, Go1.20 Jun 24, 2022
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
@mdempsky
Copy link
Member Author

This is fixed with GOEXPERIMENT=unified, which will be enabled by default in 1.20.

@golang golang locked and limited conversation to collaborators Nov 30, 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 NeedsFix The path to resolution is known, but the work has not been done.
Projects
Development

No branches or pull requests

3 participants