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í <mvdan@mvdan.cc>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Daniel Martí 2017-10-10 17:36:03 +01:00
parent d5a2925b7d
commit f14f7b3141
3 changed files with 9 additions and 19 deletions

View 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

View 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)

View 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{