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: make type switch error as precise as type assertion error #16040

Closed
as opened this issue Jun 12, 2016 · 1 comment
Closed

cmd/compile: make type switch error as precise as type assertion error #16040

as opened this issue Jun 12, 2016 · 1 comment
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@as
Copy link
Contributor

as commented Jun 12, 2016

Version: go1.7beta1 plan9/amd64

The type assertion currently returns a more-useful error message than the type switch when a method lacks a pointer receiver.

  1. What did you do?
    https://play.golang.org/p/GWDP3WG57H
  2. What did you expect to see?
    Switch error mentions pointer receiver.
  3. What did you see instead?
    Switch error states that the method is missing, unlike the type assertion, which gives clear cause.

Result from playground link

Type assertion: Clear error message
prog.go:10: impossible type assertion: ast.Ident does not implement ast.Node (End method has pointer receiver)

Type switch: Technically correct error message, but confusing since we have a more descriptive error message in the assertion
prog.go:12: impossible type switch case: node (type ast.Node) cannot have dynamic type ast.Ident (missing End method)

It seems that the type switch (swt.go) has less granularity than the type assertion (typecheck.go)

$GOROOT/src/cmd/compile/internal/gc/swt.go:166: (wrong type for %v method)
$GOROOT/src/cmd/compile/internal/gc/swt.go:168: (missing %v method)

$GOROOT/src/cmd/compile/internal/gc/typecheck.go:967:   (wrong type for %v method)
$GOROOT/src/cmd/compile/internal/gc/typecheck.go:969:   (%v method has pointer receiver)"
$GOROOT/src/cmd/compile/internal/gc/typecheck.go:971:   (missing %v method)
$GOROOT/src/cmd/compile/internal/gc/typecheck.go:973:   (missing %v method)
@ianlancetaylor ianlancetaylor added this to the Go1.8 milestone Jun 12, 2016
@josharian josharian self-assigned this Jun 12, 2016
@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 11, 2016
@rsc rsc modified the milestones: Go1.9, Go1.8 Oct 21, 2016
@odeke-em
Copy link
Member

I fixed this issue with CL https://go-review.googlesource.com/c/35235. It is a duplicate of #10561.

When run on Go1.9, we get:

$ go run main.go 
# command-line-arguments
./main.go:10: impossible type assertion:
	ast.Ident does not implement ast.Node (End method has pointer receiver)
./main.go:12: impossible type switch case: node (type ast.Node) cannot have dynamic type ast.Ident (End method has pointer receiver)

It now mentions that the End method has a pointer receiver as it does with the switch.

Please reopen if am mistaken.

@golang golang locked and limited conversation to collaborators Feb 23, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants