]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: panic if named type instances are mutated
authorRobert Findley <rfindley@google.com>
Sat, 22 Jan 2022 17:24:41 +0000 (12:24 -0500)
committerRobert Findley <rfindley@google.com>
Mon, 24 Jan 2022 19:02:43 +0000 (19:02 +0000)
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>

src/cmd/compile/internal/types2/named.go
src/go/types/named.go

index 51ea27a6dbd19dab05d799c8315bbb28d33e0102..c4217fa5080eb19471fc5686b9dcd055b9c5ef7d 100644 (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)
index 82a053dd0d2d344dcbc1cda52db410a89524618f..a44686bc361ee71a3b9549e702a6fad5ee0adac5 100644 (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)