Skip to content

Commit b004470

Browse files
committed
go/types, types2: shorter list for 2nd phase of function type inference
In the 2nd phase of function argument type inference we only consider parameters with types that are single type parameters. Thus there is no need to collect anything else in the first phase. This matches the algorithm description in the forthcoming spec more closely. Change-Id: Ie5c29f30ff43b1e37d719ecbe1688b50ed2177f3 Reviewed-on: https://go-review.googlesource.com/c/go/+/381554 Trust: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 475ce82 commit b004470

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
179179
if arg.mode == invalid {
180180
// An error was reported earlier. Ignore this targ
181181
// and continue, we may still be able to infer all
182-
// targs resulting in fewer follon-on errors.
182+
// targs resulting in fewer follow-on errors.
183183
continue
184184
}
185185
if targ := arg.typ; isTyped(targ) {
@@ -190,7 +190,12 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
190190
errorf("type", par.typ, targ, arg)
191191
return nil
192192
}
193-
} else {
193+
} else if _, ok := par.typ.(*TypeParam); ok {
194+
// Since default types are all basic (i.e., non-composite) types, an
195+
// untyped argument will never match a composite parameter type; the
196+
// only parameter type it can possibly match against is a *TypeParam.
197+
// Thus, for untyped arguments we only need to look at parameter types
198+
// that are single type parameters.
194199
indices = append(indices, i)
195200
}
196201
}
@@ -219,20 +224,17 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
219224
// Some generic parameters with untyped arguments may have been given
220225
// a type by now, we can ignore them.
221226
for _, i := range indices {
222-
par := params.At(i)
223-
// Since untyped types are all basic (i.e., non-composite) types, an
224-
// untyped argument will never match a composite parameter type; the
225-
// only parameter type it can possibly match against is a *TypeParam.
226-
// Thus, only consider untyped arguments for generic parameters that
227-
// are not of composite types and which don't have a type inferred yet.
228-
if tpar, _ := par.typ.(*TypeParam); tpar != nil && targs[tpar.index] == nil {
227+
tpar := params.At(i).typ.(*TypeParam) // is type parameter by construction of indices
228+
// Only consider untyped arguments for which the corresponding type
229+
// parameter doesn't have an inferred type yet.
230+
if targs[tpar.index] == nil {
229231
arg := args[i]
230232
targ := Default(arg.typ)
231233
// The default type for an untyped nil is untyped nil. We must not
232234
// infer an untyped nil type as type parameter type. Ignore untyped
233235
// nil by making sure all default argument types are typed.
234-
if isTyped(targ) && !u.unify(par.typ, targ) {
235-
errorf("default type", par.typ, targ, arg)
236+
if isTyped(targ) && !u.unify(tpar, targ) {
237+
errorf("default type", tpar, targ, arg)
236238
return nil
237239
}
238240
}

src/go/types/infer.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
183183
if arg.mode == invalid {
184184
// An error was reported earlier. Ignore this targ
185185
// and continue, we may still be able to infer all
186-
// targs resulting in fewer follon-on errors.
186+
// targs resulting in fewer follow-on errors.
187187
continue
188188
}
189189
if targ := arg.typ; isTyped(targ) {
@@ -194,7 +194,12 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
194194
errorf("type", par.typ, targ, arg)
195195
return nil
196196
}
197-
} else {
197+
} else if _, ok := par.typ.(*TypeParam); ok {
198+
// Since default types are all basic (i.e., non-composite) types, an
199+
// untyped argument will never match a composite parameter type; the
200+
// only parameter type it can possibly match against is a *TypeParam.
201+
// Thus, for untyped arguments we only need to look at parameter types
202+
// that are single type parameters.
198203
indices = append(indices, i)
199204
}
200205
}
@@ -221,20 +226,17 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
221226
// Some generic parameters with untyped arguments may have been given
222227
// a type by now, we can ignore them.
223228
for _, i := range indices {
224-
par := params.At(i)
225-
// Since untyped types are all basic (i.e., non-composite) types, an
226-
// untyped argument will never match a composite parameter type; the
227-
// only parameter type it can possibly match against is a *TypeParam.
228-
// Thus, only consider untyped arguments for generic parameters that
229-
// are not of composite types and which don't have a type inferred yet.
230-
if tpar, _ := par.typ.(*TypeParam); tpar != nil && targs[tpar.index] == nil {
229+
tpar := params.At(i).typ.(*TypeParam) // is type parameter by construction of indices
230+
// Only consider untyped arguments for which the corresponding type
231+
// parameter doesn't have an inferred type yet.
232+
if targs[tpar.index] == nil {
231233
arg := args[i]
232234
targ := Default(arg.typ)
233235
// The default type for an untyped nil is untyped nil. We must not
234236
// infer an untyped nil type as type parameter type. Ignore untyped
235237
// nil by making sure all default argument types are typed.
236-
if isTyped(targ) && !u.unify(par.typ, targ) {
237-
errorf("default type", par.typ, targ, arg)
238+
if isTyped(targ) && !u.unify(tpar, targ) {
239+
errorf("default type", tpar, targ, arg)
238240
return nil
239241
}
240242
}

0 commit comments

Comments
 (0)