Skip to main content

Module examples

Module examples 

Source
Expand description

Behavioral evaluation of signature-level examples { ... } blocks (#369 slice 2).

Slice 1 (PR #370) shipped the AST + parser + type-checking of example args and expected values against the function’s signature. This pass takes the next step: it actually runs each example through the bytecode VM and compares the result to the declared expected value. A mismatch becomes a TypeError::ExampleMismatch with rule_tag = "example-mismatch", surfaced through the same structured-JSON error envelope as every other lex check diagnostic.

§Implementation strategy

For each pure function with non-empty examples, synthesize a small set of zero-argument helper functions and append them as new stages alongside the original program:

  • __ex_<fn>_<K>_arg_<I> returning the Ith argument of case K.
  • __ex_<fn>_<K>_expected returning the declared expected value of case K.

Compile the augmented program to bytecode (the user’s program plus the helpers all see the same global scope), and for each case:

  1. Call each __ex_<fn>_<K>_arg_<I> helper through the VM to get a runtime Value for the argument.
  2. Call the original function with those values to get the actual Value.
  3. Call __ex_<fn>_<K>_expected to get the declared Value.
  4. Compare the two via Value’s PartialEq. On mismatch, emit ExampleMismatch with pretty-printed expected and got.

§v1 restrictions

  • Generic functions (with type_params) are skipped — the helper synthesis would need to monomorphize. Examples on generic functions still get the slice-1 type-level checks; they just don’t get behavioral checks. Worth a follow-up issue if examples on generics become a real need.
  • Pure-only (already enforced by ExamplesOnEffectfulFn in slice 1).

Functions§

evaluate_examples
Run the behavioral-evaluation pass over stages and return any ExampleMismatch errors discovered. Returns the empty vec when every example case passes (or when there are no eligible cases).