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

go/types: "missing return" error should be "unreachable statement" #65794

Closed
BigET opened this issue Feb 19, 2024 · 3 comments
Closed

go/types: "missing return" error should be "unreachable statement" #65794

BigET opened this issue Feb 19, 2024 · 3 comments
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@BigET
Copy link

BigET commented Feb 19, 2024

Go version

go version go1.22rc2 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/biget/.cache/go-build'
GOENV='/home/biget/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/edi/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/edi/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go-1.22'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go-1.22/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22rc2'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/user/1300/go-build1494716450=/tmp/go-build -gno-record-gcc-switches'

What did you do?

The following function should either compile without problem or give a "unreachable code", but it gives "missing return".

func bad_return(x bool) int8 {
	switch x {
	case true:
		return 1
		break
	default:
		return 0
	}
}

It might has something to do with the order of validation.

If I add a return to the end of the function, then I get "unreachable code".

Here is the same function in the playground

What did you see happen?

./prog.go:15:1: missing return

What did you expect to see?

./prog.go:11:3: unreachable code

@adonovan adonovan changed the title "missing return error" reported when "unreachable code" shoud be issued. go/types: "missing return" error should be "unreachable statement" Feb 19, 2024
@findleyr findleyr added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Feb 19, 2024
@findleyr findleyr added this to the Backlog milestone Feb 19, 2024
@findleyr findleyr removed their assignment Feb 19, 2024
@findleyr
Copy link
Contributor

findleyr commented Feb 19, 2024

The error about unreachable code is a vet check, not a type checker error.
The type checker implements a rather strict definition of terminating statements, specified here. Per that specification, the type checker is correct.

We could improve this, but I'm not sure it's a common enough mistake, nor confusing enough, to warrant immediate attention. I'm moving this into the backlog, though I'll discuss with @griesemer in our next meeting.

@griesemer
Copy link
Contributor

The reported error is correct per the spec. I don't think we should do anything here.

I note that the program with an additional return at the end actually runs in the playground, even with the "unreachable code" error.

@findleyr
Copy link
Contributor

Agreed, closing this as working as intended.

@findleyr findleyr closed this as not planned Won't fix, can't repro, duplicate, stale Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

3 participants