[dev.go2go] go/types: don't crash instantiating arrays/slices that are not fully set up

Addresses crash #18 of #39634.

Updates #39634.

Change-Id: I4b1a3d81ce9dc2b59ae9af3146d26f79d8c05973
Reviewed-on: https://go-review.googlesource.com/c/go/+/240904
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Robert Griesemer 2020-07-03 22:09:57 -07:00
parent 0e1fc80d04
commit 005397e36d
2 changed files with 16 additions and 2 deletions

View File

@ -35,6 +35,9 @@ type Z17 interface {
}
func F17(type T Z17)(T)
// crash 18
type o18(type T) []func(_ o18([]_ /* ERROR cannot use _ */ ))
// crash 19
type Z19 [][[]Z19{}[0][0]]c19 /* ERROR undeclared */

View File

@ -240,19 +240,20 @@ type subster struct {
func (subst *subster) typ(typ Type) Type {
switch t := typ.(type) {
case nil:
// Call typOrNil if it's possible that typ is nil.
panic("nil typ")
case *Basic, *bottom, *top:
// nothing to do
case *Array:
elem := subst.typ(t.elem)
elem := subst.typOrNil(t.elem)
if elem != t.elem {
return &Array{len: t.len, elem: elem}
}
case *Slice:
elem := subst.typ(t.elem)
elem := subst.typOrNil(t.elem)
if elem != t.elem {
return &Slice{elem: elem}
}
@ -448,6 +449,16 @@ func typeListString(list []Type) string {
return buf.String()
}
// typOrNil is like typ but if the argument is nil it is replaced with Typ[Invalid].
// A nil type may appear in pathological cases such as type T(type P) []func(_ T([]_))
// where an array/slice element is accessed before it is set up.
func (subst *subster) typOrNil(typ Type) Type {
if typ == nil {
return Typ[Invalid]
}
return subst.typ(typ)
}
func (subst *subster) var_(v *Var) *Var {
if v != nil {
if typ := subst.typ(v.typ); typ != v.typ {