|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | +// |
| 4 | +// Level 2 — Region-binding (schema lookup) |
| 5 | +// |
| 6 | +// L2 attests that every declared region has a resolvable schema and |
| 7 | +// that the checker surfaces structural lookups before type reasoning |
| 8 | +// runs. The positive attestation is "parse + check clean with the |
| 9 | +// region present"; the negative attestation is "checker complains |
| 10 | +// about a region-targeting construct whose name is undeclared". |
| 11 | +// |
| 12 | +// Surface exposure: the most direct L2 trigger in the ReScript checker |
| 13 | +// is a `match` statement whose target references a field of an |
| 14 | +// undeclared region (walkExpr threads the region environment; the |
| 15 | +// match arm resolver bottoms out in a "unknown region"-shaped |
| 16 | +// diagnostic). A second trigger is a session `transition consume X` |
| 17 | +// against a region name not declared anywhere in the module. |
| 18 | +// |
| 19 | +// Run: node tests/levels/L2.mjs |
| 20 | + |
| 21 | +import { parseModule } from "../../src/parser/Parser.mjs"; |
| 22 | +import { checkModule } from "../../src/parser/Checker.mjs"; |
| 23 | + |
| 24 | +let passed = 0; |
| 25 | +let failed = 0; |
| 26 | + |
| 27 | +function run(source, expect, label) { |
| 28 | + const pr = parseModule(source); |
| 29 | + if (pr.TAG !== "Ok") { |
| 30 | + failed++; |
| 31 | + console.log(` FAIL ${label} — parse error: ${pr._0.message}`); |
| 32 | + return; |
| 33 | + } |
| 34 | + const diags = checkModule(pr._0); |
| 35 | + if (expect.kind === "clean") { |
| 36 | + if (diags.length === 0) { |
| 37 | + passed++; |
| 38 | + console.log(` OK ${label} — clean check`); |
| 39 | + } else { |
| 40 | + failed++; |
| 41 | + const msgs = diags.map(d => d.message).join("; "); |
| 42 | + console.log(` FAIL ${label} — expected clean, got ${diags.length}: ${msgs}`); |
| 43 | + } |
| 44 | + } else { |
| 45 | + const allMsgs = diags.map(d => d.message).join(" | "); |
| 46 | + const missing = expect.needles.filter(n => !allMsgs.includes(n)); |
| 47 | + if (missing.length === 0 && diags.length > 0) { |
| 48 | + passed++; |
| 49 | + console.log(` OK ${label} — diagnostic fired as expected`); |
| 50 | + } else { |
| 51 | + failed++; |
| 52 | + console.log(` FAIL ${label} — expected needles ${expect.needles.join(", ")}, got "${allMsgs}"`); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +console.log("=== Level 2: Region-binding (schema lookup) ===\n"); |
| 58 | + |
| 59 | +// ── Positive: region present, match resolves cleanly ───────────────── |
| 60 | + |
| 61 | +run( |
| 62 | + `region Enemies[10] { |
| 63 | + kind: union { |
| 64 | + Minion: i32; |
| 65 | + Boss: i32; |
| 66 | + }; |
| 67 | + } |
| 68 | + fn dispatch(e: ®ion<Enemies>, i: i32) { |
| 69 | + match $e[i] .kind { |
| 70 | + | Minion => { return; } |
| 71 | + | Boss => { return; } |
| 72 | + } |
| 73 | + }`, |
| 74 | + { kind: "clean" }, |
| 75 | + "match against declared region — clean", |
| 76 | +); |
| 77 | + |
| 78 | +// ── Negative: match targets a region that is not declared ──────────── |
| 79 | +// |
| 80 | +// Observation: Checker's match resolution walks the function parameter |
| 81 | +// list to find the region binding. Passing a parameter whose declared |
| 82 | +// region name is absent from the module is the structural L2 failure. |
| 83 | + |
| 84 | +run( |
| 85 | + `region Enemies[10] { |
| 86 | + kind: union { |
| 87 | + Minion: i32; |
| 88 | + Boss: i32; |
| 89 | + }; |
| 90 | + } |
| 91 | + fn dispatch(e: ®ion<NotDeclared>, i: i32) { |
| 92 | + match $e[i] .kind { |
| 93 | + | Minion => { return; } |
| 94 | + | Boss => { return; } |
| 95 | + } |
| 96 | + }`, |
| 97 | + { kind: "diagnostic", needles: ["unknown region"] }, |
| 98 | + "match target names undeclared region", |
| 99 | +); |
| 100 | + |
| 101 | +// ── Positive: session referencing declared region-typed state ──────── |
| 102 | + |
| 103 | +run( |
| 104 | + `region Players[8] { hp: u32; } |
| 105 | + session OrderFlow { |
| 106 | + state Idle : i32; |
| 107 | + state Pending : i64; |
| 108 | + transition submit : consume Idle -> yield Pending; |
| 109 | + }`, |
| 110 | + { kind: "clean" }, |
| 111 | + "session with primitive-typed states alongside a region", |
| 112 | +); |
| 113 | + |
| 114 | +// ── Summary ────────────────────────────────────────────────────────── |
| 115 | + |
| 116 | +console.log(`\n--- L2: ${passed} passed, ${failed} failed ---`); |
| 117 | +process.exit(failed > 0 ? 1 : 0); |
0 commit comments