From 48ec6df16c285cda50bd38970b82402e8c46919b Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Sat, 22 Jan 2022 12:24:41 -0500 Subject: [PATCH] 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 Run-TryBot: Robert Findley Reviewed-by: Robert Griesemer TryBot-Result: Gopher Robot --- src/cmd/compile/internal/types2/named.go | 10 +++++++++- src/go/types/named.go | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/cmd/compile/internal/types2/named.go b/src/cmd/compile/internal/types2/named.go index 51ea27a6db..c4217fa508 100644 --- a/src/cmd/compile/internal/types2/named.go +++ b/src/cmd/compile/internal/types2/named.go @@ -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) diff --git a/src/go/types/named.go b/src/go/types/named.go index 82a053dd0d..a44686bc36 100644 --- a/src/go/types/named.go +++ b/src/go/types/named.go @@ -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) -- 2.50.0