]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: suppress errors after "cannot assign to X"
authorMatthew Dempsky <mdempsky@google.com>
Tue, 27 Jun 2017 19:58:32 +0000 (12:58 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Tue, 27 Jun 2017 20:29:33 +0000 (20:29 +0000)
If the LHS is unassignable, there's no point in trying to make sure
the RHS can be assigned to it or making sure they're realizable
types. This is consistent with go/types.

In particular, this prevents "1 = 2" from causing a panic when "1"
still ends up with the type "untyped int", which is not realizable.

Fixes #20813.

Change-Id: I4710bdaac2e375ef12ec29b888b8ac84fb640e56
Reviewed-on: https://go-review.googlesource.com/46835
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/typecheck.go
test/fixedbugs/issue20813.go [new file with mode: 0644]

index bff278b7aedeff5af51def52e2573776b25740a6..795bdcdd358e300e00cb254ac938e418d012544c 100644 (file)
@@ -3288,10 +3288,10 @@ func checkassign(stmt *Node, n *Node) {
 
        if n.Op == ODOT && n.Left.Op == OINDEXMAP {
                yyerror("cannot assign to struct field %v in map", n)
-               return
+       } else {
+               yyerror("cannot assign to %v", n)
        }
-
-       yyerror("cannot assign to %v", n)
+       n.Type = nil
 }
 
 func checkassignlist(stmt *Node, l Nodes) {
diff --git a/test/fixedbugs/issue20813.go b/test/fixedbugs/issue20813.go
new file mode 100644 (file)
index 0000000..b931aea
--- /dev/null
@@ -0,0 +1,11 @@
+// errorcheck
+
+// Copyright 2017 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 p
+
+func f() {
+       1 = 2 // ERROR "cannot assign to 1"
+}