mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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 <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
c2b4ec8f49
commit
b9f135d98f
5 changed files with 33 additions and 35 deletions
|
|
@ -806,12 +806,10 @@ func (check *Checker) applyTypeFunc(f func(Type) Type, x Type) Type {
|
||||||
if tp := asTypeParam(x); tp != nil {
|
if tp := asTypeParam(x); tp != nil {
|
||||||
// Test if t satisfies the requirements for the argument
|
// Test if t satisfies the requirements for the argument
|
||||||
// type and collect possible result types at the same time.
|
// type and collect possible result types at the same time.
|
||||||
var rtypes []Type
|
var terms []*Term
|
||||||
var tildes []bool
|
|
||||||
if !tp.iface().typeSet().is(func(t *term) bool {
|
if !tp.iface().typeSet().is(func(t *term) bool {
|
||||||
if r := f(t.typ); r != nil {
|
if r := f(t.typ); r != nil {
|
||||||
rtypes = append(rtypes, r)
|
terms = append(terms, NewTerm(t.tilde, r))
|
||||||
tildes = append(tildes, t.tilde)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|
@ -823,7 +821,7 @@ func (check *Checker) applyTypeFunc(f func(Type) Type, x Type) Type {
|
||||||
// type param is placed in the current package so export/import
|
// type param is placed in the current package so export/import
|
||||||
// works as expected.
|
// works as expected.
|
||||||
tpar := NewTypeName(token.NoPos, check.pkg, "<type parameter>", nil)
|
tpar := NewTypeName(token.NoPos, check.pkg, "<type parameter>", nil)
|
||||||
ptyp := check.NewTypeParam(tpar, NewInterfaceType(nil, []Type{newUnion(rtypes, tildes)})) // assigns type to tpar as a side-effect
|
ptyp := check.NewTypeParam(tpar, NewInterfaceType(nil, []Type{NewUnion(terms)})) // assigns type to tpar as a side-effect
|
||||||
ptyp.index = tp.index
|
ptyp.index = tp.index
|
||||||
|
|
||||||
return ptyp
|
return ptyp
|
||||||
|
|
|
||||||
|
|
@ -394,19 +394,19 @@ func (subst *subster) typeList(in []Type) (out []Type, copied bool) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (subst *subster) termlist(in []*term) (out []*term, copied bool) {
|
func (subst *subster) termlist(in []*Term) (out []*Term, copied bool) {
|
||||||
out = in
|
out = in
|
||||||
for i, t := range in {
|
for i, t := range in {
|
||||||
if u := subst.typ(t.typ); u != t.typ {
|
if u := subst.typ(t.typ); u != t.typ {
|
||||||
if !copied {
|
if !copied {
|
||||||
// first function that got substituted => allocate new out slice
|
// first function that got substituted => allocate new out slice
|
||||||
// and copy all functions
|
// and copy all functions
|
||||||
new := make([]*term, len(in))
|
new := make([]*Term, len(in))
|
||||||
copy(new, out)
|
copy(new, out)
|
||||||
out = new
|
out = new
|
||||||
copied = true
|
copied = true
|
||||||
}
|
}
|
||||||
out[i] = &term{t.tilde, u}
|
out[i] = NewTerm(t.tilde, u)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -357,7 +357,7 @@ func computeUnionTypeSet(check *Checker, pos token.Pos, utyp *Union) *_TypeSet {
|
||||||
// This case is handled during union parsing.
|
// This case is handled during union parsing.
|
||||||
unreachable()
|
unreachable()
|
||||||
default:
|
default:
|
||||||
terms = termlist{t}
|
terms = termlist{(*term)(t)}
|
||||||
}
|
}
|
||||||
// The type set of a union expression is the union
|
// The type set of a union expression is the union
|
||||||
// of the type sets of each term.
|
// of the type sets of each term.
|
||||||
|
|
|
||||||
|
|
@ -131,7 +131,7 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
|
||||||
case *Union:
|
case *Union:
|
||||||
// Unions only appear as (syntactic) embedded elements
|
// Unions only appear as (syntactic) embedded elements
|
||||||
// in interfaces and syntactically cannot be empty.
|
// in interfaces and syntactically cannot be empty.
|
||||||
if t.NumTerms() == 0 {
|
if t.Len() == 0 {
|
||||||
panic("empty union")
|
panic("empty union")
|
||||||
}
|
}
|
||||||
for i, t := range t.terms {
|
for i, t := range t.terms {
|
||||||
|
|
|
||||||
|
|
@ -14,46 +14,46 @@ import (
|
||||||
|
|
||||||
// A Union represents a union of terms embedded in an interface.
|
// A Union represents a union of terms embedded in an interface.
|
||||||
type Union struct {
|
type Union struct {
|
||||||
terms []*term // list of syntactical terms (not a canonicalized termlist)
|
terms []*Term // list of syntactical terms (not a canonicalized termlist)
|
||||||
tset *_TypeSet // type set described by this union, computed lazily
|
tset *_TypeSet // type set described by this union, computed lazily
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUnion returns a new Union type with the given terms (types[i], tilde[i]).
|
// NewUnion returns a new Union type with the given terms.
|
||||||
// The lengths of both arguments must match. It is an error to create an empty
|
// It is an error to create an empty union; they are syntactically not possible.
|
||||||
// union; they are syntactically not possible.
|
func NewUnion(terms []*Term) *Union {
|
||||||
func NewUnion(types []Type, tilde []bool) *Union { return newUnion(types, tilde) }
|
if len(terms) == 0 {
|
||||||
|
panic("empty union")
|
||||||
|
}
|
||||||
|
return &Union{terms, nil}
|
||||||
|
}
|
||||||
|
|
||||||
func (u *Union) IsEmpty() bool { return len(u.terms) == 0 }
|
func (u *Union) Len() int { return len(u.terms) }
|
||||||
func (u *Union) NumTerms() int { return len(u.terms) }
|
func (u *Union) Term(i int) *Term { return u.terms[i] }
|
||||||
func (u *Union) Term(i int) (Type, bool) { t := u.terms[i]; return t.typ, t.tilde }
|
|
||||||
|
|
||||||
func (u *Union) Underlying() Type { return u }
|
func (u *Union) Underlying() Type { return u }
|
||||||
func (u *Union) String() string { return TypeString(u, nil) }
|
func (u *Union) String() string { return TypeString(u, nil) }
|
||||||
|
|
||||||
|
// A Term represents a term in a Union.
|
||||||
|
type Term term
|
||||||
|
|
||||||
|
// NewTerm returns a new union term.
|
||||||
|
func NewTerm(tilde bool, typ Type) *Term { return &Term{tilde, typ} }
|
||||||
|
|
||||||
|
func (t *Term) Tilde() bool { return t.tilde }
|
||||||
|
func (t *Term) Type() Type { return t.typ }
|
||||||
|
func (t *Term) String() string { return (*term)(t).String() }
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Implementation
|
// Implementation
|
||||||
|
|
||||||
func newUnion(types []Type, tilde []bool) *Union {
|
|
||||||
assert(len(types) == len(tilde))
|
|
||||||
if len(types) == 0 {
|
|
||||||
panic("empty union")
|
|
||||||
}
|
|
||||||
t := new(Union)
|
|
||||||
t.terms = make([]*term, len(types))
|
|
||||||
for i, typ := range types {
|
|
||||||
t.terms[i] = &term{tilde[i], typ}
|
|
||||||
}
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseUnion(check *Checker, tlist []ast.Expr) Type {
|
func parseUnion(check *Checker, tlist []ast.Expr) Type {
|
||||||
var terms []*term
|
var terms []*Term
|
||||||
for _, x := range tlist {
|
for _, x := range tlist {
|
||||||
tilde, typ := parseTilde(check, x)
|
tilde, typ := parseTilde(check, x)
|
||||||
if len(tlist) == 1 && !tilde {
|
if len(tlist) == 1 && !tilde {
|
||||||
return typ // single type
|
return typ // single type
|
||||||
}
|
}
|
||||||
terms = append(terms, &term{tilde, typ})
|
terms = append(terms, NewTerm(tilde, typ))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check validity of terms.
|
// Check validity of terms.
|
||||||
|
|
@ -128,7 +128,7 @@ func parseTilde(check *Checker, x ast.Expr) (tilde bool, typ Type) {
|
||||||
// overlappingTerm reports the index of the term x in terms which is
|
// overlappingTerm reports the index of the term x in terms which is
|
||||||
// overlapping (not disjoint) from y. The result is < 0 if there is no
|
// overlapping (not disjoint) from y. The result is < 0 if there is no
|
||||||
// such term.
|
// such term.
|
||||||
func overlappingTerm(terms []*term, y *term) int {
|
func overlappingTerm(terms []*Term, y *Term) int {
|
||||||
for i, x := range terms {
|
for i, x := range terms {
|
||||||
// disjoint requires non-nil, non-top arguments
|
// disjoint requires non-nil, non-top arguments
|
||||||
if debug {
|
if debug {
|
||||||
|
|
@ -136,7 +136,7 @@ func overlappingTerm(terms []*term, y *term) int {
|
||||||
panic("empty or top union term")
|
panic("empty or top union term")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !x.disjoint(y) {
|
if !(*term)(x).disjoint((*term)(y)) {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue