Skip to content

Commit b98b8b9

Browse files
committed
[dev.typeparams] go/types: remove unused *Checker arguments (cleanup)
This is a straightforward port of CL 331512 to go/types. API usage in methodset.go was also updated. Change-Id: I6701265c9d2ae40eb9aa0ea5f00c98ce3516edab Reviewed-on: https://go-review.googlesource.com/c/go/+/335009 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
1 parent fce6290 commit b98b8b9

File tree

13 files changed

+59
-86
lines changed

13 files changed

+59
-86
lines changed

src/go/types/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,11 @@ func Implements(V Type, T *Interface) bool {
422422
// Identical reports whether x and y are identical types.
423423
// Receivers of Signature types are ignored.
424424
func Identical(x, y Type) bool {
425-
return (*Checker)(nil).identical(x, y)
425+
return identical(x, y, true, nil)
426426
}
427427

428428
// IdenticalIgnoreTags reports whether x and y are identical types if tags are ignored.
429429
// Receivers of Signature types are ignored.
430430
func IdenticalIgnoreTags(x, y Type) bool {
431-
return (*Checker)(nil).identicalIgnoreTags(x, y)
431+
return identical(x, y, false, nil)
432432
}

src/go/types/builtins.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
286286
}
287287

288288
// both argument types must be identical
289-
if !check.identical(x.typ, y.typ) {
289+
if !Identical(x.typ, y.typ) {
290290
check.invalidArg(x, _InvalidComplex, "mismatched types %s and %s", x.typ, y.typ)
291291
return
292292
}
@@ -351,7 +351,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
351351
return
352352
}
353353

354-
if !check.identical(dst, src) {
354+
if !Identical(dst, src) {
355355
check.invalidArg(x, _InvalidCopy, "arguments to copy %s and %s have different element types %s and %s", x, &y, dst, src)
356356
return
357357
}
@@ -644,7 +644,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
644644

645645
base := derefStructPtr(x.typ)
646646
sel := selx.Sel.Name
647-
obj, index, indirect := check.lookupFieldOrMethod(base, false, check.pkg, sel)
647+
obj, index, indirect := LookupFieldOrMethod(base, false, check.pkg, sel)
648648
switch obj.(type) {
649649
case nil:
650650
check.invalidArg(x, _MissingFieldOrMethod, "%s has no single field %s", base, sel)

src/go/types/call.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
470470

471471
check.instantiatedOperand(x)
472472

473-
obj, index, indirect = check.lookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel)
473+
obj, index, indirect = LookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel)
474474
if obj == nil {
475475
switch {
476476
case index != nil:
@@ -500,7 +500,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
500500
} else {
501501
changeCase = string(unicode.ToUpper(r)) + sel[1:]
502502
}
503-
if obj, _, _ = check.lookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, changeCase); obj != nil {
503+
if obj, _, _ = LookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, changeCase); obj != nil {
504504
why += ", but does have " + changeCase
505505
}
506506
}

src/go/types/conversions.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ func (x *operand) convertibleTo(check *Checker, T Type, reason *string) bool {
9494
V := x.typ
9595
Vu := under(V)
9696
Tu := under(T)
97-
if check.identicalIgnoreTags(Vu, Tu) {
97+
if IdenticalIgnoreTags(Vu, Tu) {
9898
return true
9999
}
100100

101101
// "x's type and T are unnamed pointer types and their pointer base types
102102
// have identical underlying types if tags are ignored"
103103
if V, ok := V.(*Pointer); ok {
104104
if T, ok := T.(*Pointer); ok {
105-
if check.identicalIgnoreTags(under(V.base), under(T.base)) {
105+
if IdenticalIgnoreTags(under(V.base), under(T.base)) {
106106
return true
107107
}
108108
}
@@ -143,7 +143,7 @@ func (x *operand) convertibleTo(check *Checker, T Type, reason *string) bool {
143143
if s := asSlice(V); s != nil {
144144
if p := asPointer(T); p != nil {
145145
if a := asArray(p.Elem()); a != nil {
146-
if check.identical(s.Elem(), a.Elem()) {
146+
if Identical(s.Elem(), a.Elem()) {
147147
if check == nil || check.allowVersion(check.pkg, 1, 17) {
148148
return true
149149
}

src/go/types/expr.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ func (check *Checker) binary(x *operand, e ast.Expr, lhs, rhs ast.Expr, op token
955955
return
956956
}
957957

958-
if !check.identical(x.typ, y.typ) {
958+
if !Identical(x.typ, y.typ) {
959959
// only report an error if we have valid types
960960
// (otherwise we had an error reported elsewhere already)
961961
if x.typ != Typ[Invalid] && y.typ != Typ[Invalid] {
@@ -1281,7 +1281,7 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
12811281
xkey := keyVal(x.val)
12821282
if asInterface(utyp.key) != nil {
12831283
for _, vtyp := range visited[xkey] {
1284-
if check.identical(vtyp, x.typ) {
1284+
if Identical(vtyp, x.typ) {
12851285
duplicate = true
12861286
break
12871287
}
@@ -1468,7 +1468,7 @@ func (check *Checker) typeAssertion(at positioner, x *operand, xtyp *Interface,
14681468
}
14691469
var msg string
14701470
if wrongType != nil {
1471-
if check.identical(method.typ, wrongType.typ) {
1471+
if Identical(method.typ, wrongType.typ) {
14721472
msg = fmt.Sprintf("missing method %s (%s has pointer receiver)", method.name, method.name)
14731473
} else {
14741474
msg = fmt.Sprintf("wrong type for method %s (have %s, want %s)", method.name, wrongType.typ, method.typ)

src/go/types/infer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (check *Checker) infer(posn positioner, tparams []*TypeName, targs []Type,
9393
// Unify parameter and argument types for generic parameters with typed arguments
9494
// and collect the indices of generic parameters with untyped arguments.
9595
// Terminology: generic parameter = function parameter with a type-parameterized type
96-
u := newUnifier(check, false)
96+
u := newUnifier(false)
9797
u.x.init(tparams)
9898

9999
// Set the type arguments which we know already.
@@ -369,7 +369,7 @@ func (check *Checker) inferB(tparams []*TypeName, targs []Type, report bool) (ty
369369

370370
// Setup bidirectional unification between those structural bounds
371371
// and the corresponding type arguments (which may be nil!).
372-
u := newUnifier(check, false)
372+
u := newUnifier(false)
373373
u.x.init(tparams)
374374
u.y = u.x // type parameters between LHS and RHS of unification are identical
375375

src/go/types/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func newTypeSet(check *Checker, pos token.Pos, ityp *Interface) *TypeSet {
212212
}
213213
// check != nil
214214
check.later(func() {
215-
if !check.allowVersion(m.pkg, 1, 14) || !check.identical(m.typ, other.Type()) {
215+
if !check.allowVersion(m.pkg, 1, 14) || !Identical(m.typ, other.Type()) {
216216
check.errorf(atPos(pos), _DuplicateDecl, "duplicate method %s", m.name)
217217
check.errorf(atPos(mpos[other.(*Func)]), _DuplicateDecl, "\tother declaration of %s", m.name) // secondary error, \t indented
218218
}

src/go/types/lookup.go

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ package types
88

99
import "go/token"
1010

11+
// Internal use of LookupFieldOrMethod: If the obj result is a method
12+
// associated with a concrete (non-interface) type, the method's signature
13+
// may not be fully set up. Call Checker.objDecl(obj, nil) before accessing
14+
// the method's type.
15+
1116
// LookupFieldOrMethod looks up a field or method with given package and name
1217
// in T and returns the corresponding *Var or *Func, an index sequence, and a
1318
// bool indicating if there were any pointer indirections on the path to the
@@ -35,19 +40,6 @@ import "go/token"
3540
// the method's formal receiver base type, nor was the receiver addressable.
3641
//
3742
func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
38-
return (*Checker)(nil).lookupFieldOrMethod(T, addressable, pkg, name)
39-
}
40-
41-
// Internal use of Checker.lookupFieldOrMethod: If the obj result is a method
42-
// associated with a concrete (non-interface) type, the method's signature
43-
// may not be fully set up. Call Checker.objDecl(obj, nil) before accessing
44-
// the method's type.
45-
// TODO(gri) Now that we provide the *Checker, we can probably remove this
46-
// caveat by calling Checker.objDecl from lookupFieldOrMethod. Investigate.
47-
48-
// lookupFieldOrMethod is like the external version but completes interfaces
49-
// as necessary.
50-
func (check *Checker) lookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
5143
// Methods cannot be associated to a named pointer type
5244
// (spec: "The type denoted by T is called the receiver base type;
5345
// it must not be a pointer or interface type and it must be declared
@@ -57,26 +49,25 @@ func (check *Checker) lookupFieldOrMethod(T Type, addressable bool, pkg *Package
5749
// not have found it for T (see also issue 8590).
5850
if t := asNamed(T); t != nil {
5951
if p, _ := t.Underlying().(*Pointer); p != nil {
60-
obj, index, indirect = check.rawLookupFieldOrMethod(p, false, pkg, name)
52+
obj, index, indirect = lookupFieldOrMethod(p, false, pkg, name)
6153
if _, ok := obj.(*Func); ok {
6254
return nil, nil, false
6355
}
6456
return
6557
}
6658
}
6759

68-
return check.rawLookupFieldOrMethod(T, addressable, pkg, name)
60+
return lookupFieldOrMethod(T, addressable, pkg, name)
6961
}
7062

7163
// TODO(gri) The named type consolidation and seen maps below must be
7264
// indexed by unique keys for a given type. Verify that named
7365
// types always have only one representation (even when imported
7466
// indirectly via different packages.)
7567

76-
// rawLookupFieldOrMethod should only be called by lookupFieldOrMethod and missingMethod.
77-
func (check *Checker) rawLookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
68+
// lookupFieldOrMethod should only be called by LookupFieldOrMethod and missingMethod.
69+
func lookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
7870
// WARNING: The code in this function is extremely subtle - do not modify casually!
79-
// This function and NewMethodSet should be kept in sync.
8071

8172
if name == "_" {
8273
return // blank fields/methods are never found
@@ -226,7 +217,7 @@ func (check *Checker) rawLookupFieldOrMethod(T Type, addressable bool, pkg *Pack
226217
return
227218
}
228219

229-
current = check.consolidateMultiples(next)
220+
current = consolidateMultiples(next)
230221
}
231222

232223
return nil, nil, false // not found
@@ -243,15 +234,15 @@ type embeddedType struct {
243234
// consolidateMultiples collects multiple list entries with the same type
244235
// into a single entry marked as containing multiples. The result is the
245236
// consolidated list.
246-
func (check *Checker) consolidateMultiples(list []embeddedType) []embeddedType {
237+
func consolidateMultiples(list []embeddedType) []embeddedType {
247238
if len(list) <= 1 {
248239
return list // at most one entry - nothing to do
249240
}
250241

251242
n := 0 // number of entries w/ unique type
252243
prev := make(map[Type]int) // index at which type was previously seen
253244
for _, e := range list {
254-
if i, found := check.lookupType(prev, e.typ); found {
245+
if i, found := lookupType(prev, e.typ); found {
255246
list[i].multiples = true
256247
// ignore this entry
257248
} else {
@@ -263,14 +254,14 @@ func (check *Checker) consolidateMultiples(list []embeddedType) []embeddedType {
263254
return list[:n]
264255
}
265256

266-
func (check *Checker) lookupType(m map[Type]int, typ Type) (int, bool) {
257+
func lookupType(m map[Type]int, typ Type) (int, bool) {
267258
// fast path: maybe the types are equal
268259
if i, found := m[typ]; found {
269260
return i, true
270261
}
271262

272263
for t, i := range m {
273-
if check.identical(t, typ) {
264+
if Identical(t, typ) {
274265
return i, true
275266
}
276267
}
@@ -336,7 +327,7 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
336327
// to see if they can be made to match.
337328
// TODO(gri) is this always correct? what about type bounds?
338329
// (Alternative is to rename/subst type parameters and compare.)
339-
u := newUnifier(check, true)
330+
u := newUnifier(true)
340331
u.x.init(ftyp.tparams)
341332
if !u.unify(ftyp, mtyp) {
342333
return m, f
@@ -351,12 +342,12 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
351342
Vn := asNamed(Vd)
352343
for _, m := range T.typeSet().methods {
353344
// TODO(gri) should this be calling lookupFieldOrMethod instead (and why not)?
354-
obj, _, _ := check.rawLookupFieldOrMethod(V, false, m.pkg, m.name)
345+
obj, _, _ := lookupFieldOrMethod(V, false, m.pkg, m.name)
355346

356347
// Check if *V implements this method of T.
357348
if obj == nil {
358349
ptr := NewPointer(V)
359-
obj, _, _ = check.rawLookupFieldOrMethod(ptr, false, m.pkg, m.name)
350+
obj, _, _ = lookupFieldOrMethod(ptr, false, m.pkg, m.name)
360351
if obj != nil {
361352
return m, obj.(*Func)
362353
}
@@ -412,7 +403,7 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
412403
// to see if they can be made to match.
413404
// TODO(gri) is this always correct? what about type bounds?
414405
// (Alternative is to rename/subst type parameters and compare.)
415-
u := newUnifier(check, true)
406+
u := newUnifier(true)
416407
u.x.init(ftyp.rparams)
417408
if !u.unify(ftyp, mtyp) {
418409
return m, f

src/go/types/methodset.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,7 @@ func NewMethodSet(T Type) *MethodSet {
190190
}
191191
}
192192

193-
// It's ok to call consolidateMultiples with a nil *Checker because
194-
// MethodSets are not used internally (outside debug mode). When used
195-
// externally, interfaces are expected to be completed and then we do
196-
// not need a *Checker to complete them when (indirectly) calling
197-
// Checker.identical via consolidateMultiples.
198-
current = (*Checker)(nil).consolidateMultiples(next)
193+
current = consolidateMultiples(next)
199194
}
200195

201196
if len(base) == 0 {

src/go/types/operand.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func (x *operand) assignableTo(check *Checker, T Type, reason *string) (bool, er
240240
}
241241

242242
// x's type is identical to T
243-
if check.identical(V, T) {
243+
if Identical(V, T) {
244244
return true, 0
245245
}
246246

@@ -272,7 +272,7 @@ func (x *operand) assignableTo(check *Checker, T Type, reason *string) (bool, er
272272

273273
// x's type V and T have identical underlying types
274274
// and at least one of V or T is not a named type
275-
if check.identical(Vu, Tu) && (!isNamed(V) || !isNamed(T)) {
275+
if Identical(Vu, Tu) && (!isNamed(V) || !isNamed(T)) {
276276
return true, 0
277277
}
278278

@@ -281,7 +281,7 @@ func (x *operand) assignableTo(check *Checker, T Type, reason *string) (bool, er
281281
if m, wrongType := check.missingMethod(V, Ti, true); m != nil /* Implements(V, Ti) */ {
282282
if reason != nil {
283283
if wrongType != nil {
284-
if check.identical(m.typ, wrongType.typ) {
284+
if Identical(m.typ, wrongType.typ) {
285285
*reason = fmt.Sprintf("missing method %s (%s has pointer receiver)", m.name, m.name)
286286
} else {
287287
*reason = fmt.Sprintf("wrong type for method %s (have %s, want %s)", m.Name(), wrongType.typ, m.typ)
@@ -300,7 +300,7 @@ func (x *operand) assignableTo(check *Checker, T Type, reason *string) (bool, er
300300
// type, x's type V and T have identical element types,
301301
// and at least one of V or T is not a named type
302302
if Vc, ok := Vu.(*Chan); ok && Vc.dir == SendRecv {
303-
if Tc, ok := Tu.(*Chan); ok && check.identical(Vc.elem, Tc.elem) {
303+
if Tc, ok := Tu.(*Chan); ok && Identical(Vc.elem, Tc.elem) {
304304
return !isNamed(V) || !isNamed(T), _InvalidChanAssign
305305
}
306306
}

0 commit comments

Comments
 (0)