Skip to content

cmd/compile: break inconsistent with continue for switch in a loop #71401

@mneverov

Description

@mneverov

Go version

go1.24-ff2376dbe3

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/mneverov/.cache/go-build'
GODEBUG=''
GOENV='/home/mneverov/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/home/mneverov/tmp/go-build1984094783=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/mneverov/go/pkg/mod'
GOOS='linux'
GOPATH='/home/mneverov/go'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/mneverov/go/src/github.com/golang/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/mneverov/.config/go/telemetry'
GOTMPDIR='/home/mneverov/tmp'
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/mneverov/go/src/github.com/golang/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='devel go1.24-ff2376dbe3 Sat Nov 16 08:22:18 2024 +0000'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

https://go.dev/play/p/Tt15KxMoRpO

func main() {
	for k := range 4 {
		switch k {
		case 0:
			continue
		case 2:
			break
		default:
			fmt.Printf("inside switch %d; ", k)
		}
		fmt.Printf("outside switch %d\n", k)
	}
}

What did you see happen?

continue statement works for the loop i.e. on the first iteration (k=0) nothing will be printed.
break statement works for the switch, i.e. when k=2 outside switch 2 will be printed.

What did you expect to see?

Both continue and break work for either switch statement or loop statement.
I don't see a value in breaking a switch statement and would prefer both break and continue operating on the first loop switch statement belongs to.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeLanguageProposalIssues describing a requested change to the Go language specification.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions