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: bad error message when type switch assertion used outside of type switch #24470

Closed
myitcv opened this issue Mar 21, 2018 · 6 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@myitcv
Copy link
Member

myitcv commented Mar 21, 2018

Please answer these questions before submitting your issue. Thanks!

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

go version go1.10 linux/amd64
go version devel +5f0a9ba134 Tue Mar 20 22:46:00 2018 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes, and tip

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/myitcv/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/myitcv/gostuff"
GORACE=""
GOROOT="/home/myitcv/gos"
GOTMPDIR=""
GOTOOLDIR="/home/myitcv/gos/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build631842484=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/5SSETQWcdrI

I typed something similar to this example by accident. I meant to assert a specific type, but instead used the special type-switch-form of a type assertion by accident.

What did you expect to see?

An error message that clearly identifies the problem on line 10. e.g.

prog.go:10:15: use of .(type) outside type switch

What did you see instead?

A number of error messages, the first of which is, to my mind, a derived/less significant error message:

prog.go:10:5: cannot assign 1 value to 2 variables
prog.go:10:15: use of .(type) outside type switch
prog.go:10:24: undefined: ok
prog.go:11:36: undefined: x

It's early-ish in the morning, but my eyes got fixed on the first error message... and it took longer than is perhaps sensible to get to the meat of the problem which is the second error message.

cc: @griesemer / @mdempsky

Thanks to @mvdan for helping to confirm the issue.

@griesemer
Copy link
Contributor

go/types gives a better error message:

$ gotype x.go
x.go:10:14: invalid AST: use of .(type) outside type switch

so it's conceivable that the compiler could do as well.

Marking for 1.11 in case we get to it, but definitively not urgent.

@griesemer griesemer self-assigned this Mar 21, 2018
@griesemer griesemer added this to the Go1.11 milestone Mar 21, 2018
@myitcv
Copy link
Member Author

myitcv commented Mar 21, 2018

go/types gives a better error message:

Sorry, I should have included that result too. Thanks for doing the legwork.

... but definitively not urgent.

Agreed!

@andybons andybons added the NeedsFix The path to resolution is known, but the work has not been done. label Mar 21, 2018
@griesemer
Copy link
Contributor

This is a cmd/compile/internal/syntax error.

There's also inconsistency. For the following if statement

if x, ok = i.(type); ok {
	fmt.Println("Hello, playground", x)
}

using a = instead of a := the syntax parser reports no error.

@griesemer
Copy link
Contributor

Furthermore, the code:

switch x = i.(type) {
case T:
}

reports (in a syntax parser test):

parser_test.go:25: x.go:12:22: syntax error: assignment x = i.(type) used as value

while

switch x, ok := i.(type) {
case T:
}

reports (test case of issue):

parser_test.go:25: x.go:12:9: cannot assign 1 value to 2 variables

The problem is in the asymmetric handling of type switch guards in the syntax parser.

@gopherbot
Copy link

Change https://golang.org/cl/103615 mentions this issue: cmd/compile: better handling of incorrect type switches

@myitcv
Copy link
Member Author

myitcv commented Apr 3, 2018

Fantastic - thanks very much @griesemer!

For anyone following this issue, the output from my playground example is now:

./main.go:10:15: use of .(type) outside type switch

@golang golang locked and limited conversation to collaborators Apr 3, 2019
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

4 participants