go-delve/delve · error

invalid argument 1 %s (type %s) to complex

Error message

invalid argument 1 %s (type %s) to complex

What it means

Delve's `complex()` builtin requires its first argument (the real part) to evaluate to an integer or float constant. This error fires when the argument is unreadable, nil, or of another constant kind (bool, string, complex, etc.), naming the offending expression and its type.

Source

Thrown at pkg/proc/eval.go:1987

		return nil, fmt.Errorf("wrong number of arguments to complex: %d", len(args))
	}

	realev := args[0]
	imagev := args[1]

	realev.loadValue(loadSingleValue)
	imagev.loadValue(loadSingleValue)

	if realev.Unreadable != nil {
		return nil, realev.Unreadable
	}

	if imagev.Unreadable != nil {
		return nil, imagev.Unreadable
	}

	if realev.Value == nil || ((realev.Value.Kind() != constant.Int) && (realev.Value.Kind() != constant.Float)) {
		return nil, fmt.Errorf("invalid argument 1 %s (type %s) to complex", astutil.ExprToString(nodeargs[0]), realev.TypeString())
	}

	if imagev.Value == nil || ((imagev.Value.Kind() != constant.Int) && (imagev.Value.Kind() != constant.Float)) {
		return nil, fmt.Errorf("invalid argument 2 %s (type %s) to complex", astutil.ExprToString(nodeargs[1]), imagev.TypeString())
	}

	sz := int64(0)
	if realev.RealType != nil {
		sz = realev.RealType.(*godwarf.FloatType).Size()
	}
	if imagev.RealType != nil {
		isz := imagev.RealType.(*godwarf.FloatType).Size()
		if isz > sz {
			sz = isz
		}
	}

	if sz == 0 {

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Ensure the real argument is an int or float typed expression: `complex(floatVar, imagVar)`.
  2. Convert explicitly, e.g. wrap or use a numeric field: `complex(x.real, x.imag)`.
  3. Check the variable is readable (not optimized out) — inspect it alone first.
  4. If the value is already complex, use it directly instead of calling complex().

Example fix

// before
complex(name, imagPart)   // name is a string
// after
complex(realPart, imagPart)
Defensive patterns

Strategy: type-guard

Validate before calling

// before evaluating complex(re, im): real part must be int/float
re, err := evalLoad(re)
if err != nil || re.Value == nil ||
    (re.Value.Kind() != constant.Int && re.Value.Kind() != constant.Float) {
    return fmt.Errorf("real part must be numeric, got %s", re.TypeString())
}

Type guard

func isNumericConst(v *proc.Variable) bool {
    return v.Unreadable == nil && v.Value != nil &&
        (v.Value.Kind() == constant.Int || v.Value.Kind() == constant.Float)
}

Try / catch

val, err := evalComplex(reExpr, imExpr)
if err != nil && strings.Contains(err.Error(), "invalid argument 1") {
    // inspect/fix the real-part expression before retrying
}

Prevention

When it happens

Trigger: Evaluating `complex(s, i)` where the first argument is a string, bool, complex value, or a variable whose value could not be loaded (realev.Value == nil) (eval.go:1987, complexBuiltin).

Common situations: Passing a string where a float was intended, passing an already-complex value to complex(), or evaluating on a variable whose memory could not be read (Value nil).

Related errors


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