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 blue_lang_check::{NameTable, Namespace};
20
21use crate::erase::erase_types;
22use crate::inputs::Inputs;
23use crate::uses::Entry;
24
25/// Why a run stopped short.
26#[derive(Debug, thiserror::Error)]
27pub enum RunError {
28 #[error("parse error: {0}")]
29 Parse(String),
30 /// The check stage rejected the program — a type error, an unbound name,
31 /// or any other error-severity rule in `blue_lang_check::RULES`. Carries
32 /// every error, not just the first: a caller fixing one wants to see the
33 /// rest. (The variant keeps its first name because it is public API on a
34 /// released crate.)
35 ///
36 /// Each one is already rendered `file:line:col: message` against the file
37 /// the offending form came from — which, in a program with imports, is
38 /// frequently not the file the user named. The rendering happens here
39 /// rather than at the consumer because here is where the file table exists;
40 /// see `uses::ResolvedProgram::locate`.
41 #[error("{} error(s) in the check stage:\n{}", .0.len(), .0.join("\n"))]
42 Types(Vec<String>),
43 /// **No longer reachable, and that is the point.** This reported "blue
44 /// emitted a tree the reader could not read back" — a failure only a
45 /// print-then-reparse hop could have. [`crate::lower_to_spanned`] deleted
46 /// the hop, so there is nothing left to fail: the tree the evaluator gets
47 /// IS the tree erasure produced, not a re-reading of its text.
48 ///
49 /// Kept rather than removed because it is public API on a released crate
50 /// and a consumer may still match on it, per ★★ MODULARIZE, DON'T DELETE.
51 /// It is retired, not orphaned — if a future stage ever serialises again
52 /// it has a typed home. **Nothing constructs it today**; do not read its
53 /// presence as evidence the pipeline can still fail this way.
54 #[error("the emitted tatara-lisp could not be read back: {0}")]
55 Lower(String),
56 /// The program ran and raised.
57 ///
58 /// Rendered `file:line:col: message` against the file the failing form's
59 /// top-level form came from — the same `uses::ResolvedProgram::locate`
60 /// machinery, the same file table and the same join key the type errors
61 /// above use. Until erasure learned to carry spans this was a bare
62 /// message, because the evaluator was handed a tree whose every node had
63 /// been stamped `Span::synthetic()` on the way in.
64 ///
65 /// **The honest limit, stated so nobody reads more into a position than is
66 /// there.** The index names the top-level form that was EXECUTING; the span
67 /// names where the failing text SITS. Those agree on the file whenever the
68 /// failing code is in the same file as the top-level form that reached it —
69 /// which is every single-file program, and an imported package whose own
70 /// top-level code raises. They can disagree when a top-level form in file A
71 /// calls a function defined in file B and the raise happens inside B: the
72 /// path reported is A's. `locate` refuses to print a `line:col` it cannot
73 /// justify (the span must be a real range in that file), so the usual shape
74 /// of that case is a file with no position rather than a precise-looking
75 /// wrong one — but a same-length pair of files can still put a plausible
76 /// number on the wrong file. Closing it needs per-frame file identity at
77 /// the evaluator, which is a call stack blue does not have.
78 #[error("runtime error: {0}")]
79 Eval(String),
80 /// A `use("name")` could not be resolved.
81 ///
82 /// Its own variant rather than folded into `Parse`, because the reader's
83 /// next action is different: a parse error is in the source in front of
84 /// them, an import error is in their packaging — a missing bidama, a
85 /// BLUE_PATH that does not contain it, or no loader at all.
86 #[error("import error: {0}")]
87 Import(String),
88}
89
90/// What a run produced, plus what the checker did on the way.
91#[derive(Debug)]
92pub struct Run {
93 pub value: Value,
94 /// Nodes the type walk visited. Zero for a fully untyped program — this
95 /// is what makes "no annotations, no analysis" a *measurement* rather
96 /// than a claim.
97 pub visited: usize,
98 /// Declarations that carried an annotation.
99 pub typed_decls: usize,
100 /// Boundaries where typed code meets untyped code.
101 pub seams: usize,
102}
103
104/// Parse blue source to tatara-lisp forms.
105pub fn parse(src: &str) -> Result<Vec<Sexp>, RunError> {
106 parse_with_depth(src, blue_lang_syntax::MAX_EXPR_DEPTH)
107}
108
109/// [`parse`] with the parser's nesting bound supplied by the caller.
110///
111/// The bound exists so a stack overflow — which `catch_unwind` cannot catch —
112/// arrives as a typed `Err` instead. It is a *limit*, not a dialect: raising
113/// it changes no program's meaning, which is exactly why it is safe to expose
114/// as configuration (`blue-lang-cli`'s `config` module holds the rule).
115pub fn parse_with_depth(src: &str, max_depth: usize) -> Result<Vec<Sexp>, RunError> {
116 blue_lang_syntax::parse_program_with_depth(src, max_depth)
117 .map_err(|e| RunError::Parse(e.to_string()))
118}
119
120/// [`parse_with_depth`] keeping **every node's** source span.
121///
122/// The door for anything that will report a position to a human. It exists here,
123/// beside the spanless one, so a caller that wants spans still parses under the
124/// CONFIGURED nesting bound — a separate `blue_lang_syntax` call would be the
125/// second door `parse_with_depth`'s own docs exist to prevent, with
126/// `max_expr_depth` true of some subcommands and not others.
127pub fn parse_tree_with_depth(
128 src: &str,
129 max_depth: usize,
130) -> Result<Vec<blue_lang_syntax::Spanned>, RunError> {
131 blue_lang_syntax::parse_program_tree_with_depth(src, max_depth)
132 .map_err(|e| RunError::Parse(e.to_string()))
133}
134
135/// [`parse`] keeping **every node's** source span.
136///
137/// The spanned twin of [`parse`], at the same default bound — the door for
138/// anything downstream of a parse that will report a position, which since
139/// `use` learned to carry file identity is every path through
140/// [`run_in_surface`].
141pub fn parse_tree(src: &str) -> Result<Vec<blue_lang_syntax::Spanned>, RunError> {
142 parse_tree_with_depth(src, blue_lang_syntax::MAX_EXPR_DEPTH)
143}
144
145/// Run blue source with no build inputs.
146pub fn run(src: &str) -> Result<Run, RunError> {
147 run_with_inputs(src, Inputs::new())
148}
149
150/// Run blue source, giving the macro phase access to verified build inputs.
151///
152/// `inputs` is already verified — [`Inputs`] cannot hold bytes that do not match
153/// their declared hash — so nothing here re-checks. The capability a macro gains
154/// is exactly "these hashed bytes", never a path.
155pub fn run_with_inputs(src: &str, inputs: Inputs) -> Result<Run, RunError> {
156 run_with_loader(src, inputs, &crate::uses::NoLoader)
157}
158
159/// Run blue source with a loader, so `use("name")` can resolve.
160///
161/// Split from [`run_with_inputs`] rather than folded into it because loading a
162/// package reads a filesystem, and this crate has a `wasm32-unknown-unknown`
163/// consumer with zero host imports. The capability is injected by callers that
164/// have it — `blue_lang_pkg::LoadPath` is the real one — and absent by default,
165/// where a `use` is a typed error naming the package.
166pub fn run_with_loader(
167 src: &str,
168 inputs: Inputs,
169 loader: &dyn crate::uses::Loader,
170) -> Result<Run, RunError> {
171 run_in_surface(Entry::anonymous(src), inputs, loader, None)
172}
173
174/// Run blue source written in a `yakugo` surface.
175///
176/// The pack applies at PARSE time and nowhere else — by the time the checker
177/// sees the program it is canonical, so every stage below is identical whatever
178/// surface the author wrote in. That is what makes a surface a surface: it
179/// changes how a program is spelled and nothing about how it runs.
180///
181/// `entry` carries the source AND the file it was read from, because a type
182/// error has to be reported somewhere: a caller with a path should pass it, and
183/// one without ([`Entry::anonymous`]) gets diagnostics that say so rather than
184/// diagnostics that guess. Imported packages name themselves through the
185/// loader either way.
186///
187/// # Errors
188///
189/// As [`run_with_loader`].
190pub fn run_in_surface(
191 entry: Entry<'_>,
192 inputs: Inputs,
193 loader: &dyn crate::uses::Loader,
194 surface: Option<&blue_lang_syntax::yakugo::Yakugo>,
195) -> Result<Run, RunError> {
196 // The interpreter is built BEFORE the check stage, because it is the check
197 // stage's name oracle: every builtin, macro and special form a program may
198 // name is read off the interpreter that will run it, never off a list.
199 // Building it is not a stage; nothing is evaluated until `eval_prepared`.
200 let mut interp = crate::interpreter_hostless();
201 crate::inputs::install_input_primitives(&mut interp, inputs);
202 let prepared = prepare(entry, loader, surface, &builtin_names(&interp))?;
203 let value = eval_prepared(&mut interp, &prepared, &mut ())?;
204 Ok(Run {
205 value,
206 visited: prepared.visited,
207 typed_decls: prepared.typed_decls,
208 seams: prepared.seams,
209 })
210}
211
212/// A program past every stage before evaluation: parsed, its imports
213/// resolved, type-checked and erased. What [`run_in_surface`] and
214/// [`crate::hosted::load_hosted`] both evaluate, so the stage order this
215/// module exists to own is written once for both.
216pub(crate) struct Prepared {
217 program: crate::uses::ResolvedProgram,
218 erased: Vec<tatara_lisp::Spanned>,
219 visited: usize,
220 typed_decls: usize,
221 seams: usize,
222}
223
224impl Prepared {
225 /// The entry file's namespace.
226 pub(crate) fn entry_namespace(&self) -> Namespace {
227 let first = (0..self.program.forms().len())
228 .find(|i| self.program.owner_of(*i) == Some(crate::uses::ResolvedProgram::ENTRY));
229 match first {
230 Some(i) => namespace_of(&self.program, i),
231 None => match self
232 .program
233 .file(crate::uses::ResolvedProgram::ENTRY)
234 .and_then(|f| f.package.clone())
235 {
236 Some(p) => Namespace::Bidama(p),
237 None => Namespace::File(String::new()),
238 },
239 }
240 }
241}
242
243/// Parse, resolve, check and erase, in that order.
244///
245/// `builtins` is the name table of the interpreter that will evaluate the
246/// result ([`builtin_names`]); the check stage resolves the program's names
247/// against it.
248pub(crate) fn prepare(
249 entry: Entry<'_>,
250 loader: &dyn crate::uses::Loader,
251 surface: Option<&blue_lang_syntax::yakugo::Yakugo>,
252 builtins: &NameTable,
253) -> Result<Prepared, RunError> {
254 let mut program = parse_and_resolve(entry, loader, surface)?;
255
256 // `test` blocks are declarations for the harness, not code to run.
257 //
258 // Dropped here rather than in `resolve_uses`, because `blue test` calls
259 // the resolver and then NEEDS the entry file's blocks — so the two
260 // callers want different things and the split has to live at this level.
261 //
262 // Without this, `blue run` on a file containing its own tests fails with
263 // `unbound symbol: deftest`: every package in the bidama distribution
264 // carries tests, so every one of them was unrunnable.
265 //
266 // Through `retain`, which drops each form's owner with it. A plain filter
267 // over the forms alone would slide every later form onto the wrong file.
268 program.retain(|f| !crate::uses::is_test_form(f));
269
270 // CHECK, on the annotated tree — the only tree that has annotations.
271 //
272 // **On the REAL spanned tree, including every imported package's.** This
273 // was the one caller that checked a spanless lift, because `resolve_uses`
274 // flattened the entry file and its imports into one list and `Span` is a
275 // byte range with no file identity — so a real span here would have
276 // reported an imported package's error at that offset in the ENTRY file, a
277 // precise-looking answer pointing at unrelated code.
278 //
279 // The fix is not a wider `Span` (that type is upstream, and its own docs
280 // put file identity on the caller: spans "are meaningful only relative to
281 // the string that produced them, which the caller is responsible for
282 // holding onto"). blue holds onto it BESIDE the span, per top-level form —
283 // see `uses::ResolvedProgram`.
284 let (outcome, names) = check_stage(&program, builtins, false);
285 if !outcome.ok() {
286 return Err(RunError::Types(
287 outcome
288 .errors()
289 // `file:line:col: error[CODE]: message`, resolved against the
290 // file the form actually came from, then its help line.
291 .map(|d| render(&program, d))
292 .collect(),
293 ));
294 }
295
296 // ERASE, so the interpreter never sees a type — **on the spanned tree,
297 // and back out as one.**
298 //
299 // This used to be `erase_types(&program.sexps())` followed by a
300 // `lower_to_spanned` that stamped `Span::synthetic()` over every node, and
301 // the comment here said carrying real positions through was "a larger
302 // piece and is NOT built". It was not larger: erasure only ever DELETES
303 // nodes — the single node it invents is the `define` replacing
304 // `define-typed` — so there was never anything for a synthetic span to
305 // stand in for. See `crate::erase`.
306 //
307 // No lowering step follows. The tree the evaluator receives IS the tree the
308 // parser built, minus annotations, with the author's byte offsets intact.
309 let erased = erase_types(&lower(&program, &names));
310
311 Ok(Prepared {
312 program,
313 erased,
314 visited: outcome.stats.visited,
315 typed_decls: outcome.stats.typed_decls,
316 seams: outcome.seams.len(),
317 })
318}
319
320/// Parse the entry (in `surface`, if one is given) and splice in its imports.
321fn parse_and_resolve(
322 entry: Entry<'_>,
323 loader: &dyn crate::uses::Loader,
324 surface: Option<&blue_lang_syntax::yakugo::Yakugo>,
325) -> Result<crate::uses::ResolvedProgram, RunError> {
326 let forms = match surface {
327 Some(pack) => blue_lang_syntax::parse_program_tree_in(entry.text, pack)
328 .map_err(|e| RunError::Parse(e.to_string()))?,
329 None => parse_tree(entry.text)?,
330 };
331
332 // RESOLVE imports first, so everything below sees ONE program.
333 //
334 // Before the check on purpose: imported code is type-checked at the point
335 // its consumer imports it, rather than at whatever later moment its code
336 // first runs. A package that does not typecheck should break its importer's
337 // build, not their production run.
338 //
339 // One program, but not one FILE: the result records which file each
340 // top-level form came from, which is what lets a diagnostic below name a
341 // place instead of only a problem.
342 crate::uses::resolve_uses(forms, entry, loader).map_err(RunError::Import)
343}
344
345/// **The check stage**: every rule in `blue_lang_check::RULES`, over a
346/// resolved program, with waivers applied.
347///
348/// One function, called by every door — `run`, the hosted loader, `blue test`
349/// and `blue check` — so a rule added to the registry is enforced by all of
350/// them at once. That is what makes an architecture rule a pipeline error
351/// rather than an optional report: there is no door that skips it.
352///
353/// Warnings (unused bindings, unused waivers) are reported for the ENTRY
354/// file only; an imported package's warnings belong to its author. Errors
355/// are reported for every file, because an imported package with one is
356/// broken for its importer.
357fn check_stage(
358 program: &crate::uses::ResolvedProgram,
359 builtins: &NameTable,
360 with_tests: bool,
361) -> (blue_lang_check::Outcome, NameTable) {
362 use crate::uses::ResolvedProgram;
363 let forms = program.forms();
364 let is_entry = |i: usize| program.owner_of(i) == Some(ResolvedProgram::ENTRY);
365
366 // Rules B0003–B0005: the typing ladder.
367 let mut outcome = blue_lang_check::check_program(forms);
368
369 // Rules B0001–B0002: names. The program's own scopes come first (the
370 // entry file, then each bidama in import order), then the interpreter's.
371 let table = program_names(program, builtins);
372 let whole = |i: usize| with_tests && is_entry(i);
373 let (names, resolved) = blue_lang_check::names::check_names_whole(
374 forms,
375 &table,
376 &|i| namespace_of(program, i),
377 &is_entry,
378 &whole,
379 );
380 outcome.diagnostics.extend(names);
381 outcome.stats.names_resolved = resolved;
382
383 // Waivers, per file: B0007 for a malformed one, B0008 for one that
384 // suppresses nothing.
385 let mut waivers = Vec::new();
386 for file in program.files() {
387 let spans: Vec<(usize, tatara_lisp::Span)> = forms
388 .iter()
389 .enumerate()
390 .filter(|(i, _)| program.owner_of(*i) == Some(file.id))
391 .map(|(i, f)| (i, f.span))
392 .collect();
393 let (found, malformed) = blue_lang_check::waiver::collect(&file.text, &spans);
394 waivers.extend(found);
395 outcome.diagnostics.extend(malformed);
396 }
397 outcome.waived = blue_lang_check::waiver::apply(&mut outcome.diagnostics, waivers, &is_entry);
398 // A rule whose census has not reached zero is computed, and counted, but
399 // not enforced: its findings move to `census` (see `RULES`' `ratchet`).
400 let (enforced, census): (Vec<_>, Vec<_>) = std::mem::take(&mut outcome.diagnostics)
401 .into_iter()
402 .partition(|d| d.code.rule().active());
403 outcome.diagnostics = enforced;
404 outcome.census = census;
405 outcome
406 .diagnostics
407 .sort_by_key(|d| (d.top_level, d.span.start, d.code));
408 (outcome, table)
409}
410
411/// The whole name table the check stage resolves against: the program's
412/// own scopes (each file and bidama, by namespace) followed by `builtins`.
413///
414/// **A top-level call to a builtin macro can define names**: `defflow(slug,
415/// trim, downcase)` expands to `(define slug …)`, and nothing in the source
416/// spells `define`. Such a call is expanded here, on a hostless fork, and what
417/// its expansion defines is bound in the calling file's namespace. Only
418/// BUILTIN macros are expanded: their bodies are the runtime's own
419/// templates. A program's own `defmacro` is not run at check time — checking a
420/// file (and an editor checking on every keystroke) must not execute the code
421/// it checks — so names a program macro generates are not seen; none of the
422/// corpus's macros generates one (measured 2026-09-29).
423#[must_use]
424pub fn program_names(program: &crate::uses::ResolvedProgram, builtins: &NameTable) -> NameTable {
425 use blue_lang_check::Binding;
426 let mut table = NameTable::new();
427 table.add_program(program.forms(), |i| namespace_of(program, i));
428 let builtin_macro = |head: &str| {
429 builtins
430 .scopes()
431 .iter()
432 .any(|s| s.namespace == Namespace::Macro && s.get(head).is_some())
433 };
434 let mut expander: Option<tatara_lisp_eval::Interpreter<()>> = None;
435 for (i, form) in program.forms().iter().enumerate() {
436 let Some(head) = form
437 .as_list()
438 .and_then(|items| items.first())
439 .and_then(tatara_lisp::Spanned::as_symbol)
440 else {
441 continue;
442 };
443 if !builtin_macro(head) {
444 continue;
445 }
446 let interp = expander.get_or_insert_with(crate::interpreter_hostless);
447 // A failed expansion defines nothing here; the evaluator will report
448 // it, with its position, when the form runs.
449 let Ok(expanded) = interp.fully_expand(form, &mut ()) else {
450 continue;
451 };
452 for (name, span, kind) in blue_lang_check::names::definitions_of(&expanded) {
453 table.define(
454 namespace_of(program, i),
455 Binding {
456 name,
457 kind,
458 span: Some(span),
459 top_level: Some(i),
460 },
461 );
462 }
463 }
464 for scope in builtins.scopes() {
465 table.push(scope.clone());
466 }
467 let form_file: Vec<usize> = (0..program.forms().len())
468 .map(|i| program.owner_of(i).map_or(usize::MAX, |f| f.index()))
469 .collect();
470 let mut imports: std::collections::BTreeMap<usize, blue_lang_check::names::FileImports> =
471 std::collections::BTreeMap::new();
472 for (file, import) in program.imports() {
473 let entry = imports.entry(file.index()).or_default();
474 entry.add(&import.package, import.names.iter().map(|(n, _)| n.clone()));
475 let at = import.span.start;
476 entry.use_at = Some(entry.use_at.map_or(at, |a| a.min(at)));
477 }
478 // A FACADE — a bidama that defines nothing and `use`s others (zenbu) —
479 // exists so a consumer can depend on "everything": `use("zenbu")` makes
480 // every member's QUALIFIER reachable (`toukei::median`), never a bare
481 // name. Followed to a fixpoint, so a facade of facades works too.
482 let facade_uses =
483 |pkg: &str,
484 imports: &std::collections::BTreeMap<usize, blue_lang_check::names::FileImports>| {
485 let defines = table.bidama(pkg).is_some_and(|s| !s.is_empty());
486 if defines {
487 return std::collections::BTreeSet::new();
488 }
489 program
490 .files()
491 .iter()
492 .filter(|f| f.package.as_deref() == Some(pkg))
493 .filter_map(|f| imports.get(&f.id.index()))
494 .flat_map(|i| i.uses.iter().cloned())
495 .collect::<std::collections::BTreeSet<String>>()
496 };
497 loop {
498 let mut grew = false;
499 let files: Vec<usize> = imports.keys().copied().collect();
500 for f in files {
501 let used: Vec<String> = imports[&f].uses.iter().cloned().collect();
502 for p in used {
503 for member in facade_uses(&p, &imports) {
504 if let Some(i) = imports.get_mut(&f) {
505 if i.uses.insert(member.clone()) {
506 i.via.insert(member, p.clone());
507 grew = true;
508 }
509 }
510 }
511 }
512 }
513 if !grew {
514 break;
515 }
516 }
517 let mut pkg_uses: std::collections::BTreeMap<String, std::collections::BTreeSet<String>> =
518 std::collections::BTreeMap::new();
519 for (file, import) in program.imports() {
520 if let Some(p) = program.file(*file).and_then(|f| f.package.clone()) {
521 pkg_uses
522 .entry(p)
523 .or_default()
524 .insert(import.package.clone());
525 }
526 }
527 table.attach_files(form_file, imports);
528 table.attach_package_uses(pkg_uses);
529 table.attach_needs(program.needs().clone());
530 // Where each file lives and which distribution each bidama ships in: a
531 // bridge reference (B0021) is an error inside the bidama's own
532 // distribution and a warning outside it.
533 let canonical = |p: &std::path::Path| {
534 std::fs::canonicalize(p)
535 .unwrap_or_else(|_| p.to_path_buf())
536 .display()
537 .to_string()
538 };
539 let mut file_paths = std::collections::BTreeMap::new();
540 let mut pkg_roots = std::collections::BTreeMap::new();
541 for f in program.files() {
542 if let Some(path) = &f.path {
543 file_paths.insert(f.id.index(), canonical(path));
544 if let (Some(pkg), Some(root)) =
545 (&f.package, path.parent().and_then(std::path::Path::parent))
546 {
547 pkg_roots
548 .entry(pkg.clone())
549 .or_insert_with(|| canonical(root));
550 }
551 }
552 }
553 table.attach_paths(file_paths, pkg_roots);
554 let versions = program.versions().clone();
555 table.apply_legacy(program.forms(), &|i| namespace_of(program, i), &|p| {
556 versions.get(p).cloned()
557 });
558 table
559}
560
561/// The forms the evaluator runs, from a checked program: every `use`
562/// declaration inert (`nil`, so each index still names its top-level form)
563/// and every name at the key it runs under — the RESOLVED tree under
564/// per-bidama namespaces (`blue_lang_check::names::Resolved::resolved_tree`).
565///
566/// A definition runs as `retsu/first` (a script's as `%root/f`), a
567/// reference to it as the same key, and a builtin as its bare name. Keys
568/// cannot collide across namespaces, so a bidama's `first` no longer
569/// replaces the builtin for anyone who did not import it, and an importer's
570/// `helper` no longer replaces a bidama's own: the runtime binds exactly what
571/// the check stage resolved.
572#[must_use]
573pub fn lower(
574 program: &crate::uses::ResolvedProgram,
575 names: &NameTable,
576) -> Vec<tatara_lisp::Spanned> {
577 let resolved = blue_lang_check::names::resolve_program(program.forms(), names, &|i| {
578 namespace_of(program, i)
579 });
580 resolved
581 .resolved_tree(program.forms(), blue_lang_check::names::Rule::Namespaced)
582 .into_iter()
583 .zip(program.forms())
584 .map(|(resolved, written)| {
585 if crate::uses::use_target(written).is_some()
586 || blue_lang_syntax::scope::legacy_target(written).is_some()
587 {
588 tatara_lisp::Spanned::new(written.span, tatara_lisp::SpannedForm::Nil)
589 } else {
590 resolved
591 }
592 })
593 .collect()
594}
595
596/// The namespace top-level form `i` of `program` defines into.
597#[must_use]
598pub fn namespace_of(program: &crate::uses::ResolvedProgram, i: usize) -> Namespace {
599 let file = program.owner_of(i).and_then(|id| program.file(id));
600 match file {
601 Some(f) => match &f.package {
602 Some(p) => Namespace::Bidama(p.clone()),
603 None => Namespace::File(
604 f.path
605 .as_ref()
606 .map_or_else(|| "<anonymous>".to_string(), |p| p.display().to_string()),
607 ),
608 },
609 None => Namespace::File("<unknown file>".to_string()),
610 }
611}
612
613/// Names only the test harness binds.
614///
615/// In scope for EVERY check, not only for test blocks. A bidama may carry a
616/// test helper — a plain `def` that asserts, called only from its `test`
617/// blocks (`heni`'s `hn_end_to_end` is one) — and every program importing that
618/// bidama would otherwise be refused for a function it never calls. The
619/// refusal would be scoped to the importer's whole program, not to the fault.
620/// The cost, stated: an `assert` outside any test that `blue run` actually
621/// reaches is caught at runtime (`blue-assert` is unbound there), not here.
622///
623/// `blue-lang-test` registers these on its own interpreter, and this crate
624/// cannot depend on it (it depends on this one), so the list lives beside the
625/// only name in it: the lowered `assert`, which `blue-lang-syntax` owns.
626pub const HARNESS_NAMES: &[&str] = &[blue_lang_syntax::LOWERED_ASSERT];
627
628/// The check stage's builtin scopes, read off the interpreter that will run
629/// the program: the test harness's names ([`HARNESS_NAMES`]), then its special
630/// forms, its macros, and every value it binds — the last three in the order
631/// the evaluator consults for a head.
632///
633/// Read from a live interpreter rather than a list, for the reason
634/// `Interpreter::resolve_head` gives: a list is a second copy of the truth and
635/// goes stale on the next installer that lands. An embedder's own
636/// primitives are in scope here because the embedder installs them first.
637#[must_use]
638pub fn builtin_names<H: 'static>(interp: &tatara_lisp_eval::Interpreter<H>) -> NameTable {
639 use blue_lang_check::{Binding, Scope, ScopeKind};
640 use tatara_lisp_eval::HeadBinding;
641 let mut special = Scope::new(Namespace::SpecialForm);
642 let mut macros = Scope::new(Namespace::Macro);
643 let mut values = Scope::new(Namespace::Builtin);
644 // `reserved_head_names` reads the ROOT frame only, so a name installed on
645 // a fork (the input primitives, on every `run`) is invisible to it; those
646 // are asked about by name instead, which `resolve_head` answers through
647 // every frame.
648 let mut names: Vec<std::sync::Arc<str>> = interp.reserved_head_names().into_iter().collect();
649 names.extend(
650 crate::inputs::INPUT_PRIMITIVES
651 .iter()
652 .filter(|n| interp.resolve_head(n).is_some())
653 .map(|n| std::sync::Arc::from(*n)),
654 );
655 for name in names {
656 let (scope, kind) = match interp.resolve_head(&name) {
657 Some(HeadBinding::SpecialForm) => (&mut special, ScopeKind::SpecialForm),
658 Some(HeadBinding::Macro) => (&mut macros, ScopeKind::Macro),
659 _ => (&mut values, ScopeKind::Value),
660 };
661 scope.bind(Binding {
662 name: name.to_string(),
663 kind,
664 span: None,
665 top_level: None,
666 });
667 }
668 let mut table = NameTable::new();
669 let mut harness = Scope::new(Namespace::Harness);
670 for n in HARNESS_NAMES {
671 harness.bind(Binding {
672 name: (*n).to_string(),
673 kind: ScopeKind::Value,
674 span: None,
675 top_level: None,
676 });
677 }
678 table.push(harness);
679 table.push(special);
680 table.push(macros);
681 table.push(values);
682 table
683}
684
685/// A diagnostic as a person reads it: `file:line:col: error[B0001]: message`,
686/// then an indented `help:` line when it has one.
687#[must_use]
688pub fn render(program: &crate::uses::ResolvedProgram, d: &blue_lang_check::Diagnostic) -> String {
689 let head = d.to_string();
690 let mut out = program.locate(d.top_level, d.span, &head).to_string();
691 if let Some(help) = &d.help {
692 out.push_str("\n help: ");
693 out.push_str(help);
694 }
695 out
696}
697
698/// What [`check_entry`] checks.
699#[derive(Clone, Copy, Debug, PartialEq, Eq)]
700pub enum Checking {
701 /// The program `blue run` would evaluate: test blocks dropped.
702 Program,
703 /// The program plus the entry file's test blocks, as `blue test` and
704 /// `blue check` see it.
705 WithTests,
706}
707
708/// A program through the check stage, with everything the stage found.
709pub struct Checked {
710 pub program: crate::uses::ResolvedProgram,
711 pub outcome: blue_lang_check::Outcome,
712 /// The name table the stage resolved against: every program scope, then
713 /// the builtins. What an editor completes from.
714 pub names: NameTable,
715}
716
717impl Checked {
718 /// The forms an evaluator runs: [`lower`]ed, then erased — the tree
719 /// `run` evaluates, for a door that brings its own evaluator (the VM).
720 #[must_use]
721 pub fn erased(&self) -> Vec<tatara_lisp::Spanned> {
722 erase_types(&lower(&self.program, &self.names))
723 }
724
725 /// What `blue test` hands the harness: the program [`lower`]ed, spans
726 /// projected away.
727 #[must_use]
728 pub fn evaluable(&self) -> Vec<tatara_lisp::Sexp> {
729 lower(&self.program, &self.names)
730 .iter()
731 .map(tatara_lisp::Spanned::to_sexp)
732 .collect()
733 }
734
735 /// Every non-local reference, resolved under both rules — today's flat
736 /// one and per-bidama namespaces — and the resolved trees built from them
737 /// (`blue ast --resolved`).
738 #[must_use]
739 pub fn resolve(&self) -> blue_lang_check::names::Resolved {
740 blue_lang_check::names::resolve_program(self.program.forms(), &self.names, &|i| {
741 namespace_of(&self.program, i)
742 })
743 }
744}
745
746/// Parse, resolve and CHECK, without running: the door for `blue check`,
747/// `blue test` and the language server.
748///
749/// Returns every diagnostic, errors and warnings alike, rather than failing on
750/// the first error: a reader fixing a file wants the whole list, and an agent
751/// repairing one wants it in one pass. Whether errors stop anything is the
752/// caller's decision — `blue test` refuses to run tests over a program with
753/// one, exactly as `run` refuses to evaluate it.
754///
755/// The name oracle is the interpreter `blue run` would build: hostless plus
756/// input primitives.
757///
758/// # Errors
759///
760/// A parse or import error, which leaves no program to check.
761pub fn check_entry(
762 entry: Entry<'_>,
763 loader: &dyn crate::uses::Loader,
764 surface: Option<&blue_lang_syntax::yakugo::Yakugo>,
765 checking: Checking,
766) -> Result<Checked, RunError> {
767 let mut program = parse_and_resolve(entry, loader, surface)?;
768 if checking == Checking::Program {
769 program.retain(|f| !crate::uses::is_test_form(f));
770 }
771 let mut interp = crate::interpreter_hostless();
772 crate::inputs::install_input_primitives(&mut interp, Inputs::new());
773 let builtins = builtin_names(&interp);
774 let (outcome, names) = check_stage(&program, &builtins, checking == Checking::WithTests);
775 Ok(Checked {
776 program,
777 outcome,
778 names,
779 })
780}
781
782/// A syntax error in `text` as a [`B0006`](blue_lang_check::Code::B0006)
783/// diagnostic, so a tool reading `--format json` gets a parse failure in the
784/// same shape as every other finding. `None` when `text` parses.
785#[must_use]
786pub fn syntax_diagnostic(text: &str) -> Option<blue_lang_check::Diagnostic> {
787 let e = blue_lang_syntax::parse_program_tree(text).err()?;
788 Some(
789 blue_lang_check::Diagnostic::new(blue_lang_check::Code::B0006, e.message, e.span)
790 .at_top_level(0),
791 )
792}
793
794/// A runtime message as the author wrote the names in it: a definition runs
795/// under its key (`retsu/first`, `%root/f`), and a message naming one in
796/// backticks names it `retsu::first`, or `f` for the author's own script.
797#[must_use]
798pub fn display_keys(message: &str) -> String {
799 let root = format!(
800 "{}{}",
801 blue_lang_check::names::ROOT_QUALIFIER,
802 blue_lang_syntax::QUALIFIER
803 );
804 let mut out = String::with_capacity(message.len());
805 let mut parts = message.split('`');
806 if let Some(first) = parts.next() {
807 out.push_str(first);
808 }
809 for (i, part) in parts.enumerate() {
810 out.push('`');
811 // Odd pieces (0-based here) are inside backticks.
812 if i % 2 == 0 {
813 if let Some(rest) = part.strip_prefix(&root) {
814 out.push_str(rest);
815 } else if let Some((pkg, name)) = blue_lang_syntax::qualified(part) {
816 out.push_str(&format!("{pkg}::{name}"));
817 } else {
818 out.push_str(part);
819 }
820 } else {
821 out.push_str(part);
822 }
823 }
824 out
825}
826
827/// Evaluate a prepared program's top-level forms into `interp`, with `host`.
828pub(crate) fn eval_prepared<H: 'static>(
829 interp: &mut tatara_lisp_eval::Interpreter<H>,
830 prepared: &Prepared,
831 host: &mut H,
832) -> Result<Value, RunError> {
833 let Prepared {
834 program, erased, ..
835 } = prepared;
836
837 // RUN, one top-level form at a time, **counting them**.
838 //
839 // This is literally the loop `Interpreter::eval_program` runs internally
840 // (tatara-lisp-eval `eval.rs`) — unrolled here for exactly one reason: the
841 // INDEX. `resolve_uses` flattens N files into one form list and a `Span` is
842 // a byte range with no file identity, so an offset alone is ambiguous
843 // across files — 66 means something in every one of them. The index is the
844 // join key back to `ResolvedProgram`'s file table, and `eval_program`
845 // consumes it internally and hands back only the error.
846 //
847 // The same key, the same renderer and the same file table the type errors
848 // above already use. A runtime error was the one diagnostic in this
849 // function still reporting a bare message.
850 let mut value = Value::Nil;
851 for (top_level, form) in erased.iter().enumerate() {
852 value = interp.eval_top_form(form, host).map_err(|e| {
853 // `span()` is `None` for the arms that genuinely have no position
854 // (`Reader`, `Halted`, `NotImplemented`). Synthetic is the honest
855 // stand-in — `locate` renders it as a file with no line rather
856 // than inventing one.
857 let at = e.span().unwrap_or_else(tatara_lisp::Span::synthetic);
858 // `short_message`, NOT `Display`. Upstream's `Display` ends every
859 // positioned arm with ` at {span}` — a raw byte range — and once
860 // blue prefixes a resolved `path:line:col` that suffix is the same
861 // fact told twice, the second time in a unit no reader can spend.
862 //
863 // Worse than redundant: a byte range is exactly the thing this
864 // whole file-identity apparatus exists to stop being reported on
865 // its own, because an offset means something different in every
866 // file. On the one case blue deliberately declines to place — a
867 // raise inside a callee from another file — `Display` would print
868 // the CALLEE's offsets beside the CALLER's path, re-creating the
869 // precise-looking wrong answer `locate`'s guard just refused.
870 // `short_message` is upstream's own "no source context" accessor,
871 // and blue supplies the context.
872 //
873 // Through `messages::describe`, which is `short_message` except
874 // where blue can say more: a wrong argument count names the
875 // function the author called, read off the call in this tree,
876 // instead of upstream's `<closure>` and a Rust `Debug` arity.
877 let message = display_keys(&crate::messages::describe(&e, erased));
878 RunError::Eval(program.locate(top_level, at, &message).to_string())
879 })?;
880 }
881 Ok(value)
882}
883
884/// **A runtime error names a place.** The gates for the last stage that
885/// reported a bare message.
886///
887/// Each fixture is sized so that resolving the failing span against ANY file
888/// but the right one lands somewhere else — a different path and a different
889/// line — so a green run here is evidence about the join and not about the
890/// renderer being called at all.
891///
892/// **Each raise is an unbound name, WAIVED.** Since the check stage resolves
893/// names (B0001), an unbound call no longer reaches the evaluator; these
894/// fixtures need exactly that runtime raise, so each carries
895/// `# waive B0001: …` above the definition holding it — the one escape hatch,
896/// used for what it is for. The waiver adds a line above each failing call,
897/// which is why the positions below are one line later than their red runs
898/// (recorded against the unwaived fixtures) show.
899#[cfg(test)]
900mod position_tests {
901 use super::*;
902 use crate::uses::{Entry, Loader};
903 use std::collections::BTreeMap;
904 use std::path::Path;
905
906 struct MemLoader(BTreeMap<&'static str, &'static str>);
907
908 impl Loader for MemLoader {
909 fn load(&self, name: &str) -> Result<Vec<(String, String)>, String> {
910 self.0
911 .get(name)
912 .map(|s| vec![(format!("{name}.b"), (*s).to_owned())])
913 .ok_or_else(|| format!("no bidama named \"{name}\""))
914 }
915 }
916
917 /// A package whose OWN top-level code raises, deep inside a file that is
918 /// deliberately taller than either of the other two.
919 ///
920 /// Byte 66 — where `kore_wa_sonzai_shinai` starts — is past the end of the
921 /// entry file (30 bytes) and past the end of `kotae.b` (21 bytes). So the
922 /// only file this span can honestly resolve against is this one, and the
923 /// other two now refuse to answer rather than walking off their ends.
924 const BAKUHATSU: &str = "\
925def tsukawanai_a()
926 1
927end
928
929def tsukawanai_b()
930 2
931end
932
933# waive B0001: the fixture needs a raise at runtime
934def bakuhatsu()
935 kore_wa_sonzai_shinai()
936end
937
938bakuhatsu()";
939
940 const KOTAE: &str = "def kotae()\n 42\nend";
941
942 /// The same failing call, but only ever reached by a CALLER in another
943 /// file — the shape that exercises `RunError::Eval`'s stated limit.
944 const BAKUHATSU2: &str =
945 "# waive B0001: a raise at runtime\ndef yobu()\n kore_wa_sonzai_shinai()\nend";
946
947 fn loader() -> MemLoader {
948 MemLoader(BTreeMap::from([
949 ("kotae", KOTAE),
950 ("bakuhatsu", BAKUHATSU),
951 ("bakuhatsu2", BAKUHATSU2),
952 ]))
953 }
954
955 fn run_named(path: &str, src: &str) -> RunError {
956 run_in_surface(
957 Entry {
958 path: Some(Path::new(path)),
959 text: src,
960 },
961 Inputs::new(),
962 &loader(),
963 None,
964 )
965 .expect_err("the fixture must raise")
966 }
967
968 /// **The load-bearing gate: a raise inside an IMPORTED package reports the
969 /// IMPORTED file's path and line.**
970 ///
971 /// Hand-computed. In `bakuhatsu.b` the failing call sits on line 11 at
972 /// column 3, and the whole point of the fixture's height is that no other
973 /// file in the program can produce that pair: the entry file has 2 lines
974 /// and `kotae.b` has 3, so *any* mis-attribution is visible as a different
975 /// path AND a different position, never as a coincidence.
976 ///
977 /// **Three red runs, one per edit this change makes** — each isolates a
978 /// different half of the mechanism, which is what stops the gate being a
979 /// tautology over "some renderer ran".
980 ///
981 /// 1. `eval_program` + `.map_err(|e| RunError::Eval(e.to_string()))`
982 /// restored (the pre-change code):
983 /// ```text
984 /// left: "runtime error: unbound symbol: kore_wa_sonzai_shinai at 74..95"
985 /// right: "runtime error: bakuhatsu.b:10:3: unbound symbol `kore_wa_sonzai_shinai`"
986 /// ```
987 /// No place at all — the state this change is fixing — and the trailing
988 /// `at 74..95` is upstream's `Display`, an offset with no file, which is
989 /// why the loop renders `short_message` instead.
990 ///
991 /// 2. The loop kept, but `0` passed to `locate` in place of `top_level`:
992 /// ```text
993 /// left: "runtime error: kotae.b: unbound symbol `kore_wa_sonzai_shinai`"
994 /// right: "runtime error: bakuhatsu.b:10:3: unbound symbol `kore_wa_sonzai_shinai`"
995 /// ```
996 /// **This is the one that proves the INDEX is load-bearing** rather than
997 /// the renderer: form 0 belongs to `kotae.b`, so the wrong file is named
998 /// — and named without a position, because the guard in `locate` sees
999 /// that byte 74 is not a range in a 21-byte file and declines to invent
1000 /// one. A fixture with one imported package could not tell these apart.
1001 ///
1002 /// 3. `crate::lower_to_spanned(&crate::to_sexps(&erased))` reinstated
1003 /// between erasure and the loop:
1004 /// ```text
1005 /// left: "runtime error: bakuhatsu.b: unbound symbol `kore_wa_sonzai_shinai`"
1006 /// right: "runtime error: bakuhatsu.b:10:3: unbound symbol `kore_wa_sonzai_shinai`"
1007 /// ```
1008 /// The right FILE with no position — which is exactly the shape of the
1009 /// bug: the index survived the lift and every span did not.
1010 /// kotae loads first so bakuhatsu's forms do not start at index 0: the
1011 /// order is the fixture, and the waiver says so (B0015 wants them sorted).
1012 const TWO_IMPORTS: &str =
1013 "use(\"kotae\")\n# waive B0015: kotae loads first on purpose\nuse(\"bakuhatsu\")\n";
1014
1015 #[test]
1016 fn a_raise_inside_an_imported_package_names_that_package() {
1017 let err = run_named("entry.b", TWO_IMPORTS);
1018 assert_eq!(
1019 err.to_string(),
1020 "runtime error: bakuhatsu.b:11:3: unbound symbol `kore_wa_sonzai_shinai`"
1021 );
1022 }
1023
1024 /// Anti-vacuity for the fixture itself: the position asserted above is a
1025 /// FACT ABOUT `bakuhatsu.b`, checked against the source text rather than
1026 /// against the thing that produced it.
1027 ///
1028 /// Without this, `11:3` is just a number that happened to come out of the
1029 /// code under test, and a change that moved every reported line by one
1030 /// would move this assertion with it.
1031 #[test]
1032 fn line_11_column_3_of_the_fixture_is_the_failing_call() {
1033 let line = BAKUHATSU.lines().nth(10).expect("line 11 exists");
1034 assert_eq!(line, " kore_wa_sonzai_shinai()");
1035 assert_eq!(
1036 &line[2..],
1037 "kore_wa_sonzai_shinai()",
1038 "column 3 (1-indexed) is where the call starts"
1039 );
1040 // And the other two files cannot reach that far, which is what makes a
1041 // mis-attribution loud instead of plausible.
1042 let offset = BAKUHATSU.find("kore_wa_sonzai_shinai").expect("in fixture");
1043 assert!(
1044 offset > KOTAE.len(),
1045 "kotae.b could answer for byte {offset}"
1046 );
1047 assert!(
1048 offset > TWO_IMPORTS.len(),
1049 "the entry file could answer for byte {offset}"
1050 );
1051 }
1052
1053 /// **The `FileId(0)` path: a raise in the ENTRY file reports the entry
1054 /// file.** The single-file case, which is every program that imports
1055 /// nothing — and the one where the index and the span cannot disagree.
1056 ///
1057 /// **Red run** (2026-08-12), `eval_program` +
1058 /// `.map_err(|e| RunError::Eval(e.to_string()))` restored:
1059 /// ```text
1060 /// left: "runtime error: unbound symbol: nani_mo_nai at 17..28"
1061 /// right: "runtime error: honmono.b:5:1: unbound symbol `nani_mo_nai`"
1062 /// ```
1063 ///
1064 /// **Second red run**, `lower_to_spanned` reinstated after erasure — the
1065 /// one that isolates the SPAN half from the index half, since a
1066 /// single-file program cannot get its file wrong:
1067 /// ```text
1068 /// left: "runtime error: honmono.b: unbound symbol `nani_mo_nai`"
1069 /// right: "runtime error: honmono.b:5:1: unbound symbol `nani_mo_nai`"
1070 /// ```
1071 #[test]
1072 fn a_raise_in_the_entry_file_names_the_entry_file() {
1073 let err = run_named(
1074 "honmono.b",
1075 "def f()\n 1\nend\n\n# waive B0001: a raise at runtime\nnani_mo_nai()\n",
1076 );
1077 assert_eq!(
1078 err.to_string(),
1079 "runtime error: honmono.b:6:1: unbound symbol `nani_mo_nai`"
1080 );
1081 }
1082
1083 /// A raise from a form that came through the SURFACE-level erasure still
1084 /// reports a real position — the annotated path, where erasure rewrites
1085 /// the node rather than passing it through.
1086 ///
1087 /// Separate from the two above because the erasure rewrite is the only
1088 /// place a node is built rather than kept, and a synthetic span there
1089 /// would be invisible to a fixture whose failing form is untouched.
1090 ///
1091 /// **Red run** (2026-08-12), `lower_to_spanned` reinstated after erasure:
1092 /// ```text
1093 /// left: "runtime error: chuu.b: unbound symbol `mada_nai`"
1094 /// right: "runtime error: chuu.b:2:3: unbound symbol `mada_nai`"
1095 /// ```
1096 #[test]
1097 fn a_raise_inside_an_annotated_def_still_names_its_line() {
1098 let err = run_named(
1099 "chuu.b",
1100 "# waive B0001: a raise at runtime\ndef f(a: Int) -> Int\n mada_nai(a)\nend\n\nf(1)\n",
1101 );
1102 assert_eq!(
1103 err.to_string(),
1104 "runtime error: chuu.b:3:3: unbound symbol `mada_nai`"
1105 );
1106 }
1107
1108 /// **The stated limit, pinned rather than left to be discovered.**
1109 ///
1110 /// A top-level form in the entry file calls a function defined in an
1111 /// imported one, and the raise happens inside the callee. The index names
1112 /// the executing form (the entry's), the span names the callee's bytes,
1113 /// and the two disagree about the file. `locate`'s in-range guard is what
1114 /// keeps that from rendering as a precise-looking `entry:line:col`: the
1115 /// offset is not a range in the entry file, so no position is printed.
1116 ///
1117 /// This test asserts the CURRENT behaviour, including the part that is
1118 /// wrong — the path is the caller's. It is here so the next author reads
1119 /// the limit off a green suite instead of off a bug report. Closing it
1120 /// needs per-frame file identity at the evaluator; see `RunError::Eval`.
1121 ///
1122 /// **Red run** (2026-08-12), the `span.end <= file.text.len()` guard
1123 /// removed from `uses::ResolvedProgram::locate` — and it is the ONLY test
1124 /// in the crate that moves, which is the same run's evidence that the
1125 /// guard is a no-op on every check-time diagnostic:
1126 /// ```text
1127 /// left: "runtime error: yobidashi.b:1:14: unbound symbol `kore_wa_sonzai_shinai`"
1128 /// right: "runtime error: yobidashi.b: unbound symbol `kore_wa_sonzai_shinai`"
1129 /// ```
1130 /// `1:14` lands in the middle of `use("bakuhatsu2")` — the callee's start
1131 /// offset happens to be in range for the caller's file even though its end
1132 /// is not, so a wrong file gets a precise number pointing at innocent
1133 /// code. Exactly the failure this repo's file-identity work exists to
1134 /// refuse, which is why the guard tests BOTH ends.
1135 ///
1136 /// **Re-run 2026-09-29**, after the fixtures gained their waiver lines:
1137 /// the guard's END check replaced by a start check (`span.start <= len`),
1138 /// with the caller padded so the callee's start (byte 47) is in range and
1139 /// its end (byte 70) is not:
1140 /// ```text
1141 /// left: "runtime error: yobidashi.b:3:1: unbound symbol `kore_wa_sonzai_shinai`"
1142 /// right: "runtime error: yobidashi.b: unbound symbol `kore_wa_sonzai_shinai`"
1143 /// ```
1144 #[test]
1145 fn a_raise_in_a_callee_from_another_file_reports_no_position_rather_than_a_wrong_one() {
1146 // The comment is padding, and load-bearing: `bakuhatsu2.b`'s failing
1147 // call starts at byte 47 (after its waiver line) and ends at 70, and
1148 // this file is 57 bytes, so the start is in range here and the end is
1149 // not — the half-in-range case the guard's END check exists for.
1150 let err = run_named(
1151 "yobidashi.b",
1152 "use(\"bakuhatsu2\")\n# pad: 47 in range\nbakuhatsu2::yobu()\n",
1153 );
1154 assert_eq!(
1155 err.to_string(),
1156 "runtime error: yobidashi.b: unbound symbol `kore_wa_sonzai_shinai`",
1157 "the caller's file is named (the stated limit) but NOT with a \
1158 position it cannot justify"
1159 );
1160 }
1161}
1162
1163#[cfg(test)]
1164mod tests {
1165 use super::*;
1166
1167 fn int(src: &str) -> i64 {
1168 match run(src).unwrap_or_else(|e| panic!("{src:?}: {e}")).value {
1169 Value::Int(v) => v,
1170 other => panic!("{src:?} produced {other:?}"),
1171 }
1172 }
1173
1174 /// **The sliding scale, as one assertion.** Annotating changes the
1175 /// analysis and nothing else.
1176 #[test]
1177 fn annotating_buys_analysis_and_changes_nothing_else() {
1178 let plain = run("def add(a, b)\n a + b\nend\nadd(2, 3)").expect("plain");
1179 let typed = run("def add(a: Int, b: Int) -> Int\n a + b\nend\nadd(2, 3)").expect("typed");
1180
1181 assert!(matches!(plain.value, Value::Int(5)));
1182 assert!(
1183 matches!(typed.value, Value::Int(5)),
1184 "the annotated program must compute the same answer"
1185 );
1186 assert_eq!(plain.visited, 0, "no annotations means no analysis");
1187 assert!(
1188 typed.visited > 0,
1189 "an annotation must actually buy analysis, not just decorate"
1190 );
1191 assert_eq!(plain.typed_decls, 0);
1192 assert_eq!(typed.typed_decls, 1);
1193 }
1194
1195 /// **Checking happens before erasure.** This is the test that catches the
1196 /// reordering: a program with a declared-type violation must be rejected,
1197 /// and it can only be rejected while the annotations still exist.
1198 #[test]
1199 fn a_type_error_is_reported_and_the_program_does_not_run() {
1200 let err = run("def add(a: Int, b: Int) -> Str\n a + b\nend\nadd(1, 2)")
1201 .expect_err("a declared Str return from an Int body must be rejected");
1202 assert!(
1203 matches!(err, RunError::Types(ref d) if !d.is_empty()),
1204 "expected type diagnostics, got {err}"
1205 );
1206 }
1207
1208 /// And the untyped version of the same program runs, so the rejection
1209 /// above is the annotation's doing rather than a parse failure.
1210 #[test]
1211 fn the_same_program_without_annotations_runs() {
1212 assert_eq!(int("def add(a, b)\n a + b\nend\nadd(1, 2)"), 3);
1213 }
1214
1215 #[test]
1216 fn a_parse_error_is_reported_as_one() {
1217 assert!(matches!(run("def (").unwrap_err(), RunError::Parse(_)));
1218 }
1219
1220 /// Every stage reports in its own vocabulary, so a failure names which
1221 /// stage failed rather than surfacing as a generic error.
1222 #[test]
1223 fn a_runtime_error_is_reported_as_one() {
1224 let err = run("throw(error(\"boom\"))").expect_err("a raise");
1225 assert!(matches!(err, RunError::Eval(_)), "got {err}");
1226 }
1227
1228 /// Stdlib and primitives are both reachable through the pipeline — the
1229 /// gap that made `6 % 3` fail.
1230 #[test]
1231 fn the_pipeline_reaches_both_runtime_layers() {
1232 assert_eq!(int("6 % 3"), 0);
1233 assert_eq!(int("7 % 3"), 1);
1234 assert_eq!(int("2 + 3 * 4"), 14);
1235 }
1236
1237 /// **The deleted hop was a no-op on everything blue emits — so removing it
1238 /// is a swap, not a behaviour change.**
1239 ///
1240 /// The old lowering printed the erased tree and read it back through
1241 /// `tatara_lisp::read_spanned`. This walks a corpus and asserts the two
1242 /// paths land on the same tree, which is the equivalence the swap rests on.
1243 /// It is stated as a *measurement over this corpus*, not as a theorem:
1244 /// the round trip is not identity in general (that is precisely why it had
1245 /// to go), it merely happened to be identity for the bytes blue emits.
1246 #[test]
1247 fn the_deleted_round_trip_agreed_with_the_direct_lowering() {
1248 let corpus = [
1249 "def add(a, b)\n a + b\nend\nadd(2, 3)",
1250 "def fact(n)\n if n < 2\n 1\n else\n n * fact(n - 1)\n end\nend\nfact(5)",
1251 "def f(a, b)\n c = a + b\n c * 2\nend\nf(1, 2)",
1252 "defmacro sq(e)\n quote\n unquote(e) * unquote(e)\n end\nend\nsq(2 + 3)",
1253 "\"a string with spaces, a ( and a )\"",
1254 "def g(a: Int) -> Int\n a + 1\nend\ng(1)",
1255 "6 % 3",
1256 "1.5 + 2.25",
1257 ];
1258 for src in corpus {
1259 // Projected to `Sexp` because that is what the deleted hop
1260 // operated on. Erasure itself no longer goes near it.
1261 let erased = crate::to_sexps(&erase_types(&parse_tree(src).expect("parse")));
1262
1263 let direct: Vec<Sexp> = crate::lower_to_spanned(&erased)
1264 .iter()
1265 .map(tatara_lisp::Spanned::to_sexp)
1266 .collect();
1267 assert_eq!(direct, erased, "the direct lowering must be the identity");
1268
1269 let text = erased
1270 .iter()
1271 .map(ToString::to_string)
1272 .collect::<Vec<_>>()
1273 .join("\n");
1274 let round_tripped: Vec<Sexp> = tatara_lisp::read_spanned(&text)
1275 .unwrap_or_else(|e| panic!("{src:?}: the old path could not read back: {e:?}"))
1276 .iter()
1277 .map(tatara_lisp::Spanned::to_sexp)
1278 .collect();
1279 assert_eq!(
1280 round_tripped, erased,
1281 "{src:?}: the old print-and-reparse path changed the tree"
1282 );
1283 }
1284 }
1285
1286 /// Anti-vacuity for the test above: the round trip really is *not* the
1287 /// identity in general, so agreeing on the corpus was a property of what
1288 /// blue happens to emit rather than a property of the reader.
1289 ///
1290 /// **`Atom::Symbol`'s `Display` writes the name raw, with no escaping.**
1291 /// `Atom::Str` escapes and its docs explain at length why; the symbol arm
1292 /// is `f.write_str(s)`. So print-then-read is not inverse over the symbol
1293 /// domain, and the failure is *silent*: a symbol containing a space prints
1294 /// as two tokens, reads back as two symbols, and the result is a perfectly
1295 /// well-formed tree with a different meaning. No error, nothing to catch.
1296 ///
1297 /// Measured 2026-08-02 across the separators: `a b` and `x'y` come back
1298 /// `Ok` with a different tree; `x)y`, `x"y` and `x;y` come back `Err`;
1299 /// `x{y` and `x[y` DO round-trip at this level — those two are one symbol
1300 /// in and one symbol out, so the brace-fusion reported in tatara *source*
1301 /// is not what bites a printed tree. The silent pair is what makes this a
1302 /// miscompile class rather than a noisy one.
1303 #[test]
1304 fn the_round_trip_is_not_the_identity_in_general() {
1305 let tree = Sexp::List(vec![
1306 Sexp::Atom(tatara_lisp::Atom::Symbol("f".into())),
1307 Sexp::Atom(tatara_lisp::Atom::Symbol("a b".into())),
1308 ]);
1309 let text = tree.to_string();
1310 let back: Vec<Sexp> = tatara_lisp::read_spanned(&text)
1311 .expect("it reads back cleanly — that IS the problem")
1312 .iter()
1313 .map(tatara_lisp::Spanned::to_sexp)
1314 .collect();
1315 assert_ne!(
1316 back,
1317 vec![tree.clone()],
1318 "if print-then-read became inverse over symbols, the class would be \
1319 closed upstream and this test should be deleted rather than relaxed"
1320 );
1321 // …and the direct lowering is unaffected by any of it.
1322 let direct: Vec<Sexp> = crate::lower_to_spanned(std::slice::from_ref(&tree))
1323 .iter()
1324 .map(tatara_lisp::Spanned::to_sexp)
1325 .collect();
1326 assert_eq!(direct, vec![tree]);
1327 }
1328}
1329
1330#[cfg(test)]
1331mod macro_tests {
1332 use super::*;
1333
1334 fn int(src: &str) -> i64 {
1335 match run(src).unwrap_or_else(|e| panic!("{src:?}: {e}")).value {
1336 Value::Int(v) => v,
1337 other => panic!("{src:?} produced {other:?}"),
1338 }
1339 }
1340
1341 /// **A blue macro expands and runs.** Tenet 2's surface, end to end.
1342 #[test]
1343 fn a_macro_expands_and_runs() {
1344 assert_eq!(
1345 int("defmacro double(x)\n quote\n unquote(x) + unquote(x)\n end\nend\ndouble(21)"),
1346 42
1347 );
1348 }
1349
1350 /// A macro receives *source forms*, not values — so it can duplicate its
1351 /// argument, which a function cannot do without re-evaluating it.
1352 #[test]
1353 fn a_macro_operates_on_syntax_not_values() {
1354 assert_eq!(
1355 int("defmacro sq(e)\n quote\n unquote(e) * unquote(e)\n end\nend\nsq(2 + 3)"),
1356 25,
1357 "the argument form `2 + 3` must be substituted twice"
1358 );
1359 }
1360
1361 /// **A runaway macro is a typed error, not a dead compiler.** This is the
1362 /// property that makes the metaprogramming surface safe to hand to a user.
1363 ///
1364 /// The assertion was `contains("expansion limit")` and now reads
1365 /// `contains("expansion")`, because the pipeline renders `short_message`
1366 /// rather than `Display`: upstream words the same fact as "exceeded 256
1367 /// expansion steps" instead of "exceeded the expansion limit of 256
1368 /// rewrites". The PROPERTY is unchanged and still asserted — the macro is
1369 /// named, and the failure is attributed to expansion rather than to
1370 /// evaluation. **This reword was the whole measured blast radius of that
1371 /// switch**, one test in the workspace.
1372 ///
1373 /// The number is deliberately NOT pinned. It is upstream's constant, this
1374 /// test owns none of it, and matching it would turn an upstream bump into
1375 /// a red run here that proves nothing about blue.
1376 ///
1377 /// The third clause is new capability rather than repair: the message now
1378 /// carries `<anonymous>:6:1`, so a runaway macro says WHERE it ran away.
1379 #[test]
1380 fn a_runaway_macro_fails_the_compilation_rather_than_the_process() {
1381 let err =
1382 run("defmacro forever(x)\n quote\n forever(unquote(x))\n end\nend\nforever(1)")
1383 .expect_err("a self-referential macro must be rejected");
1384 let msg = err.to_string();
1385 assert!(
1386 msg.contains("forever") && msg.contains("expansion"),
1387 "the error must name the macro and the limit it hit: {msg}"
1388 );
1389 assert!(
1390 msg.contains("<anonymous>:6:1"),
1391 "and must place the call that ran away — line 6 is `forever(1)`: {msg}"
1392 );
1393 }
1394}
1395
1396#[cfg(test)]
1397mod input_tests {
1398 use super::*;
1399 use crate::inputs::{Declaration, Inputs};
1400
1401 /// A schema a macro will generate code from.
1402 const SCHEMA: &[u8] = b"3";
1403
1404 fn with_schema(src: &str) -> Result<Run, RunError> {
1405 let hash = Inputs::hash_of(SCHEMA);
1406 let mut inputs = Inputs::new();
1407 inputs
1408 .bind(
1409 &Declaration {
1410 name: "schema".to_string(),
1411 hash,
1412 },
1413 SCHEMA.to_vec(),
1414 )
1415 .expect("bind");
1416 run_with_inputs(src, inputs)
1417 }
1418
1419 fn decl_line() -> String {
1420 let mut s = String::from("definput(\"schema\", \"");
1421 s.push_str(&Inputs::hash_of(SCHEMA));
1422 s.push_str("\")\n");
1423 s
1424 }
1425
1426 /// **A macro reads a declared build input.** This is §VI OPEN #6 closed —
1427 /// the spec names it as gating blue's whole "stronger than Ruby's
1428 /// metaprogramming" claim, because a macro that cannot read a schema cannot
1429 /// generate code from one.
1430 #[test]
1431 fn a_macro_can_read_a_declared_build_input() {
1432 let src = decl_line() + "input(\"schema\")";
1433 let out = with_schema(&src).expect("run");
1434 assert!(
1435 matches!(out.value, Value::Str(ref s) if &**s == "3"),
1436 "got {:?}",
1437 out.value
1438 );
1439 }
1440
1441 /// **An undeclared input is an error, not a file read and not nil.**
1442 /// Returning nil is how a macro generates an empty table and nobody notices
1443 /// until runtime.
1444 #[test]
1445 fn an_undeclared_input_is_an_error() {
1446 let err = with_schema("input(\"not_declared\")").expect_err("must fail");
1447 let msg = err.to_string();
1448 assert!(msg.contains("not_declared"), "must name it: {msg}");
1449 assert!(msg.contains("definput"), "and say how to declare it: {msg}");
1450 }
1451
1452 /// **There is no path-based read at all.** The capability is the absence of
1453 /// the primitive, not a check inside one — so this is an unbound symbol.
1454 ///
1455 /// Holds for the DEFAULT surface — the one every embedder gets. The `sys`
1456 /// cargo feature (CLI only) is the one declared exception: it is the
1457 /// operator's own trusted host surface, and is asserted in
1458 /// `sys_read_file_is_the_trusted_cli_only_exception` below.
1459 #[cfg(not(feature = "sys"))]
1460 #[test]
1461 fn there_is_no_ambient_file_read() {
1462 for attempt in [
1463 "read_file(\"/etc/passwd\")",
1464 "File(\"/etc/passwd\")",
1465 "slurp(\"/etc/passwd\")",
1466 "open(\"/etc/passwd\")",
1467 ] {
1468 let err = with_schema(attempt).expect_err("must not resolve");
1469 assert!(
1470 err.to_string().contains("unbound"),
1471 "{attempt} must be UNBOUND — a capability removed by absence, \
1472 not guarded by a check: {err}"
1473 );
1474 }
1475 }
1476
1477 /// With the `sys` feature compiled in, `read_file` IS bound — that is the
1478 /// point of the feature. The doctrine does not move: this is the operator's
1479 /// own machine (the CLI), not an embedder's sandbox. Pin the boundary so a
1480 /// future default-build change is heard, and assert that `input()` still
1481 /// works beside it.
1482 #[cfg(feature = "sys")]
1483 #[test]
1484 fn sys_read_file_is_the_trusted_cli_only_exception() {
1485 let err = with_schema("definitely_not_a_primitive(\"x\")").expect_err("must not resolve");
1486 assert!(err.to_string().contains("unbound"), "{err}");
1487 assert!(
1488 with_schema("read_file(\"/etc/passwd\")").is_ok(),
1489 "with `sys` on, read_file is the trusted CLI surface"
1490 );
1491 let out = with_schema("input(\"schema\")").expect("run");
1492 assert!(
1493 matches!(out.value, Value::Str(ref s) if &**s == "3"),
1494 "input() still binds beside the sys surface: {:?}",
1495 out.value
1496 );
1497 }
1498
1499 /// Anti-vacuity: with no inputs supplied at all, even a declared name fails
1500 /// — so the success above is the binding's doing.
1501 #[test]
1502 fn a_declared_input_with_no_bytes_supplied_fails() {
1503 let src = decl_line() + "input(\"schema\")";
1504 assert!(run(&src).is_err(), "no bytes were supplied");
1505 }
1506}
1507
1508#[cfg(test)]
1509mod tier2_tests {
1510 use super::*;
1511 use crate::inputs::{Declaration, Inputs};
1512
1513 /// **The Tier-2 conversion §V.6.3 said was gated: a macro that emits real
1514 /// declarations FROM A SCHEMA.**
1515 ///
1516 /// `theory/BLUE.md` §VI OPEN #6 states the blocker plainly — "tenet 2
1517 /// installs a `NoLoader`, so a macro cannot read a schema — which gates
1518 /// every Tier-2 conversion in §V.6 and therefore blue's whole 'stronger than
1519 /// Ruby's metaprogramming' claim."
1520 ///
1521 /// Here the schema supplies a *value the generated code depends on*, read at
1522 /// expansion time. Ruby and Elixir can both do this — with the whole
1523 /// filesystem open. blue does it through a name bound to a content hash.
1524 #[test]
1525 fn a_macro_generates_code_from_a_schema() {
1526 let schema = b"7";
1527 let mut inputs = Inputs::new();
1528 inputs
1529 .bind(
1530 &Declaration {
1531 name: "arity".to_string(),
1532 hash: Inputs::hash_of(schema),
1533 },
1534 schema.to_vec(),
1535 )
1536 .expect("bind");
1537
1538 // The macro reads the input at EXPANSION time and splices the value it
1539 // found into the code it emits.
1540 let mut src = String::from("definput(\"arity\", \"");
1541 src.push_str(&Inputs::hash_of(schema));
1542 src.push_str("\")\n");
1543 src.push_str(
1544 "defmacro from_schema()\n quote\n unquote(to_int(input(\"arity\")))\n end\nend\n\
1545 from_schema() * 6",
1546 );
1547
1548 let out = run_with_inputs(&src, inputs).expect("run");
1549 assert!(
1550 matches!(out.value, Value::Int(42)),
1551 "the schema's 7 must reach the generated code: got {:?}",
1552 out.value
1553 );
1554 }
1555}