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

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

Open
adg opened this Issue Jan 24, 2013 · 12 comments

Comments

Projects
None yet
7 participants
@adg
Copy link
Contributor

adg commented Jan 24, 2013

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

This comment has been minimized.

Copy link
Member

bradfitz commented Jan 24, 2013

Comment 1:

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

This comment has been minimized.

Copy link
Contributor

robpike commented Mar 7, 2013

Comment 2:

Labels changed: removed go1.1maybe.

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Jul 30, 2013

Comment 3:

Labels changed: added go1.2maybe.

@robpike

This comment has been minimized.

Copy link
Contributor

robpike commented Aug 19, 2013

Comment 4:

Not likely in 1.2.

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

@remyoudompheng

This comment has been minimized.

Copy link
Contributor

remyoudompheng commented Aug 19, 2013

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

This comment has been minimized.

Copy link
Contributor

robpike commented Aug 20, 2013

Comment 6:

Labels changed: removed go1.3maybe.

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Nov 27, 2013

Comment 7:

Labels changed: added go1.3maybe.

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Dec 4, 2013

Comment 8:

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

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Dec 4, 2013

Comment 9:

Labels changed: added repo-main.

@adg adg added accepted labels Dec 4, 2013

@rsc rsc added this to the Unplanned milestone Apr 10, 2015

@rsc rsc changed the title cmd/gc: error message for bad case in switch should suggest select cmd/compile: error message for bad case in switch should suggest select Jun 8, 2015

@odeke-em

This comment has been minimized.

Copy link
Member

odeke-em commented Feb 23, 2017

@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Feb 23, 2017

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

@odeke-em

This comment has been minimized.

Copy link
Member

odeke-em commented Feb 23, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.