go-delve/delve · error

can not convert %s constant to %s

Error message

can not convert %s constant to %s

What it means

converr is the fallback error in convertToType in eval.go. It is created up-front as 'can not convert <constant> to <type>' and returned whenever the constant value cannot be represented as / converted to the requested target type — including the immediate case where v.Value is nil (no constant available). It is the generic constant-conversion failure for this evaluator.

Source

Thrown at pkg/proc/eval.go:2785

			return &typeConvErr{v.DwarfType, typ}
		}
		return nil
	}

	if typ == nil {
		return nil
	}

	if v == nilVariable {
		switch kind {
		case reflect.Slice, reflect.Map, reflect.Func, reflect.Ptr, reflect.Chan, reflect.Interface:
			return nil
		default:
			return fmt.Errorf("mismatched types nil and %s", typ.String())
		}
	}

	converr := fmt.Errorf("can not convert %s constant to %s", v.Value, typ.String())

	if v.Value == nil {
		return converr
	}

	switch typ.(type) {
	case *godwarf.IntType:
		if v.Value.Kind() != constant.Int {
			return converr
		}
	case *godwarf.UintType:
		if v.Value.Kind() != constant.Int {
			return converr
		}
	case *godwarf.FloatType:
		if (v.Value.Kind() != constant.Int) && (v.Value.Kind() != constant.Float) {
			return converr
		}

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Use a literal/value convertible to the target type: int(3), float64(1.5), string-literal casts only where Go allows
  2. Check Go's constant conversion rules — only numeric constants convert to numeric types, strings only from integer constants
  3. Simplify the expression and evaluate the constant alone first to see its type
  4. Rewrite the breakpoint condition to avoid the exotic cast

Example fix

// before
cond: MyStruct(1.5) == x
// after
cond: x.field == 1.5   // compare fields instead of converting to struct
Defensive patterns

Strategy: validation

Validate before calling

// test the constant alone first:
print(1.5)        // confirm its type
print(int(1.5))   // numeric->numeric is OK
// avoid: MyStruct(1.5), float64("abc")

Prevention

When it happens

Trigger: convertToType with a constant v.Value that is nil, or whose constant kind does not match any branch the switch on typ handles (e.g. converting a float constant to a struct type, a complex constant to an int, a string to a float in an invalid way).

Common situations: Casting literals to incompatible types in debugger conditions (e.g. float64("abc"), MyStruct(1.5)); typos in constant expressions; using complex literals where reals are expected.

Related errors


AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31). Data as JSON: /api/errors/d2bd2416c61a70a2. Report an issue: GitHub.