mirror of
https://github.com/golang/go
synced 2024-11-26 14:08:37 -07:00
go/types, types2: factor out type parameter access into genericType
Also, remove types2.Signature.SetTypeParams as it is not used and does not exist in go/types. Change-Id: I16c3ae988988d3735907e9c6c56e8626497ea405 Reviewed-on: https://go-review.googlesource.com/c/go/+/581817 Reviewed-by: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
bf279b71e2
commit
3c8f925606
@ -14,6 +14,12 @@ import (
|
|||||||
. "internal/types/errors"
|
. "internal/types/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// A genericType implements access to its type parameters.
|
||||||
|
type genericType interface {
|
||||||
|
Type
|
||||||
|
TypeParams() *TypeParamList
|
||||||
|
}
|
||||||
|
|
||||||
// Instantiate instantiates the type orig with the given type arguments targs.
|
// Instantiate instantiates the type orig with the given type arguments targs.
|
||||||
// orig must be a *Named or a *Signature type. If there is no error, the
|
// orig must be a *Named or a *Signature type. If there is no error, the
|
||||||
// resulting Type is an instantiated type of the same kind (either a *Named or
|
// resulting Type is an instantiated type of the same kind (either a *Named or
|
||||||
@ -41,17 +47,15 @@ import (
|
|||||||
// count is incorrect; for *Named types, a panic may occur later inside the
|
// count is incorrect; for *Named types, a panic may occur later inside the
|
||||||
// *Named API.
|
// *Named API.
|
||||||
func Instantiate(ctxt *Context, orig Type, targs []Type, validate bool) (Type, error) {
|
func Instantiate(ctxt *Context, orig Type, targs []Type, validate bool) (Type, error) {
|
||||||
|
assert(len(targs) > 0)
|
||||||
if ctxt == nil {
|
if ctxt == nil {
|
||||||
ctxt = NewContext()
|
ctxt = NewContext()
|
||||||
}
|
}
|
||||||
|
orig_ := orig.(genericType) // signature of Instantiate must not change for backward-compatibility
|
||||||
|
|
||||||
if validate {
|
if validate {
|
||||||
var tparams []*TypeParam
|
tparams := orig_.TypeParams().list()
|
||||||
switch t := orig.(type) {
|
assert(len(tparams) > 0)
|
||||||
case *Named:
|
|
||||||
tparams = t.TypeParams().list()
|
|
||||||
case *Signature:
|
|
||||||
tparams = t.TypeParams().list()
|
|
||||||
}
|
|
||||||
if len(targs) != len(tparams) {
|
if len(targs) != len(tparams) {
|
||||||
return nil, fmt.Errorf("got %d type arguments but %s has %d type parameters", len(targs), orig, len(tparams))
|
return nil, fmt.Errorf("got %d type arguments but %s has %d type parameters", len(targs), orig, len(tparams))
|
||||||
}
|
}
|
||||||
@ -60,7 +64,7 @@ func Instantiate(ctxt *Context, orig Type, targs []Type, validate bool) (Type, e
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inst := (*Checker)(nil).instance(nopos, orig, targs, nil, ctxt)
|
inst := (*Checker)(nil).instance(nopos, orig_, targs, nil, ctxt)
|
||||||
return inst, nil
|
return inst, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +79,7 @@ func Instantiate(ctxt *Context, orig Type, targs []Type, validate bool) (Type, e
|
|||||||
// must be non-nil.
|
// must be non-nil.
|
||||||
//
|
//
|
||||||
// For Named types the resulting instance may be unexpanded.
|
// For Named types the resulting instance may be unexpanded.
|
||||||
func (check *Checker) instance(pos syntax.Pos, orig Type, targs []Type, expanding *Named, ctxt *Context) (res Type) {
|
func (check *Checker) instance(pos syntax.Pos, orig genericType, targs []Type, expanding *Named, ctxt *Context) (res Type) {
|
||||||
// The order of the contexts below matters: we always prefer instances in the
|
// The order of the contexts below matters: we always prefer instances in the
|
||||||
// expanding instance context in order to preserve reference cycles.
|
// expanding instance context in order to preserve reference cycles.
|
||||||
//
|
//
|
||||||
|
@ -73,9 +73,6 @@ func (s *Signature) Recv() *Var { return s.recv }
|
|||||||
// TypeParams returns the type parameters of signature s, or nil.
|
// TypeParams returns the type parameters of signature s, or nil.
|
||||||
func (s *Signature) TypeParams() *TypeParamList { return s.tparams }
|
func (s *Signature) TypeParams() *TypeParamList { return s.tparams }
|
||||||
|
|
||||||
// SetTypeParams sets the type parameters of signature s.
|
|
||||||
func (s *Signature) SetTypeParams(tparams []*TypeParam) { s.tparams = bindTParams(tparams) }
|
|
||||||
|
|
||||||
// RecvTypeParams returns the receiver type parameters of signature s, or nil.
|
// RecvTypeParams returns the receiver type parameters of signature s, or nil.
|
||||||
func (s *Signature) RecvTypeParams() *TypeParamList { return s.rparams }
|
func (s *Signature) RecvTypeParams() *TypeParamList { return s.rparams }
|
||||||
|
|
||||||
|
@ -17,6 +17,12 @@ import (
|
|||||||
. "internal/types/errors"
|
. "internal/types/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// A genericType implements access to its type parameters.
|
||||||
|
type genericType interface {
|
||||||
|
Type
|
||||||
|
TypeParams() *TypeParamList
|
||||||
|
}
|
||||||
|
|
||||||
// Instantiate instantiates the type orig with the given type arguments targs.
|
// Instantiate instantiates the type orig with the given type arguments targs.
|
||||||
// orig must be a *Named or a *Signature type. If there is no error, the
|
// orig must be a *Named or a *Signature type. If there is no error, the
|
||||||
// resulting Type is an instantiated type of the same kind (either a *Named or
|
// resulting Type is an instantiated type of the same kind (either a *Named or
|
||||||
@ -44,17 +50,15 @@ import (
|
|||||||
// count is incorrect; for *Named types, a panic may occur later inside the
|
// count is incorrect; for *Named types, a panic may occur later inside the
|
||||||
// *Named API.
|
// *Named API.
|
||||||
func Instantiate(ctxt *Context, orig Type, targs []Type, validate bool) (Type, error) {
|
func Instantiate(ctxt *Context, orig Type, targs []Type, validate bool) (Type, error) {
|
||||||
|
assert(len(targs) > 0)
|
||||||
if ctxt == nil {
|
if ctxt == nil {
|
||||||
ctxt = NewContext()
|
ctxt = NewContext()
|
||||||
}
|
}
|
||||||
|
orig_ := orig.(genericType) // signature of Instantiate must not change for backward-compatibility
|
||||||
|
|
||||||
if validate {
|
if validate {
|
||||||
var tparams []*TypeParam
|
tparams := orig_.TypeParams().list()
|
||||||
switch t := orig.(type) {
|
assert(len(tparams) > 0)
|
||||||
case *Named:
|
|
||||||
tparams = t.TypeParams().list()
|
|
||||||
case *Signature:
|
|
||||||
tparams = t.TypeParams().list()
|
|
||||||
}
|
|
||||||
if len(targs) != len(tparams) {
|
if len(targs) != len(tparams) {
|
||||||
return nil, fmt.Errorf("got %d type arguments but %s has %d type parameters", len(targs), orig, len(tparams))
|
return nil, fmt.Errorf("got %d type arguments but %s has %d type parameters", len(targs), orig, len(tparams))
|
||||||
}
|
}
|
||||||
@ -63,7 +67,7 @@ func Instantiate(ctxt *Context, orig Type, targs []Type, validate bool) (Type, e
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inst := (*Checker)(nil).instance(nopos, orig, targs, nil, ctxt)
|
inst := (*Checker)(nil).instance(nopos, orig_, targs, nil, ctxt)
|
||||||
return inst, nil
|
return inst, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,7 +82,7 @@ func Instantiate(ctxt *Context, orig Type, targs []Type, validate bool) (Type, e
|
|||||||
// must be non-nil.
|
// must be non-nil.
|
||||||
//
|
//
|
||||||
// For Named types the resulting instance may be unexpanded.
|
// For Named types the resulting instance may be unexpanded.
|
||||||
func (check *Checker) instance(pos token.Pos, orig Type, targs []Type, expanding *Named, ctxt *Context) (res Type) {
|
func (check *Checker) instance(pos token.Pos, orig genericType, targs []Type, expanding *Named, ctxt *Context) (res Type) {
|
||||||
// The order of the contexts below matters: we always prefer instances in the
|
// The order of the contexts below matters: we always prefer instances in the
|
||||||
// expanding instance context in order to preserve reference cycles.
|
// expanding instance context in order to preserve reference cycles.
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user