Skip to content

Commit cda461b

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: unifier constructor to accept type parameters and arguments
Change-Id: I2f20cb8f1dd95ba97de7630d0bbe6dee4e019f94 Reviewed-on: https://go-review.googlesource.com/c/go/+/463990 Reviewed-by: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent bd74950 commit cda461b

File tree

4 files changed

+28
-40
lines changed

4 files changed

+28
-40
lines changed

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,7 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
135135
// Unify parameter and argument types for generic parameters with typed arguments
136136
// and collect the indices of generic parameters with untyped arguments.
137137
// Terminology: generic parameter = function parameter with a type-parameterized type
138-
u := newUnifier(tparams)
139-
140-
// Set the type arguments which we know already.
141-
for i, targ := range targs {
142-
if targ != nil {
143-
u.set(tparams[i], targ)
144-
}
145-
}
138+
u := newUnifier(tparams, targs)
146139

147140
errorf := func(kind string, tpar, targ Type, arg *operand) {
148141
// provide a better error message if we can
@@ -462,14 +455,7 @@ func (check *Checker) inferB(tparams []*TypeParam, targs []Type) (types []Type,
462455
}
463456

464457
// Unify type parameters with their constraints.
465-
u := newUnifier(tparams)
466-
467-
// Set the type arguments which we know already.
468-
for i, targ := range targs {
469-
if targ != nil {
470-
u.set(tparams[i], targ)
471-
}
472-
}
458+
u := newUnifier(tparams, targs)
473459

474460
// Repeatedly apply constraint type inference as long as
475461
// there are still unknown type arguments and progress is

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,23 @@ type unifier struct {
6161
depth int // recursion depth during unification
6262
}
6363

64-
// newUnifier returns a new unifier initialized with the given type parameter list.
65-
func newUnifier(tparams []*TypeParam) *unifier {
64+
// newUnifier returns a new unifier initialized with the given type parameter
65+
// and corresponding type argument lists. The type argument list may be shorter
66+
// than the type parameter list, and it may contain nil types. Matching type
67+
// parameters and arguments must have the same index.
68+
func newUnifier(tparams []*TypeParam, targs []Type) *unifier {
69+
assert(len(tparams) >= len(targs))
6670
handles := make(map[*TypeParam]*Type, len(tparams))
6771
// Allocate all handles up-front: in a correct program, all type parameters
6872
// must be resolved and thus eventually will get a handle.
6973
// Also, sharing of handles caused by unified type parameters is rare and
7074
// so it's ok to not optimize for that case (and delay handle allocation).
71-
for _, x := range tparams {
72-
handles[x] = new(Type)
75+
for i, x := range tparams {
76+
var t Type
77+
if i < len(targs) {
78+
t = targs[i]
79+
}
80+
handles[x] = &t
7381
}
7482
return &unifier{tparams, handles, 0}
7583
}

src/go/types/infer.go

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

src/go/types/unify.go

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

0 commit comments

Comments
 (0)