Skip to content

Commit 48ec6df

Browse files
committed
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 <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 97e740e commit 48ec6df

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ func (t *Named) Origin() *Named { return t.orig }
8888
func (t *Named) TypeParams() *TypeParamList { return t.resolve(nil).tparams }
8989

9090
// SetTypeParams sets the type parameters of the named type t.
91-
func (t *Named) SetTypeParams(tparams []*TypeParam) { t.resolve(nil).tparams = bindTParams(tparams) }
91+
// t must not have type arguments.
92+
func (t *Named) SetTypeParams(tparams []*TypeParam) {
93+
assert(t.targs.Len() == 0)
94+
t.resolve(nil).tparams = bindTParams(tparams)
95+
}
9296

9397
// TypeArgs returns the type arguments used to instantiate the named type t.
9498
func (t *Named) TypeArgs() *TypeList { return t.targs }
@@ -100,7 +104,9 @@ func (t *Named) NumMethods() int { return len(t.resolve(nil).methods) }
100104
func (t *Named) Method(i int) *Func { return t.resolve(nil).methods[i] }
101105

102106
// SetUnderlying sets the underlying type and marks t as complete.
107+
// t must not have type arguments.
103108
func (t *Named) SetUnderlying(underlying Type) {
109+
assert(t.targs.Len() == 0)
104110
if underlying == nil {
105111
panic("underlying type must not be nil")
106112
}
@@ -111,7 +117,9 @@ func (t *Named) SetUnderlying(underlying Type) {
111117
}
112118

113119
// AddMethod adds method m unless it is already in the method list.
120+
// t must not have type arguments.
114121
func (t *Named) AddMethod(m *Func) {
122+
assert(t.targs.Len() == 0)
115123
t.resolve(nil)
116124
if i, _ := lookupMethod(t.methods, m.pkg, m.name); i < 0 {
117125
t.methods = append(t.methods, m)

src/go/types/named.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ func (t *Named) Origin() *Named { return t.orig }
9090
func (t *Named) TypeParams() *TypeParamList { return t.resolve(nil).tparams }
9191

9292
// SetTypeParams sets the type parameters of the named type t.
93-
func (t *Named) SetTypeParams(tparams []*TypeParam) { t.resolve(nil).tparams = bindTParams(tparams) }
93+
// t must not have type arguments.
94+
func (t *Named) SetTypeParams(tparams []*TypeParam) {
95+
assert(t.targs.Len() == 0)
96+
t.resolve(nil).tparams = bindTParams(tparams)
97+
}
9498

9599
// TypeArgs returns the type arguments used to instantiate the named type t.
96100
func (t *Named) TypeArgs() *TypeList { return t.targs }
@@ -102,7 +106,9 @@ func (t *Named) NumMethods() int { return len(t.resolve(nil).methods) }
102106
func (t *Named) Method(i int) *Func { return t.resolve(nil).methods[i] }
103107

104108
// SetUnderlying sets the underlying type and marks t as complete.
109+
// t must not have type arguments.
105110
func (t *Named) SetUnderlying(underlying Type) {
111+
assert(t.targs.Len() == 0)
106112
if underlying == nil {
107113
panic("underlying type must not be nil")
108114
}
@@ -113,7 +119,9 @@ func (t *Named) SetUnderlying(underlying Type) {
113119
}
114120

115121
// AddMethod adds method m unless it is already in the method list.
122+
// t must not have type arguments.
116123
func (t *Named) AddMethod(m *Func) {
124+
assert(t.targs.Len() == 0)
117125
t.resolve(nil)
118126
if i, _ := lookupMethod(t.methods, m.pkg, m.name); i < 0 {
119127
t.methods = append(t.methods, m)

0 commit comments

Comments
 (0)