lutzroeder/netron · error · Error

Unexpected affine expression kind '${this.kind}'.

Error message

Unexpected affine expression kind '${this.kind}'.

What it means

Thrown while printing/serializing an MLIR affine expression whose 'kind' does not match any known AffineExprKind (Add, Mul, Mod, FloorDiv, CeilDiv). The printer switch falls through to the default case, meaning the expression object was constructed with an invalid or undefined kind value.

Source

Thrown at source/mlir.js:1422

    constructor(kind, lhs, rhs) {
        super(kind);
        this.lhs = lhs;
        this.rhs = rhs;
    }

    isSymbolicOrConstant() {
        return this.lhs.isSymbolicOrConstant() && this.rhs.isSymbolicOrConstant();
    }

    printAffineExprInternal(enclosingStrong) {
        let op = '';
        switch (this.kind) {
            case _.AffineExprKind.Add: op = ' + '; break;
            case _.AffineExprKind.Mul: op = ' * '; break;
            case _.AffineExprKind.Mod: op = ' mod '; break;
            case _.AffineExprKind.FloorDiv: op = ' floordiv '; break;
            case _.AffineExprKind.CeilDiv: op = ' ceildiv '; break;
            default: throw new Error(`Unexpected affine expression kind '${this.kind}'.`);
        }
        const isAdd = this.kind === _.AffineExprKind.Add;
        const needsParens = enclosingStrong === true;
        if (!isAdd) {
            if (this.kind === _.AffineExprKind.Mul && this.rhs instanceof _.AffineConstantExpr && this.rhs.value === -1) {
                const inner = `-${this.lhs.printAffineExprInternal(true)}`;
                return needsParens ? `(${inner})` : inner;
            }
            const inner = `${this.lhs.printAffineExprInternal(true)}${op}${this.rhs.printAffineExprInternal(true)}`;
            return needsParens ? `(${inner})` : inner;
        }
        if (this.rhs instanceof _.AffineBinaryOpExpr && this.rhs.kind === _.AffineExprKind.Mul &&
            this.rhs.rhs instanceof _.AffineConstantExpr && this.rhs.rhs.value === -1) {
            const lhsStr = this.lhs.printAffineExprInternal(false);
            const rhsNeedsParens = this.rhs.lhs instanceof _.AffineBinaryOpExpr && this.rhs.lhs.kind === _.AffineExprKind.Add;
            const rhsStr = this.rhs.lhs.printAffineExprInternal(rhsNeedsParens);
            const inner = `${lhsStr} - ${rhsStr}`;
            return needsParens ? `(${inner})` : inner;

View on GitHub (pinned to d8a543f5f8)

Solutions

  1. Inspect this.kind at the call site — it is likely undefined; ensure the AffineBinaryOpExpr constructor receives a valid _.AffineExprKind value.
  2. Check that the affine map data being parsed was produced by a compatible MLIR dialect version.
  3. Guard before printing: if (this.kind === undefined) fall back to raw operand printing or skip.

Example fix

// before
const expr = new mlir.AffineBinaryOpExpr(undefined, lhs, rhs);
expr.printAffineExprInternal(); // throws

// after
const expr = new mlir.AffineBinaryOpExpr(mlir.AffineExprKind.Add, lhs, rhs);
expr.printAffineExprInternal();
Defensive patterns

Strategy: validation

Validate before calling

const validKinds = new Set(Object.values(mlir.AffineExprKind));
if (!validKinds.has(expr.kind)) {
    throw new Error(`Cannot print affine expression with kind=${expr.kind}`);
}

Type guard

function isPrintableAffineExpr(e) {
    const kinds = new Set(Object.values(mlir.AffineExprKind));
    return e != null && kinds.has(e.kind);
}

Try / catch

try { out = expr.printAffineExprInternal(); }
catch (e) { if (/Unexpected affine expression kind/.test(e.message)) out = '<invalid-affine-expr>'; else throw e; }

Prevention

When it happens

Trigger: Calling printAffineExprInternal (directly or via toString/print on an AffineExpr) on an expression whose kind is undefined or an out-of-range enum value, e.g. building AffineBinaryOpExpr without passing kind, or deserializing an affine map with a corrupt kind field.

Common situations: Constructing affine expressions manually for MLIR map parsing/printing, version skew between the bundled MLIR enum values and parsed input, or a half-initialized object where kind was never set.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.


AI-assisted analysis of lutzroeder/netron@d8a543f5f8 (2026-08-27). Data as JSON: /api/errors/6e5ab50a5f611fcb. Report an issue: GitHub.