Skip to content

Commit 681751a

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: implement underIs, coreType, coreString via typeset iterator
Remove remaining underIs methods and call underIs function instead. Change-Id: Ic98430d3a56b85f6f4b35c4508c4c67dafbfa3f2 Reviewed-on: https://go-review.googlesource.com/c/go/+/614240 Reviewed-by: Tim King <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Robert Griesemer <[email protected]>
1 parent d39b366 commit 681751a

File tree

16 files changed

+54
-128
lines changed

16 files changed

+54
-128
lines changed

src/cmd/compile/internal/types2/builtins.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
184184
if !isTypeParam(x.typ) {
185185
break
186186
}
187-
if t.typeSet().underIs(func(t Type) bool {
188-
switch t := arrayPtrDeref(t).(type) {
187+
if underIs(x.typ, func(u Type) bool {
188+
switch t := arrayPtrDeref(u).(type) {
189189
case *Basic:
190190
if isString(t) && id == _Len {
191191
return true

src/cmd/compile/internal/types2/conversions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (check *Checker) conversion(x *operand, T Type) {
5656
// If T's type set is empty, or if it doesn't
5757
// have specific types, constant x cannot be
5858
// converted.
59-
ok = Unalias(T).(*TypeParam).underIs(func(u Type) bool {
59+
ok = underIs(T, func(u Type) bool {
6060
// u is nil if there are no specific type terms
6161
if u == nil {
6262
cause = check.sprintf("%s does not contain specific types", T)

src/cmd/compile/internal/types2/expr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ func (check *Checker) implicitTypeAndValue(x *operand, target Type) (Type, const
434434
}
435435
case *Interface:
436436
if isTypeParam(target) {
437-
if !u.typeSet().underIs(func(u Type) bool {
437+
if !underIs(target, func(u Type) bool {
438438
if u == nil {
439439
return false
440440
}

src/cmd/compile/internal/types2/index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (check *Checker) indexExpr(x *operand, e *syntax.IndexExpr) (isFuncInst boo
108108
var key, elem Type // key != nil: we must have all maps
109109
mode := variable // non-maps result mode
110110
// TODO(gri) factor out closure and use it for non-typeparam cases as well
111-
if typ.typeSet().underIs(func(u Type) bool {
111+
if underIs(x.typ, func(u Type) bool {
112112
l := int64(-1) // valid if >= 0
113113
var k, e Type // k is only set for maps
114114
switch t := u.(type) {

src/cmd/compile/internal/types2/predicates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func hasNil(t Type) bool {
211211
case *Slice, *Pointer, *Signature, *Map, *Chan:
212212
return true
213213
case *Interface:
214-
return !isTypeParam(t) || u.typeSet().underIs(func(u Type) bool {
214+
return !isTypeParam(t) || underIs(t, func(u Type) bool {
215215
return u != nil && hasNil(u)
216216
})
217217
}

src/cmd/compile/internal/types2/typeparam.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,6 @@ func (t *TypeParam) is(f func(*term) bool) bool {
155155
return t.iface().typeSet().is(f)
156156
}
157157

158-
// underIs calls f with the underlying types of the specific type terms
159-
// of t's constraint and reports whether all calls to f returned true.
160-
// If there are no specific terms, underIs returns the result of f(nil).
161-
func (t *TypeParam) underIs(f func(Type) bool) bool {
162-
return t.iface().typeSet().underIs(f)
163-
}
164-
165158
// typeset is an iterator over the (type/underlying type) pairs of the
166159
// specific type terms of t's constraint.
167160
// If there are no specific terms, typeset calls yield with (nil, nil).

src/cmd/compile/internal/types2/typeset.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -145,30 +145,6 @@ func (s *_TypeSet) is(f func(*term) bool) bool {
145145
return true
146146
}
147147

148-
// underIs calls f with the underlying types of the specific type terms
149-
// of s and reports whether all calls to f returned true. If there are
150-
// no specific terms, underIs returns the result of f(nil).
151-
func (s *_TypeSet) underIs(f func(Type) bool) bool {
152-
if !s.hasTerms() {
153-
return f(nil)
154-
}
155-
for _, t := range s.terms {
156-
assert(t.typ != nil)
157-
// Unalias(x) == under(x) for ~x terms
158-
u := Unalias(t.typ)
159-
if !t.tilde {
160-
u = under(u)
161-
}
162-
if debug {
163-
assert(Identical(u, under(u)))
164-
}
165-
if !f(u) {
166-
return false
167-
}
168-
}
169-
return true
170-
}
171-
172148
// topTypeSet may be used as type set for the empty interface.
173149
var topTypeSet = _TypeSet{terms: allTermlist}
174150

src/cmd/compile/internal/types2/under.go

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ func under(t Type) Type {
1818
// If typ is a type parameter, underIs returns the result of typ.underIs(f).
1919
// Otherwise, underIs returns the result of f(under(typ)).
2020
func underIs(typ Type, f func(Type) bool) bool {
21-
typ = Unalias(typ)
22-
if tpar, _ := typ.(*TypeParam); tpar != nil {
23-
return tpar.underIs(f)
24-
}
25-
return f(under(typ))
21+
var ok bool
22+
typeset(typ, func(_, u Type) bool {
23+
ok = f(u)
24+
return ok
25+
})
26+
return ok
2627
}
2728

2829
// typeset is an iterator over the (type/underlying type) pairs of the
@@ -46,45 +47,38 @@ func typeset(t Type, yield func(t, u Type) bool) {
4647
// identical element types), the single underlying type is the restricted
4748
// channel type if the restrictions are always the same, or nil otherwise.
4849
func coreType(t Type) Type {
49-
t = Unalias(t)
50-
tpar, _ := t.(*TypeParam)
51-
if tpar == nil {
52-
return under(t)
53-
}
54-
5550
var su Type
56-
if tpar.underIs(func(u Type) bool {
51+
typeset(t, func(_, u Type) bool {
5752
if u == nil {
5853
return false
5954
}
6055
if su != nil {
6156
u = match(su, u)
6257
if u == nil {
58+
su = nil
6359
return false
6460
}
6561
}
6662
// su == nil || match(su, u) != nil
6763
su = u
6864
return true
69-
}) {
70-
return su
71-
}
72-
return nil
65+
})
66+
return su
7367
}
7468

7569
// coreString is like coreType but also considers []byte
7670
// and strings as identical. In this case, if successful and we saw
7771
// a string, the result is of type (possibly untyped) string.
7872
func coreString(t Type) Type {
79-
t = Unalias(t)
80-
tpar, _ := t.(*TypeParam)
81-
if tpar == nil {
82-
return under(t) // string or untyped string
73+
// This explicit case is needed because otherwise the
74+
// result would be string if t is an untyped string.
75+
if !isTypeParam(t) {
76+
return under(t) // untyped string remains untyped
8377
}
8478

8579
var su Type
8680
hasString := false
87-
if tpar.underIs(func(u Type) bool {
81+
typeset(t, func(_, u Type) bool {
8882
if u == nil {
8983
return false
9084
}
@@ -95,19 +89,19 @@ func coreString(t Type) Type {
9589
if su != nil {
9690
u = match(su, u)
9791
if u == nil {
92+
su = nil
93+
hasString = false
9894
return false
9995
}
10096
}
10197
// su == nil || match(su, u) != nil
10298
su = u
10399
return true
104-
}) {
105-
if hasString {
106-
return Typ[String]
107-
}
108-
return su
100+
})
101+
if hasString {
102+
return Typ[String]
109103
}
110-
return nil
104+
return su
111105
}
112106

113107
// If x and y are identical, match returns x.

src/go/types/builtins.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/conversions.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/expr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ func (check *Checker) implicitTypeAndValue(x *operand, target Type) (Type, const
410410
}
411411
case *Interface:
412412
if isTypeParam(target) {
413-
if !u.typeSet().underIs(func(u Type) bool {
413+
if !underIs(target, func(u Type) bool {
414414
if u == nil {
415415
return false
416416
}

src/go/types/index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (check *Checker) indexExpr(x *operand, e *typeparams.IndexExpr) (isFuncInst
109109
var key, elem Type // key != nil: we must have all maps
110110
mode := variable // non-maps result mode
111111
// TODO(gri) factor out closure and use it for non-typeparam cases as well
112-
if typ.typeSet().underIs(func(u Type) bool {
112+
if underIs(x.typ, func(u Type) bool {
113113
l := int64(-1) // valid if >= 0
114114
var k, e Type // k is only set for maps
115115
switch t := u.(type) {

src/go/types/predicates.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/typeparam.go

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/typeset.go

Lines changed: 0 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/under.go

Lines changed: 21 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)