diff options
author | Robert Griesemer <gri@golang.org> | 2020-11-16 22:23:48 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2020-11-17 22:57:34 +0000 |
commit | 041a4e4c34e21d769de35c54a86b32cdb0475f65 (patch) | |
tree | d0c4cce22cb4891330273c09b6de051f6d9b761e /src/go | |
parent | 05082c90d5b35935ccc27acb070e00702df91a3a (diff) | |
download | go-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')
-rw-r--r-- | src/go/types/expr.go | 4 | ||||
-rw-r--r-- | src/go/types/testdata/expr3.src | 1 |
2 files changed, 2 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 diff --git a/src/go/types/testdata/expr3.src b/src/go/types/testdata/expr3.src index 6f2201c365..e6777aad2b 100644 --- a/src/go/types/testdata/expr3.src +++ b/src/go/types/testdata/expr3.src @@ -102,6 +102,7 @@ func indexes() { var ok mybool _, ok = m["bar"] _ = ok + _ = m[0 /* ERROR "cannot use 0" */ ] + "foo" // ERROR "cannot convert" var t string _ = t[- /* ERROR "negative" */ 1] |