{"record":{"id":"e09f4633451c7bcf","repo":"go-delve/delve","slug":"shift-count-must-not-be-negative","errorCode":null,"errorMessage":"shift count must not be negative","messagePattern":"shift count must not be negative","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/eval.go","lineNumber":2446,"sourceCode":"\tif xv == nilVariable {\n\t\treturn nil, negotiateTypeNil(op, yv)\n\t}\n\n\tif yv == nilVariable {\n\t\treturn nil, negotiateTypeNil(op, xv)\n\t}\n\n\tif op == token.SHR || op == token.SHL {\n\t\tif xv.Value == nil || xv.Value.Kind() != constant.Int {\n\t\t\treturn nil, fmt.Errorf(\"shift of type %s\", xv.Kind)\n\t\t}\n\n\t\tswitch yv.Kind {\n\t\tcase reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:\n\t\t\t// ok\n\t\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\n\t\t\tif constant.Sign(yv.Value) < 0 {\n\t\t\t\treturn nil, errors.New(\"shift count must not be negative\")\n\t\t\t}\n\t\tdefault:\n\t\t\treturn nil, fmt.Errorf(\"shift count type %s, must be unsigned integer\", yv.Kind.String())\n\t\t}\n\n\t\treturn xv.DwarfType, nil\n\t}\n\n\tif xv.DwarfType == nil && yv.DwarfType == nil {\n\t\treturn nil, nil\n\t}\n\n\tif xv.DwarfType != nil && yv.DwarfType != nil {\n\t\tif xv.DwarfType.String() != yv.DwarfType.String() {\n\t\t\treturn nil, fmt.Errorf(\"mismatched types %q and %q\", xv.DwarfType.String(), yv.DwarfType.String())\n\t\t}\n\t\treturn xv.DwarfType, nil\n\t} else if xv.DwarfType != nil && yv.DwarfType == nil {","sourceCodeStart":2428,"sourceCodeEnd":2464,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/eval.go#L2428-L2464","documentation":"Delve's expression evaluator supports the shift operators << and >> on Go values. Before applying a shift it validates the shift count: it must be an integer of any signedness, but its value must not be negative, and non-integer types are rejected entirely. This error is thrown when the right-hand operand of a shift is a signed integer with a negative constant value.","triggerScenarios":"Evaluating an expression like 'a << b' or 'a >> b' in the dlv debugger (eval, print, or watch commands) where b is a signed integer variable holding a negative value, e.g. 'print x << n' with n == -1.","commonSituations":"Stepping through bit-manipulation code where the shift amount is computed (e.g. from a subtraction or loop counter) and has gone negative due to an off-by-one or uninitialized value; debugging the very bug the Go compiler would reject as a constant expression.","solutions":["Inspect the shift count expression before the shift, e.g. 'print n' or 'print i-j', and fix the underlying negative value","Guard the shift in the source: if n >= 0 { x << n } else { ... }","Reinterpret the count as unsigned if negative values are impossible in your logic: uint(n)","If the variable display seems wrong, verify the variable's type/value with 'locals' or 'print' to confirm it is truly negative"],"exampleFix":"// before (evaluating in dlv)\n(dlv) print x << n        // n = -3 -> error\n\n// after\n(dlv) print n             // inspect first\n-3\n(dlv) print uint(x) >> 3  // or fix n in the program before shifting","handlingStrategy":"validation","validationCode":"// before evaluating a shift in the debugger\nif n, ok := shiftCount.(int64); !ok || n < 0 {\n    // fix the count or use uint(n) in the expression\n    fmt.Println(\"shift count must be a non-negative integer, got\", n)\n}","typeGuard":"func validShiftCount(v interface{}) bool {\n    switch c := v.(type) {\n    case uint, uint8, uint16, uint32, uint64:\n        return true\n    case int:\n        return c >= 0\n    case int64:\n        return c >= 0\n    default:\n        return false\n    }\n}","tryCatchPattern":null,"preventionTips":["Print the shift count expression ('print n') before shifting","Guard shifts in source code with an if n >= 0 check","Cast to unsigned only when negativity is provably impossible","Remember the Go compiler rejects negative constant shifts; runtime-computed counts can still go negative"],"tags":["go","debugger","expression-evaluation","shift-operator"],"backgroundTag":"negative-shift-count","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}