1
0
mirror of https://github.com/golang/go synced 2024-11-14 19:50:21 -07:00

go/types, types2: rename kindString to compositeKind and simplify function

Simplify functionality of compositeKind (formerly: kindString) by
giving it a smaller scope. Move it into operand.go for future use
in that file. Adjust existing uses.

Change-Id: I73d04a8c0be44d9604e56bd4c0289afdcdd32238
Reviewed-on: https://go-review.googlesource.com/c/go/+/621457
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Tim King <taking@google.com>
This commit is contained in:
Robert Griesemer 2024-10-22 10:13:29 -07:00 committed by Gopher Robot
parent 34e96356b7
commit 7ad9a2c65f
4 changed files with 78 additions and 58 deletions

View File

@ -566,7 +566,12 @@ Error:
}
cause = check.sprintf("type parameter %s cannot use operator %s", errOp.typ, op)
} else {
cause = check.sprintf("operator %s not defined on %s", op, check.kindString(errOp.typ)) // catch-all
// catch-all: neither x nor y is a type parameter
what := compositeKind(errOp.typ)
if what == "" {
what = check.sprintf("%s", errOp.typ)
}
cause = check.sprintf("operator %s not defined on %s", op, what)
}
}
if switchCase {
@ -582,7 +587,7 @@ Error:
func (check *Checker) incomparableCause(typ Type) string {
switch under(typ).(type) {
case *Slice, *Signature, *Map:
return check.kindString(typ) + " can only be compared to nil"
return compositeKind(typ) + " can only be compared to nil"
}
// see if we can extract a more specific error
var cause string
@ -592,33 +597,6 @@ func (check *Checker) incomparableCause(typ Type) string {
return cause
}
// kindString returns the type kind as a string.
func (check *Checker) kindString(typ Type) string {
switch under(typ).(type) {
case *Array:
return "array"
case *Slice:
return "slice"
case *Struct:
return "struct"
case *Pointer:
return "pointer"
case *Signature:
return "func"
case *Interface:
if isTypeParam(typ) {
return check.sprintf("type parameter %s", typ)
}
return "interface"
case *Map:
return "map"
case *Chan:
return "chan"
default:
return check.sprintf("%s", typ) // catch-all
}
}
// If e != nil, it must be the shift expression; it may be nil for non-constant shifts.
func (check *Checker) shift(x, y *operand, e syntax.Expr, op syntax.Operator) {
// TODO(gri) This function seems overly complex. Revisit.

View File

@ -207,6 +207,38 @@ func operandString(x *operand, qf Qualifier) string {
return buf.String()
}
// compositeKind returns the kind of the given composite type
// ("array", "slice", etc.) or the empty string if typ is not
// composite but a basic type.
func compositeKind(typ Type) string {
switch under(typ).(type) {
case *Basic:
return ""
case *Array:
return "array"
case *Slice:
return "slice"
case *Struct:
return "struct"
case *Pointer:
return "pointer"
case *Signature:
return "func"
case *Interface:
return "interface"
case *Map:
return "map"
case *Chan:
return "chan"
case *Tuple:
return "tuple"
case *Union:
return "union"
default:
panic("unreachable")
}
}
func (x *operand) String() string {
return operandString(x, nil)
}

View File

@ -556,7 +556,12 @@ Error:
}
cause = check.sprintf("type parameter %s cannot use operator %s", errOp.typ, op)
} else {
cause = check.sprintf("operator %s not defined on %s", op, check.kindString(errOp.typ)) // catch-all
// catch-all neither x nor y is a type parameter
what := compositeKind(errOp.typ)
if what == "" {
what = check.sprintf("%s", errOp.typ)
}
cause = check.sprintf("operator %s not defined on %s", op, what)
}
}
if switchCase {
@ -572,7 +577,7 @@ Error:
func (check *Checker) incomparableCause(typ Type) string {
switch under(typ).(type) {
case *Slice, *Signature, *Map:
return check.kindString(typ) + " can only be compared to nil"
return compositeKind(typ) + " can only be compared to nil"
}
// see if we can extract a more specific error
var cause string
@ -582,33 +587,6 @@ func (check *Checker) incomparableCause(typ Type) string {
return cause
}
// kindString returns the type kind as a string.
func (check *Checker) kindString(typ Type) string {
switch under(typ).(type) {
case *Array:
return "array"
case *Slice:
return "slice"
case *Struct:
return "struct"
case *Pointer:
return "pointer"
case *Signature:
return "func"
case *Interface:
if isTypeParam(typ) {
return check.sprintf("type parameter %s", typ)
}
return "interface"
case *Map:
return "map"
case *Chan:
return "chan"
default:
return check.sprintf("%s", typ) // catch-all
}
}
// If e != nil, it must be the shift expression; it may be nil for non-constant shifts.
func (check *Checker) shift(x, y *operand, e ast.Expr, op token.Token) {
// TODO(gri) This function seems overly complex. Revisit.

View File

@ -211,6 +211,38 @@ func operandString(x *operand, qf Qualifier) string {
return buf.String()
}
// compositeKind returns the kind of the given composite type
// ("array", "slice", etc.) or the empty string if typ is not
// composite but a basic type.
func compositeKind(typ Type) string {
switch under(typ).(type) {
case *Basic:
return ""
case *Array:
return "array"
case *Slice:
return "slice"
case *Struct:
return "struct"
case *Pointer:
return "pointer"
case *Signature:
return "func"
case *Interface:
return "interface"
case *Map:
return "map"
case *Chan:
return "chan"
case *Tuple:
return "tuple"
case *Union:
return "union"
default:
panic("unreachable")
}
}
func (x *operand) String() string {
return operandString(x, nil)
}