From: Daniel Martí Date: Tue, 10 Oct 2017 16:36:03 +0000 (+0100) Subject: cmd/compile: make bad Ctypes be only 0 X-Git-Tag: go1.10beta1~806 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f14f7b31416ee655dc588eb3a1772a7a4be2a61c;p=gostls13.git cmd/compile: make bad Ctypes be only 0 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í Reviewed-by: Matthew Dempsky TryBot-Result: Gobot Gobot --- diff --git a/src/cmd/compile/internal/gc/const.go b/src/cmd/compile/internal/gc/const.go index 4d52d091b0..1d67e5fd10 100644 --- a/src/cmd/compile/internal/gc/const.go +++ b/src/cmd/compile/internal/gc/const.go @@ -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 diff --git a/src/cmd/compile/internal/gc/order.go b/src/cmd/compile/internal/gc/order.go index c555a5eabc..fdee8b89a5 100644 --- a/src/cmd/compile/internal/gc/order.go +++ b/src/cmd/compile/internal/gc/order.go @@ -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) diff --git a/src/cmd/compile/internal/gc/swt.go b/src/cmd/compile/internal/gc/swt.go index b97e68f60c..31d018b152 100644 --- a/src/cmd/compile/internal/gc/swt.go +++ b/src/cmd/compile/internal/gc/swt.go @@ -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{