aboutsummaryrefslogtreecommitdiff
path: root/src/go/types/expr.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2020-11-16 22:23:48 -0800
committerRobert Griesemer <gri@golang.org>2020-11-17 22:57:34 +0000
commit041a4e4c34e21d769de35c54a86b32cdb0475f65 (patch)
treed0c4cce22cb4891330273c09b6de051f6d9b761e /src/go/types/expr.go
parent05082c90d5b35935ccc27acb070e00702df91a3a (diff)
downloadgo-041a4e4c34e21d769de35c54a86b32cdb0475f65.tar.gz
go-041a4e4c34e21d769de35c54a86b32cdb0475f65.zip
go/types: add test case for incorrect map index expression
The existing code for map index expressions checked the wrong variable (x rather than key) to see if the index assignment was correct. Since x.mode was always valid in that case, type-checking didn't follow the error exit in case of an incorrect map index expression. However, since we know the correct map element type irrespective of the validity of the map key, the existing code path is preferrable over exiting early via an error because the map index expression returns a valid type which then can be used for further type-checking. Removed the unneeded 'if' statement and added a test case producing the expected two errors (rather than only one if we would "correct" the 'if' statement instead). In summary, this commit adds a test but doesn't change the behavior of type-checking of map index expressions. Change-Id: I67845bfaa03600c9400f9a1462d7a68a66921ad4 Reviewed-on: https://go-review.googlesource.com/c/go/+/270658 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'src/go/types/expr.go')
-rw-r--r--src/go/types/expr.go4
1 files changed, 1 insertions, 3 deletions
diff --git a/src/go/types/expr.go b/src/go/types/expr.go
index 1f8b946407..11f9411284 100644
--- a/src/go/types/expr.go
+++ b/src/go/types/expr.go
@@ -1357,9 +1357,7 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
var key operand
check.expr(&key, e.Index)
check.assignment(&key, typ.key, "map index")
- if x.mode == invalid {
- goto Error
- }
+ // ok to continue even if indexing failed - map element type is known
x.mode = mapindex
x.typ = typ.elem
x.expr = e