package p
func f(e interface{}) {
switch e := e.(type) {
case T:
e.M()
}
}
$ go tool compile x.go
x.go:5:7: undefined: T
x.go:6:4: e.M undefined (type interface {} is interface with no methods)
The first error message is accurate and helpful. We should suppress the second error message, which is confusing.
I call dibs. 🙂
@josharian Hi. Just to be clear. This happens in select when the selected type upon does not exist. And the compiler still continues even though it should in the case of that case just stop there since the type doesn't exist. Is this assessment right?
This happens in select when the selected type upon does not exist.
Yes.
And the compiler still continues even though it should in the case of that case just stop there since the type doesn't exist.
The compiler continues (for a while) past many errors, since it is often helpful to print out multiple errors instead of the first one. What is needed here is to either mark the body of the case as typechecked without actually typechecking it, or mark the original type of e with Broke() so no further errors involving e get reported. Neither is perfect. With the former, if there are multiple missing type cases they will all get errors reported; with the latter, unrelated typechecking errors in the body will get reported. But this isn’t mission critical, so probably do whatever is cleanest in the code. And don’t forget to add a test—see “errorcheck” style tests in GOROOT/test.
Thanks a lot for the information and the reminder on the test! Much appreciated! 👍
@josharian Hi. I have an idea. A vague one though and I would like to ask if that is an option at all.
So I was thinking that this error is happening because the type is non existent in the type switch. So the back assignment fails to happen to the value. What if I could somehow mark that happening an once the compiler continues it's thing it can detect that the type assert back then actually failed and that it should tell you so when it's inside the case statement. Something like:
$ go tool compile x.go
x.go:5:7: undefined: T
x.go:6:4: type assert failed for this case leg. this type is of type interface thus has no method called `M`.
Or... something in this regard. What say you?
That way, compilation could continue and the error wouldn't be meaningless.
I think it’d be simpler to just suppress the second error and sufficiently informative.
Ack
@josharian so... if I do this:
yyerrorl(lineno, "undefined: %v", n.Sym)
n.Type.SetBroke(true)
It will stop the compiler dead in its track. Nothing further will be reported. This fixes the current issue, but like you said, it's not very ideal.
~/Temp/go/typecheck
❯ go tool compile main.go
main.go:6:7: undefined: T
I have to read the code a bit further to see if anything else is actually possible. Like, I can't just mark the case as errored because nothing is actually returned. And because of switching on interface types is allowed, I can't even use that.
Also, am I even in the right place siwthc swt.go under compile/gc? I think that's the right place to poke at things. But it's actually not that easy to debug.
Oh! This might help..
typecheckdef(n)
if n.Op == ONONAME {
n.Type = nil
return n
}
So if ONONAME is detected, it will return nil. That's interesting. I think I can work out something with that.
Change https://golang.org/cl/151323 mentions this issue: cmd/compile: suppress typecheck errors in a type switch case with broken type
@josharian ping. Hi. :-) would you please mind taking a look at the CL and see if you are satisfied with my solution? At your earliest convenience. Thanks! :-)
Thanks for checking in. This fell off my radar. I am unlikely to look before early next week. If you haven’t heard from me by late next week, please do ping again.
@josharian pinging for the glory of the Sontaran Empire.

Change https://golang.org/cl/158617 mentions this issue: cmd/compile: suppress typecheck errors in a type switch case with broken type
@josharian I'm so sorry, I had to open a new CL. :( Since my PR's fork died after the complete rebase the PR was orphaned. I tried to use the same change-id as @bradfitz suggested, but I kept ending up fighting the gerrit bot which threw me back to the previous version of my change set even though I abandoned the PR and did the steps outline in the Gerrit work flow.
Sincerest apologies for the trouble caused. :/
I addressed your comments in the new CL here: https://go-review.googlesource.com/c/go/+/158617
I'm so sorry, I had to open a new CL.
No prob. Please abandon the old CL when you get a chance.
Yeah I don't have permission to abandon the other one cause it was created through a PR I guess.
Two related cases that might be worth looking into (not necessarily as part of CL 158617):
"case 42" in a type switch is currently treated as "case int" (aside from emitting an error). It might be better to treat the case as broken, like we do "case doesnotexist:". For example:
switch e := interface{}(nil).(type) {
case 42: // want an error here
e.M() // probably don't need an error here
}
The problem here is we're not checking ncase.List.First().Op == OTYPE before setting nvar.Type = ncase.List.First().Type.
We should try to still typecheck broken case bodies, because there's possibly still code that doesn't depend on the type switch variable that we can produce meaningful errors about.
switch e := interface{}(nil).(type) {
case doesnotexist: // want an error here
alsodoesnotexist() // an error here would be good too
}
Theoretically this should be as simple as just setting nvar.Type = nil and continuing with typechecking, but I couldn't immediately figure out how to prevent typecheckdef from giving false positive warnings about using .(type) outside of a switch statement.
@mdempsky Thanks, I'll extract it into a separate Issue! :)
Most helpful comment
Thanks for checking in. This fell off my radar. I am unlikely to look before early next week. If you haven’t heard from me by late next week, please do ping again.