Skip to content

Commit 005397e

Browse files
committed
[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 <[email protected]>
1 parent 0e1fc80 commit 005397e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/go/types/fixedbugs/issue39634.go2

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ type Z17 interface {
3535
}
3636
func F17(type T Z17)(T)
3737

38+
// crash 18
39+
type o18(type T) []func(_ o18([]_ /* ERROR cannot use _ */ ))
40+
3841
// crash 19
3942
type Z19 [][[]Z19{}[0][0]]c19 /* ERROR undeclared */
4043

src/go/types/subst.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,20 @@ type subster struct {
240240
func (subst *subster) typ(typ Type) Type {
241241
switch t := typ.(type) {
242242
case nil:
243+
// Call typOrNil if it's possible that typ is nil.
243244
panic("nil typ")
244245

245246
case *Basic, *bottom, *top:
246247
// nothing to do
247248

248249
case *Array:
249-
elem := subst.typ(t.elem)
250+
elem := subst.typOrNil(t.elem)
250251
if elem != t.elem {
251252
return &Array{len: t.len, elem: elem}
252253
}
253254

254255
case *Slice:
255-
elem := subst.typ(t.elem)
256+
elem := subst.typOrNil(t.elem)
256257
if elem != t.elem {
257258
return &Slice{elem: elem}
258259
}
@@ -448,6 +449,16 @@ func typeListString(list []Type) string {
448449
return buf.String()
449450
}
450451

452+
// typOrNil is like typ but if the argument is nil it is replaced with Typ[Invalid].
453+
// A nil type may appear in pathological cases such as type T(type P) []func(_ T([]_))
454+
// where an array/slice element is accessed before it is set up.
455+
func (subst *subster) typOrNil(typ Type) Type {
456+
if typ == nil {
457+
return Typ[Invalid]
458+
}
459+
return subst.typ(typ)
460+
}
461+
451462
func (subst *subster) var_(v *Var) *Var {
452463
if v != nil {
453464
if typ := subst.typ(v.typ); typ != v.typ {

0 commit comments

Comments
 (0)