Skip to content

Commit

Permalink
go/types, types2: make Checker.renameTParams work on any type
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
Run-TryBot: Robert Griesemer <[email protected]>
Auto-Submit: Robert Griesemer <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
  • Loading branch information
griesemer authored and gopherbot committed May 4, 2023
1 parent 2c49bf8 commit 8dea635
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
8 changes: 5 additions & 3 deletions src/cmd/compile/internal/types2/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions src/cmd/compile/internal/types2/infer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/go/types/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions src/go/types/infer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8dea635

Please sign in to comment.