]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: implement len(array) / cap(array) rule
authorRuss Cox <rsc@golang.org>
Thu, 8 Mar 2012 03:43:28 +0000 (22:43 -0500)
committerRuss Cox <rsc@golang.org>
Thu, 8 Mar 2012 03:43:28 +0000 (22:43 -0500)
The spec is looser than the current implementation.
The spec edit was made in CL 4444050 (May 2011)
but I never implemented it.

Fixes #3244.

R=ken2
CC=golang-dev
https://golang.org/cl/5785049

src/cmd/gc/typecheck.c
test/const4.go [new file with mode: 0644]
test/const5.go [new file with mode: 0644]

index 6dc3fd187a059a143c9b1aef80a050522df630b4..e98d538572484608879db7a2db03896cf806fc52 100644 (file)
@@ -190,6 +190,46 @@ typecheck(Node **np, int top)
        return n;
 }
 
+/*
+ * does n contain a call or receive operation?
+ */
+static int callrecvlist(NodeList*);
+
+static int
+callrecv(Node *n)
+{
+       if(n == nil)
+               return 0;
+       
+       switch(n->op) {
+       case OCALL:
+       case OCALLMETH:
+       case OCALLINTER:
+       case OCALLFUNC:
+       case ORECV:
+               return 1;
+       }
+
+       return callrecv(n->left) ||
+               callrecv(n->right) ||
+               callrecv(n->ntest) ||
+               callrecv(n->nincr) ||
+               callrecvlist(n->ninit) ||
+               callrecvlist(n->nbody) ||
+               callrecvlist(n->nelse) ||
+               callrecvlist(n->list) ||
+               callrecvlist(n->rlist);
+}
+
+static int
+callrecvlist(NodeList *l)
+{
+       for(; l; l=l->next)
+               if(callrecv(l->n))
+                       return 1;
+       return 0;
+}
+
 static void
 typecheck1(Node **np, int top)
 {
@@ -995,12 +1035,14 @@ reswitch:
                        }
                        break;
                case TARRAY:
-                       if(t->bound >= 0 && l->op == ONAME) {
-                               r = nod(OXXX, N, N);
-                               nodconst(r, types[TINT], t->bound);
-                               r->orig = n;
-                               n = r;
-                       }
+                       if(t->bound < 0) // slice
+                               break;
+                       if(callrecv(l)) // has call or receive
+                               break;
+                       r = nod(OXXX, N, N);
+                       nodconst(r, types[TINT], t->bound);
+                       r->orig = n;
+                       n = r;
                        break;
                }
                n->type = types[TINT];
diff --git a/test/const4.go b/test/const4.go
new file mode 100644 (file)
index 0000000..677fcef
--- /dev/null
@@ -0,0 +1,77 @@
+// run
+
+// Copyright 2011 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.
+
+// Test len constants and non-constants, http://golang.org/issue/3244.
+
+package main
+
+var b struct {
+       a[10]int
+}
+
+var m map[string][20]int
+
+var s [][30]int
+
+const (
+       n1 = len(b.a)
+       n2 = len(m[""])
+       n3 = len(s[10])
+)
+
+// Non-constants (see also const5.go).
+var (
+       n4 = len(f())
+       n5 = len(<-c)
+       n6 = cap(g())
+       n7 = cap(<-c1)
+)
+
+var calledF = false
+
+func f() *[40]int {
+       calledF = true
+       return nil
+}
+
+var c = func() chan *[50]int {
+       c := make(chan *[50]int, 2)
+       c <- nil
+       c <- new([50]int)
+       return c
+}()
+
+var calledG = false
+
+func g() *[60]int {
+       calledG = true
+       return nil
+}
+
+var c1 = func() chan *[70]int {
+       c := make(chan *[70]int, 2)
+       c <- nil
+       c <- new([70]int)
+       return c
+}()
+
+func main() {
+       if n1 != 10 || n2 != 20 || n3 != 30 || n4 != 40 || n5 != 50 || n6 != 60 || n7 != 70 {
+               println("BUG:", n1, n2, n3, n4, n5, n6, n7)
+       }
+       if !calledF {
+               println("BUG: did not call f")
+       }
+       if <-c == nil {
+               println("BUG: did not receive from c")
+       }
+       if !calledG {
+               println("BUG: did not call g")
+       }
+       if <-c1 == nil {
+               println("BUG: did not receive from c1")
+       }
+}
diff --git a/test/const5.go b/test/const5.go
new file mode 100644 (file)
index 0000000..8e0385e
--- /dev/null
@@ -0,0 +1,33 @@
+// errorcheck
+
+// Copyright 2011 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.
+
+// Test that len non-constants are not constants, http://golang.org/issue/3244.
+
+package p
+
+var b struct {
+       a[10]int
+}
+
+var m map[string][20]int
+
+var s [][30]int
+
+func f() *[40]int
+var c chan *[50]int
+
+const (
+       n1 = len(b.a)
+       n2 = len(m[""])
+       n3 = len(s[10])
+
+       n4 = len(f())  // ERROR "must be constant"
+       n5 = len(<-c) // ERROR "must be constant"
+
+       n6 = cap(f())  // ERROR "must be constant"
+       n7 = cap(<-c) // ERROR "must be constant"
+)
+