go-delve/delve · error

can not convert constant %s to int

Error message

can not convert constant %s to int

What it means

Variable.asInt converts a variable to an int64 for uses like array indexing or shift counts. If the variable has no DWARF type, it is treated as an untyped constant, and only constant.Int values can be converted; a float or string constant reaching asInt produces this error.

Source

Thrown at pkg/proc/eval.go:2723

func equalChildren(xv, yv *Variable, shortcircuit bool) (bool, error) {
	r := true
	for i := range xv.Children {
		eql, err := compareOp(token.EQL, &xv.Children[i], &yv.Children[i])
		if err != nil {
			return false, err
		}
		r = r && eql
		if !r && shortcircuit {
			return false, nil
		}
	}
	return r, nil
}

func (v *Variable) asInt() (int64, error) {
	if v.DwarfType == nil {
		if v.Value.Kind() != constant.Int {
			return 0, fmt.Errorf("can not convert constant %s to int", v.Value)
		}
	} else {
		v.loadValue(loadSingleValue)
		if v.Unreadable != nil {
			return 0, v.Unreadable
		}
		if _, ok := v.DwarfType.(*godwarf.IntType); !ok {
			return 0, fmt.Errorf("can not convert value of type %s to int", v.DwarfType.String())
		}
	}
	n, _ := constant.Int64Val(v.Value)
	return n, nil
}

func (v *Variable) asUint() (uint64, error) {
	if v.DwarfType == nil {
		if v.Value.Kind() != constant.Int {
			return 0, fmt.Errorf("can not convert constant %s to uint", v.Value)

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Use an integer literal or cast: `p arr[int(2.0)]` or `p arr[2]`.
  2. Convert floats explicitly with int(): `p arr[int(f)]`.
  3. Check the constant for a stray decimal point or wrong value.

Example fix

// before
p arr[2.0]
// after
p arr[2]
Defensive patterns

Strategy: validation

Validate before calling

// indexes and shift counts must be integer constants
// write arr[2], not arr[2.0]; cast floats explicitly: arr[int(f)]

Type guard

func isIntConstant(v *proc.Variable) bool {
    if v == nil {
        return false
    }
    if v.DwarfType == nil {
        return v.Value != nil && v.Value.Kind() == constant.Int
    }
    switch v.Kind {
    case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
        reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
        return true
    }
    return false
}

Prevention

When it happens

Trigger: Using a non-integer constant where an integer is required: `p arr[1.5]`, `p arr["key"]`, or a shift count given as an untyped float constant, in expressions evaluated by EvalScope.

Common situations: Typing an index with a decimal point out of habit (`a[2.0]`), or using a string where a numeric index is expected while probing arrays/slices in the debugger.

Related errors


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