go/types: don't return an array type with invalid length

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>
This commit is contained in:
Robert Findley 2021-11-09 14:56:39 -05:00
parent 4aa0746f6a
commit cb908f1d4d
2 changed files with 8 additions and 6 deletions

View 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)`},

View 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