blue_lang_runtime/pipeline.rs
1//! The blue pipeline: **parse → check → erase → run**, in that order, once.
2//!
3//! The order is the whole reason this module exists. Each stage is available
4//! separately for tools that want one, but the *default* path is a single
5//! function, because two of the four orderings are silently wrong:
6//!
7//! - **Erase before check** discards every annotation, so a program with type
8//! errors passes. The checker sees `(define …)` and has nothing to check.
9//! - **Run before check** reports a type error after the side effects.
10//!
11//! Neither fails loudly. Both produce a green run on a program that should
12//! have been rejected. Leaving the order to each caller means every caller
13//! is one reordering away from turning the type checker off — so the order
14//! lives here, and callers ask for a *result*, not a sequence of steps.
15
16use tatara_lisp::Sexp;
17use tatara_lisp_eval::Value;
18
19use crate::erase::erase_types;
20use crate::inputs::Inputs;
21use crate::uses::Entry;
22
23/// Why a run stopped short.
24#[derive(Debug, thiserror::Error)]
25pub enum RunError {
26 #[error("parse error: {0}")]
27 Parse(String),
28 /// The type checker rejected the program. Carries every diagnostic, not
29 /// just the first: a caller fixing one error wants to see the rest.
30 ///
31 /// Each one is already rendered `file:line:col: message` against the file
32 /// the offending form came from — which, in a program with imports, is
33 /// frequently not the file the user named. The rendering happens here
34 /// rather than at the consumer because here is where the file table exists;
35 /// see `uses::ResolvedProgram::locate`.
36 #[error("{} type error(s):\n{}", .0.len(), .0.join("\n"))]
37 Types(Vec<String>),
38 /// **No longer reachable, and that is the point.** This reported "blue
39 /// emitted a tree the reader could not read back" — a failure only a
40 /// print-then-reparse hop could have. [`crate::lower_to_spanned`] deleted
41 /// the hop, so there is nothing left to fail: the tree the evaluator gets
42 /// IS the tree erasure produced, not a re-reading of its text.
43 ///
44 /// Kept rather than removed because it is public API on a released crate
45 /// and a consumer may still match on it, per ★★ MODULARIZE, DON'T DELETE.
46 /// It is retired, not orphaned — if a future stage ever serialises again
47 /// it has a typed home. **Nothing constructs it today**; do not read its
48 /// presence as evidence the pipeline can still fail this way.
49 #[error("the emitted tatara-lisp could not be read back: {0}")]
50 Lower(String),
51 /// The program ran and raised.
52 ///
53 /// Rendered `file:line:col: message` against the file the failing form's
54 /// top-level form came from — the same `uses::ResolvedProgram::locate`
55 /// machinery, the same file table and the same join key the type errors
56 /// above use. Until erasure learned to carry spans this was a bare
57 /// message, because the evaluator was handed a tree whose every node had
58 /// been stamped `Span::synthetic()` on the way in.
59 ///
60 /// **The honest limit, stated so nobody reads more into a position than is
61 /// there.** The index names the top-level form that was EXECUTING; the span
62 /// names where the failing text SITS. Those agree on the file whenever the
63 /// failing code is in the same file as the top-level form that reached it —
64 /// which is every single-file program, and an imported package whose own
65 /// top-level code raises. They can disagree when a top-level form in file A
66 /// calls a function defined in file B and the raise happens inside B: the
67 /// path reported is A's. `locate` refuses to print a `line:col` it cannot
68 /// justify (the span must be a real range in that file), so the usual shape
69 /// of that case is a file with no position rather than a precise-looking
70 /// wrong one — but a same-length pair of files can still put a plausible
71 /// number on the wrong file. Closing it needs per-frame file identity at
72 /// the evaluator, which is a call stack blue does not have.
73 #[error("runtime error: {0}")]
74 Eval(String),
75 /// A `use("name")` could not be resolved.
76 ///
77 /// Its own variant rather than folded into `Parse`, because the reader's
78 /// next action is different: a parse error is in the source in front of
79 /// them, an import error is in their packaging — a missing bidama, a
80 /// BLUE_PATH that does not contain it, or no loader at all.
81 #[error("import error: {0}")]
82 Import(String),
83}
84
85/// What a run produced, plus what the checker did on the way.
86#[derive(Debug)]
87pub struct Run {
88 pub value: Value,
89 /// Nodes the type walk visited. Zero for a fully untyped program — this
90 /// is what makes "no annotations, no analysis" a *measurement* rather
91 /// than a claim.
92 pub visited: usize,
93 /// Declarations that carried an annotation.
94 pub typed_decls: usize,
95 /// Boundaries where typed code meets untyped code.
96 pub seams: usize,
97}
98
99/// Parse blue source to tatara-lisp forms.
100pub fn parse(src: &str) -> Result<Vec<Sexp>, RunError> {
101 parse_with_depth(src, blue_lang_syntax::MAX_EXPR_DEPTH)
102}
103
104/// [`parse`] with the parser's nesting bound supplied by the caller.
105///
106/// The bound exists so a stack overflow — which `catch_unwind` cannot catch —
107/// arrives as a typed `Err` instead. It is a *limit*, not a dialect: raising
108/// it changes no program's meaning, which is exactly why it is safe to expose
109/// as configuration (`blue-lang-cli`'s `config` module holds the rule).
110pub fn parse_with_depth(src: &str, max_depth: usize) -> Result<Vec<Sexp>, RunError> {
111 blue_lang_syntax::parse_program_with_depth(src, max_depth)
112 .map_err(|e| RunError::Parse(e.to_string()))
113}
114
115/// [`parse_with_depth`] keeping **every node's** source span.
116///
117/// The door for anything that will report a position to a human. It exists here,
118/// beside the spanless one, so a caller that wants spans still parses under the
119/// CONFIGURED nesting bound — a separate `blue_lang_syntax` call would be the
120/// second door `parse_with_depth`'s own docs exist to prevent, with
121/// `max_expr_depth` true of some subcommands and not others.
122pub fn parse_tree_with_depth(
123 src: &str,
124 max_depth: usize,
125) -> Result<Vec<blue_lang_syntax::Spanned>, RunError> {
126 blue_lang_syntax::parse_program_tree_with_depth(src, max_depth)
127 .map_err(|e| RunError::Parse(e.to_string()))
128}
129
130/// [`parse`] keeping **every node's** source span.
131///
132/// The spanned twin of [`parse`], at the same default bound — the door for
133/// anything downstream of a parse that will report a position, which since
134/// `use` learned to carry file identity is every path through
135/// [`run_in_surface`].
136pub fn parse_tree(src: &str) -> Result<Vec<blue_lang_syntax::Spanned>, RunError> {
137 parse_tree_with_depth(src, blue_lang_syntax::MAX_EXPR_DEPTH)
138}
139
140/// Run blue source with no build inputs.
141pub fn run(src: &str) -> Result<Run, RunError> {
142 run_with_inputs(src, Inputs::new())
143}
144
145/// Run blue source, giving the macro phase access to verified build inputs.
146///
147/// `inputs` is already verified — [`Inputs`] cannot hold bytes that do not match
148/// their declared hash — so nothing here re-checks. The capability a macro gains
149/// is exactly "these hashed bytes", never a path.
150pub fn run_with_inputs(src: &str, inputs: Inputs) -> Result<Run, RunError> {
151 run_with_loader(src, inputs, &crate::uses::NoLoader)
152}
153
154/// Run blue source with a loader, so `use("name")` can resolve.
155///
156/// Split from [`run_with_inputs`] rather than folded into it because loading a
157/// package reads a filesystem, and this crate has a `wasm32-unknown-unknown`
158/// consumer with zero host imports. The capability is injected by callers that
159/// have it — `blue_lang_pkg::LoadPath` is the real one — and absent by default,
160/// where a `use` is a typed error naming the package.
161pub fn run_with_loader(
162 src: &str,
163 inputs: Inputs,
164 loader: &dyn crate::uses::Loader,
165) -> Result<Run, RunError> {
166 run_in_surface(Entry::anonymous(src), inputs, loader, None)
167}
168
169/// Run blue source written in a `yakugo` surface.
170///
171/// The pack applies at PARSE time and nowhere else — by the time the checker
172/// sees the program it is canonical, so every stage below is identical whatever
173/// surface the author wrote in. That is what makes a surface a surface: it
174/// changes how a program is spelled and nothing about how it runs.
175///
176/// `entry` carries the source AND the file it was read from, because a type
177/// error has to be reported somewhere: a caller with a path should pass it, and
178/// one without ([`Entry::anonymous`]) gets diagnostics that say so rather than
179/// diagnostics that guess. Imported packages name themselves through the
180/// loader either way.
181///
182/// # Errors
183///
184/// As [`run_with_loader`].
185pub fn run_in_surface(
186 entry: Entry<'_>,
187 inputs: Inputs,
188 loader: &dyn crate::uses::Loader,
189 surface: Option<&blue_lang_syntax::yakugo::Yakugo>,
190) -> Result<Run, RunError> {
191 let forms = match surface {
192 Some(pack) => blue_lang_syntax::parse_program_tree_in(entry.text, pack)
193 .map_err(|e| RunError::Parse(e.to_string()))?,
194 None => parse_tree(entry.text)?,
195 };
196
197 // RESOLVE imports first, so everything below sees ONE program.
198 //
199 // Before the check on purpose: imported code is type-checked at the point
200 // its consumer imports it, rather than at whatever later moment its code
201 // first runs. A package that does not typecheck should break its importer's
202 // build, not their production run.
203 //
204 // One program, but not one FILE: the result records which file each
205 // top-level form came from, which is what lets a diagnostic below name a
206 // place instead of only a problem.
207 let mut program = crate::uses::resolve_uses(forms, entry, loader).map_err(RunError::Import)?;
208
209 // `test` blocks are declarations for the harness, not code to run.
210 //
211 // Dropped here rather than in `resolve_uses`, because `blue test` calls
212 // the resolver and then NEEDS the entry file's blocks — so the two
213 // callers want different things and the split has to live at this level.
214 //
215 // Without this, `blue run` on a file containing its own tests fails with
216 // `unbound symbol: deftest`: every package in the bidama distribution
217 // carries tests, so every one of them was unrunnable.
218 //
219 // Through `retain`, which drops each form's owner with it. A plain filter
220 // over the forms alone would slide every later form onto the wrong file.
221 program.retain(|f| !crate::uses::is_test_form(f));
222
223 // CHECK, on the annotated tree — the only tree that has annotations.
224 //
225 // **On the REAL spanned tree, including every imported package's.** This
226 // was the one caller that checked a spanless lift, because `resolve_uses`
227 // flattened the entry file and its imports into one list and `Span` is a
228 // byte range with no file identity — so a real span here would have
229 // reported an imported package's error at that offset in the ENTRY file, a
230 // precise-looking answer pointing at unrelated code.
231 //
232 // The fix is not a wider `Span` (that type is upstream, and its own docs
233 // put file identity on the caller: spans "are meaningful only relative to
234 // the string that produced them, which the caller is responsible for
235 // holding onto"). blue holds onto it BESIDE the span, per top-level form —
236 // see `uses::ResolvedProgram`.
237 let outcome = blue_lang_check::check_program(program.forms());
238 if !outcome.ok() {
239 return Err(RunError::Types(
240 outcome
241 .diagnostics
242 .iter()
243 // `file:line:col: message`, resolved against the file the form
244 // actually came from. A typed `Display` builds it, per ★★ TYPED
245 // EMISSION — `locate` returns the renderer, not a string.
246 .map(|d| program.locate(d.top_level, d.span, &d.message).to_string())
247 .collect(),
248 ));
249 }
250
251 // ERASE, so the interpreter never sees a type — **on the spanned tree,
252 // and back out as one.**
253 //
254 // This used to be `erase_types(&program.sexps())` followed by a
255 // `lower_to_spanned` that stamped `Span::synthetic()` over every node, and
256 // the comment here said carrying real positions through was "a larger
257 // piece and is NOT built". It was not larger: erasure only ever DELETES
258 // nodes — the single node it invents is the `define` replacing
259 // `define-typed` — so there was never anything for a synthetic span to
260 // stand in for. See `crate::erase`.
261 //
262 // No lowering step follows. The tree the evaluator receives IS the tree the
263 // parser built, minus annotations, with the author's byte offsets intact.
264 let erased = erase_types(program.forms());
265
266 let mut interp = crate::interpreter_hostless();
267 crate::inputs::install_input_primitives(&mut interp, inputs);
268
269 // RUN, one top-level form at a time, **counting them**.
270 //
271 // This is literally the loop `Interpreter::eval_program` runs internally
272 // (tatara-lisp-eval `eval.rs`) — unrolled here for exactly one reason: the
273 // INDEX. `resolve_uses` flattens N files into one form list and a `Span` is
274 // a byte range with no file identity, so an offset alone is ambiguous
275 // across files — 66 means something in every one of them. The index is the
276 // join key back to `ResolvedProgram`'s file table, and `eval_program`
277 // consumes it internally and hands back only the error.
278 //
279 // The same key, the same renderer and the same file table the type errors
280 // above already use. A runtime error was the one diagnostic in this
281 // function still reporting a bare message.
282 let mut value = Value::Nil;
283 for (top_level, form) in erased.iter().enumerate() {
284 value = interp.eval_top_form(form, &mut ()).map_err(|e| {
285 // `span()` is `None` for the arms that genuinely have no position
286 // (`Reader`, `Halted`, `NotImplemented`). Synthetic is the honest
287 // stand-in — `locate` renders it as a file with no line rather
288 // than inventing one.
289 let at = e.span().unwrap_or_else(tatara_lisp::Span::synthetic);
290 // `short_message`, NOT `Display`. Upstream's `Display` ends every
291 // positioned arm with ` at {span}` — a raw byte range — and once
292 // blue prefixes a resolved `path:line:col` that suffix is the same
293 // fact told twice, the second time in a unit no reader can spend.
294 //
295 // Worse than redundant: a byte range is exactly the thing this
296 // whole file-identity apparatus exists to stop being reported on
297 // its own, because an offset means something different in every
298 // file. On the one case blue deliberately declines to place — a
299 // raise inside a callee from another file — `Display` would print
300 // the CALLEE's offsets beside the CALLER's path, re-creating the
301 // precise-looking wrong answer `locate`'s guard just refused.
302 // `short_message` is upstream's own "no source context" accessor,
303 // and blue supplies the context.
304 let message = e.short_message();
305 RunError::Eval(program.locate(top_level, at, &message).to_string())
306 })?;
307 }
308
309 Ok(Run {
310 value,
311 visited: outcome.stats.visited,
312 typed_decls: outcome.stats.typed_decls,
313 seams: outcome.seams.len(),
314 })
315}
316
317/// **A runtime error names a place.** The gates for the last stage that
318/// reported a bare message.
319///
320/// Each fixture is sized so that resolving the failing span against ANY file
321/// but the right one lands somewhere else — a different path and a different
322/// line — so a green run here is evidence about the join and not about the
323/// renderer being called at all.
324#[cfg(test)]
325mod position_tests {
326 use super::*;
327 use crate::uses::{Entry, Loader};
328 use std::collections::BTreeMap;
329 use std::path::Path;
330
331 struct MemLoader(BTreeMap<&'static str, &'static str>);
332
333 impl Loader for MemLoader {
334 fn load(&self, name: &str) -> Result<Vec<(String, String)>, String> {
335 self.0
336 .get(name)
337 .map(|s| vec![(format!("{name}.b"), (*s).to_owned())])
338 .ok_or_else(|| format!("no bidama named \"{name}\""))
339 }
340 }
341
342 /// A package whose OWN top-level code raises, deep inside a file that is
343 /// deliberately taller than either of the other two.
344 ///
345 /// Byte 66 — where `kore_wa_sonzai_shinai` starts — is past the end of the
346 /// entry file (30 bytes) and past the end of `kotae.b` (21 bytes). So the
347 /// only file this span can honestly resolve against is this one, and the
348 /// other two now refuse to answer rather than walking off their ends.
349 const BAKUHATSU: &str = "\
350def tsukawanai_a()
351 1
352end
353
354def tsukawanai_b()
355 2
356end
357
358def bakuhatsu()
359 kore_wa_sonzai_shinai()
360end
361
362bakuhatsu()";
363
364 const KOTAE: &str = "def kotae()\n 42\nend";
365
366 /// The same failing call, but only ever reached by a CALLER in another
367 /// file — the shape that exercises `RunError::Eval`'s stated limit.
368 const BAKUHATSU2: &str = "def yobu()\n kore_wa_sonzai_shinai()\nend";
369
370 fn loader() -> MemLoader {
371 MemLoader(BTreeMap::from([
372 ("kotae", KOTAE),
373 ("bakuhatsu", BAKUHATSU),
374 ("bakuhatsu2", BAKUHATSU2),
375 ]))
376 }
377
378 fn run_named(path: &str, src: &str) -> RunError {
379 run_in_surface(
380 Entry {
381 path: Some(Path::new(path)),
382 text: src,
383 },
384 Inputs::new(),
385 &loader(),
386 None,
387 )
388 .expect_err("the fixture must raise")
389 }
390
391 /// **The load-bearing gate: a raise inside an IMPORTED package reports the
392 /// IMPORTED file's path and line.**
393 ///
394 /// Hand-computed. In `bakuhatsu.b` the failing call sits on line 10 at
395 /// column 3, and the whole point of the fixture's height is that no other
396 /// file in the program can produce that pair: the entry file has 2 lines
397 /// and `kotae.b` has 3, so *any* mis-attribution is visible as a different
398 /// path AND a different position, never as a coincidence.
399 ///
400 /// **Three red runs, one per edit this change makes** — each isolates a
401 /// different half of the mechanism, which is what stops the gate being a
402 /// tautology over "some renderer ran".
403 ///
404 /// 1. `eval_program` + `.map_err(|e| RunError::Eval(e.to_string()))`
405 /// restored (the pre-change code):
406 /// ```text
407 /// left: "runtime error: unbound symbol: kore_wa_sonzai_shinai at 74..95"
408 /// right: "runtime error: bakuhatsu.b:10:3: unbound symbol `kore_wa_sonzai_shinai`"
409 /// ```
410 /// No place at all — the state this change is fixing — and the trailing
411 /// `at 74..95` is upstream's `Display`, an offset with no file, which is
412 /// why the loop renders `short_message` instead.
413 ///
414 /// 2. The loop kept, but `0` passed to `locate` in place of `top_level`:
415 /// ```text
416 /// left: "runtime error: kotae.b: unbound symbol `kore_wa_sonzai_shinai`"
417 /// right: "runtime error: bakuhatsu.b:10:3: unbound symbol `kore_wa_sonzai_shinai`"
418 /// ```
419 /// **This is the one that proves the INDEX is load-bearing** rather than
420 /// the renderer: form 0 belongs to `kotae.b`, so the wrong file is named
421 /// — and named without a position, because the guard in `locate` sees
422 /// that byte 74 is not a range in a 21-byte file and declines to invent
423 /// one. A fixture with one imported package could not tell these apart.
424 ///
425 /// 3. `crate::lower_to_spanned(&crate::to_sexps(&erased))` reinstated
426 /// between erasure and the loop:
427 /// ```text
428 /// left: "runtime error: bakuhatsu.b: unbound symbol `kore_wa_sonzai_shinai`"
429 /// right: "runtime error: bakuhatsu.b:10:3: unbound symbol `kore_wa_sonzai_shinai`"
430 /// ```
431 /// The right FILE with no position — which is exactly the shape of the
432 /// bug: the index survived the lift and every span did not.
433 #[test]
434 fn a_raise_inside_an_imported_package_names_that_package() {
435 let err = run_named("entry.b", "use(\"kotae\")\nuse(\"bakuhatsu\")\n");
436 assert_eq!(
437 err.to_string(),
438 "runtime error: bakuhatsu.b:10:3: unbound symbol `kore_wa_sonzai_shinai`"
439 );
440 }
441
442 /// Anti-vacuity for the fixture itself: the position asserted above is a
443 /// FACT ABOUT `bakuhatsu.b`, checked against the source text rather than
444 /// against the thing that produced it.
445 ///
446 /// Without this, `10:3` is just a number that happened to come out of the
447 /// code under test, and a change that moved every reported line by one
448 /// would move this assertion with it.
449 #[test]
450 fn line_10_column_3_of_the_fixture_is_the_failing_call() {
451 let line = BAKUHATSU.lines().nth(9).expect("line 10 exists");
452 assert_eq!(line, " kore_wa_sonzai_shinai()");
453 assert_eq!(
454 &line[2..],
455 "kore_wa_sonzai_shinai()",
456 "column 3 (1-indexed) is where the call starts"
457 );
458 // And the other two files cannot reach that far, which is what makes a
459 // mis-attribution loud instead of plausible.
460 let offset = BAKUHATSU.find("kore_wa_sonzai_shinai").expect("in fixture");
461 assert!(
462 offset > KOTAE.len(),
463 "kotae.b could answer for byte {offset}"
464 );
465 assert!(
466 offset > "use(\"kotae\")\nuse(\"bakuhatsu\")\n".len(),
467 "the entry file could answer for byte {offset}"
468 );
469 }
470
471 /// **The `FileId(0)` path: a raise in the ENTRY file reports the entry
472 /// file.** The single-file case, which is every program that imports
473 /// nothing — and the one where the index and the span cannot disagree.
474 ///
475 /// **Red run** (2026-08-12), `eval_program` +
476 /// `.map_err(|e| RunError::Eval(e.to_string()))` restored:
477 /// ```text
478 /// left: "runtime error: unbound symbol: nani_mo_nai at 17..28"
479 /// right: "runtime error: honmono.b:5:1: unbound symbol `nani_mo_nai`"
480 /// ```
481 ///
482 /// **Second red run**, `lower_to_spanned` reinstated after erasure — the
483 /// one that isolates the SPAN half from the index half, since a
484 /// single-file program cannot get its file wrong:
485 /// ```text
486 /// left: "runtime error: honmono.b: unbound symbol `nani_mo_nai`"
487 /// right: "runtime error: honmono.b:5:1: unbound symbol `nani_mo_nai`"
488 /// ```
489 #[test]
490 fn a_raise_in_the_entry_file_names_the_entry_file() {
491 let err = run_named("honmono.b", "def f()\n 1\nend\n\nnani_mo_nai()\n");
492 assert_eq!(
493 err.to_string(),
494 "runtime error: honmono.b:5:1: unbound symbol `nani_mo_nai`"
495 );
496 }
497
498 /// A raise from a form that came through the SURFACE-level erasure still
499 /// reports a real position — the annotated path, where erasure rewrites
500 /// the node rather than passing it through.
501 ///
502 /// Separate from the two above because the erasure rewrite is the only
503 /// place a node is built rather than kept, and a synthetic span there
504 /// would be invisible to a fixture whose failing form is untouched.
505 ///
506 /// **Red run** (2026-08-12), `lower_to_spanned` reinstated after erasure:
507 /// ```text
508 /// left: "runtime error: chuu.b: unbound symbol `mada_nai`"
509 /// right: "runtime error: chuu.b:2:3: unbound symbol `mada_nai`"
510 /// ```
511 #[test]
512 fn a_raise_inside_an_annotated_def_still_names_its_line() {
513 let err = run_named(
514 "chuu.b",
515 "def f(a: Int) -> Int\n mada_nai(a)\nend\n\nf(1)\n",
516 );
517 assert_eq!(
518 err.to_string(),
519 "runtime error: chuu.b:2:3: unbound symbol `mada_nai`"
520 );
521 }
522
523 /// **The stated limit, pinned rather than left to be discovered.**
524 ///
525 /// A top-level form in the entry file calls a function defined in an
526 /// imported one, and the raise happens inside the callee. The index names
527 /// the executing form (the entry's), the span names the callee's bytes,
528 /// and the two disagree about the file. `locate`'s in-range guard is what
529 /// keeps that from rendering as a precise-looking `entry:line:col`: the
530 /// offset is not a range in the entry file, so no position is printed.
531 ///
532 /// This test asserts the CURRENT behaviour, including the part that is
533 /// wrong — the path is the caller's. It is here so the next author reads
534 /// the limit off a green suite instead of off a bug report. Closing it
535 /// needs per-frame file identity at the evaluator; see `RunError::Eval`.
536 ///
537 /// **Red run** (2026-08-12), the `span.end <= file.text.len()` guard
538 /// removed from `uses::ResolvedProgram::locate` — and it is the ONLY test
539 /// in the crate that moves, which is the same run's evidence that the
540 /// guard is a no-op on every check-time diagnostic:
541 /// ```text
542 /// left: "runtime error: yobidashi.b:1:14: unbound symbol `kore_wa_sonzai_shinai`"
543 /// right: "runtime error: yobidashi.b: unbound symbol `kore_wa_sonzai_shinai`"
544 /// ```
545 /// `1:14` lands in the middle of `use("bakuhatsu2")` — the callee's start
546 /// offset happens to be in range for the caller's file even though its end
547 /// is not, so a wrong file gets a precise number pointing at innocent
548 /// code. Exactly the failure this repo's file-identity work exists to
549 /// refuse, which is why the guard tests BOTH ends.
550 #[test]
551 fn a_raise_in_a_callee_from_another_file_reports_no_position_rather_than_a_wrong_one() {
552 let err = run_named("yobidashi.b", "use(\"bakuhatsu2\")\nyobu()\n");
553 assert_eq!(
554 err.to_string(),
555 "runtime error: yobidashi.b: unbound symbol `kore_wa_sonzai_shinai`",
556 "the caller's file is named (the stated limit) but NOT with a \
557 position it cannot justify"
558 );
559 }
560}
561
562#[cfg(test)]
563mod tests {
564 use super::*;
565
566 fn int(src: &str) -> i64 {
567 match run(src).unwrap_or_else(|e| panic!("{src:?}: {e}")).value {
568 Value::Int(v) => v,
569 other => panic!("{src:?} produced {other:?}"),
570 }
571 }
572
573 /// **The sliding scale, as one assertion.** Annotating changes the
574 /// analysis and nothing else.
575 #[test]
576 fn annotating_buys_analysis_and_changes_nothing_else() {
577 let plain = run("def add(a, b)\n a + b\nend\nadd(2, 3)").expect("plain");
578 let typed = run("def add(a: Int, b: Int) -> Int\n a + b\nend\nadd(2, 3)").expect("typed");
579
580 assert!(matches!(plain.value, Value::Int(5)));
581 assert!(
582 matches!(typed.value, Value::Int(5)),
583 "the annotated program must compute the same answer"
584 );
585 assert_eq!(plain.visited, 0, "no annotations means no analysis");
586 assert!(
587 typed.visited > 0,
588 "an annotation must actually buy analysis, not just decorate"
589 );
590 assert_eq!(plain.typed_decls, 0);
591 assert_eq!(typed.typed_decls, 1);
592 }
593
594 /// **Checking happens before erasure.** This is the test that catches the
595 /// reordering: a program with a declared-type violation must be rejected,
596 /// and it can only be rejected while the annotations still exist.
597 #[test]
598 fn a_type_error_is_reported_and_the_program_does_not_run() {
599 let err = run("def add(a: Int, b: Int) -> Str\n a + b\nend\nadd(1, 2)")
600 .expect_err("a declared Str return from an Int body must be rejected");
601 assert!(
602 matches!(err, RunError::Types(ref d) if !d.is_empty()),
603 "expected type diagnostics, got {err}"
604 );
605 }
606
607 /// And the untyped version of the same program runs, so the rejection
608 /// above is the annotation's doing rather than a parse failure.
609 #[test]
610 fn the_same_program_without_annotations_runs() {
611 assert_eq!(int("def add(a, b)\n a + b\nend\nadd(1, 2)"), 3);
612 }
613
614 #[test]
615 fn a_parse_error_is_reported_as_one() {
616 assert!(matches!(run("def (").unwrap_err(), RunError::Parse(_)));
617 }
618
619 /// Every stage reports in its own vocabulary, so a failure names which
620 /// stage failed rather than surfacing as a generic error.
621 #[test]
622 fn a_runtime_error_is_reported_as_one() {
623 let err = run("no_such_function(1)").expect_err("unbound");
624 assert!(matches!(err, RunError::Eval(_)), "got {err}");
625 }
626
627 /// Stdlib and primitives are both reachable through the pipeline — the
628 /// gap that made `6 % 3` fail.
629 #[test]
630 fn the_pipeline_reaches_both_runtime_layers() {
631 assert_eq!(int("6 % 3"), 0);
632 assert_eq!(int("7 % 3"), 1);
633 assert_eq!(int("2 + 3 * 4"), 14);
634 }
635
636 /// **The deleted hop was a no-op on everything blue emits — so removing it
637 /// is a swap, not a behaviour change.**
638 ///
639 /// The old lowering printed the erased tree and read it back through
640 /// `tatara_lisp::read_spanned`. This walks a corpus and asserts the two
641 /// paths land on the same tree, which is the equivalence the swap rests on.
642 /// It is stated as a *measurement over this corpus*, not as a theorem:
643 /// the round trip is not identity in general (that is precisely why it had
644 /// to go), it merely happened to be identity for the bytes blue emits.
645 #[test]
646 fn the_deleted_round_trip_agreed_with_the_direct_lowering() {
647 let corpus = [
648 "def add(a, b)\n a + b\nend\nadd(2, 3)",
649 "def fact(n)\n if n < 2\n 1\n else\n n * fact(n - 1)\n end\nend\nfact(5)",
650 "def f(a, b)\n c = a + b\n c * 2\nend\nf(1, 2)",
651 "defmacro sq(e)\n quote\n unquote(e) * unquote(e)\n end\nend\nsq(2 + 3)",
652 "\"a string with spaces, a ( and a )\"",
653 "def g(a: Int) -> Int\n a + 1\nend\ng(1)",
654 "6 % 3",
655 "1.5 + 2.25",
656 ];
657 for src in corpus {
658 // Projected to `Sexp` because that is what the deleted hop
659 // operated on. Erasure itself no longer goes near it.
660 let erased = crate::to_sexps(&erase_types(&parse_tree(src).expect("parse")));
661
662 let direct: Vec<Sexp> = crate::lower_to_spanned(&erased)
663 .iter()
664 .map(tatara_lisp::Spanned::to_sexp)
665 .collect();
666 assert_eq!(direct, erased, "the direct lowering must be the identity");
667
668 let text = erased
669 .iter()
670 .map(ToString::to_string)
671 .collect::<Vec<_>>()
672 .join("\n");
673 let round_tripped: Vec<Sexp> = tatara_lisp::read_spanned(&text)
674 .unwrap_or_else(|e| panic!("{src:?}: the old path could not read back: {e:?}"))
675 .iter()
676 .map(tatara_lisp::Spanned::to_sexp)
677 .collect();
678 assert_eq!(
679 round_tripped, erased,
680 "{src:?}: the old print-and-reparse path changed the tree"
681 );
682 }
683 }
684
685 /// Anti-vacuity for the test above: the round trip really is *not* the
686 /// identity in general, so agreeing on the corpus was a property of what
687 /// blue happens to emit rather than a property of the reader.
688 ///
689 /// **`Atom::Symbol`'s `Display` writes the name raw, with no escaping.**
690 /// `Atom::Str` escapes and its docs explain at length why; the symbol arm
691 /// is `f.write_str(s)`. So print-then-read is not inverse over the symbol
692 /// domain, and the failure is *silent*: a symbol containing a space prints
693 /// as two tokens, reads back as two symbols, and the result is a perfectly
694 /// well-formed tree with a different meaning. No error, nothing to catch.
695 ///
696 /// Measured 2026-08-02 across the separators: `a b` and `x'y` come back
697 /// `Ok` with a different tree; `x)y`, `x"y` and `x;y` come back `Err`;
698 /// `x{y` and `x[y` DO round-trip at this level — those two are one symbol
699 /// in and one symbol out, so the brace-fusion reported in tatara *source*
700 /// is not what bites a printed tree. The silent pair is what makes this a
701 /// miscompile class rather than a noisy one.
702 #[test]
703 fn the_round_trip_is_not_the_identity_in_general() {
704 let tree = Sexp::List(vec![
705 Sexp::Atom(tatara_lisp::Atom::Symbol("f".into())),
706 Sexp::Atom(tatara_lisp::Atom::Symbol("a b".into())),
707 ]);
708 let text = tree.to_string();
709 let back: Vec<Sexp> = tatara_lisp::read_spanned(&text)
710 .expect("it reads back cleanly — that IS the problem")
711 .iter()
712 .map(tatara_lisp::Spanned::to_sexp)
713 .collect();
714 assert_ne!(
715 back,
716 vec![tree.clone()],
717 "if print-then-read became inverse over symbols, the class would be \
718 closed upstream and this test should be deleted rather than relaxed"
719 );
720 // …and the direct lowering is unaffected by any of it.
721 let direct: Vec<Sexp> = crate::lower_to_spanned(std::slice::from_ref(&tree))
722 .iter()
723 .map(tatara_lisp::Spanned::to_sexp)
724 .collect();
725 assert_eq!(direct, vec![tree]);
726 }
727}
728
729#[cfg(test)]
730mod macro_tests {
731 use super::*;
732
733 fn int(src: &str) -> i64 {
734 match run(src).unwrap_or_else(|e| panic!("{src:?}: {e}")).value {
735 Value::Int(v) => v,
736 other => panic!("{src:?} produced {other:?}"),
737 }
738 }
739
740 /// **A blue macro expands and runs.** Tenet 2's surface, end to end.
741 #[test]
742 fn a_macro_expands_and_runs() {
743 assert_eq!(
744 int("defmacro double(x)\n quote\n unquote(x) + unquote(x)\n end\nend\ndouble(21)"),
745 42
746 );
747 }
748
749 /// A macro receives *source forms*, not values — so it can duplicate its
750 /// argument, which a function cannot do without re-evaluating it.
751 #[test]
752 fn a_macro_operates_on_syntax_not_values() {
753 assert_eq!(
754 int("defmacro sq(e)\n quote\n unquote(e) * unquote(e)\n end\nend\nsq(2 + 3)"),
755 25,
756 "the argument form `2 + 3` must be substituted twice"
757 );
758 }
759
760 /// **A runaway macro is a typed error, not a dead compiler.** This is the
761 /// property that makes the metaprogramming surface safe to hand to a user.
762 ///
763 /// The assertion was `contains("expansion limit")` and now reads
764 /// `contains("expansion")`, because the pipeline renders `short_message`
765 /// rather than `Display`: upstream words the same fact as "exceeded 256
766 /// expansion steps" instead of "exceeded the expansion limit of 256
767 /// rewrites". The PROPERTY is unchanged and still asserted — the macro is
768 /// named, and the failure is attributed to expansion rather than to
769 /// evaluation. **This reword was the whole measured blast radius of that
770 /// switch**, one test in the workspace.
771 ///
772 /// The number is deliberately NOT pinned. It is upstream's constant, this
773 /// test owns none of it, and matching it would turn an upstream bump into
774 /// a red run here that proves nothing about blue.
775 ///
776 /// The third clause is new capability rather than repair: the message now
777 /// carries `<anonymous>:6:1`, so a runaway macro says WHERE it ran away.
778 #[test]
779 fn a_runaway_macro_fails_the_compilation_rather_than_the_process() {
780 let err =
781 run("defmacro forever(x)\n quote\n forever(unquote(x))\n end\nend\nforever(1)")
782 .expect_err("a self-referential macro must be rejected");
783 let msg = err.to_string();
784 assert!(
785 msg.contains("forever") && msg.contains("expansion"),
786 "the error must name the macro and the limit it hit: {msg}"
787 );
788 assert!(
789 msg.contains("<anonymous>:6:1"),
790 "and must place the call that ran away — line 6 is `forever(1)`: {msg}"
791 );
792 }
793}
794
795#[cfg(test)]
796mod input_tests {
797 use super::*;
798 use crate::inputs::{Declaration, Inputs};
799
800 /// A schema a macro will generate code from.
801 const SCHEMA: &[u8] = b"3";
802
803 fn with_schema(src: &str) -> Result<Run, RunError> {
804 let hash = Inputs::hash_of(SCHEMA);
805 let mut inputs = Inputs::new();
806 inputs
807 .bind(
808 &Declaration {
809 name: "schema".to_string(),
810 hash,
811 },
812 SCHEMA.to_vec(),
813 )
814 .expect("bind");
815 run_with_inputs(src, inputs)
816 }
817
818 fn decl_line() -> String {
819 let mut s = String::from("definput(\"schema\", \"");
820 s.push_str(&Inputs::hash_of(SCHEMA));
821 s.push_str("\")\n");
822 s
823 }
824
825 /// **A macro reads a declared build input.** This is §VI OPEN #6 closed —
826 /// the spec names it as gating blue's whole "stronger than Ruby's
827 /// metaprogramming" claim, because a macro that cannot read a schema cannot
828 /// generate code from one.
829 #[test]
830 fn a_macro_can_read_a_declared_build_input() {
831 let src = decl_line() + "input(\"schema\")";
832 let out = with_schema(&src).expect("run");
833 assert!(
834 matches!(out.value, Value::Str(ref s) if &**s == "3"),
835 "got {:?}",
836 out.value
837 );
838 }
839
840 /// **An undeclared input is an error, not a file read and not nil.**
841 /// Returning nil is how a macro generates an empty table and nobody notices
842 /// until runtime.
843 #[test]
844 fn an_undeclared_input_is_an_error() {
845 let err = with_schema("input(\"not_declared\")").expect_err("must fail");
846 let msg = err.to_string();
847 assert!(msg.contains("not_declared"), "must name it: {msg}");
848 assert!(msg.contains("definput"), "and say how to declare it: {msg}");
849 }
850
851 /// **There is no path-based read at all.** The capability is the absence of
852 /// the primitive, not a check inside one — so this is an unbound symbol.
853 ///
854 /// Holds for the DEFAULT surface — the one every embedder gets. The `sys`
855 /// cargo feature (CLI only) is the one declared exception: it is the
856 /// operator's own trusted host surface, and is asserted in
857 /// `sys_read_file_is_the_trusted_cli_only_exception` below.
858 #[cfg(not(feature = "sys"))]
859 #[test]
860 fn there_is_no_ambient_file_read() {
861 for attempt in [
862 "read_file(\"/etc/passwd\")",
863 "File(\"/etc/passwd\")",
864 "slurp(\"/etc/passwd\")",
865 "open(\"/etc/passwd\")",
866 ] {
867 let err = with_schema(attempt).expect_err("must not resolve");
868 assert!(
869 err.to_string().contains("unbound"),
870 "{attempt} must be UNBOUND — a capability removed by absence, \
871 not guarded by a check: {err}"
872 );
873 }
874 }
875
876 /// With the `sys` feature compiled in, `read_file` IS bound — that is the
877 /// point of the feature. The doctrine does not move: this is the operator's
878 /// own machine (the CLI), not an embedder's sandbox. Pin the boundary so a
879 /// future default-build change is heard, and assert that `input()` still
880 /// works beside it.
881 #[cfg(feature = "sys")]
882 #[test]
883 fn sys_read_file_is_the_trusted_cli_only_exception() {
884 let err = with_schema("definitely_not_a_primitive(\"x\")").expect_err("must not resolve");
885 assert!(err.to_string().contains("unbound"), "{err}");
886 assert!(
887 with_schema("read_file(\"/etc/passwd\")").is_ok(),
888 "with `sys` on, read_file is the trusted CLI surface"
889 );
890 let out = with_schema("input(\"schema\")").expect("run");
891 assert!(
892 matches!(out.value, Value::Str(ref s) if &**s == "3"),
893 "input() still binds beside the sys surface: {:?}",
894 out.value
895 );
896 }
897
898 /// Anti-vacuity: with no inputs supplied at all, even a declared name fails
899 /// — so the success above is the binding's doing.
900 #[test]
901 fn a_declared_input_with_no_bytes_supplied_fails() {
902 let src = decl_line() + "input(\"schema\")";
903 assert!(run(&src).is_err(), "no bytes were supplied");
904 }
905}
906
907#[cfg(test)]
908mod tier2_tests {
909 use super::*;
910 use crate::inputs::{Declaration, Inputs};
911
912 /// **The Tier-2 conversion §V.6.3 said was gated: a macro that emits real
913 /// declarations FROM A SCHEMA.**
914 ///
915 /// `theory/BLUE.md` §VI OPEN #6 states the blocker plainly — "tenet 2
916 /// installs a `NoLoader`, so a macro cannot read a schema — which gates
917 /// every Tier-2 conversion in §V.6 and therefore blue's whole 'stronger than
918 /// Ruby's metaprogramming' claim."
919 ///
920 /// Here the schema supplies a *value the generated code depends on*, read at
921 /// expansion time. Ruby and Elixir can both do this — with the whole
922 /// filesystem open. blue does it through a name bound to a content hash.
923 #[test]
924 fn a_macro_generates_code_from_a_schema() {
925 let schema = b"7";
926 let mut inputs = Inputs::new();
927 inputs
928 .bind(
929 &Declaration {
930 name: "arity".to_string(),
931 hash: Inputs::hash_of(schema),
932 },
933 schema.to_vec(),
934 )
935 .expect("bind");
936
937 // The macro reads the input at EXPANSION time and splices the value it
938 // found into the code it emits.
939 let mut src = String::from("definput(\"arity\", \"");
940 src.push_str(&Inputs::hash_of(schema));
941 src.push_str("\")\n");
942 src.push_str(
943 "defmacro from_schema()\n quote\n unquote(to_int(input(\"arity\")))\n end\nend\n\
944 from_schema() * 6",
945 );
946
947 let out = run_with_inputs(&src, inputs).expect("run");
948 assert!(
949 matches!(out.value, Value::Int(42)),
950 "the schema's 7 must reach the generated code: got {:?}",
951 out.value
952 );
953 }
954}