]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: make bad Ctypes be only 0
authorDaniel Martí <mvdan@mvdan.cc>
Tue, 10 Oct 2017 16:36:03 +0000 (17:36 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Tue, 10 Oct 2017 17:06:53 +0000 (17:06 +0000)
Before, -1 meant a node being nil or not an OLITERAL, and 0 meant an
OLITERAL missing a Val.

However, the use of this value was confusing and led to some issues,
such as swt.go checking for < 0 instead of <= 0, causing panics.

We never need to differentiate these two cases, so collapse both into 0.
To make it clear that negative values can no longer happen, make Ctype
an uint8.

With this change, we can now get rid of the two n.Type == nil checks
in swt.go added to fix a couple of these panics.

Thanks to Matthew Dempsky for spotting this inconsistency.

Fixes #22001.

Change-Id: I51c65a76f38a3e16788b6a3b57932dad3436dc7e
Reviewed-on: https://go-review.googlesource.com/69510
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/const.go
src/cmd/compile/internal/gc/order.go
src/cmd/compile/internal/gc/swt.go

index 4d52d091b043251f15e9c5d53ff277fc861de3dd..1d67e5fd104e01324405390c04f3bc7c15d50779 100644 (file)
@@ -12,7 +12,7 @@ import (
 )
 
 // Ctype describes the constant kind of an "ideal" (untyped) constant.
-type Ctype int8
+type Ctype uint8
 
 const (
        CTxxx Ctype = iota
@@ -297,7 +297,7 @@ func convlit1(n *Node, t *types.Type, explicit bool, reuse canReuseNode) *Node {
 
        ct := consttype(n)
        var et types.EType
-       if ct < 0 {
+       if ct == 0 {
                goto bad
        }
 
@@ -591,7 +591,7 @@ func tostr(v Val) Val {
 
 func consttype(n *Node) Ctype {
        if n == nil || n.Op != OLITERAL {
-               return -1
+               return 0
        }
        return n.Val().Ctype()
 }
@@ -693,7 +693,7 @@ func evconst(n *Node) {
        if nl == nil || nl.Type == nil {
                return
        }
-       if consttype(nl) < 0 {
+       if consttype(nl) == 0 {
                return
        }
        wl := nl.Type.Etype
@@ -840,7 +840,7 @@ func evconst(n *Node) {
        if nr.Type == nil {
                return
        }
-       if consttype(nr) < 0 {
+       if consttype(nr) == 0 {
                return
        }
        wr = nr.Type.Etype
index c555a5eabcd9aa470114ca655fbb7136b28e420d..fdee8b89a55a424dd9834d944b66e1fc75a292de 100644 (file)
@@ -187,7 +187,7 @@ func isaddrokay(n *Node) bool {
 // The result of orderaddrtemp MUST be assigned back to n, e.g.
 //     n.Left = orderaddrtemp(n.Left, order)
 func orderaddrtemp(n *Node, order *Order) *Node {
-       if consttype(n) >= 0 {
+       if consttype(n) > 0 {
                // TODO: expand this to all static composite literal nodes?
                n = defaultlit(n, nil)
                dowidth(n.Type)
index b97e68f60c67ea179a8fa68e48147b30bf04da36..31d018b15288e2e34792a9cc2e486e6da3bb525f 100644 (file)
@@ -257,7 +257,7 @@ func (s *exprSwitch) walk(sw *Node) {
        var cas []*Node
        if s.kind == switchKindTrue || s.kind == switchKindFalse {
                s.exprname = nodbool(s.kind == switchKindTrue)
-       } else if consttype(cond) >= 0 {
+       } else if consttype(cond) > 0 {
                // leave constants to enable dead code elimination (issue 9608)
                s.exprname = cond
        } else {
@@ -607,12 +607,7 @@ func checkDupExprCases(exprname *Node, clauses []*Node) {
                                //       case GOARCH == "arm" && GOARM == "5":
                                //       case GOARCH == "arm":
                                //     which would both evaluate to false for non-ARM compiles.
-                               if ct := consttype(n); ct < 0 || ct == CTBOOL {
-                                       continue
-                               }
-                               // If the value has no type, we have
-                               // already printed an error about it.
-                               if n.Type == nil {
+                               if ct := consttype(n); ct == 0 || ct == CTBOOL {
                                        continue
                                }
 
@@ -637,12 +632,7 @@ func checkDupExprCases(exprname *Node, clauses []*Node) {
        seen := make(map[typeVal]*Node)
        for _, ncase := range clauses {
                for _, n := range ncase.List.Slice() {
-                       if ct := consttype(n); ct < 0 || ct == CTBOOL {
-                               continue
-                       }
-                       // If the value has no type, we have
-                       // already printed an error about it.
-                       if n.Type == nil {
+                       if ct := consttype(n); ct == 0 || ct == CTBOOL {
                                continue
                        }
                        tv := typeVal{