1
0
mirror of https://github.com/golang/go synced 2024-11-11 20:01:37 -07:00

[dev.regabi] cmd/compile: cleanup type switch typechecking

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>
This commit is contained in:
Matthew Dempsky 2020-11-16 08:44:40 -08:00
parent 96f3fb7244
commit 668e3a598f
2 changed files with 10 additions and 9 deletions

View File

@ -89,22 +89,22 @@ func typecheckTypeSwitch(n *Node) {
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)
}

View File

@ -12,6 +12,7 @@ func main() {
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"
}
}