From 668e3a598f56d2c9618d800a163f3e784ba3ae0b Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Mon, 16 Nov 2020 08:44:40 -0800 Subject: [PATCH] [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 TryBot-Result: Go Bot Reviewed-by: Robert Griesemer Trust: Matthew Dempsky --- src/cmd/compile/internal/gc/swt.go | 16 ++++++++-------- test/fixedbugs/bug340.go | 3 ++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/cmd/compile/internal/gc/swt.go b/src/cmd/compile/internal/gc/swt.go index 9205f4142a..9ab5f0c248 100644 --- a/src/cmd/compile/internal/gc/swt.go +++ b/src/cmd/compile/internal/gc/swt.go @@ -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) } diff --git a/test/fixedbugs/bug340.go b/test/fixedbugs/bug340.go index 118bbacc22..a067940408 100644 --- a/test/fixedbugs/bug340.go +++ b/test/fixedbugs/bug340.go @@ -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" } } -- 2.50.0