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

Build errors while placing an indentifier in a switch statement. #40603

Closed
Jorropo opened this issue Aug 6, 2020 · 4 comments
Closed

Build errors while placing an indentifier in a switch statement. #40603

Jorropo opened this issue Aug 6, 2020 · 4 comments

Comments

@Jorropo
Copy link
Member

Jorropo commented Aug 6, 2020

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

$ go version
go version go1.14.6 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/Jorropo/.cache/go-build"
GOENV="/home/Jorropo/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/Jorropo/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/Jorropo/Project/go.mod"
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-build491127916=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I have switch where there is a case A, case B and a default. The 2 cases must fallthough to the default (due to obvious geometrical reasons this can't fit), so one case get a fallthrough while the other goto a label placed before the default.
https://play.golang.org/p/Xh4eFsskySm

What did you expect to see?

I expect the first case to catch 1 and then jump over the case 2 and execute the default.

What did you see instead?

Build errors with : ./prog.go:12:11: syntax error: missing statement after label.

I also tried switching the place of the default and the label but this doesn't build (that expected since this would be jumping in a block).
The other case can also be completely removed and this still doesn't work (but yes with this a fallthrough could do it but that not what I need).

@ianlancetaylor
Copy link
Contributor

Well, that is how the language works. Permitting a label on a case or default clause would be hard to define, because of code like

    switch x := v.(type) {
    case int:
        goto label
    case float32:
        break
  label:
    case int32:
        // what is the type of x here?
    }

In other words, the same reasons that prevent you from jumping into a block also prevent you from jumping to a case clause.

@ianlancetaylor
Copy link
Contributor

Since this is the way that the language is designed and intended, I'm going to close this issue.

@Jorropo
Copy link
Member Author

Jorropo commented Aug 6, 2020

Well, that is how the language works. Permitting a label on a case or default clause would be hard to define, because of code like

    switch x := v.(type) {
    case int:
        goto label
    case float32:
        break
  label:
    case int32:
        // what is the type of x here?
    }

In other words, the same reasons that prevent you from jumping into a block also prevent you from jumping to a case clause.

This is only a problem in a type switch.
See how switch deals with fallthrough in type switch :
https://play.golang.org/p/6VwniYemffi
error with : cannot fallthrough in type switch.

I don't see why the same thing can't be done for goto.
In your example the build would just error with something like this: cannot jump in type switch.
For all other non type switch this would just works.
What I want to do is exactly the same as a fallthrough except a fallthrough use the natural increment of rip while I want to move rip using a jump instruction. I don't see any major difference.

@ianlancetaylor
Copy link
Contributor

True, it probably could be made to work, but the cost would be another exception to the language.

If you want to propose a change to the language, see https://golang.org/s/proposal. But I would not accept this proposal to be accepted.

@golang golang locked and limited conversation to collaborators Aug 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants