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

unexpected missing return at end of func with switch cases exhausting all possible values of a type #39268

Closed
bboozzoo opened this issue May 27, 2020 · 2 comments

Comments

@bboozzoo
Copy link

@bboozzoo bboozzoo commented May 27, 2020

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

$ go version
go version go1.14.3 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/maciek/.cache/go-build"
GOENV="/home/maciek/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH=""
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/maciek/code/go/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/maciek/code/go/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build201835476=/tmp/go-build -gno-record-gcc-switches"

What did you do?

The following piece of code does not compile, even though all possible values of given type are handled in cases and return.

func foo(val bool) error {
	switch val {
	case true:
		return nil
	case false:
		return nil
//	default: // WAT?
//		return nil
	}
}

See https://play.golang.org/p/gUjEufse8gF

Adding a default case that returns makes the code compile too.

What did you expect to see?

no errors

What did you see instead?

./prog.go:16:1: missing return at end of function
@randall77
Copy link
Contributor

@randall77 randall77 commented May 27, 2020

This error follows from the definition of terminating statements, and the use of that definition in function declarations.

The definition of terminating statement does not include exhaustive switches over finite data types. Maybe it could, but it can get tricky. What about this:

x := true
y := false
switch val {
    case x: return nil
    case y: return nil
}

or this

switch {
    case val == true:
    case val == false:
}

or this

switch {
    case val:
    case !val:
}
@randall77
Copy link
Contributor

@randall77 randall77 commented May 27, 2020

To fix this issue, we'd have to change the spec. That requires going through the proposal process and specifying what exactly the new rule would be.

I'm going to close this issue. Feel free to go through the proposal process if you have a spec change you think would work.

@randall77 randall77 closed this May 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.