Skip to content

Commit 8989747

Browse files
committed
[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 <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 1ea3596 commit 8989747

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/go/types/typeparam.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,32 @@ func (t *TypeParam) _SetId(id uint64) {
6060
t.id = id
6161
}
6262

63-
// TODO(rfindley): document the Bound and SetBound methods.
63+
// Constraint returns the type constraint specified for t.
64+
func (t *TypeParam) Constraint() Type {
65+
// compute the type set if possible (we may not have an interface)
66+
if iface, _ := under(t.bound).(*Interface); iface != nil {
67+
// use the type bound position if we have one
68+
pos := token.NoPos
69+
if n, _ := t.bound.(*Named); n != nil {
70+
pos = n.obj.pos
71+
}
72+
computeTypeSet(t.check, pos, iface)
73+
}
74+
return t.bound
75+
}
6476

77+
// Bound returns the underlying type of the type parameter's
78+
// constraint.
79+
// Deprecated for external use. Use Constraint instead.
6580
func (t *TypeParam) Bound() *Interface {
66-
// we may not have an interface (error reported elsewhere)
67-
iface, _ := under(t.bound).(*Interface)
68-
if iface == nil {
69-
return &emptyInterface
81+
if iface, _ := under(t.Constraint()).(*Interface); iface != nil {
82+
return iface
7083
}
71-
// use the type bound position if we have one
72-
pos := token.NoPos
73-
if n, _ := t.bound.(*Named); n != nil {
74-
pos = n.obj.pos
75-
}
76-
// TODO(rFindley) switch this to an unexported method on Checker.
77-
computeTypeSet(t.check, pos, iface)
78-
return iface
84+
return &emptyInterface
7985
}
8086

87+
// TODO(rfindley): document the SetBound methods.
88+
8189
func (t *TypeParam) SetBound(bound Type) {
8290
if bound == nil {
8391
panic("internal error: bound must not be nil")

0 commit comments

Comments
 (0)