aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Findley <rfindley@google.com>2022-01-22 12:24:41 -0500
committerRobert Findley <rfindley@google.com>2022-01-24 19:02:43 +0000
commit48ec6df16c285cda50bd38970b82402e8c46919b (patch)
tree9e106b5a9f45e69d3dbd124f884b54a7d8eb1453
parent97e740e8b0ff1b32b164b0cbef06c12c4d591f3f (diff)
downloadgo-48ec6df16c285cda50bd38970b82402e8c46919b.tar.gz
go-48ec6df16c285cda50bd38970b82402e8c46919b.zip
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>
-rw-r--r--src/cmd/compile/internal/types2/named.go10
-rw-r--r--src/go/types/named.go10
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)