Skip to content

Commit b9f135d

Browse files
committed
go/types: change types2.Union API to accept a list of Terms
This is a straightforward port of CL 340250 to go/types. Change-Id: I8fc1c78833b5393fb39344fd248529df57870a72 Reviewed-on: https://go-review.googlesource.com/c/go/+/342437 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 c2b4ec8 commit b9f135d

File tree

5 files changed

+33
-35
lines changed

5 files changed

+33
-35
lines changed

src/go/types/builtins.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -806,12 +806,10 @@ func (check *Checker) applyTypeFunc(f func(Type) Type, x Type) Type {
806806
if tp := asTypeParam(x); tp != nil {
807807
// Test if t satisfies the requirements for the argument
808808
// type and collect possible result types at the same time.
809-
var rtypes []Type
810-
var tildes []bool
809+
var terms []*Term
811810
if !tp.iface().typeSet().is(func(t *term) bool {
812811
if r := f(t.typ); r != nil {
813-
rtypes = append(rtypes, r)
814-
tildes = append(tildes, t.tilde)
812+
terms = append(terms, NewTerm(t.tilde, r))
815813
return true
816814
}
817815
return false
@@ -823,7 +821,7 @@ func (check *Checker) applyTypeFunc(f func(Type) Type, x Type) Type {
823821
// type param is placed in the current package so export/import
824822
// works as expected.
825823
tpar := NewTypeName(token.NoPos, check.pkg, "<type parameter>", nil)
826-
ptyp := check.NewTypeParam(tpar, NewInterfaceType(nil, []Type{newUnion(rtypes, tildes)})) // assigns type to tpar as a side-effect
824+
ptyp := check.NewTypeParam(tpar, NewInterfaceType(nil, []Type{NewUnion(terms)})) // assigns type to tpar as a side-effect
827825
ptyp.index = tp.index
828826

829827
return ptyp

src/go/types/subst.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,19 +394,19 @@ func (subst *subster) typeList(in []Type) (out []Type, copied bool) {
394394
return
395395
}
396396

397-
func (subst *subster) termlist(in []*term) (out []*term, copied bool) {
397+
func (subst *subster) termlist(in []*Term) (out []*Term, copied bool) {
398398
out = in
399399
for i, t := range in {
400400
if u := subst.typ(t.typ); u != t.typ {
401401
if !copied {
402402
// first function that got substituted => allocate new out slice
403403
// and copy all functions
404-
new := make([]*term, len(in))
404+
new := make([]*Term, len(in))
405405
copy(new, out)
406406
out = new
407407
copied = true
408408
}
409-
out[i] = &term{t.tilde, u}
409+
out[i] = NewTerm(t.tilde, u)
410410
}
411411
}
412412
return

src/go/types/typeset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ func computeUnionTypeSet(check *Checker, pos token.Pos, utyp *Union) *_TypeSet {
357357
// This case is handled during union parsing.
358358
unreachable()
359359
default:
360-
terms = termlist{t}
360+
terms = termlist{(*term)(t)}
361361
}
362362
// The type set of a union expression is the union
363363
// of the type sets of each term.

src/go/types/typestring.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
131131
case *Union:
132132
// Unions only appear as (syntactic) embedded elements
133133
// in interfaces and syntactically cannot be empty.
134-
if t.NumTerms() == 0 {
134+
if t.Len() == 0 {
135135
panic("empty union")
136136
}
137137
for i, t := range t.terms {

src/go/types/union.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,46 @@ import (
1414

1515
// A Union represents a union of terms embedded in an interface.
1616
type Union struct {
17-
terms []*term // list of syntactical terms (not a canonicalized termlist)
17+
terms []*Term // list of syntactical terms (not a canonicalized termlist)
1818
tset *_TypeSet // type set described by this union, computed lazily
1919
}
2020

21-
// NewUnion returns a new Union type with the given terms (types[i], tilde[i]).
22-
// The lengths of both arguments must match. It is an error to create an empty
23-
// union; they are syntactically not possible.
24-
func NewUnion(types []Type, tilde []bool) *Union { return newUnion(types, tilde) }
21+
// NewUnion returns a new Union type with the given terms.
22+
// It is an error to create an empty union; they are syntactically not possible.
23+
func NewUnion(terms []*Term) *Union {
24+
if len(terms) == 0 {
25+
panic("empty union")
26+
}
27+
return &Union{terms, nil}
28+
}
2529

26-
func (u *Union) IsEmpty() bool { return len(u.terms) == 0 }
27-
func (u *Union) NumTerms() int { return len(u.terms) }
28-
func (u *Union) Term(i int) (Type, bool) { t := u.terms[i]; return t.typ, t.tilde }
30+
func (u *Union) Len() int { return len(u.terms) }
31+
func (u *Union) Term(i int) *Term { return u.terms[i] }
2932

3033
func (u *Union) Underlying() Type { return u }
3134
func (u *Union) String() string { return TypeString(u, nil) }
3235

36+
// A Term represents a term in a Union.
37+
type Term term
38+
39+
// NewTerm returns a new union term.
40+
func NewTerm(tilde bool, typ Type) *Term { return &Term{tilde, typ} }
41+
42+
func (t *Term) Tilde() bool { return t.tilde }
43+
func (t *Term) Type() Type { return t.typ }
44+
func (t *Term) String() string { return (*term)(t).String() }
45+
3346
// ----------------------------------------------------------------------------
3447
// Implementation
3548

36-
func newUnion(types []Type, tilde []bool) *Union {
37-
assert(len(types) == len(tilde))
38-
if len(types) == 0 {
39-
panic("empty union")
40-
}
41-
t := new(Union)
42-
t.terms = make([]*term, len(types))
43-
for i, typ := range types {
44-
t.terms[i] = &term{tilde[i], typ}
45-
}
46-
return t
47-
}
48-
4949
func parseUnion(check *Checker, tlist []ast.Expr) Type {
50-
var terms []*term
50+
var terms []*Term
5151
for _, x := range tlist {
5252
tilde, typ := parseTilde(check, x)
5353
if len(tlist) == 1 && !tilde {
5454
return typ // single type
5555
}
56-
terms = append(terms, &term{tilde, typ})
56+
terms = append(terms, NewTerm(tilde, typ))
5757
}
5858

5959
// Check validity of terms.
@@ -128,15 +128,15 @@ func parseTilde(check *Checker, x ast.Expr) (tilde bool, typ Type) {
128128
// overlappingTerm reports the index of the term x in terms which is
129129
// overlapping (not disjoint) from y. The result is < 0 if there is no
130130
// such term.
131-
func overlappingTerm(terms []*term, y *term) int {
131+
func overlappingTerm(terms []*Term, y *Term) int {
132132
for i, x := range terms {
133133
// disjoint requires non-nil, non-top arguments
134134
if debug {
135135
if x == nil || x.typ == nil || y == nil || y.typ == nil {
136136
panic("empty or top union term")
137137
}
138138
}
139-
if !x.disjoint(y) {
139+
if !(*term)(x).disjoint((*term)(y)) {
140140
return i
141141
}
142142
}

0 commit comments

Comments
 (0)