Address outstanding TODO, which simplifies subsequent CLs.
Now the compiler always type checks type-switch case clauses (like
gccgo), but it treats clause variables as broken if an appropriate
type cannot be determined for it (like go/types).
Passes toolstash-check.
Change-Id: Iedfe9cdf38c6865211e4b93391f1cf72c1bed136
Reviewed-on: https://go-review.googlesource.com/c/go/+/272648
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>
if len(ls) == 1 {
if ls[0].Op == OTYPE {
vt = ls[0].Type
- } else if ls[0].Op != OLITERAL { // TODO(mdempsky): Should be !ls[0].isNil()
+ } else if !ls[0].isNil() {
// Invalid single-type case;
// mark variable as broken.
vt = nil
}
}
- // TODO(mdempsky): It should be possible to
- // still typecheck the case body.
- if vt == nil {
- continue
- }
-
nvar := ncase.Rlist.First()
nvar.Type = vt
- nvar = typecheck(nvar, ctxExpr|ctxAssign)
+ if vt != nil {
+ nvar = typecheck(nvar, ctxExpr|ctxAssign)
+ } else {
+ // Clause variable is broken; prevent typechecking.
+ nvar.SetTypecheck(1)
+ nvar.SetWalkdef(1)
+ }
ncase.Rlist.SetFirst(nvar)
}
var x interface{}
switch t := x.(type) {
case 0: // ERROR "type"
- t.x = 1 // ERROR "type interface \{\}|reference to undefined field or method"
+ t.x = 1
+ x.x = 1 // ERROR "type interface \{\}|reference to undefined field or method"
}
}