mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
[dev.typeparams] go/types: update TypeParam APIs to match types2
This is a partial port of CL 323029, consisting only of changes to go/types. Changes to the importer will be made in a separate CL. Change-Id: I3b300f5e8f4df36c2c87e3f164705cd3c36218ac Reviewed-on: https://go-review.googlesource.com/c/go/+/335145 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
22a38ba5ca
commit
9e147c55b7
4 changed files with 22 additions and 19 deletions
|
|
@ -783,9 +783,11 @@ func (check *Checker) applyTypeFunc(f func(Type) Type, x Type) Type {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// construct a suitable new type parameter
|
// Construct a suitable new type parameter for the sum type. The
|
||||||
tpar := NewTypeName(token.NoPos, nil /* = Universe pkg */, "<type parameter>", nil)
|
// type param is placed in the current package so export/import
|
||||||
ptyp := check.newTypeParam(tpar, 0, &emptyInterface) // assigns type to tpar as a side-effect
|
// works as expected.
|
||||||
|
tpar := NewTypeName(token.NoPos, check.pkg, "<type parameter>", nil)
|
||||||
|
ptyp := check.NewTypeParam(tpar, 0, &emptyInterface) // assigns type to tpar as a side-effect
|
||||||
tsum := newUnion(rtypes, tildes)
|
tsum := newUnion(rtypes, tildes)
|
||||||
ptyp.bound = &Interface{complete: true, tset: &TypeSet{types: tsum}}
|
ptyp.bound = &Interface{complete: true, tset: &TypeSet{types: tsum}}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -758,7 +758,7 @@ func (check *Checker) collectTypeParams(list *ast.FieldList) []*TypeName {
|
||||||
func (check *Checker) declareTypeParams(tparams []*TypeName, names []*ast.Ident) []*TypeName {
|
func (check *Checker) declareTypeParams(tparams []*TypeName, names []*ast.Ident) []*TypeName {
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
tpar := NewTypeName(name.Pos(), check.pkg, name.Name, nil)
|
tpar := NewTypeName(name.Pos(), check.pkg, name.Name, nil)
|
||||||
check.newTypeParam(tpar, len(tparams), &emptyInterface) // assigns type to tpar as a side-effect
|
check.NewTypeParam(tpar, len(tparams), &emptyInterface) // assigns type to tpar as a side-effect
|
||||||
check.declare(check.scope, name, tpar, check.scope.pos) // TODO(gri) check scope position
|
check.declare(check.scope, name, tpar, check.scope.pos) // TODO(gri) check scope position
|
||||||
tparams = append(tparams, tpar)
|
tparams = append(tparams, tpar)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,11 +41,13 @@ func under(t Type) Type {
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
// optype returns a type's operational type. Except for type parameters,
|
// optype returns a type's operational type. Except for
|
||||||
// the operational type is the same as the underlying type (as returned
|
// type parameters, the operational type is the same
|
||||||
// by under). For Type parameters, the operational type is determined
|
// as the underlying type (as returned by under). For
|
||||||
// by the corresponding type constraint. The result may be the top type,
|
// Type parameters, the operational type is determined
|
||||||
// but it is never the incoming type parameter.
|
// by the corresponding type bound's type list. The
|
||||||
|
// result may be the bottom or top type, but it is never
|
||||||
|
// the incoming type parameter.
|
||||||
func optype(typ Type) Type {
|
func optype(typ Type) Type {
|
||||||
if t := asTypeParam(typ); t != nil {
|
if t := asTypeParam(typ); t != nil {
|
||||||
// If the optype is typ, return the top type as we have
|
// If the optype is typ, return the top type as we have
|
||||||
|
|
|
||||||
|
|
@ -27,22 +27,14 @@ type TypeParam struct {
|
||||||
bound Type // *Named or *Interface; underlying type is always *Interface
|
bound Type // *Named or *Interface; underlying type is always *Interface
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTypeParam returns a new TypeParam.
|
// NewTypeParam returns a new TypeParam. bound can be nil (and set later).
|
||||||
func NewTypeParam(obj *TypeName, index int, bound Type) *TypeParam {
|
func (check *Checker) NewTypeParam(obj *TypeName, index int, bound Type) *TypeParam {
|
||||||
return (*Checker)(nil).newTypeParam(obj, index, bound)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(rfindley): this is factored slightly differently in types2.
|
|
||||||
func (check *Checker) newTypeParam(obj *TypeName, index int, bound Type) *TypeParam {
|
|
||||||
assert(bound != nil)
|
|
||||||
|
|
||||||
// Always increment lastID, even if it is not used.
|
// Always increment lastID, even if it is not used.
|
||||||
id := nextID()
|
id := nextID()
|
||||||
if check != nil {
|
if check != nil {
|
||||||
check.nextID++
|
check.nextID++
|
||||||
id = check.nextID
|
id = check.nextID
|
||||||
}
|
}
|
||||||
|
|
||||||
typ := &TypeParam{check: check, id: id, obj: obj, index: index, bound: bound}
|
typ := &TypeParam{check: check, id: id, obj: obj, index: index, bound: bound}
|
||||||
if obj.typ == nil {
|
if obj.typ == nil {
|
||||||
obj.typ = typ
|
obj.typ = typ
|
||||||
|
|
@ -79,6 +71,13 @@ func (t *TypeParam) Bound() *Interface {
|
||||||
return iface
|
return iface
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TypeParam) _SetBound(bound Type) {
|
||||||
|
if bound == nil {
|
||||||
|
panic("internal error: bound must not be nil")
|
||||||
|
}
|
||||||
|
t.bound = bound
|
||||||
|
}
|
||||||
|
|
||||||
func (t *TypeParam) Underlying() Type { return t }
|
func (t *TypeParam) Underlying() Type { return t }
|
||||||
func (t *TypeParam) String() string { return TypeString(t, nil) }
|
func (t *TypeParam) String() string { return TypeString(t, nil) }
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue