Skip to content

Commit 8dea635

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: make Checker.renameTParams work on any type
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]>
1 parent 2c49bf8 commit 8dea635

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ func (check *Checker) funcInst(tsig *Signature, pos syntax.Pos, x *operand, inst
9696
}
9797

9898
// Rename type parameters to avoid problems with recursive instantiations.
99-
// Note that NewTuple(params...) below is nil if len(params) == 0, as desired.
99+
// Note that NewTuple(params...) below is (*Tuple)(nil) if len(params) == 0, as desired.
100100
tparams, params2 := check.renameTParams(pos, sig.TypeParams().list(), NewTuple(params...))
101101

102-
targs = check.infer(pos, tparams, targs, params2, args)
102+
targs = check.infer(pos, tparams, targs, params2.(*Tuple), args)
103103
if targs == nil {
104104
// error was already reported
105105
x.mode = invalid
@@ -489,7 +489,9 @@ func (check *Checker) arguments(call *syntax.CallExpr, sig *Signature, targs []T
489489
}
490490
}
491491
// rename type parameters to avoid problems with recursive calls
492-
tparams, sigParams = check.renameTParams(call.Pos(), sig.TypeParams().list(), sigParams)
492+
var tmp Type
493+
tparams, tmp = check.renameTParams(call.Pos(), sig.TypeParams().list(), sigParams)
494+
sigParams = tmp.(*Tuple)
493495
}
494496

495497
// collect type parameters from generic function arguments

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,14 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
380380
return
381381
}
382382

383-
// renameTParams renames the type parameters in a function signature described by its
384-
// type and ordinary parameters (tparams and params) such that each type parameter is
385-
// given a new identity. renameTParams returns the new type and ordinary parameters.
383+
// renameTParams renames the type parameters in the given type such that each type
384+
// parameter is given a new identity. renameTParams returns the new type parameters
385+
// and updated type. If the result type is unchanged from the argument type, none
386+
// of the type parameters in tparams occurred in the type.
387+
// If typ is a generic function, type parameters held with typ are not changed and
388+
// must be updated separately if desired.
386389
// The positions is only used for debug traces.
387-
func (check *Checker) renameTParams(pos syntax.Pos, tparams []*TypeParam, params *Tuple) ([]*TypeParam, *Tuple) {
390+
func (check *Checker) renameTParams(pos syntax.Pos, tparams []*TypeParam, typ Type) ([]*TypeParam, Type) {
388391
// For the purpose of type inference we must differentiate type parameters
389392
// occurring in explicit type or value function arguments from the type
390393
// 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
413416
// Type parameter renaming turns the first example into the second
414417
// example by renaming the type parameter P into P2.
415418
if len(tparams) == 0 {
416-
return nil, params // nothing to do
419+
return nil, typ // nothing to do
417420
}
418421

419422
tparams2 := make([]*TypeParam, len(tparams))
@@ -428,7 +431,7 @@ func (check *Checker) renameTParams(pos syntax.Pos, tparams []*TypeParam, params
428431
tparams2[i].bound = check.subst(pos, tparam.bound, renameMap, nil, check.context())
429432
}
430433

431-
return tparams2, check.subst(pos, params, renameMap, nil, check.context()).(*Tuple)
434+
return tparams2, check.subst(pos, typ, renameMap, nil, check.context())
432435
}
433436

434437
// typeParamsString produces a string containing all the type parameter names

src/go/types/call.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ func (check *Checker) funcInst(tsig *Signature, pos token.Pos, x *operand, ix *t
100100
}
101101

102102
// Rename type parameters to avoid problems with recursive instantiations.
103-
// Note that NewTuple(params...) below is nil if len(params) == 0, as desired.
103+
// Note that NewTuple(params...) below is (*Tuple)(nil) if len(params) == 0, as desired.
104104
tparams, params2 := check.renameTParams(pos, sig.TypeParams().list(), NewTuple(params...))
105105

106-
targs = check.infer(atPos(pos), tparams, targs, params2, args)
106+
targs = check.infer(atPos(pos), tparams, targs, params2.(*Tuple), args)
107107
if targs == nil {
108108
// error was already reported
109109
x.mode = invalid
@@ -492,7 +492,9 @@ func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type
492492
}
493493
}
494494
// rename type parameters to avoid problems with recursive calls
495-
tparams, sigParams = check.renameTParams(call.Pos(), sig.TypeParams().list(), sigParams)
495+
var tmp Type
496+
tparams, tmp = check.renameTParams(call.Pos(), sig.TypeParams().list(), sigParams)
497+
sigParams = tmp.(*Tuple)
496498
}
497499

498500
// collect type parameters from generic function arguments

src/go/types/infer.go

+9-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)