]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: shorter list for 2nd phase of function type inference
authorRobert Griesemer <gri@golang.org>
Fri, 28 Jan 2022 02:55:29 +0000 (18:55 -0800)
committerRobert Griesemer <gri@golang.org>
Thu, 3 Feb 2022 16:13:58 +0000 (16:13 +0000)
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 <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/infer.go
src/go/types/infer.go

index 51d0d2214426d36e3ee76bde1b71b2ce10eed209..51b26eb2aa25b5c12a656d7b1160c01ea73a38b1 100644 (file)
@@ -179,7 +179,7 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
                        if arg.mode == invalid {
                                // An error was reported earlier. Ignore this targ
                                // and continue, we may still be able to infer all
-                               // targs resulting in fewer follon-on errors.
+                               // targs resulting in fewer follow-on errors.
                                continue
                        }
                        if targ := arg.typ; isTyped(targ) {
@@ -190,7 +190,12 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
                                        errorf("type", par.typ, targ, arg)
                                        return nil
                                }
-                       } else {
+                       } else if _, ok := par.typ.(*TypeParam); ok {
+                               // Since default types are all basic (i.e., non-composite) types, an
+                               // untyped argument will never match a composite parameter type; the
+                               // only parameter type it can possibly match against is a *TypeParam.
+                               // Thus, for untyped arguments we only need to look at parameter types
+                               // that are single type parameters.
                                indices = append(indices, i)
                        }
                }
@@ -219,20 +224,17 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
        // Some generic parameters with untyped arguments may have been given
        // a type by now, we can ignore them.
        for _, i := range indices {
-               par := params.At(i)
-               // Since untyped types are all basic (i.e., non-composite) types, an
-               // untyped argument will never match a composite parameter type; the
-               // only parameter type it can possibly match against is a *TypeParam.
-               // Thus, only consider untyped arguments for generic parameters that
-               // are not of composite types and which don't have a type inferred yet.
-               if tpar, _ := par.typ.(*TypeParam); tpar != nil && targs[tpar.index] == nil {
+               tpar := params.At(i).typ.(*TypeParam) // is type parameter by construction of indices
+               // Only consider untyped arguments for which the corresponding type
+               // parameter doesn't have an inferred type yet.
+               if targs[tpar.index] == nil {
                        arg := args[i]
                        targ := Default(arg.typ)
                        // The default type for an untyped nil is untyped nil. We must not
                        // infer an untyped nil type as type parameter type. Ignore untyped
                        // nil by making sure all default argument types are typed.
-                       if isTyped(targ) && !u.unify(par.typ, targ) {
-                               errorf("default type", par.typ, targ, arg)
+                       if isTyped(targ) && !u.unify(tpar, targ) {
+                               errorf("default type", tpar, targ, arg)
                                return nil
                        }
                }
index 2678da3bf5ea6d994c683de877110e8ca300d587..6a9a66256588247fd12c4915df8c3eb27bc8565f 100644 (file)
@@ -183,7 +183,7 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
                        if arg.mode == invalid {
                                // An error was reported earlier. Ignore this targ
                                // and continue, we may still be able to infer all
-                               // targs resulting in fewer follon-on errors.
+                               // targs resulting in fewer follow-on errors.
                                continue
                        }
                        if targ := arg.typ; isTyped(targ) {
@@ -194,7 +194,12 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
                                        errorf("type", par.typ, targ, arg)
                                        return nil
                                }
-                       } else {
+                       } else if _, ok := par.typ.(*TypeParam); ok {
+                               // Since default types are all basic (i.e., non-composite) types, an
+                               // untyped argument will never match a composite parameter type; the
+                               // only parameter type it can possibly match against is a *TypeParam.
+                               // Thus, for untyped arguments we only need to look at parameter types
+                               // that are single type parameters.
                                indices = append(indices, i)
                        }
                }
@@ -221,20 +226,17 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
        // Some generic parameters with untyped arguments may have been given
        // a type by now, we can ignore them.
        for _, i := range indices {
-               par := params.At(i)
-               // Since untyped types are all basic (i.e., non-composite) types, an
-               // untyped argument will never match a composite parameter type; the
-               // only parameter type it can possibly match against is a *TypeParam.
-               // Thus, only consider untyped arguments for generic parameters that
-               // are not of composite types and which don't have a type inferred yet.
-               if tpar, _ := par.typ.(*TypeParam); tpar != nil && targs[tpar.index] == nil {
+               tpar := params.At(i).typ.(*TypeParam) // is type parameter by construction of indices
+               // Only consider untyped arguments for which the corresponding type
+               // parameter doesn't have an inferred type yet.
+               if targs[tpar.index] == nil {
                        arg := args[i]
                        targ := Default(arg.typ)
                        // The default type for an untyped nil is untyped nil. We must not
                        // infer an untyped nil type as type parameter type. Ignore untyped
                        // nil by making sure all default argument types are typed.
-                       if isTyped(targ) && !u.unify(par.typ, targ) {
-                               errorf("default type", par.typ, targ, arg)
+                       if isTyped(targ) && !u.unify(tpar, targ) {
+                               errorf("default type", tpar, targ, arg)
                                return nil
                        }
                }