{"record":{"id":"af4e242a756e846b","repo":"go-delve/delve","slug":"operator-s-can-not-be-applied-to-nil","errorCode":null,"errorMessage":"operator %s can not be applied to \"nil\"","messagePattern":"operator (.+?) can not be applied to \"nil\"","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/eval.go","lineNumber":2481,"sourceCode":"\t\treturn xv.DwarfType, nil\n\t} else if xv.DwarfType != nil && yv.DwarfType == nil {\n\t\tif err := yv.isType(xv.DwarfType, xv.Kind); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\treturn xv.DwarfType, nil\n\t} else if xv.DwarfType == nil && yv.DwarfType != nil {\n\t\tif err := xv.isType(yv.DwarfType, yv.Kind); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\treturn yv.DwarfType, nil\n\t}\n\n\tpanic(\"unreachable\")\n}\n\nfunc negotiateTypeNil(op token.Token, v *Variable) error {\n\tif op != token.EQL && op != token.NEQ {\n\t\treturn fmt.Errorf(\"operator %s can not be applied to \\\"nil\\\"\", op.String())\n\t}\n\tswitch v.Kind {\n\tcase reflect.Ptr, reflect.UnsafePointer, reflect.Chan, reflect.Map, reflect.Interface, reflect.Slice, reflect.Func:\n\t\treturn nil\n\tdefault:\n\t\treturn fmt.Errorf(\"can not compare %s to nil\", v.Kind.String())\n\t}\n}\n\nfunc (scope *EvalScope) evalBinary(binop *evalop.Binary, stack *evalStack) {\n\tnode := binop.Node\n\n\tyv := stack.pop()\n\txv := stack.pop()\n\n\tif xv.Kind != reflect.String { // delay loading strings until we use them\n\t\txv.loadValue(LoadFullValue())\n\t}","sourceCodeStart":2463,"sourceCodeEnd":2499,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/eval.go#L2463-L2499","documentation":"In Go, nil comparison is only defined for == and !=; Delve enforces this in negotiateTypeNil, which handles comparisons of a variable against the nil literal. Any other operator (such as <, >, +) applied to nil yields this error before the kind check.","triggerScenarios":"Evaluating an expression like `p nil < x`, `p nil + 1`, or any binary op other than EQL/NEQ where one operand is the nil keyword; negotiateTypeNil is called when one operand is nilVariable.","commonSituations":"Typo in an interactive expression during debugging, e.g. intending `ptr == nil` but typing `ptr < nil`, or attempting arithmetic involving nil while probing pointer logic.","solutions":["Use == or != when comparing against nil: `p ptr == nil`.","Dereference before applying other operators, guarding the nil case first.","If testing emptiness of a map/slice, compare with len(): `p len(s) == 0`."],"exampleFix":"// before\np ptr < nil\n// after\np ptr == nil","handlingStrategy":"validation","validationCode":"// only == / != are valid against nil\nif op != token.EQL && op != token.NEQ {\n    // rewrite the expression using == or !=\n}","typeGuard":"func nilComparable(op token.Token) bool {\n    return op == token.EQL || op == token.NEQ\n}","tryCatchPattern":null,"preventionTips":["Restrict nil usage to equality checks.","Guard arithmetic with nil checks before dereferencing pointers.","Use len() == 0 for slice/map emptiness instead of nil comparisons."],"tags":["debugger","expression-evaluation","nil"],"backgroundTag":"invalid-operator-on-nil","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}