Skip to content

Commit 2189684

Browse files
committed
internal/typeparams: use NewSignatureType, IsMethodSet, and Context
Update the internal/typeparams API proxy following CL 352615, CL 352616, and CL 353089. Change-Id: Ib65a1876c7820945189e1dc953e1a82e98547a09 Reviewed-on: https://go-review.googlesource.com/c/tools/+/352852 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Go Bot <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent e89823e commit 2189684

File tree

3 files changed

+35
-46
lines changed

3 files changed

+35
-46
lines changed

go/internal/gcimporter/iimport.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,7 @@ func (r *importReader) obj(name string) {
352352
if tag == 'G' {
353353
tparams = r.tparamList()
354354
}
355-
sig := r.signature(nil)
356-
typeparams.SetForSignature(sig, tparams)
355+
sig := r.signature(nil, nil, tparams)
357356
r.declare(types.NewFunc(pos, r.currPkg, name, sig))
358357

359358
case 'T', 'U':
@@ -377,12 +376,12 @@ func (r *importReader) obj(name string) {
377376
mpos := r.pos()
378377
mname := r.ident()
379378
recv := r.param()
380-
msig := r.signature(recv)
381379

382380
// If the receiver has any targs, set those as the
383381
// rparams of the method (since those are the
384382
// typeparams being used in the method sig/body).
385-
targs := typeparams.NamedTypeArgs(baseType(msig.Recv().Type()))
383+
targs := typeparams.NamedTypeArgs(baseType(recv.Type()))
384+
var rparams []*typeparams.TypeParam
386385
if targs.Len() > 0 {
387386
rparams := make([]*typeparams.TypeParam, targs.Len())
388387
for i := range rparams {
@@ -392,8 +391,8 @@ func (r *importReader) obj(name string) {
392391
// library importer stricter.
393392
rparams[i] = targs.At(i).(*typeparams.TypeParam)
394393
}
395-
typeparams.SetRecvTypeParams(msig, rparams)
396394
}
395+
msig := r.signature(recv, rparams, nil)
397396

398397
named.AddMethod(types.NewFunc(mpos, r.currPkg, mname, msig))
399398
}
@@ -653,7 +652,7 @@ func (r *importReader) doType(base *types.Named) types.Type {
653652
return types.NewMap(r.typ(), r.typ())
654653
case signatureType:
655654
r.currPkg = r.pkg()
656-
return r.signature(nil)
655+
return r.signature(nil, nil, nil)
657656

658657
case structType:
659658
r.currPkg = r.pkg()
@@ -693,7 +692,7 @@ func (r *importReader) doType(base *types.Named) types.Type {
693692
recv = types.NewVar(token.NoPos, r.currPkg, "", base)
694693
}
695694

696-
msig := r.signature(recv)
695+
msig := r.signature(recv, nil, nil)
697696
methods[i] = types.NewFunc(mpos, r.currPkg, mname, msig)
698697
}
699698

@@ -750,11 +749,11 @@ func (r *importReader) kind() itag {
750749
return itag(r.uint64())
751750
}
752751

753-
func (r *importReader) signature(recv *types.Var) *types.Signature {
752+
func (r *importReader) signature(recv *types.Var, rparams []*typeparams.TypeParam, tparams []*typeparams.TypeParam) *types.Signature {
754753
params := r.paramList()
755754
results := r.paramList()
756755
variadic := params.Len() > 0 && r.bool()
757-
return types.NewSignature(recv, params, results, variadic)
756+
return typeparams.NewSignatureType(recv, rparams, tparams, params, results, variadic)
758757
}
759758

760759
func (r *importReader) tparamList() []*typeparams.TypeParam {

internal/typeparams/typeparams_go117.go

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,40 +91,35 @@ func SetTypeParamConstraint(tparam *TypeParam, constraint types.Type) {
9191
unsupported()
9292
}
9393

94+
// NewSignatureType calls types.NewSignature, panicking if recvTypeParams or
95+
// typeParams is non-empty.
96+
func NewSignatureType(recv *types.Var, recvTypeParams, typeParams []*TypeParam, params, results *types.Tuple, variadic bool) *types.Signature {
97+
if len(recvTypeParams) != 0 || len(typeParams) != 0 {
98+
panic("signatures cannot have type parameters at this Go version")
99+
}
100+
return types.NewSignature(recv, params, results, variadic)
101+
}
102+
94103
// ForSignature returns an empty slice.
95104
func ForSignature(*types.Signature) *TypeParamList {
96105
return nil
97106
}
98107

99-
// SetForSignature panics if tparams is non-empty.
100-
func SetForSignature(_ *types.Signature, tparams []*TypeParam) {
101-
if len(tparams) > 0 {
102-
unsupported()
103-
}
104-
}
105-
106108
// RecvTypeParams returns a nil slice.
107109
func RecvTypeParams(sig *types.Signature) *TypeParamList {
108110
return nil
109111
}
110112

111-
// SetRecvTypeParams panics if rparams is non-empty.
112-
func SetRecvTypeParams(sig *types.Signature, rparams []*TypeParam) {
113-
if len(rparams) > 0 {
114-
unsupported()
115-
}
116-
}
117-
118113
// IsComparable returns false, as no interfaces are type-restricted at this Go
119114
// version.
120115
func IsComparable(*types.Interface) bool {
121116
return false
122117
}
123118

124-
// IsConstraint returns false, as no interfaces are type-restricted at this Go
119+
// IsMethodSet returns true, as no interfaces are type-restricted at this Go
125120
// version.
126-
func IsConstraint(*types.Interface) bool {
127-
return false
121+
func IsMethodSet(*types.Interface) bool {
122+
return true
128123
}
129124

130125
// ForNamed returns an empty type parameter list, as type parameters are not
@@ -185,12 +180,12 @@ func InitInstanceInfo(*types.Info) {}
185180
// version.
186181
func GetInstance(*types.Info, *ast.Ident) (*TypeList, types.Type) { return nil, nil }
187182

188-
// Environment is a placeholder type, as type parameters are not supported at
183+
// Context is a placeholder type, as type parameters are not supported at
189184
// this Go version.
190-
type Environment struct{}
185+
type Context struct{}
191186

192187
// Instantiate is unsupported on this Go version, and panics.
193-
func Instantiate(env *Environment, typ types.Type, targs []types.Type, validate bool) (types.Type, error) {
188+
func Instantiate(ctxt *Context, typ types.Type, targs []types.Type, validate bool) (types.Type, error) {
194189
unsupported()
195190
return nil, nil
196191
}

internal/typeparams/typeparams_go118.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,34 +96,29 @@ func SetTypeParamConstraint(tparam *TypeParam, constraint types.Type) {
9696
tparam.SetConstraint(constraint)
9797
}
9898

99+
// NewSignatureType calls types.NewSignatureType.
100+
func NewSignatureType(recv *types.Var, recvTypeParams, typeParams []*TypeParam, params, results *types.Tuple, variadic bool) *types.Signature {
101+
return types.NewSignatureType(recv, recvTypeParams, typeParams, params, results, variadic)
102+
}
103+
99104
// ForSignature returns sig.TypeParams()
100105
func ForSignature(sig *types.Signature) *TypeParamList {
101106
return sig.TypeParams()
102107
}
103108

104-
// SetForSignature calls sig.SetTypeParams(tparams)
105-
func SetForSignature(sig *types.Signature, tparams []*TypeParam) {
106-
sig.SetTypeParams(tparams)
107-
}
108-
109109
// RecvTypeParams returns sig.RecvTypeParams().
110110
func RecvTypeParams(sig *types.Signature) *TypeParamList {
111111
return sig.RecvTypeParams()
112112
}
113113

114-
// SetRecvTypeParams calls sig.SetRecvTypeParams(rparams).
115-
func SetRecvTypeParams(sig *types.Signature, rparams []*TypeParam) {
116-
sig.SetRecvTypeParams(rparams)
117-
}
118-
119114
// IsComparable calls iface.IsComparable().
120115
func IsComparable(iface *types.Interface) bool {
121116
return iface.IsComparable()
122117
}
123118

124-
// IsConstraint calls iface.IsConstraint().
125-
func IsConstraint(iface *types.Interface) bool {
126-
return iface.IsConstraint()
119+
// IsMethodSet calls iface.IsMethodSet().
120+
func IsMethodSet(iface *types.Interface) bool {
121+
return iface.IsMethodSet()
127122
}
128123

129124
// ForNamed extracts the (possibly empty) type parameter object list from
@@ -181,10 +176,10 @@ func GetInstance(info *types.Info, id *ast.Ident) (*TypeList, types.Type) {
181176
return nil, nil
182177
}
183178

184-
// Environment is an alias for types.Environment.
185-
type Environment = types.Environment
179+
// Context is an alias for types.Context.
180+
type Context = types.Context
186181

187182
// Instantiate calls types.Instantiate.
188-
func Instantiate(env *Environment, typ types.Type, targs []types.Type, validate bool) (types.Type, error) {
189-
return types.Instantiate(env, typ, targs, validate)
183+
func Instantiate(ctxt *Context, typ types.Type, targs []types.Type, validate bool) (types.Type, error) {
184+
return types.Instantiate(ctxt, typ, targs, validate)
190185
}

0 commit comments

Comments
 (0)