]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: make type switch case positions consistent
authorJosh Bleecher Snyder <josharian@gmail.com>
Tue, 14 Apr 2020 06:28:32 +0000 (23:28 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Tue, 14 Apr 2020 17:44:16 +0000 (17:44 +0000)
CL 228106 moved the position at which we
checked whether a type switch variable had a particular type
from the type switch to the case statement, but only for
single, concrete types. This is a better position,
so this change changes the rest.

Change-Id: I601d4a5c4a0d9400e7804b9f1e729af948349a8f
Reviewed-on: https://go-review.googlesource.com/c/go/+/228220
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
src/cmd/compile/internal/gc/swt.go

index 88c8ea8146a6405cfc37d1994d594724751d2456..138b0acc537be9216b007cdc0b4ded9305873672 100644 (file)
@@ -569,10 +569,10 @@ func walkTypeSwitch(sw *Node) {
                        }
 
                        if singleType != nil && singleType.IsInterface() {
-                               s.Add(n1.Type, caseVar, jmp)
+                               s.Add(ncase.Pos, n1.Type, caseVar, jmp)
                                caseVarInitialized = true
                        } else {
-                               s.Add(n1.Type, nil, jmp)
+                               s.Add(ncase.Pos, n1.Type, nil, jmp)
                        }
                }
 
@@ -629,12 +629,12 @@ type typeClause struct {
        body Nodes
 }
 
-func (s *typeSwitch) Add(typ *types.Type, caseVar *Node, jmp *Node) {
+func (s *typeSwitch) Add(pos src.XPos, typ *types.Type, caseVar, jmp *Node) {
        var body Nodes
        if caseVar != nil {
                l := []*Node{
-                       nod(ODCL, caseVar, nil),
-                       nod(OAS, caseVar, nil),
+                       nodl(pos, ODCL, caseVar, nil),
+                       nodl(pos, OAS, caseVar, nil),
                }
                typecheckslice(l, ctxStmt)
                body.Append(l...)
@@ -643,9 +643,9 @@ func (s *typeSwitch) Add(typ *types.Type, caseVar *Node, jmp *Node) {
        }
 
        // cv, ok = iface.(type)
-       as := nod(OAS2, nil, nil)
+       as := nodl(pos, OAS2, nil, nil)
        as.List.Set2(caseVar, s.okname) // cv, ok =
-       dot := nod(ODOTTYPE, s.facename, nil)
+       dot := nodl(pos, ODOTTYPE, s.facename, nil)
        dot.Type = typ // iface.(type)
        as.Rlist.Set1(dot)
        as = typecheck(as, ctxStmt)
@@ -653,7 +653,7 @@ func (s *typeSwitch) Add(typ *types.Type, caseVar *Node, jmp *Node) {
        body.Append(as)
 
        // if ok { goto label }
-       nif := nod(OIF, nil, nil)
+       nif := nodl(pos, OIF, nil, nil)
        nif.Left = s.okname
        nif.Nbody.Set1(jmp)
        body.Append(nif)