]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: don't return an array type with invalid length
authorRobert Findley <rfindley@google.com>
Tue, 9 Nov 2021 19:56:39 +0000 (14:56 -0500)
committerRobert Findley <rfindley@google.com>
Tue, 9 Nov 2021 22:14:19 +0000 (22:14 +0000)
In preparation for porting CL 361412, fix a discrepancy in go/types,
where [-1]T is returned for an array type with invalid length.

Change-Id: Ia32f5b66c9c561ccf0c32af1922fc4690c66dbc3
Reviewed-on: https://go-review.googlesource.com/c/go/+/362738
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/types/api_test.go
src/go/types/typexpr.go

index 807bffbff6569256a29b806a6813c0bcaa9fb5c0..3e10be59852898a16b30967569bc61ea980cee2c 100644 (file)
@@ -342,7 +342,7 @@ func TestTypesInfo(t *testing.T) {
                {broken + `x2; func _() { var a, b string; type x struct {f string}; z := &x{f: a; f: b;}}`, `b`, `string`},
                {broken + `x3; var x = panic("");`, `panic`, `func(interface{})`},
                {`package x4; func _() { panic("") }`, `panic`, `func(interface{})`},
-               {broken + `x5; func _() { var x map[string][...]int; x = map[string][...]int{"": {1,2,3}} }`, `x`, `map[string][-1]int`},
+               {broken + `x5; func _() { var x map[string][...]int; x = map[string][...]int{"": {1,2,3}} }`, `x`, `map[string]invalid type`},
 
                // parameterized functions
                {genericPkg + `p0; func f[T any](T) {}; var _ = f[int]`, `f`, `func[T interface{}](T)`},
index e1d942a5c6f2562554f9b2be6d1e5b8d23c4d73f..cc2bd622098ea398626bab3602367a4c7aa018e8 100644 (file)
@@ -271,18 +271,20 @@ func (check *Checker) typInternal(e0 ast.Expr, def *Named) (T Type) {
                return check.definedType(e.X, def)
 
        case *ast.ArrayType:
-               if e.Len != nil {
-                       typ := new(Array)
+               if e.Len == nil {
+                       typ := new(Slice)
                        def.setUnderlying(typ)
-                       typ.len = check.arrayLength(e.Len)
                        typ.elem = check.varType(e.Elt)
                        return typ
                }
 
-               typ := new(Slice)
+               typ := new(Array)
                def.setUnderlying(typ)
+               typ.len = check.arrayLength(e.Len)
                typ.elem = check.varType(e.Elt)
-               return typ
+               if typ.len >= 0 {
+                       return typ
+               }
 
        case *ast.Ellipsis:
                // dots are handled explicitly where they are legal