go-delve/delve · error

can not convert constant %s to uint

Error message

can not convert constant %s to uint

What it means

This error comes from Variable.asUint() in eval.go. When the variable has no DWARF type (it is a constant), the constant's exact value must be of kind constant.Int (an integer literal) to be converted to uint. If the constant is a float, string, bool, or complex literal, Delve returns this error naming the constant text.

Source

Thrown at pkg/proc/eval.go:2741

			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)
		}
	} else {
		v.loadValue(loadSingleValue)
		if v.Unreadable != nil {
			return 0, v.Unreadable
		}
		if _, ok := v.DwarfType.(*godwarf.UintType); !ok {
			return 0, fmt.Errorf("can not convert value of type %s to uint", v.DwarfType.String())
		}
	}
	n, _ := constant.Uint64Val(v.Value)
	return n, nil
}

type typeConvErr struct {
	srcType, dstType godwarf.Type
}

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Use an integer literal in the cast: uint(42) instead of uint(1.5)
  2. Truncate explicitly: uint(int64(1.5)) if lossy truncation is intended
  3. Verify the constant is non-negative if it should fit in an unsigned type
  4. If a variable was expected (not a constant), ensure the expression resolves to a real variable with DWARF type info

Example fix

// before
cond: x == uint(3.14)
// after
cond: x == uint(3)  // or uint(int64(3.14))
Defensive patterns

Strategy: validation

Validate before calling

// constants must be integer literals for uint conversion
// OK: uint(42)   BAD: uint(3.14), uint("7"), uint(true)

Prevention

When it happens

Trigger: Variable.asUint() is called when v.DwarfType == nil and v.Value.Kind() != constant.Int — i.e., evaluating expressions like uint(3.14), uint("abc"), or uint(true) on untyped constants in breakpoint conditions or the debugger console.

Common situations: Typing uint(1.5) or uint(-0.5) in an expression; using a string constant where an unsigned value is expected in a watch expression or condition; macros/scripts generating casts from literals.

Related errors


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