xai-org/x-algorithm · error · SemanticCheckFailure

type checking expression %s failed: expects 2 arguments, %d

Error message

type checking expression %s failed: expects 2 arguments, %d passed

What it means

ArithmeticOperation.validateSignature throws SemanticCheckFailure during semantic checking when an arithmetic expression node (+, -, etc. built via ofSum or sibling factories) does not have exactly 2 children. Binary arithmetic operators are strictly two-operand; malformed ASTs are rejected before evaluation.

Source

Thrown at botmaker/src/java/com/twitter/botmaker/function/math/ArithmeticOperation.java:145

    return new ArithmeticOperation<T>(exprText, children) {
      @Override
      public Signature getSignature() {
        return signature;
      }

      @Override
      protected Object apply(Context<Runtime> context, T numA, T numB) {
        return function.apply(context, numA, numB);
      }
    };
  }

  private static void validateSignature(
      String exprText,
      ImmutableList<ASTNode> children
  ) throws SemanticCheckFailure {
    if (children.size() != 2) {
      throw new SemanticCheckFailure(String.format(
          "type checking expression %s failed: expects 2 arguments, %d passed",
          exprText,
          children.size()
      ));
    }
  }

  public static ArithmeticOperation ofSum(String exprText, ImmutableList<ASTNode> children)
      throws SemanticCheckFailure {
    validateSignature(exprText, children);
    if (children.get(0).getReturnType().longLike()
        && children.get(1).getReturnType().longLike()) {
      return of(exprText, children, LONG_SIGNATURE, ADD_FUNCTION);
    } else if (children.get(0).getReturnType().stringLike()
        && children.get(1).getReturnType().stringLike()) {
      return of(exprText, children, STRING_SIGNATURE, CONCAT_FUNCTION);
    } else {
      return of(exprText, children, DOUBLE_SIGNATURE, ADD_FUNCTION);

View on GitHub (pinned to 24c60942c5)

Solutions

  1. Fix the AST construction so the arithmetic node gets exactly 2 children
  2. If you intended n-ary summation, use Sum()/an n-ary function instead of the binary ofSum
  3. Add a unit assertion on children.size() when building binary nodes to catch this early

Example fix

// before
ArithmeticOperation.ofSum(node, ImmutableList.of(a)); // 1 child -> failure
// after
ArithmeticOperation.ofSum(node, ImmutableList.of(a, b)); // exactly 2
Defensive patterns

Strategy: validation

Validate before calling

if (children.size() != 2) {
  throw new IllegalArgumentException("arithmetic node needs exactly 2 operands: " + children);
}
ArithmeticOperation.ofSum(node, children);

Try / catch

try {
  return ArithmeticOperation.ofSum(node, children);
} catch (SemanticCheckFailure e) {
  throw new IllegalStateException("malformed arithmetic expression: " + exprText, e);
}

Prevention

When it happens

Trigger: Building an ASTNode for sum/subtraction with 1 or 3+ children, e.g. calling ofSum with an ImmutableList of children whose size != 2, or hand-constructing arithmetic nodes from parsed fragments.

Common situations: Custom AST construction, expression parser bugs producing malformed trees, or refactorings that change child counts of binary operators.

Related errors


AI-assisted analysis of xai-org/x-algorithm@24c60942c5 (2026-08-28). Data as JSON: /api/errors/e8366a52cbba6a5c. Report an issue: GitHub.