]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: a dot expression can not be a struct literal key
authorIan Lance Taylor <iant@golang.org>
Sun, 17 Apr 2016 22:33:07 +0000 (15:33 -0700)
committerIan Lance Taylor <iant@golang.org>
Mon, 18 Apr 2016 15:09:47 +0000 (15:09 +0000)
Passes toolstash -cmp.

Fixes #15311.

Change-Id: I1d67f5c9de38e899ab2d6c8986fabd6f197df23a
Reviewed-on: https://go-review.googlesource.com/22162
Reviewed-by: David Crawshaw <crawshaw@golang.org>
src/cmd/compile/internal/gc/typecheck.go
test/fixedbugs/issue15311.go [new file with mode: 0644]

index 6067677738f6787ab2c633538f7ab96ab24cbba8..328737ee140c1d8da13d0243150dee78d94570bf 100644 (file)
@@ -3099,7 +3099,12 @@ func typecheckcomplit(n *Node) *Node {
                                }
 
                                s := l.Left.Sym
-                               if s == nil {
+
+                               // An OXDOT uses the Sym field to hold
+                               // the field to the right of the dot,
+                               // so s will be non-nil, but an OXDOT
+                               // is never a valid struct literal key.
+                               if s == nil || l.Left.Op == OXDOT {
                                        Yyerror("invalid field name %v in struct initializer", l.Left)
                                        l.Right = typecheck(l.Right, Erv)
                                        continue
diff --git a/test/fixedbugs/issue15311.go b/test/fixedbugs/issue15311.go
new file mode 100644 (file)
index 0000000..81fa541
--- /dev/null
@@ -0,0 +1,20 @@
+// errorcheck
+
+// Copyright 2016 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.
+
+// The compiler was failing to correctly report an error when a dot
+// expression was used a struct literal key.
+
+package p
+
+type T struct {
+        toInt    map[string]int
+        toString map[int]string
+}
+
+var t = T{
+        foo.toInt:    make(map[string]int), // ERROR "field name"
+        bar.toString: make(map[int]string), // ERROR "field name"
+}