]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: fix internal comments and add additional test case
authorRobert Griesemer <gri@golang.org>
Wed, 5 Sep 2018 00:18:22 +0000 (17:18 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 5 Sep 2018 15:47:42 +0000 (15:47 +0000)
https://go-review.googlesource.com/c/go/+/132355 addressed
a crash and inadvertently fixed #27346; however the comment
added to the type-checker was incorrect and misleading.

This CL fixes the comment, and adds a test case for #27346.

Fixes #27346.
Updates #22467.

Change-Id: Ib6d5caedf302fd42929c4dacc55e973c1aebfe85
Reviewed-on: https://go-review.googlesource.com/133415
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
src/go/types/expr.go
src/go/types/testdata/issues.src

index f0acc7845d89c9b07b3a0690b4aad634a7858dcb..c65c9e7681dfe6d92762d3667275377367b02cdc 100644 (file)
@@ -1156,15 +1156,20 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
                                goto Error
                        }
                        n := check.indexedElts(e.Elts, utyp.elem, utyp.len)
-                       // If we have an "open" [...]T array, set the length now that we know it
-                       // and record the type for [...] (usually done by check.typExpr which is
-                       // not called for [...]).
+                       // If we have an array of unknown length (usually [...]T arrays, but also
+                       // arrays [n]T where n is invalid) set the length now that we know it and
+                       // record the type for the array (usually done by check.typ which is not
+                       // called for [...]T). We handle [...]T arrays and arrays with invalid
+                       // length the same here because it makes sense to "guess" the length for
+                       // the latter if we have a composite literal; e.g. for [n]int{1, 2, 3}
+                       // where n is invalid for some reason, it seems fair to assume it should
+                       // be 3 (see also Checked.arrayLength and issue #27346).
                        if utyp.len < 0 {
                                utyp.len = n
-                               // e.Type may be missing in case of errors.
-                               // In "map[string][...]int{"": {1, 2, 3}}},
-                               // an error is reported for the outer literal,
-                               // then [...]int is used as a hint for the inner literal.
+                               // e.Type is missing if we have a composite literal element
+                               // that is itself a composite literal with omitted type. In
+                               // that case there is nothing to record (there is no type in
+                               // the source at that point).
                                if e.Type != nil {
                                        check.recordTypeAndValue(e.Type, typexpr, utyp, nil)
                                }
index d85e04e68cbfae59b72615fa9f80bf66581c3ac1..13f8309c829d7cfcb281867c73e0458ff48f65b0 100644 (file)
@@ -294,3 +294,11 @@ type registry struct {
 type allocator struct {
        _ [int(preloadLimit)]int
 }
+
+// Test that we don't crash when type-checking composite literals
+// containing errors in the type.
+var issue27346 = [][n /* ERROR undeclared */ ]int{
+       0: {},
+}
+
+var issue22467 = map[int][... /* ERROR invalid use of ... */ ]int{0: {}}