go/types: guard against check==nil in newNamed

When importing generic named types, it is possible for Checker.newNamed
to be called during type instantiation when the Checker is nil.

In this case we should be able to safely skip this delayed expansion.

Updates #45580

Change-Id: I75422100464d57eba24642c93e06e8b47d904fc3
Reviewed-on: https://go-review.googlesource.com/c/go/+/322974
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:
Rob Findley 2021-05-27 12:35:01 -04:00 committed by Robert Findley
parent fca7b8f3e6
commit 6b8c94b6c5

View file

@ -661,11 +661,7 @@ func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
if _, ok := underlying.(*Named); ok {
panic("types.NewNamed: underlying type must not be *Named")
}
typ := &Named{obj: obj, orig: underlying, underlying: underlying, methods: methods}
if obj.typ == nil {
obj.typ = typ
}
return typ
return (*Checker)(nil).newNamed(obj, underlying, methods)
}
func (check *Checker) newNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
@ -681,13 +677,15 @@ func (check *Checker) newNamed(obj *TypeName, underlying Type, methods []*Func)
//
// TODO(rFindley): clean this up so that under is the only function mutating
// named types.
check.later(func() {
switch typ.under().(type) {
case *Named, *instance:
panic("internal error: unexpanded underlying type")
}
typ.check = nil
})
if check != nil {
check.later(func() {
switch typ.under().(type) {
case *Named, *instance:
panic("internal error: unexpanded underlying type")
}
typ.check = nil
})
}
return typ
}