golang/go

cmd/compile: unhelpful error message if type assertion has mismatching pointer/non-pointer type

Open

#43.673 aberto em 13 de jan. de 2021

Ver no GitHub
 (2 comments) (0 reactions) (0 assignees)Go (19.008 forks)batch import
NeedsFixcompiler/runtimehelp wanted

Métricas do repositório

Stars
 (133.883 stars)
Métricas de merge de PR
 (Nenhuma PRs mesclada em 30d)

Description

What version of Go are you using (go version)?

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

n/a

What did you do?

This code:

type FooError interface {
  Something() int
  Error() string
}

err := someCall();
err.(*FooError)

Complains:

impossible type assertion:
	*FooError does not implement error (missing Error method)

The issue is that you need to write err.(FooError).

What did you expect to see?

It'd be much more helpful if it'd call out that FooError does implement error, so the problem is the added pointer indirection.

This mistake is likely because usually the concrete struct types implement error via the pointer reference, so if you change code from the concrete struct error to an interface type, you're likely to miss a * in there.

What did you see instead?

The error message above.

Guia do colaborador