Skip to content

Commit 759eaa2

Browse files
committed
cmd/compile/internal/types2: remove most asX converters (cleanup)
Make it explicit in the code where we call under. The asNamed and asTypeParam converters need to stay: asNamed does resolution if necessary, and asTypeParam may or may not call under() depending on the next CL. Reviewed uses of asNamed and .(*Named) for correctness. Removed unnecessary Named.resolve call in lookup. Change-Id: I2acf176925e00bd1703a00230a779aa65a8f5a51 Reviewed-on: https://go-review.googlesource.com/c/go/+/362254 Trust: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 3e41b18 commit 759eaa2

File tree

13 files changed

+36
-72
lines changed

13 files changed

+36
-72
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (check *Checker) assignment(x *operand, T Type, context string) {
7171
// x.typ is typed
7272

7373
// A generic (non-instantiated) function value cannot be assigned to a variable.
74-
if sig := asSignature(x.typ); sig != nil && sig.TypeParams().Len() > 0 {
74+
if sig, _ := under(x.typ).(*Signature); sig != nil && sig.TypeParams().Len() > 0 {
7575
check.errorf(x, "cannot use generic function %s without instantiation in %s", x, context)
7676
}
7777

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
294294
// (applyTypeFunc never calls f with a type parameter)
295295
f := func(typ Type) Type {
296296
assert(asTypeParam(typ) == nil)
297-
if t := asBasic(typ); t != nil {
297+
if t, _ := under(typ).(*Basic); t != nil {
298298
switch t.kind {
299299
case Float32:
300300
return Typ[Complex64]
@@ -418,7 +418,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
418418
// (applyTypeFunc never calls f with a type parameter)
419419
f := func(typ Type) Type {
420420
assert(asTypeParam(typ) == nil)
421-
if t := asBasic(typ); t != nil {
421+
if t, _ := under(typ).(*Basic); t != nil {
422422
switch t.kind {
423423
case Complex64:
424424
return Typ[Float32]
@@ -704,7 +704,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
704704
return
705705
}
706706

707-
typ := asPointer(x.typ)
707+
typ, _ := under(x.typ).(*Pointer)
708708
if typ == nil {
709709
check.errorf(x, invalidArg+"%s is not a pointer", x)
710710
return
@@ -894,7 +894,7 @@ func makeSig(res Type, args ...Type) *Signature {
894894
// otherwise it returns typ.
895895
func arrayPtrDeref(typ Type) Type {
896896
if p, ok := typ.(*Pointer); ok {
897-
if a := asArray(p.base); a != nil {
897+
if a, _ := under(p.base).(*Array); a != nil {
898898
return a
899899
}
900900
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (check *Checker) callExpr(x *operand, call *syntax.CallExpr) exprKind {
132132
case 1:
133133
check.expr(x, call.ArgList[0])
134134
if x.mode != invalid {
135-
if t := asInterface(T); t != nil {
135+
if t, _ := under(T).(*Interface); t != nil {
136136
if !t.IsMethodSet() {
137137
check.errorf(call, "cannot use interface %s in conversion (contains specific type constraints or is comparable)", T)
138138
break

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (ctxt *Context) TypeHash(typ Type, targs []Type) string {
3939
var buf bytes.Buffer
4040

4141
h := newTypeHasher(&buf, ctxt)
42-
if named, _ := typ.(*Named); named != nil && len(targs) > 0 {
42+
if named := asNamed(typ); named != nil && len(targs) > 0 {
4343
// Don't use WriteType because we need to use the provided targs
4444
// and not any targs that might already be with the *Named type.
4545
h.typePrefix(named)

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (check *Checker) conversion(x *operand, T Type) {
1818
constArg := x.mode == constant_
1919

2020
constConvertibleTo := func(T Type, val *constant.Value) bool {
21-
switch t := asBasic(T); {
21+
switch t, _ := under(T).(*Basic); {
2222
case t == nil:
2323
// nothing to do
2424
case representableConst(x.val, check, t, val):
@@ -173,9 +173,9 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
173173

174174
// "V a slice, T is a pointer-to-array type,
175175
// and the slice and array types have identical element types."
176-
if s := asSlice(V); s != nil {
177-
if p := asPointer(T); p != nil {
178-
if a := asArray(p.Elem()); a != nil {
176+
if s, _ := under(V).(*Slice); s != nil {
177+
if p, _ := under(T).(*Pointer); p != nil {
178+
if a, _ := under(p.Elem()).(*Array); a != nil {
179179
if Identical(s.Elem(), a.Elem()) {
180180
if check == nil || check.allowVersion(check.pkg, 1, 17) {
181181
return true
@@ -262,26 +262,27 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
262262
// use the toT convenience converters in the predicates below.
263263

264264
func isUintptr(typ Type) bool {
265-
t := asBasic(typ)
265+
t, _ := under(typ).(*Basic)
266266
return t != nil && t.kind == Uintptr
267267
}
268268

269269
func isUnsafePointer(typ Type) bool {
270-
// TODO(gri): Is this asBasic(typ) instead of typ.(*Basic) correct?
270+
// TODO(gri): Is this under(typ).(*Basic) instead of typ.(*Basic) correct?
271271
// (The former calls under(), while the latter doesn't.)
272272
// The spec does not say so, but gc claims it is. See also
273273
// issue 6326.
274-
t := asBasic(typ)
274+
t, _ := under(typ).(*Basic)
275275
return t != nil && t.kind == UnsafePointer
276276
}
277277

278278
func isPointer(typ Type) bool {
279-
return asPointer(typ) != nil
279+
_, ok := under(typ).(*Pointer)
280+
return ok
280281
}
281282

282283
func isBytesOrRunes(typ Type) bool {
283-
if s := asSlice(typ); s != nil {
284-
t := asBasic(s.elem)
284+
if s, _ := under(typ).(*Slice); s != nil {
285+
t, _ := under(s.elem).(*Basic)
285286
return t != nil && (t.kind == Byte || t.kind == Rune)
286287
}
287288
return false

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (check *Checker) overflow(x *operand) {
116116
// x.typ cannot be a type parameter (type
117117
// parameters cannot be constant types).
118118
if isTyped(x.typ) {
119-
check.representable(x, asBasic(x.typ))
119+
check.representable(x, under(x.typ).(*Basic))
120120
return
121121
}
122122

@@ -617,7 +617,7 @@ func (check *Checker) updateExprType(x syntax.Expr, typ Type, final bool) {
617617
// If the new type is not final and still untyped, just
618618
// update the recorded type.
619619
if !final && isUntyped(typ) {
620-
old.typ = asBasic(typ)
620+
old.typ = under(typ).(*Basic)
621621
check.untyped[x] = old
622622
return
623623
}
@@ -1394,7 +1394,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
13941394
duplicate := false
13951395
// if the key is of interface type, the type is also significant when checking for duplicates
13961396
xkey := keyVal(x.val)
1397-
if asInterface(utyp.key) != nil {
1397+
if IsInterface(utyp.key) {
13981398
for _, vtyp := range visited[xkey] {
13991399
if Identical(vtyp, x.typ) {
14001400
duplicate = true

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (check *Checker) indexExpr(x *operand, e *syntax.IndexExpr) (isFuncInst boo
3434
return false
3535

3636
case value:
37-
if sig := asSignature(x.typ); sig != nil && sig.TypeParams().Len() > 0 {
37+
if sig, _ := under(x.typ).(*Signature); sig != nil && sig.TypeParams().Len() > 0 {
3838
// function instantiation
3939
return true
4040
}
@@ -72,7 +72,7 @@ func (check *Checker) indexExpr(x *operand, e *syntax.IndexExpr) (isFuncInst boo
7272
x.typ = typ.elem
7373

7474
case *Pointer:
75-
if typ := asArray(typ.base); typ != nil {
75+
if typ, _ := under(typ.base).(*Array); typ != nil {
7676
valid = true
7777
length = typ.len
7878
x.mode = variable
@@ -120,7 +120,7 @@ func (check *Checker) indexExpr(x *operand, e *syntax.IndexExpr) (isFuncInst boo
120120
mode = value
121121
}
122122
case *Pointer:
123-
if t := asArray(t.base); t != nil {
123+
if t, _ := under(t.base).(*Array); t != nil {
124124
l = t.len
125125
e = t.elem
126126
}
@@ -245,7 +245,7 @@ func (check *Checker) sliceExpr(x *operand, e *syntax.SliceExpr) {
245245
x.typ = &Slice{elem: u.elem}
246246

247247
case *Pointer:
248-
if u := asArray(u.base); u != nil {
248+
if u, _ := under(u.base).(*Array); u != nil {
249249
valid = true
250250
length = u.len
251251
x.typ = &Slice{elem: u.elem}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ func lookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (o
122122
seen[named] = true
123123

124124
// look for a matching attached method
125-
named.resolve(nil)
126125
if i, m := lookupMethod(named.methods, pkg, name); m != nil {
127126
// potential match
128127
// caution: method may not have a proper signature yet
@@ -306,7 +305,7 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
306305
return
307306
}
308307

309-
if ityp := asInterface(V); ityp != nil {
308+
if ityp, _ := under(V).(*Interface); ityp != nil {
310309
// TODO(gri) the methods are sorted - could do this more efficiently
311310
for _, m := range T.typeSet().methods {
312311
_, f := ityp.typeSet().LookupMethod(m.pkg, m.name)
@@ -417,7 +416,7 @@ func (check *Checker) assertableTo(V *Interface, T Type) (method, wrongType *Fun
417416
// no static check is required if T is an interface
418417
// spec: "If T is an interface type, x.(T) asserts that the
419418
// dynamic type of x implements the interface T."
420-
if asInterface(T) != nil && !forceStrict {
419+
if IsInterface(T) && !forceStrict {
421420
return
422421
}
423422
return check.missingMethod(T, V, false)
@@ -435,8 +434,8 @@ func deref(typ Type) (Type, bool) {
435434
// derefStructPtr dereferences typ if it is a (named or unnamed) pointer to a
436435
// (named or unnamed) struct and returns its base. Otherwise it returns typ.
437436
func derefStructPtr(typ Type) Type {
438-
if p := asPointer(typ); p != nil {
439-
if asStruct(p.base) != nil {
437+
if p, _ := under(typ).(*Pointer); p != nil {
438+
if _, ok := under(p.base).(*Struct); ok {
440439
return p.base
441440
}
442441
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func hasName(t Type) bool {
7272
// are not fully set up.
7373
func isTyped(t Type) bool {
7474
// isTyped is called with types that are not fully
75-
// set up. Must not call asBasic()!
75+
// set up. Must not call under()!
7676
b, _ := t.(*Basic)
7777
return b == nil || b.info&IsUntyped == 0
7878
}
@@ -84,7 +84,8 @@ func isUntyped(t Type) bool {
8484

8585
// IsInterface reports whether t is an interface type.
8686
func IsInterface(t Type) bool {
87-
return asInterface(t) != nil
87+
_, ok := under(t).(*Interface)
88+
return ok
8889
}
8990

9091
// isTypeParam reports whether t is a type parameter.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func (conf *Config) offsetsof(T *Struct) []int64 {
243243
func (conf *Config) offsetof(typ Type, index []int) int64 {
244244
var o int64
245245
for _, i := range index {
246-
s := asStruct(typ)
246+
s := under(typ).(*Struct)
247247
o += conf.offsetsof(s)[i]
248248
typ = s.fields[i].typ
249249
}

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

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,8 @@ func under(t Type) Type {
2727
return t
2828
}
2929

30-
// Convenience converters
31-
32-
func asBasic(t Type) *Basic {
33-
u, _ := under(t).(*Basic)
34-
return u
35-
}
36-
37-
func asArray(t Type) *Array {
38-
u, _ := under(t).(*Array)
39-
return u
40-
}
41-
42-
func asSlice(t Type) *Slice {
43-
u, _ := under(t).(*Slice)
44-
return u
45-
}
46-
47-
func asStruct(t Type) *Struct {
48-
u, _ := under(t).(*Struct)
49-
return u
50-
}
51-
52-
func asPointer(t Type) *Pointer {
53-
u, _ := under(t).(*Pointer)
54-
return u
55-
}
56-
57-
func asSignature(t Type) *Signature {
58-
u, _ := under(t).(*Signature)
59-
return u
60-
}
61-
62-
func asInterface(t Type) *Interface {
63-
u, _ := under(t).(*Interface)
64-
return u
65-
}
66-
6730
// If the argument to asNamed, or asTypeParam is of the respective type
68-
// (possibly after expanding resolving a *Named type), these methods return that type.
31+
// (possibly after resolving a *Named type), these methods return that type.
6932
// Otherwise the result is nil.
7033

7134
func asNamed(t Type) *Named {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func (w *typeWriter) tuple(tup *Tuple, variadic bool) {
361361
} else {
362362
// special case:
363363
// append(s, "foo"...) leads to signature func([]byte, string...)
364-
if t := asBasic(typ); t == nil || t.kind != String {
364+
if t, _ := under(typ).(*Basic); t == nil || t.kind != String {
365365
w.error("expected string type")
366366
continue
367367
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (check *Checker) varType(e syntax.Expr) Type {
148148
// are in the middle of type-checking parameter declarations that might belong to
149149
// interface methods. Delay this check to the end of type-checking.
150150
check.later(func() {
151-
if t := asInterface(typ); t != nil {
151+
if t, _ := under(typ).(*Interface); t != nil {
152152
pos := syntax.StartPos(e)
153153
tset := computeInterfaceTypeSet(check, pos, t) // TODO(gri) is this the correct position?
154154
if !tset.IsMethodSet() {

0 commit comments

Comments
 (0)