{"record":{"id":"bb6234e4353bb1c0","repo":"go-delve/delve","slug":"mismatched-types-nil-and-s","errorCode":null,"errorMessage":"mismatched types nil and %s","messagePattern":"mismatched types nil and (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/eval.go","lineNumber":2781,"sourceCode":"\nfunc (v *Variable) isType(typ godwarf.Type, kind reflect.Kind) error {\n\tif v.DwarfType != nil {\n\t\tif typ == nil || !sameType(typ, v.RealType) {\n\t\t\treturn &typeConvErr{v.DwarfType, typ}\n\t\t}\n\t\treturn nil\n\t}\n\n\tif typ == nil {\n\t\treturn nil\n\t}\n\n\tif v == nilVariable {\n\t\tswitch kind {\n\t\tcase reflect.Slice, reflect.Map, reflect.Func, reflect.Ptr, reflect.Chan, reflect.Interface:\n\t\t\treturn nil\n\t\tdefault:\n\t\t\treturn fmt.Errorf(\"mismatched types nil and %s\", typ.String())\n\t\t}\n\t}\n\n\tconverr := fmt.Errorf(\"can not convert %s constant to %s\", v.Value, typ.String())\n\n\tif v.Value == nil {\n\t\treturn converr\n\t}\n\n\tswitch typ.(type) {\n\tcase *godwarf.IntType:\n\t\tif v.Value.Kind() != constant.Int {\n\t\t\treturn converr\n\t\t}\n\tcase *godwarf.UintType:\n\t\tif v.Value.Kind() != constant.Int {\n\t\t\treturn converr\n\t\t}","sourceCodeStart":2763,"sourceCodeEnd":2799,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/eval.go#L2763-L2799","documentation":"Raised by the type-conversion logic (convertToType) when the source expression is untyped nil (nilVariable). nil is only assignable to slice, map, func, pointer, chan, and interface types in Go. If the target type typ is anything else (int, string, struct, bool...), Delve reports 'mismatched types nil and <target>'.","triggerScenarios":"Evaluating expressions like (*T)(nil) on wrong types: e.g. int(nil), string(nil), or struct-typed conversions from nil in the debugger console or breakpoint conditions, hitting the nilVariable check in convertToType.","commonSituations":"Typing nil where Go itself would reject it; autogenerated conditions comparing a non-reference variable to nil (e.g. myInt == nil converted via type assertion); ported code assuming nil converts to any zero value.","solutions":["Compare against the zero value instead: x == 0 for ints, x == \"\" for strings, x == false for bools","Only assign/convert nil to reference types: *T, map, slice, chan, func, interface","If checking for emptiness of a struct, test a field or use a pointer to the struct and compare to nil","Remove the invalid nil cast from breakpoint conditions/watch expressions"],"exampleFix":"// before\ncond: x == nil            // x is int\n// after\ncond: x == 0              // zero-value comparison for int","handlingStrategy":"validation","validationCode":"// nil is only valid for slice/map/func/ptr/chan/interface\n// for other types compare to zero value:\ncond: x == 0   // int\ncond: s == \"\"  // string","typeGuard":"// only use nil when the target type is a reference type\n// func isNilable(typ string) bool { return strings.Contains(typ, \"*\") || typ==\"map\" || typ==\"slice\" || typ==\"chan\" || typ==\"func\" }","tryCatchPattern":null,"preventionTips":["Never cast nil to value types (int, string, struct, bool)","Compare value types to their zero values instead of nil","Use pointers for structs that need nil-ability"],"tags":["go","debugger","nil","type-mismatch"],"backgroundTag":"mismatched-types","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}