[dev.typeparams] go/types: implement TypeParam.Constraint

This is a clean port of CL 336989 to go/types.

Change-Id: Ib8dbe03f420d28ada6d5fc7003ab0c82c7e06c41
Reviewed-on: https://go-review.googlesource.com/c/go/+/339650
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:
Rob Findley 2021-08-03 16:24:20 -04:00 committed by Robert Findley
parent 1ea3596b41
commit 89897473e2

View file

@ -60,24 +60,32 @@ func (t *TypeParam) _SetId(id uint64) {
t.id = id t.id = id
} }
// TODO(rfindley): document the Bound and SetBound methods. // Constraint returns the type constraint specified for t.
func (t *TypeParam) Constraint() Type {
func (t *TypeParam) Bound() *Interface { // compute the type set if possible (we may not have an interface)
// we may not have an interface (error reported elsewhere) if iface, _ := under(t.bound).(*Interface); iface != nil {
iface, _ := under(t.bound).(*Interface) // use the type bound position if we have one
if iface == nil { pos := token.NoPos
return &emptyInterface if n, _ := t.bound.(*Named); n != nil {
pos = n.obj.pos
}
computeTypeSet(t.check, pos, iface)
} }
// use the type bound position if we have one return t.bound
pos := token.NoPos
if n, _ := t.bound.(*Named); n != nil {
pos = n.obj.pos
}
// TODO(rFindley) switch this to an unexported method on Checker.
computeTypeSet(t.check, pos, iface)
return iface
} }
// Bound returns the underlying type of the type parameter's
// constraint.
// Deprecated for external use. Use Constraint instead.
func (t *TypeParam) Bound() *Interface {
if iface, _ := under(t.Constraint()).(*Interface); iface != nil {
return iface
}
return &emptyInterface
}
// TODO(rfindley): document the SetBound methods.
func (t *TypeParam) SetBound(bound Type) { func (t *TypeParam) SetBound(bound Type) {
if bound == nil { if bound == nil {
panic("internal error: bound must not be nil") panic("internal error: bound must not be nil")