]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: implement adjCoreType using TypeParam.is
authorRobert Griesemer <gri@golang.org>
Wed, 23 Feb 2022 00:53:17 +0000 (16:53 -0800)
committerRobert Griesemer <gri@golang.org>
Thu, 24 Feb 2022 22:11:21 +0000 (22:11 +0000)
TypeParam.is also provides ~ (tilde) information which is needed
to fix #51229. Delete all code related to singleType as it's not
used anymore.

Also, remove TypeParam.hasTerms as it was not used.

For #51229.

Change-Id: Ie49b19d157230beecb17a444d1f17cf24aa4f6ba
Reviewed-on: https://go-review.googlesource.com/c/go/+/387774
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/infer.go
src/cmd/compile/internal/types2/termlist.go
src/cmd/compile/internal/types2/termlist_test.go
src/cmd/compile/internal/types2/typeparam.go
src/cmd/compile/internal/types2/typeset.go
src/go/types/infer.go
src/go/types/termlist.go
src/go/types/termlist_test.go
src/go/types/typeparam.go
src/go/types/typeset.go

index 2d6f26c0c92f885adb139b4d3617fc0bb3fbc7c0..617f3edad7c40699cc1174a75fe35675498bd433 100644 (file)
@@ -591,15 +591,24 @@ func (check *Checker) inferB(pos syntax.Pos, tparams []*TypeParam, targs []Type)
        return
 }
 
+// adjCoreType returns the core type of tpar unless the
+// type parameter embeds a single, possibly named type,
+// in which case it returns that single type instead.
+// (The core type is always the underlying type of that
+// single type.)
 func adjCoreType(tpar *TypeParam) Type {
-       // If the type parameter embeds a single, possibly named
-       // type, use that one instead of the core type (which is
-       // always the underlying type of that single type).
-       if single := tpar.singleType(); single != nil {
+       var single *term
+       if tpar.is(func(t *term) bool {
+               if single == nil && t != nil {
+                       single = t
+                       return true
+               }
+               return false // zero or more than one terms
+       }) {
                if debug {
-                       assert(under(single) == coreType(tpar))
+                       assert(under(single.typ) == coreType(tpar))
                }
-               return single
+               return single.typ
        }
        return coreType(tpar)
 }
index 844e39e3bf4ae275c76cb98eef96fbff35cb17c1..a0108c46383e1aee076090d8a2c2bc35d32a8880 100644 (file)
@@ -92,15 +92,6 @@ func (xl termlist) norm() termlist {
        return rl
 }
 
-// If the type set represented by xl is specified by a single (non-𝓤) term,
-// singleType returns that type. Otherwise it returns nil.
-func (xl termlist) singleType() Type {
-       if nl := xl.norm(); len(nl) == 1 {
-               return nl[0].typ // if nl.isAll() then typ is nil, which is ok
-       }
-       return nil
-}
-
 // union returns the union xl ∪ yl.
 func (xl termlist) union(yl termlist) termlist {
        return append(xl, yl...).norm()
index 1bdf9e13860abc413aa7af9961449b4abe2e1643..d1e3bdf88efcc777fc02b99999a494786ef3335d 100644 (file)
@@ -106,35 +106,6 @@ func TestTermlistNorm(t *testing.T) {
        }
 }
 
-func TestTermlistSingleType(t *testing.T) {
-       // helper to deal with nil types
-       tstring := func(typ Type) string {
-               if typ == nil {
-                       return "nil"
-               }
-               return typ.String()
-       }
-
-       for test, want := range map[string]string{
-               "∅":                 "nil",
-               "𝓤":                 "nil",
-               "int":               "int",
-               "myInt":             "myInt",
-               "~int":              "int",
-               "~int ∪ string":     "nil",
-               "~int ∪ myInt":      "int",
-               "∅ ∪ int":           "int",
-               "∅ ∪ ~int":          "int",
-               "∅ ∪ ~int ∪ string": "nil",
-       } {
-               xl := maketl(test)
-               got := tstring(xl.singleType())
-               if got != want {
-                       t.Errorf("(%v).singleType() == %v; want %v", test, got, want)
-               }
-       }
-}
-
 func TestTermlistUnion(t *testing.T) {
        for _, test := range []struct {
                xl, yl, want string
index 57613706f7089e35570f06e4362d651ad29905a1..9ed3369ff42f065b696a40170c780c4bd9f6f62b 100644 (file)
@@ -138,16 +138,6 @@ func (t *TypeParam) iface() *Interface {
        return ityp
 }
 
-// singleType returns the single type of the type parameter constraint; or nil.
-func (t *TypeParam) singleType() Type {
-       return t.iface().typeSet().singleType()
-}
-
-// hasTerms reports whether the type parameter constraint has specific type terms.
-func (t *TypeParam) hasTerms() bool {
-       return t.iface().typeSet().hasTerms()
-}
-
 // is calls f with the specific type terms of t's constraint and reports whether
 // all calls to f returned true. If there are no specific terms, is
 // returns the result of f(nil).
index 2c3e826a3fff05167a7867fbe86a03c34748cb05..65ae04819e746c74204afa6bf475a792abf14022 100644 (file)
@@ -103,9 +103,6 @@ func (s *_TypeSet) String() string {
 // hasTerms reports whether the type set has specific type terms.
 func (s *_TypeSet) hasTerms() bool { return !s.terms.isEmpty() && !s.terms.isAll() }
 
-// singleType returns the single type in s if there is exactly one; otherwise the result is nil.
-func (s *_TypeSet) singleType() Type { return s.terms.singleType() }
-
 // subsetOf reports whether s1 ⊆ s2.
 func (s1 *_TypeSet) subsetOf(s2 *_TypeSet) bool { return s1.terms.subsetOf(s2.terms) }
 
index 8f22144c839128f1995ac36da9741c4a36296202..d481aaa877ddb18567de5042ea521239a068a4e0 100644 (file)
@@ -590,15 +590,24 @@ func (check *Checker) inferB(posn positioner, tparams []*TypeParam, targs []Type
        return
 }
 
+// adjCoreType returns the core type of tpar unless the
+// type parameter embeds a single, possibly named type,
+// in which case it returns that single type instead.
+// (The core type is always the underlying type of that
+// single type.)
 func adjCoreType(tpar *TypeParam) Type {
-       // If the type parameter embeds a single, possibly named
-       // type, use that one instead of the core type (which is
-       // always the underlying type of that single type).
-       if single := tpar.singleType(); single != nil {
+       var single *term
+       if tpar.is(func(t *term) bool {
+               if single == nil && t != nil {
+                       single = t
+                       return true
+               }
+               return false // zero or more than one terms
+       }) {
                if debug {
-                       assert(under(single) == coreType(tpar))
+                       assert(under(single.typ) == coreType(tpar))
                }
-               return single
+               return single.typ
        }
        return coreType(tpar)
 }
index c4ab0e037e300aed0c173eb476e945e97877f630..94e49caee04a26ce0fd6b55ebce083fb42f62ecc 100644 (file)
@@ -92,15 +92,6 @@ func (xl termlist) norm() termlist {
        return rl
 }
 
-// If the type set represented by xl is specified by a single (non-𝓤) term,
-// singleType returns that type. Otherwise it returns nil.
-func (xl termlist) singleType() Type {
-       if nl := xl.norm(); len(nl) == 1 {
-               return nl[0].typ // if nl.isAll() then typ is nil, which is ok
-       }
-       return nil
-}
-
 // union returns the union xl ∪ yl.
 func (xl termlist) union(yl termlist) termlist {
        return append(xl, yl...).norm()
index dddca7a68201d609b4219c9ab0e2f663596f3031..f0d58ac1bcf9961cdfe24f2f3847ffaeee2187f0 100644 (file)
@@ -106,35 +106,6 @@ func TestTermlistNorm(t *testing.T) {
        }
 }
 
-func TestTermlistSingleType(t *testing.T) {
-       // helper to deal with nil types
-       tstring := func(typ Type) string {
-               if typ == nil {
-                       return "nil"
-               }
-               return typ.String()
-       }
-
-       for test, want := range map[string]string{
-               "∅":                 "nil",
-               "𝓤":                 "nil",
-               "int":               "int",
-               "myInt":             "myInt",
-               "~int":              "int",
-               "~int ∪ string":     "nil",
-               "~int ∪ myInt":      "int",
-               "∅ ∪ int":           "int",
-               "∅ ∪ ~int":          "int",
-               "∅ ∪ ~int ∪ string": "nil",
-       } {
-               xl := maketl(test)
-               got := tstring(xl.singleType())
-               if got != want {
-                       t.Errorf("(%v).singleType() == %v; want %v", test, got, want)
-               }
-       }
-}
-
 func TestTermlistUnion(t *testing.T) {
        for _, test := range []struct {
                xl, yl, want string
index 5505372cff2015b02629bd78c568f7edb8b17e92..778c687d430ff0053eab7f934a9ddba5df816a2a 100644 (file)
@@ -140,16 +140,6 @@ func (t *TypeParam) iface() *Interface {
        return ityp
 }
 
-// singleType returns the single type of the type parameter constraint; or nil.
-func (t *TypeParam) singleType() Type {
-       return t.iface().typeSet().singleType()
-}
-
-// hasTerms reports whether the type parameter constraint has specific type terms.
-func (t *TypeParam) hasTerms() bool {
-       return t.iface().typeSet().hasTerms()
-}
-
 // is calls f with the specific type terms of t's constraint and reports whether
 // all calls to f returned true. If there are no specific terms, is
 // returns the result of f(nil).
index 3bc94746606539ba8edd64cc71db3725ee138b37..4c3f018cfefed7fb0b8428b8d8d0013c101b5cc9 100644 (file)
@@ -101,9 +101,6 @@ func (s *_TypeSet) String() string {
 // hasTerms reports whether the type set has specific type terms.
 func (s *_TypeSet) hasTerms() bool { return !s.terms.isEmpty() && !s.terms.isAll() }
 
-// singleType returns the single type in s if there is exactly one; otherwise the result is nil.
-func (s *_TypeSet) singleType() Type { return s.terms.singleType() }
-
 // subsetOf reports whether s1 ⊆ s2.
 func (s1 *_TypeSet) subsetOf(s2 *_TypeSet) bool { return s1.terms.subsetOf(s2.terms) }