golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.07k stars 17.68k forks source link

cmd/compile: error message for bad case in switch should suggest select #4697

Open adg opened 11 years ago

adg commented 11 years ago
When writing a switch statement like this:

package main

func main() {
    ch := make(chan int)    
    switch {
    case <-ch:
    default:
    }
}

The compiler error is:

prog.go:6: invalid case <-ch in switch (mismatched types int and bool)

This is a correct message, but for cases where the user is doing channel operations in
the cases, can we add to the parenthetical "; did you mean select?" ? It
shouldn't be displayed if the type of the channel expression is bool, obviously.
bradfitz commented 11 years ago

Comment 1:

In addition to the original bug report, I admit that I've also made this mistake a few
times.
robpike commented 11 years ago

Comment 2:

Labels changed: removed go1.1maybe.

rsc commented 11 years ago

Comment 3:

Labels changed: added go1.2maybe.

robpike commented 11 years ago

Comment 4:

Not likely in 1.2.

Labels changed: added go1.3maybe, removed go1.2maybe.

remyoudompheng commented 11 years ago

Comment 5:

Currently if you use a send statement in a switch you get this:
$ cat z.go
package main
func main() {
      var c chan int
      switch {
      case c <- 0:
      }
}
$ go tool 6g z.go
z.go:6: send statement c <- 0 used as value; use select for non-blocking send
But it doesn't mean what you would expect and has nothing to do with switches. Actually
the message is inherited from a pre r56 version
(http://golang.org/doc/devel/weekly.html#2011-02-01) and it's rather confusing. I
suggest that if someone fixes this issue the special message is removed.
robpike commented 11 years ago

Comment 6:

Labels changed: removed go1.3maybe.

rsc commented 10 years ago

Comment 7:

Labels changed: added go1.3maybe.

rsc commented 10 years ago

Comment 8:

Labels changed: added release-none, removed go1.3maybe.

rsc commented 10 years ago

Comment 9:

Labels changed: added repo-main.

odeke-em commented 7 years ago

I've mailed https://go-review.googlesource.com/37392.

gopherbot commented 7 years ago

CL https://golang.org/cl/37392 mentions this issue.

odeke-em commented 7 years ago

As mentioned by @griesemer in CL 37392, the compiler giving such suggestions is a slippery slope, so for now let's keep the issue and the CL open in case new ideas come up.