]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: allow map index expressions in for range statements
authorDmitry Vyukov <dvyukov@google.com>
Mon, 26 Jan 2015 08:26:55 +0000 (11:26 +0300)
committerDmitry Vyukov <dvyukov@google.com>
Wed, 28 Jan 2015 15:14:04 +0000 (15:14 +0000)
Fixes #9691.

Change-Id: I22bfc82e05497e91a7b18a668913aed6c723365d
Reviewed-on: https://go-review.googlesource.com/3282
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/gc/go.h
src/cmd/gc/range.c
src/cmd/gc/typecheck.c
test/fixedbugs/issue9691.go [new file with mode: 0644]

index 4aa93b25b24ff75d766a854aab5b529fa8355c6e..f3cbd923a067d501474905d1bfd5d74ce0654bec 100644 (file)
@@ -1454,6 +1454,7 @@ void      typechecklist(NodeList *l, int top);
 Node*  typecheckdef(Node *n);
 void   copytype(Node *n, Type *t);
 void   checkreturn(Node*);
+void   checkassign(Node*);
 void   queuemethod(Node *n);
 
 /*
index 14bb1be88c82a6be16d7c69c38780d3bb99c00e7..f5f87b21655bd081f0819eac0227d6b3f7287d9e 100644 (file)
@@ -89,12 +89,14 @@ typecheckrange(Node *n)
                        v1->type = t1;
                else if(v1->type != T && assignop(t1, v1->type, &why) == 0)
                        yyerror("cannot assign type %T to %lN in range%s", t1, v1, why);
+               checkassign(v1);
        }
        if(v2) {
                if(v2->defn == n)
                        v2->type = t2;
                else if(v2->type != T && assignop(t2, v2->type, &why) == 0)
                        yyerror("cannot assign type %T to %lN in range%s", t2, v2, why);
+               checkassign(v2);
        }
 
 out:
index c71cee9b87d7fbb3b53cf57fbf5c1c8d90f27650..ef330c6baf5fa3f393bd9b56b0d2fedaac3931bc 100644 (file)
@@ -27,7 +27,6 @@ static void   typecheckas2(Node*);
 static void    typecheckas(Node*);
 static void    typecheckfunc(Node*);
 static void    checklvalue(Node*, char*);
-static void    checkassign(Node*);
 static void    checkassignlist(NodeList*);
 static void    stringtoarraylit(Node**);
 static Node*   resolve(Node*);
@@ -2811,7 +2810,7 @@ checklvalue(Node *n, char *verb)
                yyerror("cannot %s %N", verb, n);
 }
 
-static void
+void
 checkassign(Node *n)
 {
        if(islvalue(n))
diff --git a/test/fixedbugs/issue9691.go b/test/fixedbugs/issue9691.go
new file mode 100644 (file)
index 0000000..39c3dfa
--- /dev/null
@@ -0,0 +1,21 @@
+// run
+
+// Copyright 2015 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.
+
+package main
+
+func main() {
+       s := "foo"
+       b := []byte(s)
+       m := make(map[string]int)
+       // Test that map index can be used in range
+       // and that slicebytetostringtmp is not used in this context.
+       for m[string(b)] = range s {
+       }
+       b[0] = 'b'
+       if m["foo"] != 2 {
+               panic("bad")
+       }
+}