]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] go/types: (TypeParam) SetBound -> SetConstraint
authorRob Findley <rfindley@google.com>
Wed, 4 Aug 2021 01:14:31 +0000 (21:14 -0400)
committerRobert Findley <rfindley@google.com>
Wed, 4 Aug 2021 11:07:19 +0000 (11:07 +0000)
This is a straightforward port of CL 338196 to go/types, minus the
deprecated TypeParam.Bound() method (since it is not needed), plus an
adjustment for methodset.go.

Change-Id: Ie372bfeec245094102a2c3257a43499d75981447
Reviewed-on: https://go-review.googlesource.com/c/go/+/339675
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>
src/go/types/builtins.go
src/go/types/call.go
src/go/types/instantiate.go
src/go/types/lookup.go
src/go/types/methodset.go
src/go/types/predicates.go
src/go/types/type.go
src/go/types/typeparam.go

index ecb9920a8112a06a13aa8c38eab1da2a487d09ff..aae05438cd3cd54c425bbbf7dac0cea2fbd02132 100644 (file)
@@ -826,7 +826,7 @@ func (check *Checker) applyTypeFunc(f func(Type) Type, x Type) Type {
                // type and collect possible result types at the same time.
                var rtypes []Type
                var tildes []bool
-               if !tp.Bound().is(func(typ Type, tilde bool) bool {
+               if !tp.iface().is(func(typ Type, tilde bool) bool {
                        if r := f(typ); r != nil {
                                rtypes = append(rtypes, r)
                                tildes = append(tildes, tilde)
index 16b8e4eb7c43f6f865b756998c90d1fef8c10f90..da2f319a4a718975a92ca79261781064b0328d8e 100644 (file)
@@ -482,7 +482,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
                        var why string
                        if tpar := asTypeParam(x.typ); tpar != nil {
                                // Type parameter bounds don't specify fields, so don't mention "field".
-                               if tname := tpar.Bound().obj; tname != nil {
+                               if tname := tpar.iface().obj; tname != nil {
                                        why = check.sprintf("interface %s has no method %s", tname.name, sel)
                                } else {
                                        why = check.sprintf("type bound for %s has no method %s", x.typ, sel)
index 2e6c20723b37dff8a8b675ae8beca82b13c8f828..6d56eb7ea211d496bcaa583c1860d79ea4a70d2e 100644 (file)
@@ -162,7 +162,7 @@ func (check *Checker) verify(pos token.Pos, tparams []*TypeName, targs []Type, p
 // A suitable error is reported if the result is false.
 // TODO(gri) This should be a method of interfaces or type sets.
 func (check *Checker) satisfies(pos token.Pos, targ Type, tpar *TypeParam, smap *substMap) bool {
-       iface := tpar.Bound()
+       iface := tpar.iface()
        if iface.Empty() {
                return true // no type bound
        }
@@ -176,7 +176,7 @@ func (check *Checker) satisfies(pos token.Pos, targ Type, tpar *TypeParam, smap
        // if iface is comparable, targ must be comparable
        // TODO(gri) the error messages needs to be better, here
        if iface.IsComparable() && !Comparable(targ) {
-               if tpar := asTypeParam(targ); tpar != nil && tpar.Bound().typeSet().IsTop() {
+               if tpar := asTypeParam(targ); tpar != nil && tpar.iface().typeSet().IsTop() {
                        check.softErrorf(atPos(pos), _Todo, "%s has no constraints", targ)
                        return false
                }
@@ -222,7 +222,7 @@ func (check *Checker) satisfies(pos token.Pos, targ Type, tpar *TypeParam, smap
        // If targ is itself a type parameter, each of its possible types, but at least one, must be in the
        // list of iface types (i.e., the targ type list must be a non-empty subset of the iface types).
        if targ := asTypeParam(targ); targ != nil {
-               targBound := targ.Bound()
+               targBound := targ.iface()
                if targBound.typeSet().types == nil {
                        check.softErrorf(atPos(pos), _Todo, "%s does not satisfy %s (%s has no type constraints)", targ, tpar.bound, targ)
                        return false
index 6d38db452384919befe21566d9435f28be6e9dde..7cab336dbe2e8632d1551afbe0b6fec02e555d7b 100644 (file)
@@ -190,7 +190,7 @@ func lookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (o
                                }
 
                        case *TypeParam:
-                               if i, m := t.Bound().typeSet().LookupMethod(pkg, name); m != nil {
+                               if i, m := t.iface().typeSet().LookupMethod(pkg, name); m != nil {
                                        assert(m.typ != nil)
                                        index = concat(e.index, i)
                                        if obj != nil || e.multiples {
index 491917d6bcd958c8968e1018a73ac5d52b90c4d9..1462601d5869e4a135bf8be616bd4028c49c090e 100644 (file)
@@ -160,7 +160,7 @@ func NewMethodSet(T Type) *MethodSet {
                                mset = mset.add(t.typeSet().methods, e.index, true, e.multiples)
 
                        case *TypeParam:
-                               mset = mset.add(t.Bound().typeSet().methods, e.index, true, e.multiples)
+                               mset = mset.add(t.iface().typeSet().methods, e.index, true, e.multiples)
                        }
                }
 
index 579d35da4204dc700cbf273ebb98371c5e90b5ba..f9cac34a0313ca6e7f709def313351df86d63242 100644 (file)
@@ -113,7 +113,7 @@ func comparable(T Type, seen map[Type]bool) bool {
        case *Array:
                return comparable(t.elem, seen)
        case *TypeParam:
-               return t.Bound().IsComparable()
+               return t.iface().IsComparable()
        }
        return false
 }
index 2ad89d97059688d08d379c00134cddda21eaa71d..5819dd290ca6f5e1bf4bbb68a8f3917f464f096f 100644 (file)
@@ -56,7 +56,7 @@ func optype(typ Type) Type {
                // for a type parameter list of the form:
                // (type T interface { type T }).
                // See also issue #39680.
-               if a := t.Bound().typeSet().types; a != nil && a != typ {
+               if a := t.iface().typeSet().types; a != nil && a != typ {
                        // If we have a union with a single entry, ignore
                        // any tilde because under(~t) == under(t).
                        if u, _ := a.(*Union); u != nil && u.NumTerms() == 1 {
index 8cb44ea25ec84bc66f74103e35a0fc8ffed3f5a0..33a516c209734f117dc55582c91002c5ba40ed20 100644 (file)
@@ -74,23 +74,20 @@ func (t *TypeParam) Constraint() Type {
        return t.bound
 }
 
-// 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
+// SetConstraint sets the type constraint for t.
+func (t *TypeParam) SetConstraint(bound Type) {
+       if bound == nil {
+               panic("types2.TypeParam.SetConstraint: bound must not be nil")
        }
-       return &emptyInterface
+       t.bound = bound
 }
 
-// TODO(rfindley): document the SetBound methods.
-
-func (t *TypeParam) SetBound(bound Type) {
-       if bound == nil {
-               panic("internal error: bound must not be nil")
+// iface returns the constraint interface of t.
+func (t *TypeParam) iface() *Interface {
+       if iface, _ := under(t.Constraint()).(*Interface); iface != nil {
+               return iface
        }
-       t.bound = bound
+       return &emptyInterface
 }
 
 func (t *TypeParam) Underlying() Type { return t }
@@ -135,5 +132,5 @@ func bindTParams(list []*TypeName) *TypeParams {
 // Implementation
 
 func (t *TypeParam) underIs(f func(Type) bool) bool {
-       return t.Bound().typeSet().underIs(f)
+       return t.iface().typeSet().underIs(f)
 }