blue_lang_runtime/uses.rs
1//! `use("name")` — how a blue program consumes a bidama.
2//!
3//! ## The gap this closes
4//!
5//! Before this, blue's packaging had a manifest (`Bluefile`), a resolver
6//! (`blue-lang-pkg`), a git registry and a nix derivation per package — and
7//! **no way for a program to consume any of it**. `retsu/Bluefile` declared
8//! `needs("kazu", "^0.1")` while `retsu`'s source never referenced a single
9//! thing from kazu, because the language had no import form at all. The
10//! dependency graph was described by four layers and traversed by none.
11//!
12//! That is the difference between packaging that exists and packaging that
13//! works, and it is why this is a load-bearing addition rather than a
14//! convenience: a distribution whose packages cannot see each other is a
15//! directory of unrelated files wearing a distribution's name.
16//!
17//! ## Why a pipeline pass and not a builtin
18//!
19//! A `register_fn` native takes `(&[Value], &mut H, Span)`. It cannot define
20//! anything, because it never sees the interpreter — so `use` implemented as a
21//! builtin could load a package's text and would have nowhere to put its
22//! definitions. Resolution therefore happens on the FORM tree, before
23//! evaluation: a `use` is replaced by the forms of the package it names, and
24//! the whole program is then checked and run as one unit.
25//!
26//! That ordering is deliberate and worth stating, because it decides a real
27//! behaviour: the type checker sees the imported code, so a package that fails
28//! `blue-lang-check` fails at the point its consumer imports it rather than at
29//! whatever later moment its code first ran.
30//!
31//! ## Why the loader is a trait
32//!
33//! Loading a package means reading a filesystem, and this crate compiles to
34//! `wasm32-unknown-unknown` with zero host imports (`blue-lang-wasm`). A
35//! direct `std::fs` call here would break that target for every consumer,
36//! including ones that never call `use`.
37//!
38//! So the capability is injected — [`Loader`] here, the real filesystem
39//! implementation in `blue-lang-pkg`, which is native-only. This is the
40//! fleet's mockable-`Environment` seam, and it buys the usual thing: every
41//! test below drives the whole resolution pass against an in-memory loader,
42//! with no temp directories and no fixture files on disk.
43//!
44//! ## Why this module also owns file identity
45//!
46//! Splicing several files into one program is exactly the act that destroys a
47//! byte offset's meaning, so the record of where each form came from is kept by
48//! the pass that does the splicing rather than reconstructed downstream by
49//! something that no longer has the sources. [`ResolvedProgram`] is that
50//! record, and [`ResolvedProgram::locate`] spends it.
51
52use std::collections::BTreeSet;
53use std::path::{Path, PathBuf};
54
55use tatara_lisp::{Atom, Sexp, Span, Spanned, SpannedForm};
56
57/// Supplies the source of a named bidama.
58///
59/// One method, because resolution needs exactly one thing: given a name, the
60/// blue source that name refers to. *Where* it came from — a working tree, a
61/// nix store path, a git object, memory — is the implementation's business and
62/// deliberately invisible here.
63pub trait Loader {
64 /// The `.b` sources of `name`, as `(label, source)` pairs.
65 ///
66 /// The label is for diagnostics only; nothing keys on it. A package with
67 /// several files returns several pairs, and their relative order is the
68 /// implementation's to fix — [`FsLoader`](../../blue_lang_pkg/load_path/index.html)
69 /// sorts by filename so a load is reproducible rather than
70 /// directory-order-dependent.
71 ///
72 /// `Err` is a human-readable reason the package could not be loaded. It
73 /// must name what was looked for, because "package not found" without a
74 /// name sends the reader grepping a distribution to find which one.
75 fn load(&self, name: &str) -> Result<Vec<(String, String)>, String>;
76}
77
78/// A loader that resolves nothing, and says so.
79///
80/// The default for [`run`](crate::pipeline::run), so a program using `use` in
81/// a context with no packaging configured gets a typed error naming the
82/// package — not a silently-undefined function that fails much later as an
83/// unbound symbol pointing at innocent code.
84pub struct NoLoader;
85
86impl Loader for NoLoader {
87 fn load(&self, name: &str) -> Result<Vec<(String, String)>, String> {
88 Err(format!(
89 "cannot load bidama \"{name}\": no loader is installed. A program \
90 that uses packages must run with one — `blue_lang_pkg::LoadPath` \
91 reads BLUE_PATH, which `nix develop` and the bidama derivations \
92 populate."
93 ))
94 }
95}
96
97/// Identity of one source file inside a [`ResolvedProgram`].
98///
99/// Opaque on purpose. It means nothing outside the program that minted it, and
100/// the only way to spend it is [`ResolvedProgram::file`] — a caller that could
101/// compute a file index is a caller that can compute the *wrong* one, which is
102/// the failure this whole type exists to make impossible.
103#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
104pub struct FileId(usize);
105
106/// One file that contributed forms to a [`ResolvedProgram`].
107#[derive(Clone, Debug)]
108pub struct SourceFile {
109 /// Its handle in the program that owns it.
110 pub id: FileId,
111 /// Where it came from.
112 ///
113 /// `None` only for an entry program handed to the pipeline as bare text —
114 /// an embedder, a test, a WASM host. An imported file always has one,
115 /// because a [`Loader`] cannot name a package without naming a place.
116 pub path: Option<PathBuf>,
117 /// The file's text.
118 ///
119 /// Kept, not dropped after parsing, because a [`Span`] is a byte range and
120 /// nothing else: turning one into `line:col` needs the exact string it
121 /// indexes. Before this, `resolve_uses` read a package's source and let it
122 /// fall out of scope one line later, which is why an imported type error
123 /// had no position to report.
124 pub text: String,
125}
126
127/// The program the pipeline was handed, and what to call it.
128///
129/// The path travels *with* the text rather than beside it, so a caller cannot
130/// name one file and hand over another's source — the pairing is what every
131/// position in the entry file is resolved against.
132#[derive(Clone, Copy, Debug)]
133pub struct Entry<'a> {
134 /// The file the text was read from, if it was read from one.
135 pub path: Option<&'a Path>,
136 /// The source itself.
137 pub text: &'a str,
138}
139
140impl<'a> Entry<'a> {
141 /// An entry program with no file behind it.
142 #[must_use]
143 pub fn anonymous(text: &'a str) -> Self {
144 Self { path: None, text }
145 }
146}
147
148/// A program with its imports spliced in, and the file each top-level form
149/// came from.
150///
151/// ## Why file identity is per TOP-LEVEL FORM
152///
153/// [`resolve_uses`] splices whole files' worth of top-level forms: a `use` is
154/// replaced by every form of the package it names, in order. So a boundary
155/// between two files is always a boundary between two top-level forms, and
156/// every node beneath a top-level form came from exactly one file — the one
157/// that form came from. **A `FileId` per top-level form is therefore exactly as
158/// precise as a `FileId` per node**, at a fraction of the cost and with nothing
159/// upstream to change.
160///
161/// That last part is the point. `Span` lives in tatara-lisp and carries no file
162/// identity by a documented decision — "Spans are not portable across source
163/// inputs — they are meaningful only relative to the string that produced them,
164/// which the caller is responsible for holding onto." blue is the caller. This
165/// is blue holding onto them, beside the span rather than inside it.
166///
167/// ## Why the fields are private
168///
169/// `forms` and `owner` are parallel, and `files[i].id` is `FileId(i)`. Both
170/// invariants are maintained by `push` and `intern`, the only places either
171/// vector grows, and by [`retain`](Self::retain), the only place either
172/// shrinks. Public fields would make `program.forms.retain(...)` — the exact
173/// call `pipeline::run_in_surface` makes to drop `test` blocks — shift every
174/// form off its owner by one and report every subsequent diagnostic against the
175/// wrong file, silently.
176#[derive(Debug)]
177pub struct ResolvedProgram {
178 forms: Vec<Spanned>,
179 owner: Vec<FileId>,
180 files: Vec<SourceFile>,
181}
182
183impl ResolvedProgram {
184 /// The entry program's own file. Always present; always first.
185 pub const ENTRY: FileId = FileId(0);
186
187 fn new(entry: Entry<'_>) -> Self {
188 let mut program = Self {
189 forms: Vec::new(),
190 owner: Vec::new(),
191 files: Vec::new(),
192 };
193 let id = program.intern(entry.path.map(Path::to_path_buf), entry.text.to_owned());
194 debug_assert_eq!(id, Self::ENTRY);
195 program
196 }
197
198 /// Record a file's text and hand back its handle.
199 fn intern(&mut self, path: Option<PathBuf>, text: String) -> FileId {
200 let id = FileId(self.files.len());
201 self.files.push(SourceFile { id, path, text });
202 id
203 }
204
205 /// Append a top-level form together with the file it came from.
206 ///
207 /// The ONE place `forms` and `owner` grow, so they cannot grow apart.
208 fn push(&mut self, form: Spanned, owner: FileId) {
209 self.forms.push(form);
210 self.owner.push(owner);
211 }
212
213 /// Every top-level form, in evaluation order.
214 #[must_use]
215 pub fn forms(&self) -> &[Spanned] {
216 &self.forms
217 }
218
219 /// The same forms with their spans projected away, for the stages that do
220 /// not report positions — erasure, evaluation, the test harness.
221 #[must_use]
222 pub fn sexps(&self) -> Vec<Sexp> {
223 self.forms.iter().map(Spanned::to_sexp).collect()
224 }
225
226 /// Every file that contributed, entry first.
227 #[must_use]
228 pub fn files(&self) -> &[SourceFile] {
229 &self.files
230 }
231
232 /// Which file the `top_level`-th form came from.
233 #[must_use]
234 pub fn owner_of(&self, top_level: usize) -> Option<FileId> {
235 self.owner.get(top_level).copied()
236 }
237
238 /// A file by its handle.
239 #[must_use]
240 pub fn file(&self, id: FileId) -> Option<&SourceFile> {
241 self.files.get(id.0)
242 }
243
244 /// Drop the top-level forms `keep` rejects, taking their owners with them.
245 ///
246 /// The lockstep is the whole reason this method exists rather than a public
247 /// `forms` field: `Vec::retain` on one of two parallel vectors is a silent
248 /// mis-attribution, not a compile error.
249 pub fn retain(&mut self, keep: impl Fn(&Spanned) -> bool) {
250 // Zipped rather than two `Vec::retain` calls over the same predicate:
251 // this way the pairing is carried by the iterator instead of by two
252 // traversals agreeing, and there is no order assumption to be wrong
253 // about.
254 let paired = std::mem::take(&mut self.forms)
255 .into_iter()
256 .zip(std::mem::take(&mut self.owner));
257 for (form, owner) in paired {
258 if keep(&form) {
259 self.push(form, owner);
260 }
261 }
262 }
263
264 /// Resolve a diagnostic's position against the file it actually came from.
265 ///
266 /// `top_level` is `blue_lang_check::Diagnostic::top_level` — the join key.
267 /// An index with no owner (a diagnostic that escaped stamping) renders
268 /// without a position rather than borrowing the entry file's: a missing
269 /// position costs the reader a search, a wrong one sends them to innocent
270 /// code and is believed.
271 #[must_use]
272 pub fn locate<'a>(&'a self, top_level: usize, span: Span, message: &'a str) -> Located<'a> {
273 let Some(file) = self.owner_of(top_level).and_then(|id| self.file(id)) else {
274 return Located {
275 origin: Origin::Unresolved,
276 line_col: None,
277 message,
278 };
279 };
280 Located {
281 origin: file.path.as_deref().map_or(Origin::Anonymous, Origin::File),
282 // A synthetic span indexes nothing, so it resolves to no line —
283 // `Span::line_col` would happily answer for `usize::MAX` by walking
284 // off the end and returning the last position in the file.
285 line_col: (!span.is_synthetic()).then(|| Span::line_col(&file.text, span.start)),
286 message,
287 }
288 }
289}
290
291/// What a position is being reported against.
292#[derive(Clone, Copy, Debug)]
293enum Origin<'a> {
294 File(&'a Path),
295 /// Source handed over as text, with no file behind it.
296 Anonymous,
297 /// No owning file could be found. Distinct from [`Origin::Anonymous`] on
298 /// purpose: "you gave me unnamed text" and "I lost track of where this came
299 /// from" are different admissions, and collapsing them would hide the
300 /// second inside the first.
301 Unresolved,
302}
303
304/// A diagnostic rendered against the file it came from: `path:line:col: text`.
305///
306/// A typed `Display` rather than a `format!` at the call site, per ★★ TYPED
307/// EMISSION — the shape every editor and every `cc` already knows how to jump
308/// to, produced by exactly one `write!`.
309#[derive(Clone, Copy, Debug)]
310pub struct Located<'a> {
311 origin: Origin<'a>,
312 line_col: Option<(usize, usize)>,
313 message: &'a str,
314}
315
316impl std::fmt::Display for Located<'_> {
317 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
318 match self.origin {
319 Origin::File(p) => write!(f, "{}", p.display())?,
320 Origin::Anonymous => f.write_str("<anonymous>")?,
321 Origin::Unresolved => f.write_str("<unknown file>")?,
322 }
323 if let Some((line, col)) = self.line_col {
324 write!(f, ":{line}:{col}")?;
325 }
326 write!(f, ": {}", self.message)
327 }
328}
329
330/// Is this form a `use("name")` call? If so, the name.
331///
332/// Matches the *call* form only. `use "kazu"` without parentheses parses as
333/// two unrelated top-level atoms (blue has no paren-less call syntax), which
334/// would silently do nothing — so it is not treated as an import, and the
335/// bare symbol `use` then fails as an unbound name rather than being quietly
336/// ignored.
337fn use_target(form: &Spanned) -> Option<String> {
338 let [head, arg] = form.as_list()? else {
339 return None;
340 };
341 match (&head.form, &arg.form) {
342 (SpannedForm::Atom(Atom::Symbol(s)), SpannedForm::Atom(Atom::Str(name))) if s == "use" => {
343 Some(name.clone())
344 }
345 _ => None,
346 }
347}
348
349/// Is this form a lowered `test` block?
350///
351/// `test "…" … end` lowers to `(deftest …)`, which only the test harness
352/// binds. Public because two callers need it and for opposite reasons: the
353/// resolver drops IMPORTED tests (a dependency's tests are not the importer's
354/// to run), and `pipeline::run` drops ALL of them (a `test` block is a
355/// declaration for the harness, not code to execute — without this,
356/// `blue run` on any file that contains its own tests dies on an unbound
357/// `deftest`, which is every package in this distribution).
358pub fn is_test_form(form: &Spanned) -> bool {
359 let Some(items) = form.as_list() else {
360 return false;
361 };
362 matches!(items.first().and_then(Spanned::as_symbol), Some("deftest"))
363}
364
365/// Replace every `use(...)` with the forms of the package it names.
366///
367/// Transitive by construction: a loaded package's own `use` calls are resolved
368/// the same way, depth-first, so a consumer names its direct dependency and
369/// gets the closure.
370///
371/// **A package is loaded at most once.** Two importers of one package must
372/// share its definitions — loading twice would re-evaluate them, which is at
373/// best wasted work and at worst two distinct copies of anything stateful.
374/// That same visited-set is what makes a dependency CYCLE terminate: the
375/// second visit is a no-op rather than infinite recursion, so a cyclic
376/// distribution loads and runs instead of hanging.
377///
378/// The result carries **which file each top-level form came from** — see
379/// [`ResolvedProgram`] for why that is per top-level form and not per node.
380///
381/// # Errors
382///
383/// Returns the loader's message, prefixed with the import chain that reached
384/// it, when a package cannot be loaded or its source cannot be parsed.
385pub fn resolve_uses(
386 forms: Vec<Spanned>,
387 entry: Entry<'_>,
388 loader: &dyn Loader,
389) -> Result<ResolvedProgram, String> {
390 let mut out = ResolvedProgram::new(entry);
391 let mut seen = BTreeSet::new();
392 expand(
393 forms,
394 ResolvedProgram::ENTRY,
395 loader,
396 &mut out,
397 &mut seen,
398 &[],
399 )?;
400 Ok(out)
401}
402
403fn expand(
404 forms: Vec<Spanned>,
405 owner: FileId,
406 loader: &dyn Loader,
407 out: &mut ResolvedProgram,
408 seen: &mut BTreeSet<String>,
409 chain: &[String],
410) -> Result<(), String> {
411 for form in forms {
412 let Some(name) = use_target(&form) else {
413 // The file boundary is erased HERE — this is the append that used
414 // to make every form indistinguishable from every other. Each one
415 // now carries the file it came from, which is the whole fix.
416 out.push(form, owner);
417 continue;
418 };
419 if !seen.insert(name.clone()) {
420 continue;
421 }
422
423 let sources = loader.load(&name).map_err(|e| describe(chain, &name, &e))?;
424 let mut inner_chain = chain.to_vec();
425 inner_chain.push(name.clone());
426
427 for (label, src) in sources {
428 // `parse_program_tree`, not `parse_program`. The spanless door
429 // discarded every imported position one line after the text
430 // arrived, so an imported type error had nothing to report but a
431 // message — see `ResolvedProgram`.
432 let parsed = blue_lang_syntax::parse_program_tree(&src)
433 .map_err(|e| describe(chain, &name, &format!("{label}: {e}")))?;
434 // An imported package's TEST blocks do not come along.
435 //
436 // A dependency's tests are not the importer's to run, and trying
437 // is not merely untidy — it is a hard failure. `test` lowers to
438 // `deftest`, which only the test harness binds, so the ordinary
439 // evaluator sees an unbound symbol. Measured, the moment kazu
440 // gained test blocks: every program importing it died with
441 // `unbound symbol: deftest`, pointing at a line the importer never
442 // wrote.
443 //
444 // Dropping them here also makes the distribution gate honest for
445 // free: `blue test kikagaku.b` now reports kikagaku's tests
446 // rather than kikagaku's plus everything it transitively imports.
447 let parsed: Vec<Spanned> = parsed.into_iter().filter(|f| !is_test_form(f)).collect();
448 // Interned AFTER parsing, so a package that does not parse never
449 // becomes a file in the table — and BEFORE the recursion, because
450 // every form below belongs to this file, not to the importer's.
451 let id = out.intern(Some(PathBuf::from(label)), src);
452 expand(parsed, id, loader, out, seen, &inner_chain)?;
453 }
454 }
455 Ok(())
456}
457
458/// Prefix a failure with the import chain that reached it.
459///
460/// A transitive failure otherwise names only the leaf, and the reader has no
461/// way to tell which of their own imports pulled it in — the exact question
462/// they need answered to fix it.
463fn describe(chain: &[String], name: &str, reason: &str) -> String {
464 if chain.is_empty() {
465 return reason.to_owned();
466 }
467 format!("while loading {} -> {name}: {reason}", chain.join(" -> "))
468}
469
470#[cfg(test)]
471mod tests {
472 use super::*;
473 use std::collections::BTreeMap;
474
475 /// An in-memory distribution — the whole pass runs with no filesystem.
476 struct MemLoader(BTreeMap<&'static str, &'static str>);
477
478 impl Loader for MemLoader {
479 fn load(&self, name: &str) -> Result<Vec<(String, String)>, String> {
480 self.0
481 .get(name)
482 .map(|s| vec![(format!("{name}.b"), (*s).to_owned())])
483 .ok_or_else(|| format!("no bidama named \"{name}\""))
484 }
485 }
486
487 fn parse(src: &str) -> Vec<Spanned> {
488 blue_lang_syntax::parse_program_tree(src).expect("test source must parse")
489 }
490
491 /// Resolve a program that came from nowhere in particular.
492 fn resolve(src: &str, loader: &dyn Loader) -> Result<ResolvedProgram, String> {
493 resolve_uses(parse(src), Entry::anonymous(src), loader)
494 }
495
496 #[test]
497 fn a_use_is_replaced_by_the_packages_forms() {
498 let loader = MemLoader(BTreeMap::from([("kazu", "def double(n)\n n * 2\nend")]));
499 let out = resolve("use(\"kazu\")\ndouble(21)", &loader).expect("resolves");
500 // The `use` itself is GONE — it is not a call that survives to the
501 // evaluator, where `use` is not a defined function.
502 assert!(
503 out.forms().iter().all(|f| super::use_target(f).is_none()),
504 "a use form survived resolution and would reach the evaluator as \
505 an unbound function: {:?}",
506 out.forms()
507 );
508 assert!(
509 out.forms().len() > 1,
510 "the package's definitions must be spliced in, not dropped: {:?}",
511 out.forms()
512 );
513 }
514
515 #[test]
516 fn imports_are_transitive() {
517 let loader = MemLoader(BTreeMap::from([
518 ("retsu", "use(\"kazu\")\ndef sum2(a, b)\n a + b\nend"),
519 ("kazu", "def double(n)\n n * 2\nend"),
520 ]));
521 let out = resolve("use(\"retsu\")", &loader).expect("resolves");
522 // A consumer names retsu only; kazu arrives because retsu needs it.
523 assert!(
524 out.forms().len() >= 2,
525 "the transitive dependency did not arrive: {:?}",
526 out.forms()
527 );
528 }
529
530 #[test]
531 fn a_package_is_loaded_at_most_once() {
532 let loader = MemLoader(BTreeMap::from([("kazu", "def double(n)\n n * 2\nend")]));
533 let once = resolve("use(\"kazu\")", &loader).expect("resolves");
534 let twice = resolve("use(\"kazu\")\nuse(\"kazu\")", &loader).expect("resolves");
535 assert_eq!(
536 once.forms().len(),
537 twice.forms().len(),
538 "importing a package twice duplicated its definitions; two \
539 importers of one package must share it"
540 );
541 assert_eq!(
542 once.files().len(),
543 twice.files().len(),
544 "importing a package twice interned its source twice; the file \
545 table must have one entry per file, not one per import"
546 );
547 }
548
549 /// The property that keeps a cyclic distribution from hanging.
550 #[test]
551 fn a_dependency_cycle_terminates() {
552 let loader = MemLoader(BTreeMap::from([
553 ("a", "use(\"b\")\ndef fa()\n 1\nend"),
554 ("b", "use(\"a\")\ndef fb()\n 2\nend"),
555 ]));
556 let out = resolve("use(\"a\")", &loader).expect("a cycle must resolve");
557 assert!(
558 !out.forms().is_empty(),
559 "a cycle resolved to nothing: {:?}",
560 out.forms()
561 );
562 }
563
564 #[test]
565 fn a_missing_package_names_itself_and_the_chain() {
566 let loader = MemLoader(BTreeMap::from([("retsu", "use(\"nowhere\")")]));
567 let err = resolve("use(\"retsu\")", &loader).expect_err("must fail");
568 assert!(
569 err.contains("nowhere"),
570 "the error must name the missing package: {err}"
571 );
572 assert!(
573 err.contains("retsu"),
574 "the error must name the import that pulled it in, or the reader \
575 cannot tell which of their own imports is at fault: {err}"
576 );
577 }
578
579 #[test]
580 fn the_default_loader_refuses_by_name() {
581 let err = resolve("use(\"kazu\")", &NoLoader).expect_err("must fail");
582 assert!(
583 err.contains("kazu"),
584 "NoLoader must name what was asked for: {err}"
585 );
586 }
587
588 /// A non-`use` program must come out byte-identical.
589 ///
590 /// This pass runs on EVERY program, so a bug here would corrupt source
591 /// that never mentions a package.
592 /// An imported package's tests must NOT come along.
593 ///
594 /// Not a tidiness point: `deftest` is unbound outside the test harness, so
595 /// an inherited test block kills any program that imports a tested
596 /// package — which is every package in a distribution worth having.
597 #[test]
598 fn an_imported_packages_tests_are_not_inherited() {
599 let loader = MemLoader(BTreeMap::from([(
600 "kazu",
601 "def double(n)\n n * 2\nend\n\ntest \"doubles\"\n assert double(2) == 4\nend",
602 )]));
603 let out = resolve("use(\"kazu\")\ndouble(21)", &loader).expect("resolves");
604 assert!(
605 out.forms().iter().all(|f| !super::is_test_form(f)),
606 "an imported test block survived and would reach the evaluator as \
607 an unbound `deftest`: {:?}",
608 out.forms()
609 );
610 // The DEFINITIONS still arrive — dropping tests must not drop code.
611 assert!(
612 out.forms().len() >= 2,
613 "filtering tests also removed the package's definitions: {:?}",
614 out.forms()
615 );
616 }
617
618 /// But the ENTRY program keeps its own tests.
619 #[test]
620 fn the_entry_programs_own_tests_survive() {
621 let loader = MemLoader(BTreeMap::new());
622 let src = "def f(n)\n n\nend\n\ntest \"t\"\n assert f(1) == 1\nend";
623 let out = resolve(src, &loader).expect("resolves");
624 assert!(
625 out.forms().iter().any(super::is_test_form),
626 "the file's OWN tests were dropped; only imported ones should be: {:?}",
627 out.forms()
628 );
629 }
630
631 #[test]
632 fn a_program_without_imports_is_unchanged() {
633 let loader = MemLoader(BTreeMap::new());
634 let src = "def f(n)\n n + 1\nend\nf(1)";
635 let before: Vec<Sexp> = parse(src).iter().map(Spanned::to_sexp).collect();
636 let out = resolve(src, &loader).expect("resolves");
637 assert_eq!(format!("{before:?}"), format!("{:?}", out.sexps()));
638 }
639
640 // ---- file identity ---------------------------------------------------
641
642 /// **Every form's span indexes ITS OWN file, and the text proves it.**
643 ///
644 /// The independent evidence is the source itself: slice each top-level
645 /// form's span out of the file its owner names and re-parse the slice. If
646 /// the owner is wrong the bytes are some other file's, and the re-parsed
647 /// tree does not match — checked against the tree, which the file table had
648 /// no hand in producing.
649 ///
650 /// This is the contract `Span`'s own docs put on the caller — spans "are
651 /// meaningful only relative to the string that produced them" — asserted
652 /// rather than assumed.
653 ///
654 /// **Red run** (2026-08-12), `expand` pushing `ResolvedProgram::ENTRY` as
655 /// every form's owner instead of `owner`:
656 /// ```text
657 /// form 0: its own source does not re-parse: expected an expression, found
658 /// Eof at 25..25
659 /// ```
660 /// It trips at the re-parse rather than the comparison, because kazu's
661 /// byte range cut against the entry file's text lands mid-token — which is
662 /// the mis-attribution stated in bytes.
663 ///
664 /// **Second red run**, the ORIGINAL spanless `parse_program` restored in
665 /// `expand` (the bug this change fixes):
666 /// ```text
667 /// form 0 has no position at all — its file was parsed through the
668 /// spanless door
669 /// ```
670 #[test]
671 fn every_forms_span_indexes_the_file_it_came_from() {
672 let loader = MemLoader(BTreeMap::from([
673 ("retsu", "use(\"kazu\")\ndef sum2(a, b)\n a + b\nend"),
674 ("kazu", "def double(n)\n n * 2\nend"),
675 ]));
676 let out = resolve("use(\"retsu\")\nsum2(double(1), 2)", &loader).expect("resolves");
677 assert_eq!(
678 out.files().len(),
679 3,
680 "expected the entry plus retsu plus kazu: {:?}",
681 out.files()
682 );
683 for (i, form) in out.forms().iter().enumerate() {
684 let file = out
685 .owner_of(i)
686 .and_then(|id| out.file(id))
687 .unwrap_or_else(|| panic!("form {i} has no owning file"));
688 assert!(
689 !form.span.is_synthetic(),
690 "form {i} has no position at all — its file was parsed through \
691 the spanless door"
692 );
693 let slice = file
694 .text
695 .get(form.span.start..form.span.end)
696 .unwrap_or_else(|| {
697 panic!(
698 "form {i}'s span {:?} is not a range in its owner ({} bytes)",
699 form.span,
700 file.text.len()
701 )
702 });
703 let reparsed = blue_lang_syntax::parse_program_tree(slice)
704 .unwrap_or_else(|e| panic!("form {i}: its own source does not re-parse: {e}"));
705 assert_eq!(
706 reparsed.iter().map(Spanned::to_sexp).collect::<Vec<_>>(),
707 vec![form.to_sexp()],
708 "form {i} sliced out of its owner ({}) re-parses to a different \
709 tree; slice was {slice:?}",
710 file.path
711 .as_deref()
712 .map_or_else(|| "<anonymous>".to_string(), |p| p.display().to_string())
713 );
714 }
715 // Anti-vacuity: an empty program passes the loop above.
716 assert!(out.forms().len() >= 3, "{:?}", out.forms());
717 }
718
719 /// Dropping a form takes its owner with it.
720 ///
721 /// The parallel-vector failure, asserted directly: after `retain` removes
722 /// the entry file's `test` block, every surviving form must still resolve
723 /// to the file it came from. Checked through the same slice-and-re-parse
724 /// evidence, because "the lengths still match" would pass on a program
725 /// where every owner shifted by one.
726 ///
727 /// **Red run** (2026-08-12), `retain` filtering `self.forms` only and
728 /// leaving `self.owner` whole:
729 /// ```text
730 /// after retain, form 0 does not belong to the file it is attributed to:
731 /// slice "test \"t\"\n assert 1 == 1\n" of <anonymous>
732 /// left: []
733 /// right: [List([Atom(Symbol("define")), …double…])]
734 /// ```
735 /// The surviving forms kept the DROPPED form's owner, so kazu's `double`
736 /// was attributed to the entry file and sliced out of it.
737 #[test]
738 fn retain_drops_a_forms_owner_with_it() {
739 let loader = MemLoader(BTreeMap::from([("kazu", "def double(n)\n n * 2\nend")]));
740 let src = "test \"t\"\n assert 1 == 1\nend\nuse(\"kazu\")\ndouble(21)";
741 let mut out = resolve(src, &loader).expect("resolves");
742 assert!(
743 out.forms().iter().any(super::is_test_form),
744 "the fixture must contain the test block this drops"
745 );
746 out.retain(|f| !super::is_test_form(f));
747 assert!(!out.forms().iter().any(super::is_test_form));
748 for (i, form) in out.forms().iter().enumerate() {
749 let file = out
750 .owner_of(i)
751 .and_then(|id| out.file(id))
752 .unwrap_or_else(|| panic!("form {i} lost its owner"));
753 let slice = &file.text[form.span.start..form.span.end];
754 let reparsed = blue_lang_syntax::parse_program_tree(slice)
755 .map(|f| f.iter().map(Spanned::to_sexp).collect::<Vec<_>>())
756 .unwrap_or_default();
757 assert_eq!(
758 reparsed,
759 vec![form.to_sexp()],
760 "after retain, form {i} does not belong to the file it is \
761 attributed to: slice {slice:?} of {}",
762 file.path
763 .as_deref()
764 .map_or_else(|| "<anonymous>".to_string(), |p| p.display().to_string())
765 );
766 }
767 assert_eq!(out.forms().len(), 2, "{:?}", out.forms());
768 }
769
770 /// An unresolvable index reports no position rather than the entry file's.
771 #[test]
772 fn an_unstamped_diagnostic_gets_no_position_rather_than_a_wrong_one() {
773 let loader = MemLoader(BTreeMap::new());
774 let out = resolve("def f(n)\n n\nend", &loader).expect("resolves");
775 let rendered = out
776 .locate(
777 blue_lang_check::Diagnostic::UNSTAMPED,
778 tatara_lisp::Span::new(0, 1),
779 "something went wrong",
780 )
781 .to_string();
782 assert_eq!(rendered, "<unknown file>: something went wrong");
783 assert!(
784 !rendered.contains(":1:1"),
785 "an unowned diagnostic borrowed a position from somewhere: {rendered}"
786 );
787 }
788}