go-delve/delve · error

operator %s can not be applied to %q

Error message

operator %s can not be applied to %q

What it means

Thrown by evalop.Unary handling: when the operand variable xv.Value is nil (its constant value could not be produced — the variable is unreadable, not a simple value, or its contents weren't loaded), applying a unary operator would panic inside constant.UnaryOp, so Delve pre-empts it with this descriptive error naming the operator and the operand expression.

Source

Thrown at pkg/proc/eval.go:2410

	r = constant.Compare(x, op, y)
	return
}

// Evaluates expressions: -<subexpr> and +<subexpr>
func (scope *EvalScope) evalUnary(op *evalop.Unary, stack *evalStack) {
	xv := stack.pop()

	xv.loadValue(loadSingleValue)
	if xv.Unreadable != nil {
		stack.err = xv.Unreadable
		return
	}
	if xv.FloatSpecial != 0 {
		stack.err = errOperationOnSpecialFloat
		return
	}
	if xv.Value == nil {
		stack.err = fmt.Errorf("operator %s can not be applied to %q", op.Node.Op.String(), astutil.ExprToString(op.Node.X))
		return
	}
	rc, err := constantUnaryOp(op.Node.Op, xv.Value)
	if err != nil {
		stack.err = err
		return
	}
	if xv.DwarfType != nil {
		r := xv.newVariable("", 0, xv.DwarfType, scope.Mem)
		r.Value = rc
		stack.push(r)
		return
	}
	stack.push(newConstant(rc, xv.bi, xv.mem))
}

func negotiateType(op token.Token, xv, yv *Variable) (godwarf.Type, error) {
	if xv == nilVariable {

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Print the operand (`print x`) first; if unreadable/optimized-out, rebuild with `-gcflags='all=-N -l'` or step to a point where the variable is live.
  2. Only apply unary operators to scalars: `-`/`^` on ints/floats, `!` on bools.
  3. Re-enter the expression after the variable has been assigned so its value can be loaded.
  4. If a breakpoint condition triggers it, simplify the condition to reference only live scalar variables.

Example fix

// before (x is a struct / unreadable)
(dlv) print -x
operator - can not be applied to "-x"

// after
(dlv) print x.field // evaluate a numeric member first
(dlv) print -x.field // negate the scalar
Defensive patterns

Strategy: validation

Validate before calling

// Verify the operand loaded a value before unary ops:
(dlv) print x        // if unreadable/optimized-out, fix build flags or location
// only apply ! to bool, -/^ to numeric scalars

Type guard

func isScalar(v interface{}) bool {
    k := reflect.TypeOf(v).Kind()
    return (k >= reflect.Bool && k <= reflect.Complex128)
}

Prevention

When it happens

Trigger: Evaluating `-x`, `!x`, `^x` where x is a variable whose Value field is nil — e.g. x is a struct/map/chan value with no scalar constant, a variable that failed to load (Unreadable), an optimized-out variable, or a value not materialized by the evaluator before the unary op runs.

Common situations: Negating a variable that was optimized out by the compiler, applying `!` to a non-bool composite value, or unary ops in breakpoint conditions on variables Delve couldn't read at the current stop point.

Related errors


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