]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: simplify some declarations
authorDaniel Martí <mvdan@mvdan.cc>
Wed, 11 Oct 2017 09:14:31 +0000 (10:14 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Sun, 15 Oct 2017 19:24:40 +0000 (19:24 +0000)
Reduce the scope of some. Also remove vars that were simply the index or
the value in a range statement. While at it, remove a var that was
exactly the length of a slice.

Also replaced 'bad' with a more clear 'errored' of type bool, and
renamed a single-char name with a comment to a name that is
self-explanatory.

And removed a few unnecessary Index calls within loops.

Passes toolstash -cmp on std cmd.

Change-Id: I26eee5f04e8f7e5418e43e25dca34f89cca5c80a
Reviewed-on: https://go-review.googlesource.com/70930
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/fmt.go
src/cmd/compile/internal/gc/noder.go
src/cmd/compile/internal/gc/order.go
src/cmd/compile/internal/gc/reflect.go
src/cmd/compile/internal/gc/sinit.go
src/cmd/compile/internal/gc/subr.go
src/cmd/compile/internal/gc/typecheck.go
src/cmd/compile/internal/gc/walk.go

index 0ba1ba3d80b4e3bd1aea6b6b8098d7bec27eadae..0c93be506491b2ecd606f55fab961b2debb9aa5d 100644 (file)
@@ -1537,13 +1537,11 @@ func (n *Node) exprfmt(s fmt.State, prec int, mode fmtMode) {
                n.Right.exprfmt(s, nprec+1, mode)
 
        case OADDSTR:
-               i := 0
-               for _, n1 := range n.List.Slice() {
+               for i, n1 := range n.List.Slice() {
                        if i != 0 {
                                fmt.Fprint(s, " + ")
                        }
                        n1.exprfmt(s, nprec, mode)
-                       i++
                }
 
        case OCMPSTR, OCMPIFACE:
index fc49c552755f1ba18c101acc719a94ed3d9423a5..774bef1064938bdbf8ea3c0e0f18a6bbdd2a1ea4 100644 (file)
@@ -19,7 +19,6 @@ import (
 )
 
 func parseFiles(filenames []string) uint {
-       var lines uint
        var noders []*noder
        // Limit the number of simultaneously open files.
        sem := make(chan struct{}, runtime.GOMAXPROCS(0)+10)
@@ -45,6 +44,7 @@ func parseFiles(filenames []string) uint {
                }(filename)
        }
 
+       var lines uint
        for _, p := range noders {
                for e := range p.err {
                        yyerrorpos(e.Pos, "%s", e.Msg)
index fdee8b89a55a424dd9834d944b66e1fc75a292de..517aa5a8bf257746e29a8a108cfa6062ae578d82 100644 (file)
@@ -235,18 +235,16 @@ func poptemp(mark ordermarker, order *Order) {
 // above the mark on the temporary stack, but it does not pop them
 // from the stack.
 func cleantempnopop(mark ordermarker, order *Order, out *[]*Node) {
-       var kill *Node
-
        for i := len(order.temp) - 1; i >= int(mark); i-- {
                n := order.temp[i]
                if n.Name.Keepalive() {
                        n.Name.SetKeepalive(false)
                        n.SetAddrtaken(true) // ensure SSA keeps the n variable
-                       kill = nod(OVARLIVE, n, nil)
+                       kill := nod(OVARLIVE, n, nil)
                        kill = typecheck(kill, Etop)
                        *out = append(*out, kill)
                }
-               kill = nod(OVARKILL, n, nil)
+               kill := nod(OVARKILL, n, nil)
                kill = typecheck(kill, Etop)
                *out = append(*out, kill)
        }
@@ -450,26 +448,20 @@ func ordermapassign(n *Node, order *Order) {
 
        case OAS2, OAS2DOTTYPE, OAS2MAPR, OAS2FUNC:
                var post []*Node
-               var m *Node
-               var a *Node
-               for i1, n1 := range n.List.Slice() {
-                       if n1.Op == OINDEXMAP {
-                               m = n1
+               for i, m := range n.List.Slice() {
+                       switch {
+                       case m.Op == OINDEXMAP:
                                if !m.Left.IsAutoTmp() {
                                        m.Left = ordercopyexpr(m.Left, m.Left.Type, order, 0)
                                }
                                if !m.Right.IsAutoTmp() {
                                        m.Right = ordercopyexpr(m.Right, m.Right.Type, order, 0)
                                }
-                               n.List.SetIndex(i1, ordertemp(m.Type, order, false))
-                               a = nod(OAS, m, n.List.Index(i1))
-                               a = typecheck(a, Etop)
-                               post = append(post, a)
-                       } else if instrumenting && n.Op == OAS2FUNC && !isblank(n.List.Index(i1)) {
-                               m = n.List.Index(i1)
+                               fallthrough
+                       case instrumenting && n.Op == OAS2FUNC && !isblank(m):
                                t := ordertemp(m.Type, order, false)
-                               n.List.SetIndex(i1, t)
-                               a = nod(OAS, m, t)
+                               n.List.SetIndex(i, t)
+                               a := nod(OAS, m, t)
                                a = typecheck(a, Etop)
                                post = append(post, a)
                        }
@@ -765,8 +757,8 @@ func orderstmt(n *Node, order *Order) {
                        // hiter contains pointers and needs to be zeroed.
                        prealloc[n] = ordertemp(hiter(n.Type), order, true)
                }
-               for i := range n.List.Slice() {
-                       n.List.SetIndex(i, orderexprinplace(n.List.Index(i), order))
+               for i, n1 := range n.List.Slice() {
+                       n.List.SetIndex(i, orderexprinplace(n1, order))
                }
                orderblockNodes(&n.Nbody)
                order.out = append(order.out, n)
@@ -788,14 +780,11 @@ func orderstmt(n *Node, order *Order) {
        case OSELECT:
                t := marktemp(order)
 
-               var tmp1 *Node
-               var tmp2 *Node
-               var r *Node
                for _, n2 := range n.List.Slice() {
                        if n2.Op != OXCASE {
                                Fatalf("order select case %v", n2.Op)
                        }
-                       r = n2.Left
+                       r := n2.Left
                        setlineno(n2)
 
                        // Append any new body prologue to ninit.
@@ -856,16 +845,16 @@ func orderstmt(n *Node, order *Order) {
                                                // use channel element type for temporary to avoid conversions,
                                                // such as in case interfacevalue = <-intchan.
                                                // the conversion happens in the OAS instead.
-                                               tmp1 = r.Left
+                                               tmp1 := r.Left
 
                                                if r.Colas() {
-                                                       tmp2 = nod(ODCL, tmp1, nil)
+                                                       tmp2 := nod(ODCL, tmp1, nil)
                                                        tmp2 = typecheck(tmp2, Etop)
                                                        n2.Ninit.Append(tmp2)
                                                }
 
                                                r.Left = ordertemp(r.Right.Left.Type.Elem(), order, types.Haspointers(r.Right.Left.Type.Elem()))
-                                               tmp2 = nod(OAS, tmp1, r.Left)
+                                               tmp2 := nod(OAS, tmp1, r.Left)
                                                tmp2 = typecheck(tmp2, Etop)
                                                n2.Ninit.Append(tmp2)
                                        }
@@ -874,15 +863,15 @@ func orderstmt(n *Node, order *Order) {
                                                r.List.Set(nil)
                                        }
                                        if r.List.Len() != 0 {
-                                               tmp1 = r.List.First()
+                                               tmp1 := r.List.First()
                                                if r.Colas() {
-                                                       tmp2 = nod(ODCL, tmp1, nil)
+                                                       tmp2 := nod(ODCL, tmp1, nil)
                                                        tmp2 = typecheck(tmp2, Etop)
                                                        n2.Ninit.Append(tmp2)
                                                }
 
                                                r.List.Set1(ordertemp(types.Types[TBOOL], order, false))
-                                               tmp2 = okas(tmp1, r.List.First())
+                                               tmp2 := okas(tmp1, r.List.First())
                                                tmp2 = typecheck(tmp2, Etop)
                                                n2.Ninit.Append(tmp2)
                                        }
index fd9e3e0a8e5e67523992a98234ee2a8636f09a5f..41dcfe994a7d48184c467e881fb0c4f5e1a65ba6 100644 (file)
@@ -1332,11 +1332,9 @@ func dtypesym(t *types.Type) *obj.LSym {
        // ../../../../runtime/type.go:/structType
        // for security, only the exported fields.
        case TSTRUCT:
-               n := 0
-
-               for _, t1 := range t.Fields().Slice() {
+               fields := t.Fields().Slice()
+               for _, t1 := range fields {
                        dtypesym(t1.Type)
-                       n++
                }
 
                // All non-exported struct field names within a struct
@@ -1345,7 +1343,7 @@ func dtypesym(t *types.Type) *obj.LSym {
                // struct type descriptor, we can omit that
                // information from the field descriptors.
                var spkg *types.Pkg
-               for _, f := range t.Fields().Slice() {
+               for _, f := range fields {
                        if !exportname(f.Sym.Name) {
                                spkg = f.Sym.Pkg
                                break
@@ -1355,13 +1353,13 @@ func dtypesym(t *types.Type) *obj.LSym {
                ot = dcommontype(lsym, ot, t)
                ot = dgopkgpath(lsym, ot, spkg)
                ot = dsymptr(lsym, ot, lsym, ot+3*Widthptr+uncommonSize(t))
-               ot = duintptr(lsym, ot, uint64(n))
-               ot = duintptr(lsym, ot, uint64(n))
+               ot = duintptr(lsym, ot, uint64(len(fields)))
+               ot = duintptr(lsym, ot, uint64(len(fields)))
 
-               dataAdd := n * structfieldSize()
+               dataAdd := len(fields) * structfieldSize()
                ot = dextratype(lsym, ot, t, dataAdd)
 
-               for _, f := range t.Fields().Slice() {
+               for _, f := range fields {
                        // ../../../../runtime/type.go:/structField
                        ot = dnameField(lsym, ot, spkg, f)
                        ot = dsymptr(lsym, ot, dtypesym(f.Type), 0)
index 3d3899d3c6f698c33e9b47953d5ec2b186eb6f48..6b681f8bf38b62f1251b1b3aa6b8e39d859e2172 100644 (file)
@@ -228,8 +228,7 @@ func init2list(l Nodes, out *[]*Node) {
 }
 
 func initreorder(l []*Node, out *[]*Node) {
-       var n *Node
-       for _, n = range l {
+       for _, n := range l {
                switch n.Op {
                case ODCLFUNC, ODCLCONST, ODCLTYPE:
                        continue
@@ -883,11 +882,10 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) {
 
        // put dynamics into array (5)
        var index int64
-       for _, r := range n.List.Slice() {
-               value := r
-               if r.Op == OKEY {
-                       index = nonnegintconst(r.Left)
-                       value = r.Right
+       for _, value := range n.List.Slice() {
+               if value.Op == OKEY {
+                       index = nonnegintconst(value.Left)
+                       value = value.Right
                }
                a := nod(OINDEX, vauto, nodintconst(index))
                a.SetBounded(true)
index 81e5bd7e6e7c11434b5bf59d88652e2bc1af885d..8faec66aa098826a8e743d6c3ddcd8491659a6c6 100644 (file)
@@ -257,9 +257,6 @@ func restrictlookup(name string, pkg *types.Pkg) *types.Sym {
 // find all the exported symbols in package opkg
 // and make them available in the current package
 func importdot(opkg *types.Pkg, pack *Node) {
-       var s1 *types.Sym
-       var pkgerror string
-
        n := 0
        for _, s := range opkg.Syms {
                if s.Def == nil {
@@ -268,9 +265,9 @@ func importdot(opkg *types.Pkg, pack *Node) {
                if !exportname(s.Name) || strings.ContainsRune(s.Name, 0xb7) { // 0xb7 = center dot
                        continue
                }
-               s1 = lookup(s.Name)
+               s1 := lookup(s.Name)
                if s1.Def != nil {
-                       pkgerror = fmt.Sprintf("during import %q", opkg.Path)
+                       pkgerror := fmt.Sprintf("during import %q", opkg.Path)
                        redeclare(s1, pkgerror)
                        continue
                }
index ab2c77a3fb2079cfc6a73cd3d2d249d242829536..78c59b5dff675ff9b4b72263388b8863b1666c79 100644 (file)
@@ -3017,7 +3017,7 @@ func typecheckcomplit(n *Node) *Node {
                for i3, l := range n.List.Slice() {
                        setlineno(l)
                        if l.Op != OKEY {
-                               n.List.SetIndex(i3, typecheck(n.List.Index(i3), Erv))
+                               n.List.SetIndex(i3, typecheck(l, Erv))
                                yyerror("missing key in map literal")
                                continue
                        }
@@ -3044,7 +3044,7 @@ func typecheckcomplit(n *Node) *Node {
                // Need valid field offsets for Xoffset below.
                dowidth(t)
 
-               bad := 0
+               errored := false
                if n.List.Len() != 0 && nokeys(n.List) {
                        // simple list of variables
                        ls := n.List.Slice()
@@ -3053,10 +3053,10 @@ func typecheckcomplit(n *Node) *Node {
                                n1 = typecheck(n1, Erv)
                                ls[i] = n1
                                if i >= t.NumFields() {
-                                       if bad == 0 {
+                                       if !errored {
                                                yyerror("too many values in struct initializer")
+                                               errored = true
                                        }
-                                       bad++
                                        continue
                                }
 
@@ -3113,10 +3113,10 @@ func typecheckcomplit(n *Node) *Node {
                                }
 
                                if l.Op != OSTRUCTKEY {
-                                       if bad == 0 {
+                                       if !errored {
                                                yyerror("mixture of field:value and value initializers")
+                                               errored = true
                                        }
-                                       bad++
                                        ls[i] = typecheck(ls[i], Erv)
                                        continue
                                }
index 48e6319e439f670667c967b13648c843023a5b4e..b7db5b29d46e90286f9569aa73c281b587f87b14 100644 (file)
@@ -2337,18 +2337,18 @@ out:
 // then it is done first. otherwise must
 // make temp variables
 func reorder1(all []*Node) []*Node {
-       c := 0 // function calls
-       t := 0 // total parameters
+       if len(all) == 1 {
+               return all
+       }
 
+       funcCalls := 0
        for _, n := range all {
-               t++
                updateHasCall(n)
                if n.HasCall() {
-                       c++
+                       funcCalls++
                }
        }
-
-       if c == 0 || t == 1 {
+       if funcCalls == 0 {
                return all
        }
 
@@ -2363,7 +2363,7 @@ func reorder1(all []*Node) []*Node {
                }
 
                d++
-               if d == c {
+               if d == funcCalls {
                        f = n
                        continue
                }
@@ -3898,11 +3898,9 @@ func walkprintfunc(n *Node, init *Nodes) *Node {
        }
 
        t := nod(OTFUNC, nil, nil)
-       num := 0
        var printargs []*Node
-       for _, n1 := range n.List.Slice() {
-               buf := fmt.Sprintf("a%d", num)
-               num++
+       for i, n1 := range n.List.Slice() {
+               buf := fmt.Sprintf("a%d", i)
                a := namedfield(buf, n1.Type)
                t.List.Append(a)
                printargs = append(printargs, a.Left)