go/types: add NewTypeParamList, NewTypeList

Fixes #79603

Change-Id: I04b160a82aed7e6d83051f8e05a4ccad7d97c297
Reviewed-on: https://go-review.googlesource.com/c/go/+/792520
Reviewed-by: Alan Donovan <adonovan@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Mark Freeman <mark@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
qiulaidongfeng authored and Gopher Robot committed 2026-10-22 11:04:59 -07:00
1 parent f723e21aaa
commit f176f5e1a3
10 files changed
+37 -14

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
pkg go/types, func NewTypeList([]Type) *TypeList #79603
pkg go/types, func NewTypeParamList([]*TypeParam) *TypeParamList #79603
@@ -0,0 +1,3 @@
Added [NewTypeList] and [NewTypeParamList] constructors to
create [TypeList] and [TypeParamList] instances
from slices of their respective element types.
+1 -1
View File
@@ -165,7 +165,7 @@ func (check *Checker) newAliasInstance(pos syntax.Pos, orig *Alias, targs []Type
res := check.newAlias(obj, rhs)
res.orig = orig
res.tparams = orig.tparams
res.targs = newTypeList(targs)
res.targs = NewTypeList(targs)
// Ensure that res.actual is set before returning an instance constructed
// outside of package type checking (e.g. via types.Instantiate). When check != nil,
// res was added to check.cleanups by newAlias and will be cleaned up at the end
+1 -1
View File
@@ -342,7 +342,7 @@ func (check *Checker) newNamedInstance(pos syntax.Pos, orig *Named, targs []Type
assert(len(targs) > 0)
obj := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil)
inst := &instance{orig: orig, targs: newTypeList(targs)}
inst := &instance{orig: orig, targs: NewTypeList(targs)}
// Only pass the expanding context to the new instance if their packages
// match. Since type reference cycles are only possible within a single
+3 -3
View File
@@ -141,14 +141,14 @@ func (check *Checker) recordInstance(expr syntax.Expr, targs []Type, typ Type) {
copy.recv = pars.vars[0]
pars.vars = pars.vars[1:]
copy.params = &pars
m[ident] = Instance{newTypeList(targs), &copy}
m[ident] = Instance{NewTypeList(targs), &copy}
return
}
copy.recv = sig.recvold
m[ident] = Instance{newTypeList(targs), &copy}
m[ident] = Instance{NewTypeList(targs), &copy}
return
}
m[ident] = Instance{newTypeList(targs), typ}
m[ident] = Instance{NewTypeList(targs), typ}
}
}
+11 -2
View File
@@ -9,6 +9,15 @@ import "bytes"
// TypeParamList holds a list of type parameters.
type TypeParamList struct{ tparams []*TypeParam }
// NewTypeParamList returns a new TypeParamList
// with the type parameters in list.
func NewTypeParamList(list []*TypeParam) *TypeParamList {
if len(list) == 0 {
return nil
}
return &TypeParamList{list}
}
// Len returns the number of type parameters in the list.
// It is safe to call on a nil receiver.
func (l *TypeParamList) Len() int { return len(l.list()) }
@@ -42,8 +51,8 @@ func (l *TypeParamList) String() string {
// TypeList holds a list of types.
type TypeList struct{ types []Type }
// newTypeList returns a new TypeList with the types in list.
func newTypeList(list []Type) *TypeList {
// NewTypeList returns a new TypeList with the types in list.
func NewTypeList(list []Type) *TypeList {
if len(list) == 0 {
return nil
}
+1 -1
View File
@@ -168,7 +168,7 @@ func (check *Checker) newAliasInstance(pos token.Pos, orig *Alias, targs []Type,
res := check.newAlias(obj, rhs)
res.orig = orig
res.tparams = orig.tparams
res.targs = newTypeList(targs)
res.targs = NewTypeList(targs)
// Ensure that res.actual is set before returning an instance constructed
// outside of package type checking (e.g. via types.Instantiate). When check != nil,
// res was added to check.cleanups by newAlias and will be cleaned up at the end
+1 -1
View File
@@ -345,7 +345,7 @@ func (check *Checker) newNamedInstance(pos token.Pos, orig *Named, targs []Type,
assert(len(targs) > 0)
obj := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil)
inst := &instance{orig: orig, targs: newTypeList(targs)}
inst := &instance{orig: orig, targs: NewTypeList(targs)}
// Only pass the expanding context to the new instance if their packages
// match. Since type reference cycles are only possible within a single
+3 -3
View File
@@ -144,14 +144,14 @@ func (check *Checker) recordInstance(expr ast.Expr, targs []Type, typ Type) {
copy.recv = pars.vars[0]
pars.vars = pars.vars[1:]
copy.params = &pars
m[ident] = Instance{newTypeList(targs), &copy}
m[ident] = Instance{NewTypeList(targs), &copy}
return
}
copy.recv = sig.recvold
m[ident] = Instance{newTypeList(targs), &copy}
m[ident] = Instance{NewTypeList(targs), &copy}
return
}
m[ident] = Instance{newTypeList(targs), typ}
m[ident] = Instance{NewTypeList(targs), typ}
}
}
+11 -2
View File
@@ -12,6 +12,15 @@ import "bytes"
// TypeParamList holds a list of type parameters.
type TypeParamList struct{ tparams []*TypeParam }
// NewTypeParamList returns a new TypeParamList
// with the type parameters in list.
func NewTypeParamList(list []*TypeParam) *TypeParamList {
if len(list) == 0 {
return nil
}
return &TypeParamList{list}
}
// Len returns the number of type parameters in the list.
// It is safe to call on a nil receiver.
func (l *TypeParamList) Len() int { return len(l.list()) }
@@ -45,8 +54,8 @@ func (l *TypeParamList) String() string {
// TypeList holds a list of types.
type TypeList struct{ types []Type }
// newTypeList returns a new TypeList with the types in list.
func newTypeList(list []Type) *TypeList {
// NewTypeList returns a new TypeList with the types in list.
func NewTypeList(list []Type) *TypeList {
if len(list) == 0 {
return nil
}