go-delve/delve · error

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

Error message

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

What it means

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

Source

Thrown at pkg/proc/eval.go:1991

	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 {
		sz = 128
	}

	typ := godwarf.FakeBasicType("complex", int(sz))

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Ensure the imaginary argument is an int or float typed expression.
  2. If the value is already complex, extract its part or use it directly rather than wrapping.
  3. Verify the second variable is readable (print it by itself first).
  4. Check argument order: complex(real, imag).

Example fix

// before
complex(re, "0.5")
// after
complex(re, 0.5)
Defensive patterns

Strategy: type-guard

Validate before calling

// before evaluating complex(re, im): imaginary part must be int/float
im, err := evalLoad(im)
if err != nil || im.Value == nil ||
    (im.Value.Kind() != constant.Int && im.Value.Kind() != constant.Float) {
    return fmt.Errorf("imaginary part must be numeric, got %s", im.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 2") {
    // inspect/fix the imaginary-part expression before retrying
}

Prevention

When it happens

Trigger: Evaluating `complex(re, bad)` where the second argument is a string, bool, struct, or whose value failed to load (imagev.Value == nil) (eval.go:1991, complexBuiltin).

Common situations: Swapping arguments so a complex value lands in the imaginary slot, passing a string literal as the imaginary part, or evaluating a variable whose memory read failed.

Related errors


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