diff options
author | Keith Randall <khr@google.com> | 2018-11-26 15:58:03 -0800 |
---|---|---|
committer | Keith Randall <khr@golang.org> | 2018-11-27 01:21:41 +0000 |
commit | eb6c433eb38d9a0e9ecfcc1604f9ff8e035768f6 (patch) | |
tree | 97e1a0e57abe40a2afecdc27ab1e0d89058b523d /src/cmd/compile | |
parent | 3f9efe750058308bc499c5eb22bc84193fedb6b3 (diff) | |
download | go-git-eb6c433eb38d9a0e9ecfcc1604f9ff8e035768f6.tar.gz |
cmd/compile: don't convert non-Go-constants to OLITERALs
Don't convert values that aren't Go constants, like
uintptr(unsafe.Pointer(nil)), to a literal constant. This avoids
assuming they are constants for things like indexing, array sizes,
case duplication, etc.
Also, nil is an allowed duplicate in switches. CTNILs aren't Go constants.
Fixes #28078
Fixes #28079
Change-Id: I9ab8af47098651ea09ef10481787eae2ae2fb445
Reviewed-on: https://go-review.googlesource.com/c/151320
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/cmd/compile')
-rw-r--r-- | src/cmd/compile/internal/gc/const.go | 2 | ||||
-rw-r--r-- | src/cmd/compile/internal/gc/swt.go | 2 | ||||
-rw-r--r-- | src/cmd/compile/internal/gc/typecheck.go | 6 |
3 files changed, 5 insertions, 5 deletions
diff --git a/src/cmd/compile/internal/gc/const.go b/src/cmd/compile/internal/gc/const.go index afcdb95443..c01820506d 100644 --- a/src/cmd/compile/internal/gc/const.go +++ b/src/cmd/compile/internal/gc/const.go @@ -1327,7 +1327,7 @@ func (n *Node) isGoConst() bool { } return true - case OCONV: + case OCONV, OCONVNOP: if okforconst[n.Type.Etype] && n.Left.isGoConst() { return true } diff --git a/src/cmd/compile/internal/gc/swt.go b/src/cmd/compile/internal/gc/swt.go index b475e7adc3..a985626a02 100644 --- a/src/cmd/compile/internal/gc/swt.go +++ b/src/cmd/compile/internal/gc/swt.go @@ -679,7 +679,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 { + if ct := consttype(n); ct == 0 || ct == CTBOOL || ct == CTNIL { continue } tv := typeVal{ diff --git a/src/cmd/compile/internal/gc/typecheck.go b/src/cmd/compile/internal/gc/typecheck.go index 069a38cbbb..cbca685415 100644 --- a/src/cmd/compile/internal/gc/typecheck.go +++ b/src/cmd/compile/internal/gc/typecheck.go @@ -1818,9 +1818,9 @@ func typecheck1(n *Node, top int) (res *Node) { switch n.Op { case OCONVNOP: - if n.Left.Op == OLITERAL { - n.Op = OCONV - setconst(n, n.Left.Val()) + if n.Left.Op == OLITERAL && n.isGoConst() { + n.Op = OCONV // set so n.Orig gets OCONV instead of OCONVNOP + setconst(n, n.Left.Val()) // convert n to OLITERAL with the given value } else if t.Etype == n.Type.Etype { switch t.Etype { case TFLOAT32, TFLOAT64, TCOMPLEX64, TCOMPLEX128: |