1
0
mirror of https://github.com/golang/go synced 2024-09-29 13:34:30 -06:00

go/types: panic if named type instances are mutated

Change-Id: Idc4d561c7037f33aa9c844b411c38c6cb5bbfbcd
Reviewed-on: https://go-review.googlesource.com/c/go/+/380374
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Robert Findley 2022-01-22 12:24:41 -05:00
parent 97e740e8b0
commit 48ec6df16c
2 changed files with 18 additions and 2 deletions

View File

@ -88,7 +88,11 @@ func (t *Named) Origin() *Named { return t.orig }
func (t *Named) TypeParams() *TypeParamList { return t.resolve(nil).tparams }
// SetTypeParams sets the type parameters of the named type t.
func (t *Named) SetTypeParams(tparams []*TypeParam) { t.resolve(nil).tparams = bindTParams(tparams) }
// t must not have type arguments.
func (t *Named) SetTypeParams(tparams []*TypeParam) {
assert(t.targs.Len() == 0)
t.resolve(nil).tparams = bindTParams(tparams)
}
// TypeArgs returns the type arguments used to instantiate the named type t.
func (t *Named) TypeArgs() *TypeList { return t.targs }
@ -100,7 +104,9 @@ func (t *Named) NumMethods() int { return len(t.resolve(nil).methods) }
func (t *Named) Method(i int) *Func { return t.resolve(nil).methods[i] }
// SetUnderlying sets the underlying type and marks t as complete.
// t must not have type arguments.
func (t *Named) SetUnderlying(underlying Type) {
assert(t.targs.Len() == 0)
if underlying == nil {
panic("underlying type must not be nil")
}
@ -111,7 +117,9 @@ func (t *Named) SetUnderlying(underlying Type) {
}
// AddMethod adds method m unless it is already in the method list.
// t must not have type arguments.
func (t *Named) AddMethod(m *Func) {
assert(t.targs.Len() == 0)
t.resolve(nil)
if i, _ := lookupMethod(t.methods, m.pkg, m.name); i < 0 {
t.methods = append(t.methods, m)

View File

@ -90,7 +90,11 @@ func (t *Named) Origin() *Named { return t.orig }
func (t *Named) TypeParams() *TypeParamList { return t.resolve(nil).tparams }
// SetTypeParams sets the type parameters of the named type t.
func (t *Named) SetTypeParams(tparams []*TypeParam) { t.resolve(nil).tparams = bindTParams(tparams) }
// t must not have type arguments.
func (t *Named) SetTypeParams(tparams []*TypeParam) {
assert(t.targs.Len() == 0)
t.resolve(nil).tparams = bindTParams(tparams)
}
// TypeArgs returns the type arguments used to instantiate the named type t.
func (t *Named) TypeArgs() *TypeList { return t.targs }
@ -102,7 +106,9 @@ func (t *Named) NumMethods() int { return len(t.resolve(nil).methods) }
func (t *Named) Method(i int) *Func { return t.resolve(nil).methods[i] }
// SetUnderlying sets the underlying type and marks t as complete.
// t must not have type arguments.
func (t *Named) SetUnderlying(underlying Type) {
assert(t.targs.Len() == 0)
if underlying == nil {
panic("underlying type must not be nil")
}
@ -113,7 +119,9 @@ func (t *Named) SetUnderlying(underlying Type) {
}
// AddMethod adds method m unless it is already in the method list.
// t must not have type arguments.
func (t *Named) AddMethod(m *Func) {
assert(t.targs.Len() == 0)
t.resolve(nil)
if i, _ := lookupMethod(t.methods, m.pkg, m.name); i < 0 {
t.methods = append(t.methods, m)