]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix false positives in isGoConst
authorMatthew Dempsky <mdempsky@google.com>
Thu, 28 Feb 2019 01:12:23 +0000 (17:12 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 28 Feb 2019 20:31:53 +0000 (20:31 +0000)
isGoConst could spuriously return true for variables that shadow a
constant declaration with the same name.

Because even named constants are always represented by OLITERAL nodes,
the easy fix is to just ignore ONAME nodes in isGoConst. We can
similarly ignore ONONAME nodes.

Confirmed that k8s.io/kubernetes/test/e2e/storage builds again with
this fix.

Fixes #30430.

Change-Id: I899400d749982d341dc248a7cd5a18277c2795ec
Reviewed-on: https://go-review.googlesource.com/c/164319
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/compile/internal/gc/const.go
test/fixedbugs/issue30430.go [new file with mode: 0644]

index de7df645e6ca15ad23a7759602315884db4d34b0..0e6d838eaa4fb2e1f165628da70088459a234350 100644 (file)
@@ -1280,8 +1280,6 @@ func indexconst(n *Node) int64 {
 //
 // Expressions derived from nil, like string([]byte(nil)), while they
 // may be known at compile time, are not Go language constants.
-// Only called for expressions known to evaluate to compile-time
-// constants.
 func (n *Node) isGoConst() bool {
        if n.Orig != nil {
                n = n.Orig
@@ -1359,17 +1357,6 @@ func (n *Node) isGoConst() bool {
                        return true
                }
 
-       case ONAME:
-               l := asNode(n.Sym.Def)
-               if l != nil && l.Op == OLITERAL && n.Val().Ctype() != CTNIL {
-                       return true
-               }
-
-       case ONONAME:
-               if asNode(n.Sym.Def) != nil && asNode(n.Sym.Def).Op == OIOTA {
-                       return true
-               }
-
        case OALIGNOF, OOFFSETOF, OSIZEOF:
                return true
        }
diff --git a/test/fixedbugs/issue30430.go b/test/fixedbugs/issue30430.go
new file mode 100644 (file)
index 0000000..6c27b82
--- /dev/null
@@ -0,0 +1,17 @@
+// compile
+
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Issue 30430: isGoConst returned true for non-const variables,
+// resulting in ICE.
+
+package p
+
+func f() {
+       var s string
+       _ = map[string]string{s: ""}
+}
+
+const s = ""