Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/runtime/compile-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import type { Item } from "../types.js"
import type { ExecutionContext } from "./context.js"
import { MapLogicError } from "../errors.js"
import { regexpEscape } from "../stdlib.js"

/** Compiled form of a pattern Item. */
Expand Down Expand Up @@ -62,7 +63,11 @@ export function compileItem(item: Item, ctx: ExecutionContext, classCtx = false)
const stdlib = STDLIB_ALIASES[item.name]
if (stdlib) return stdlib
const resolved = ctx.resolveAlias(item.name)
if (!resolved) return { re: "", literal: "" }
// WO18 G3 (parity audit): an unresolved alias is map-logic damage,
// not an empty matcher. Compiling `re: ""` matched at every string
// boundary and silently spliced the replacement text ("a" ->
// "XaX"); ruby raises SystemConversionError and rs fails at load.
if (!resolved) throw new MapLogicError(`Alias ${item.name} not found`)
if (classCtx && !ctx.map.aliases.has(item.name) && resolved.kind === "string") {
// Imported charset alias in class position: per-character set.
return stringCharset(resolved.value)
Expand Down
17 changes: 10 additions & 7 deletions src/runtime/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import type { CompiledMap, Rule, Stage } from "../types.js"
import type { MapLoader } from "../loader.js"
import { MapLogicError } from "../errors.js"
import { ExecutionContext } from "./context.js"
import { executeRule } from "./executor.js"

Expand All @@ -27,6 +28,11 @@ export function registerAsyncFunction(name: string): void {
ASYNC_FUNCTIONS.add(name)
}

/** WO18 G4: shared missing-stage error (rs message shape). */
function missingStage(map: CompiledMap, stageName: string): MapLogicError {
return new MapLogicError(`stage "${stageName}" not found in ${map.systemCode}`)
}

/**
* Check if a rule list contains any async funcalls.
* Shallow scan — doesn't recurse into parallel/sequential groups
Expand All @@ -52,7 +58,8 @@ function containsAsyncFuncalls(rules: readonly Rule[]): boolean {
* guarded rules are inert.
*
* Throws if the stage contains ML funcalls — use `executeStageAsync`
* for those.
* for those. A missing stage raises MapLogicError (WO18 G4 parity
* audit: the old silent no-op hid map typos; mirrors rs/ruby).
*/
export function executeStage(
map: CompiledMap,
Expand All @@ -62,9 +69,7 @@ export function executeStage(
language?: string,
): string {
const stage: Stage | undefined = map.stages.find((s) => s.name === stageName)
if (!stage) {
return input
}
if (!stage) throw missingStage(map, stageName)

if (containsAsyncFuncalls(stage.rules)) {
throw new Error(
Expand Down Expand Up @@ -93,9 +98,7 @@ export async function executeStageAsync(
language?: string,
): Promise<string> {
const stage: Stage | undefined = map.stages.find((s) => s.name === stageName)
if (!stage) {
return input
}
if (!stage) throw missingStage(map, stageName)

// Fast path: no async functions → use the sync executor directly.
if (!containsAsyncFuncalls(stage.rules)) {
Expand Down
75 changes: 75 additions & 0 deletions test/isc/fail-loud.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* WO18 G3/G4 — fail-loud fixes from the merged four-runtime parity
* audit (maps docs/parity/README.md).
*
* G3: an alias that resolves to nothing compiled to the EMPTY matcher
* (`{ re: "", literal: "" }`), which matches everywhere — the `to`
* text was silently inserted at every string boundary
* ("a" -> "XaX", "zzz" -> "XzXzXzX"). Canon (ruby/rs): a named
* error; an empty matcher is never legitimate alias semantics.
*
* G4: requesting a stage the map doesn't have was a silent no-op
* returning the input, hiding map typos ("--stage mahn" silently
* "works"). Canon (rs, ruby post-fix): named MapLogicError for both
* the requested main stage and a run's target stage.
*
* API-visible change (G4): callers who relied on the missing-stage
* no-op now get MapLogicError.
*/

import { describe, it, expect, beforeEach } from "vitest"
import { parseIsc } from "../../src/isc/parser.ts"
import { iscBundledStrategy } from "../../src/isc/loader.ts"
import { configure, reset, transliterate, transliterateAsync } from "../../src/index.js"
import { MapLogicError } from "../../src/errors.js"

const UNKNOWN_ALIAS_MAP = `# G3: alias declared nowhere (nor in any dependency).
system "T:tst-unknown-alias:1" {
stage main {
parallel { sub unknown_alias "X" }
}
}`

const STAGE_MAP = `# G4: only stage "main" exists.
system "T:tst-stage-missing:1" {
stage main {
sub "a" "b"
}
}`

function loadMap(src: string): string {
const code = parseIsc(src, "probe").systemCode
configure({ strategies: [iscBundledStrategy({ [code]: src })] })
return code
}

describe("WO18 G3 — unresolved alias fails loud", () => {
beforeEach(() => reset())

it("sub unknown_alias raises MapLogicError (not silent boundary insertion)", () => {
const code = loadMap(UNKNOWN_ALIAS_MAP)
expect(() => transliterate(code, "a")).toThrow(MapLogicError)
expect(() => transliterate(code, "a")).toThrow(/Alias unknown_alias not found/)
})
})

describe("WO18 G4 — missing stage fails loud", () => {
beforeEach(() => reset())

it("requesting an absent stage raises MapLogicError (was: silent no-op)", () => {
const code = loadMap(STAGE_MAP)
expect(() => transliterate(code, "a", "nope")).toThrow(MapLogicError)
expect(() => transliterate(code, "a", "nope")).toThrow(/stage "nope" not found/)
})

it("the async path raises the same error", async () => {
const code = loadMap(STAGE_MAP)
await expect(transliterateAsync(code, "a", "nope")).rejects.toThrow(MapLogicError)
await expect(transliterateAsync(code, "a", "nope")).rejects.toThrow(/stage "nope" not found/)
})

it("the present stage still executes", () => {
const code = loadMap(STAGE_MAP)
expect(transliterate(code, "a")).toBe("b")
})
})
Loading