Error Handling

MIND provides comprehensive error diagnostics with detailed messages, source locations, and actionable suggestions.

Error Categories

CategoryCodeDescription
ParseE0001-E0099Syntax errors
TypeE0100-E0199Type mismatches, inference failures
ShapeE0200-E0299Dimension mismatches, broadcast failures
AutodiffE0300-E0399Non-differentiable operations
DeviceE0400-E0499Placement and transfer errors
RuntimeE0500-E0599Execution errors

Example Diagnostics

Shape mismatch error:

error[E0201]: shape mismatch in matmul
  --> model.mind:12:15
   |
12 |     let y = a @ b;
   |               ^ cannot multiply tensors with shapes [2, 3] and [4, 5]
   |
   = help: matmul requires inner dimensions to match
   = note: expected shape [2, 3] @ [3, ?], got [2, 3] @ [4, 5]

Type error with suggestion:

error[E0102]: type mismatch
  --> train.mind:8:20
   |
 8 |     let loss: i32 = mse(pred, target);
   |               ---   ^^^^^^^^^^^^^^^^^
   |               |     expected i32, found f32
   |               expected due to this
   |
   = help: consider changing the type annotation
   |
 8 |     let loss: f32 = mse(pred, target);
   |               ~~~

Runtime Errors

Runtime errors occur during execution and include stack traces:

runtime error[E0501]: index out of bounds
  --> inference.mind:24:10
   |
24 |     x[i]
   |     ^^^^ index 10 is out of bounds for tensor of length 5
   |
stack trace:
  0: get_element at inference.mind:24:10
  1: process_batch at inference.mind:18:5
  2: main at inference.mind:5:3

Result Type

For recoverable errors, use the Result type:

fn load_model(path: str) -> Result<Model, Error> {
    if !exists(path) {
        return Err(Error::NotFound(path));
    }
    // ...
    Ok(model)
}

fn main() {
    match load_model("model.mind.bin") {
        Ok(model) => run(model),
        Err(e) => print("Failed to load: ", e),
    }
}

Learn More

See the full error catalog at mind-spec/errors.md.