mirror of
https://github.com/golang/go
synced 2024-11-24 15:20:03 -07:00
cmd/compile: simplify keydup
Use a type switch instead of calling Val.Ctype (which in turn just uses a type switch anyway). Use continue statements to simplify the control flow. Change-Id: I65c139d706d4d78e5b4ce09d1b1505a3e424496b Reviewed-on: https://go-review.googlesource.com/21173 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
c61a55d831
commit
d53287d0c3
@ -12,7 +12,6 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
UINF = 100
|
UINF = 100
|
||||||
PRIME1 = 3
|
|
||||||
BADWIDTH = -1000000000
|
BADWIDTH = -1000000000
|
||||||
MaxStackVarSize = 10 * 1024 * 1024
|
MaxStackVarSize = 10 * 1024 * 1024
|
||||||
)
|
)
|
||||||
|
@ -2782,27 +2782,26 @@ func keydup(n *Node, hash map[uint32][]*Node) {
|
|||||||
return // we don't check variables
|
return // we don't check variables
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const PRIME1 = 3
|
||||||
|
|
||||||
var h uint32
|
var h uint32
|
||||||
switch n.Val().Ctype() {
|
switch v := n.Val().U.(type) {
|
||||||
default: // unknown, bool, nil
|
default: // unknown, bool, nil
|
||||||
h = 23
|
h = 23
|
||||||
|
|
||||||
case CTINT, CTRUNE:
|
case *Mpint:
|
||||||
h = uint32(n.Val().U.(*Mpint).Int64())
|
h = uint32(v.Int64())
|
||||||
|
|
||||||
case CTFLT:
|
case *Mpflt:
|
||||||
d := n.Val().U.(*Mpflt).Float64()
|
x := math.Float64bits(v.Float64())
|
||||||
x := math.Float64bits(d)
|
|
||||||
for i := 0; i < 8; i++ {
|
for i := 0; i < 8; i++ {
|
||||||
h = h*PRIME1 + uint32(x&0xFF)
|
h = h*PRIME1 + uint32(x&0xFF)
|
||||||
x >>= 8
|
x >>= 8
|
||||||
}
|
}
|
||||||
|
|
||||||
case CTSTR:
|
case string:
|
||||||
h = 0
|
for i := 0; i < len(v); i++ {
|
||||||
s := n.Val().U.(string)
|
h = h*PRIME1 + uint32(v[i])
|
||||||
for i := 0; i < len(s); i++ {
|
|
||||||
h = h*PRIME1 + uint32(s[i])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2810,25 +2809,19 @@ func keydup(n *Node, hash map[uint32][]*Node) {
|
|||||||
for _, a := range hash[h] {
|
for _, a := range hash[h] {
|
||||||
cmp.Op = OEQ
|
cmp.Op = OEQ
|
||||||
cmp.Left = n
|
cmp.Left = n
|
||||||
b := false
|
|
||||||
if a.Op == OCONVIFACE && orign.Op == OCONVIFACE {
|
if a.Op == OCONVIFACE && orign.Op == OCONVIFACE {
|
||||||
if Eqtype(a.Left.Type, n.Type) {
|
a = a.Left
|
||||||
cmp.Right = a.Left
|
|
||||||
evconst(&cmp)
|
|
||||||
if cmp.Op == OLITERAL {
|
|
||||||
// Sometimes evconst fails. See issue 12536.
|
|
||||||
b = cmp.Val().U.(bool)
|
|
||||||
}
|
}
|
||||||
|
if !Eqtype(a.Type, n.Type) {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
} else if Eqtype(a.Type, n.Type) {
|
|
||||||
cmp.Right = a
|
cmp.Right = a
|
||||||
evconst(&cmp)
|
evconst(&cmp)
|
||||||
if cmp.Op == OLITERAL {
|
if cmp.Op != OLITERAL {
|
||||||
b = cmp.Val().U.(bool)
|
// Sometimes evconst fails. See issue 12536.
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
if cmp.Val().U.(bool) {
|
||||||
|
|
||||||
if b {
|
|
||||||
Yyerror("duplicate key %v in map literal", n)
|
Yyerror("duplicate key %v in map literal", n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user