]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: rename kindString to compositeKind and simplify function
authorRobert Griesemer <gri@golang.org>
Tue, 22 Oct 2024 17:13:29 +0000 (10:13 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 23 Oct 2024 04:42:58 +0000 (04:42 +0000)
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>
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/operand.go
src/go/types/expr.go
src/go/types/operand.go

index 76d7891b733aeb9294797cd2c064aabfcac37505..2bf42d1c6f14943ab5fcc0462bf5eadd8608f4f8 100644 (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.
index a34af9104e884fef0e8c2e3d1c5aa777faa6472f..1ee0f499f629273913cd5c290965bee32e422ba4 100644 (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)
 }
index 8cfacca6f40e35f1cd92d4f3b8fb1936ac8518e5..d4a08927015ac98859181b5d5e6794530bdb24cc 100644 (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.
index 2fca5f4ffc69d7f3486616089d9106d195be70bd..b6e0566b1a4073430f5464419e4d76c747786c5f 100644 (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)
 }