]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: make Checker.renameTParams work on any type
authorRobert Griesemer <gri@golang.org>
Thu, 4 May 2023 00:42:01 +0000 (17:42 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 4 May 2023 16:36:39 +0000 (16:36 +0000)
This permits the rewrite of type parameters in arbitrary types,
not just tuples.

Preparation for fixing #59956.
For #59338.

Change-Id: I9ccaac1f163051cb837cae2208763cafb1d239cb
Reviewed-on: https://go-review.googlesource.com/c/go/+/492515
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/call.go
src/cmd/compile/internal/types2/infer.go
src/go/types/call.go
src/go/types/infer.go

index c6d1590016c253277b0ee4e35e94c0759695579e..c9a6e2f46ed87ccbff857262bc7467d861998011 100644 (file)
@@ -96,10 +96,10 @@ func (check *Checker) funcInst(tsig *Signature, pos syntax.Pos, x *operand, inst
                }
 
                // Rename type parameters to avoid problems with recursive instantiations.
-               // Note that NewTuple(params...) below is nil if len(params) == 0, as desired.
+               // Note that NewTuple(params...) below is (*Tuple)(nil) if len(params) == 0, as desired.
                tparams, params2 := check.renameTParams(pos, sig.TypeParams().list(), NewTuple(params...))
 
-               targs = check.infer(pos, tparams, targs, params2, args)
+               targs = check.infer(pos, tparams, targs, params2.(*Tuple), args)
                if targs == nil {
                        // error was already reported
                        x.mode = invalid
@@ -489,7 +489,9 @@ func (check *Checker) arguments(call *syntax.CallExpr, sig *Signature, targs []T
                        }
                }
                // rename type parameters to avoid problems with recursive calls
-               tparams, sigParams = check.renameTParams(call.Pos(), sig.TypeParams().list(), sigParams)
+               var tmp Type
+               tparams, tmp = check.renameTParams(call.Pos(), sig.TypeParams().list(), sigParams)
+               sigParams = tmp.(*Tuple)
        }
 
        // collect type parameters from generic function arguments
index 9c1022c46fef541d8717d64f8d0e2789ee2360d4..3d313c8ac96422c97317a8884b4f5b0dd421e30d 100644 (file)
@@ -380,11 +380,14 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
        return
 }
 
-// renameTParams renames the type parameters in a function signature described by its
-// type and ordinary parameters (tparams and params) such that each type parameter is
-// given a new identity. renameTParams returns the new type and ordinary parameters.
+// renameTParams renames the type parameters in the given type such that each type
+// parameter is given a new identity. renameTParams returns the new type parameters
+// and updated type. If the result type is unchanged from the argument type, none
+// of the type parameters in tparams occurred in the type.
+// If typ is a generic function, type parameters held with typ are not changed and
+// must be updated separately if desired.
 // The positions is only used for debug traces.
-func (check *Checker) renameTParams(pos syntax.Pos, tparams []*TypeParam, params *Tuple) ([]*TypeParam, *Tuple) {
+func (check *Checker) renameTParams(pos syntax.Pos, tparams []*TypeParam, typ Type) ([]*TypeParam, Type) {
        // For the purpose of type inference we must differentiate type parameters
        // occurring in explicit type or value function arguments from the type
        // parameters we are solving for via unification because they may be the
@@ -413,7 +416,7 @@ func (check *Checker) renameTParams(pos syntax.Pos, tparams []*TypeParam, params
        // Type parameter renaming turns the first example into the second
        // example by renaming the type parameter P into P2.
        if len(tparams) == 0 {
-               return nil, params // nothing to do
+               return nil, typ // nothing to do
        }
 
        tparams2 := make([]*TypeParam, len(tparams))
@@ -428,7 +431,7 @@ func (check *Checker) renameTParams(pos syntax.Pos, tparams []*TypeParam, params
                tparams2[i].bound = check.subst(pos, tparam.bound, renameMap, nil, check.context())
        }
 
-       return tparams2, check.subst(pos, params, renameMap, nil, check.context()).(*Tuple)
+       return tparams2, check.subst(pos, typ, renameMap, nil, check.context())
 }
 
 // typeParamsString produces a string containing all the type parameter names
index f03d9137a65d22a6c2b2017d4fad433cc74eb787..86c2da052255d90a6f8807f209e341aad8661fdd 100644 (file)
@@ -100,10 +100,10 @@ func (check *Checker) funcInst(tsig *Signature, pos token.Pos, x *operand, ix *t
                }
 
                // Rename type parameters to avoid problems with recursive instantiations.
-               // Note that NewTuple(params...) below is nil if len(params) == 0, as desired.
+               // Note that NewTuple(params...) below is (*Tuple)(nil) if len(params) == 0, as desired.
                tparams, params2 := check.renameTParams(pos, sig.TypeParams().list(), NewTuple(params...))
 
-               targs = check.infer(atPos(pos), tparams, targs, params2, args)
+               targs = check.infer(atPos(pos), tparams, targs, params2.(*Tuple), args)
                if targs == nil {
                        // error was already reported
                        x.mode = invalid
@@ -492,7 +492,9 @@ func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type
                        }
                }
                // rename type parameters to avoid problems with recursive calls
-               tparams, sigParams = check.renameTParams(call.Pos(), sig.TypeParams().list(), sigParams)
+               var tmp Type
+               tparams, tmp = check.renameTParams(call.Pos(), sig.TypeParams().list(), sigParams)
+               sigParams = tmp.(*Tuple)
        }
 
        // collect type parameters from generic function arguments
index 39bf4a14f73422c0afc9ec0c8d362904f65aefba..6b90cd6de80f984cae256d9198b8b4681be87cd6 100644 (file)
@@ -382,11 +382,14 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
        return
 }
 
-// renameTParams renames the type parameters in a function signature described by its
-// type and ordinary parameters (tparams and params) such that each type parameter is
-// given a new identity. renameTParams returns the new type and ordinary parameters.
+// renameTParams renames the type parameters in the given type such that each type
+// parameter is given a new identity. renameTParams returns the new type parameters
+// and updated type. If the result type is unchanged from the argument type, none
+// of the type parameters in tparams occurred in the type.
+// If typ is a generic function, type parameters held with typ are not changed and
+// must be updated separately if desired.
 // The positions is only used for debug traces.
-func (check *Checker) renameTParams(pos token.Pos, tparams []*TypeParam, params *Tuple) ([]*TypeParam, *Tuple) {
+func (check *Checker) renameTParams(pos token.Pos, tparams []*TypeParam, typ Type) ([]*TypeParam, Type) {
        // For the purpose of type inference we must differentiate type parameters
        // occurring in explicit type or value function arguments from the type
        // parameters we are solving for via unification because they may be the
@@ -415,7 +418,7 @@ func (check *Checker) renameTParams(pos token.Pos, tparams []*TypeParam, params
        // Type parameter renaming turns the first example into the second
        // example by renaming the type parameter P into P2.
        if len(tparams) == 0 {
-               return nil, params // nothing to do
+               return nil, typ // nothing to do
        }
 
        tparams2 := make([]*TypeParam, len(tparams))
@@ -430,7 +433,7 @@ func (check *Checker) renameTParams(pos token.Pos, tparams []*TypeParam, params
                tparams2[i].bound = check.subst(pos, tparam.bound, renameMap, nil, check.context())
        }
 
-       return tparams2, check.subst(pos, params, renameMap, nil, check.context()).(*Tuple)
+       return tparams2, check.subst(pos, typ, renameMap, nil, check.context())
 }
 
 // typeParamsString produces a string containing all the type parameter names