aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/types2/builtins.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2021-05-25 17:49:32 -0700
committerRobert Griesemer <gri@golang.org>2021-06-02 20:31:01 +0000
commit589e32dbdf89484d620c635a966c736085cae5c4 (patch)
tree75e1633438a390fa4dab5a4f010e813441636d5c /src/cmd/compile/internal/types2/builtins.go
parent7b876def6c4936cfae774d3007f8265876a9fbf7 (diff)
downloadgo-589e32dbdf89484d620c635a966c736085cae5c4.tar.gz
go-589e32dbdf89484d620c635a966c736085cae5c4.zip
[dev.typeparams] cmd/compile/internal/types2: replace Sum type with Union type
- We still mostly ignore the tilde information. - More consistent naming: A Union term is the pair (type, tilde). Rename Union.terms to Union.types; the Union.types and Union.tilde slices make up the Union terms. - Replace Sum.is with Union.underIs: underIs iterates through all union terms and calls its argument function with the underlying type of the term (and thus can ignore the tilde information). This also eliminates the need to call under in the argument function. - Added Union.is for situations where we need to consider the tilde information for each Union term. Change-Id: I70fcf1813e072651dc0f61d52d5555642ee762fd Reviewed-on: https://go-review.googlesource.com/c/go/+/323274 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'src/cmd/compile/internal/types2/builtins.go')
-rw-r--r--src/cmd/compile/internal/types2/builtins.go16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/cmd/compile/internal/types2/builtins.go b/src/cmd/compile/internal/types2/builtins.go
index 94fb506d80..1779e32c5c 100644
--- a/src/cmd/compile/internal/types2/builtins.go
+++ b/src/cmd/compile/internal/types2/builtins.go
@@ -178,9 +178,9 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
mode = value
}
- case *Sum:
- if t.is(func(t Type) bool {
- switch t := under(t).(type) {
+ case *Union:
+ if t.underIs(func(t Type) bool {
+ switch t := t.(type) {
case *Basic:
if isString(t) && id == _Len {
return true
@@ -460,8 +460,8 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
m = 2
case *Map, *Chan:
m = 1
- case *Sum:
- return t.is(valid)
+ case *Union:
+ return t.underIs(valid)
default:
return false
}
@@ -749,10 +749,14 @@ func (check *Checker) applyTypeFunc(f func(Type) Type, x Type) Type {
if tp := asTypeParam(x); tp != nil {
// Test if t satisfies the requirements for the argument
// type and collect possible result types at the same time.
+ // TODO(gri) This needs to consider the ~ information if we
+ // have a union type.
var rtypes []Type
+ var tilde []bool
if !tp.Bound().is(func(x Type) bool {
if r := f(x); r != nil {
rtypes = append(rtypes, r)
+ tilde = append(tilde, true) // for now - see TODO above
return true
}
return false
@@ -768,7 +772,7 @@ func (check *Checker) applyTypeFunc(f func(Type) Type, x Type) Type {
// construct a suitable new type parameter
tpar := NewTypeName(nopos, nil /* = Universe pkg */, "<type parameter>", nil)
ptyp := check.NewTypeParam(tpar, 0, &emptyInterface) // assigns type to tpar as a side-effect
- tsum := NewSum(rtypes)
+ tsum := newUnion(rtypes, tilde)
ptyp.bound = &Interface{allMethods: markComplete, allTypes: tsum}
return ptyp