{"record":{"id":"928d91d2c3ee2f50","repo":"go-delve/delve","slug":"invalid-argument-s-type-s-for-cap","errorCode":null,"errorMessage":"invalid argument %s (type %s) for cap","messagePattern":"invalid argument (.+?) \\(type (.+?)\\) for cap","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/eval.go","lineNumber":1898,"sourceCode":"}\n\nvar supportedBuiltins = map[string]func([]*Variable, []ast.Expr) (*Variable, error){\n\t\"cap\":     capBuiltin,\n\t\"len\":     lenBuiltin,\n\t\"complex\": complexBuiltin,\n\t\"imag\":    imagBuiltin,\n\t\"real\":    realBuiltin,\n\t\"min\":     minBuiltin,\n\t\"max\":     maxBuiltin,\n}\n\nfunc capBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {\n\tif len(args) != 1 {\n\t\treturn nil, fmt.Errorf(\"wrong number of arguments to cap: %d\", len(args))\n\t}\n\n\targ := args[0]\n\tinvalidArgErr := fmt.Errorf(\"invalid argument %s (type %s) for cap\", astutil.ExprToString(nodeargs[0]), arg.TypeString())\n\n\tswitch arg.Kind {\n\tcase reflect.Ptr:\n\t\targ = arg.maybeDereference()\n\t\tif arg.Kind != reflect.Array {\n\t\t\treturn nil, invalidArgErr\n\t\t}\n\t\tfallthrough\n\tcase reflect.Array:\n\t\treturn newConstant(constant.MakeInt64(arg.Len), arg.bi, arg.mem), nil\n\tcase reflect.Slice:\n\t\treturn newConstant(constant.MakeInt64(arg.Cap), arg.bi, arg.mem), nil\n\tcase reflect.Chan:\n\t\targ.loadValue(LoadFullValue())\n\t\tif arg.Unreadable != nil {\n\t\t\treturn nil, arg.Unreadable\n\t\t}\n\t\tif arg.Base == 0 {","sourceCodeStart":1880,"sourceCodeEnd":1916,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/eval.go#L1880-L1916","documentation":"Delve's expression evaluator throws this when the `cap()` builtin is called on an expression whose type has no capacity: only arrays, slices and channels are valid, and a pointer is accepted only if it points to an array. The error embeds the source text of the argument and its type so you can see exactly which expression was rejected.","triggerScenarios":"Evaluating `cap(x)` in the debugger where x's reflect.Kind is not Slice, Chan or Array — e.g. cap of a string, a map, an int, or a pointer that dereferences to something other than an array (eval.go:1898, capBuiltin).","commonSituations":"Typing `cap(myString)` (strings have len, not cap), `cap(ptrToSlice)` (pointer to slice is not auto-dereferenced to an array), or evaluating cap on a variable whose DWARF type resolved to something unexpected during optimization.","solutions":["Check the argument's type with `ptype <expr>`; cap only accepts slice, channel or array (or pointer-to-array).","For strings use `len <expr>` instead of cap.","If the value is a pointer you expected to be an array, dereference explicitly and verify the pointee type.","Re-check the variable was not shadowed or optimized away into a differently-typed register value."],"exampleFix":"// before (debugger console)\ncap(myString)        // invalid argument myString (type string) for cap\n// after\nlen(myString)        // strings: use len","handlingStrategy":"validation","validationCode":"// before evaluating cap(x) in the debugger\n// cap is only valid for slice, chan, array (or ptr-to-array)\nif kind := v.Kind; kind != reflect.Slice && kind != reflect.Chan && kind != reflect.Array {\n    // for pointers: only proceed if maybeDereference yields an array\n    return fmt.Errorf(\"cap requires slice/chan/array, got %s\", v.TypeString())\n}","typeGuard":"func isCapable(v *proc.Variable) bool {\n    if v.Kind == reflect.Slice || v.Kind == reflect.Chan || v.Kind == reflect.Array {\n        return true\n    }\n    if v.Kind == reflect.Ptr {\n        d := v.MaybeDereference()\n        return d.Kind == reflect.Array\n    }\n    return false\n}","tryCatchPattern":"val, err := evalCap(expr)\nif err != nil && strings.Contains(err.Error(), \"invalid argument\") {\n    // fall back to len() or report the argument type to the user\n}","preventionTips":["Use ptype in the debugger to confirm the expression's type before applying cap.","Remember strings have len but no cap.","Dereference pointers explicitly when you expect an array pointee."],"tags":["debugger","go","expression-evaluation","builtin-cap"],"backgroundTag":"invalid-builtin-argument","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}