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: confusing type assertion failure error message #49005

Closed
rsc opened this issue Oct 15, 2021 · 6 comments
Closed

cmd/compile: confusing type assertion failure error message #49005

rsc opened this issue Oct 15, 2021 · 6 comments
Assignees
Milestone

Comments

@rsc
Copy link
Contributor

@rsc rsc commented Oct 15, 2021

% cat /tmp/x.go
package p

type T interface{ M() }

func F() T

var _ = F().(*X)
% go tool compile /tmp/x.go
/tmp/x.go:7:9: impossible type assertion: F() (value of type T) (missing method M)
/tmp/x.go:7:15: undefined: X

The problem in this program is that X is not defined.
The first error is very confusing and is noise.

@rsc rsc added this to the Go1.18 milestone Oct 15, 2021
@rsc
Copy link
Contributor Author

@rsc rsc commented Oct 15, 2021

Loading

@rsc
Copy link
Contributor Author

@rsc rsc commented Oct 15, 2021

A variant:

% cat /tmp/x.go
package p

type T interface{ M() }

func F() T

var _ = F().(*X)

type X struct{}
% go tool compile /tmp/x.go
/tmp/x.go:7:9: impossible type assertion: F() (value of type T) (missing method M)
% 

In this case X is defined, and the problem is that *X does not implement the M method.
So it is now correct to print an impossible type assertion message.

But the actual message does not show a type assertion.
It should show F().(*X) instead of just F().

I assume that printing the actual assertion expression will result in

/tmp/x.go:7:12: impossible type assertion: F().(*X) (value of type *X) (missing method M)

That will be confusing too, because it will no longer mention T.
It would probably be better to have:

/tmp/x.go:7:12: impossible type assertion: F().(*X)
	*X does not implement T (missing method M)

Loading

@cuonglm
Copy link
Member

@cuonglm cuonglm commented Oct 15, 2021

The problem seems that with types2, when check.conf.CompilerErrorMessages is set, types2 changes the error message to match the compiler. Thus, the error message now does not contain invalid type, so check.err won't skip it.

But I think the root cause is that the check T == Typ[invalid] will be false if T is a pointer to an invalid type. Maybe we should change all that check to include the pointer elem if T is a pointer. So we can terminate the typecheck earlier.

Loading

@griesemer griesemer self-assigned this Oct 17, 2021
@griesemer
Copy link
Contributor

@griesemer griesemer commented Oct 17, 2021

Maybe we should change all that check to include the pointer elem if T is a pointer. So we can terminate the typecheck earlier.

good idea, @cuonglm !

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented Oct 17, 2021

Change https://golang.org/cl/356449 mentions this issue: cmd/compile, types2: avoid confusing follow-on error in invalid type assertion

Loading

gopherbot pushed a commit that referenced this issue Oct 17, 2021
…assertion

This CL avoids a useless follow-on error (that gets reported before the
actual error due to source position). This addresses the first part of
the issue below.

Thanks to @cuonglm for the suggestion for the fix.

For #49005.

Change-Id: Ifdd83072a05c32e115dc58a0233868a64f336f3f
Reviewed-on: https://go-review.googlesource.com/c/go/+/356449
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
@gopherbot
Copy link

@gopherbot gopherbot commented Oct 18, 2021

Change https://golang.org/cl/356512 mentions this issue: cmd/compile, types2: better error message for invalid type assertion

Loading

@gopherbot gopherbot closed this in 254c497 Oct 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants