Skip to content

A function that reaches program() or trigger() is refused where a program calls it - #90

Merged
jeany55 merged 1 commit into
mainfrom
forbidden-through-functions
Nov 11, 2026
Merged

jeany55 merged 1 commit into
mainfrom
forbidden-through-functions

Conversation

@jeany55

@jeany55 jeany55 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #89.

What was wrong

program() and trigger() written in a program's body were refused. The same call in one of the script's own functions, called from a program, built with no diagnostics. Nothing in such a call is the game's, so the planner hoisted it: it ran when the script was built, and the trigger existed from the start of the game whether or not the program ever reached the call.

function setup() {
  trigger(P1, [always()], [victory()]);
}
program(async () => {
  await sleep(frames(240));
  if (deaths(P1, units.TerranMarine, ">=", 5)) setup();
});

On main this builds clean and the map has an Always → Victory trigger that fires on the first frame.

What changes

The plan follows the script's functions. A call that reaches trigger(), program(), hyperTriggers() or game(), directly or through other functions of the script, is never hoisted, and is one error at the call:

main.ts:6 setup() calls trigger(), which defines triggers of its own and cannot be used inside program(); call setup() at the top of the file instead.
main.ts:8 setup() calls build(), which calls trigger(), which defines triggers of its own and …

What I checked on main before the change, and what each does now:

Written inside a program main Now
setup(), a function or an arrow in a const built silently error at the call
setup() through a second function built silently error at the call, chain named
setup() imported, or more.setup() through a namespace import built silently error at the call
setup() inside a listener, at the top of a file or in a program built silently error at the call
setup() inside a game() function built silently error at the call
[1, 2].forEach(setup), const n = setup() built silently error where it is named
hyperTriggers() in a function built silently error at the call
a function that also keeps state, a method, a call with a value of the game errors inside the function, plus "A call's arguments must be known…" one error, at the call
game() in a function "f is not a function" error at the call
the same function called at the top of a file builds builds, unchanged

The prototype in the issue followed callees written as plain names. This one also resolves the call's signature, so methods and namespace imports are covered, and a standard-library function is treated as the script's own are.

A forbidden call anywhere in a function's body counts, including in a function written inside it, as it does in a program's own body.

For #88

With this on top of #88's head, buildAutoMiningProgram() inside program() or a listener gives one error at the caller's line in main.ts instead of errors located in workers.ts, and #88's standard-library tests pass unchanged.

Testing

  • npm run typecheck clean; npx vitest run: 83 files pass.
  • Two new tests in tests/script-structured.test.ts: every row of the table above, and that a top-level call and two functions that call each other (reaching neither) still build.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VEdp4VusnFfHbBoyhRPUTt

…gram calls it

program() and trigger() written in a program's body were refused, but the same
call in one of the script's own functions, called from a program, built with no
error. Nothing in such a call is the game's, so it was worked out when the
script was built: the trigger existed from the start of the game, whether or
not the program ever reached the call. A victory trigger behind a wait and a
condition fired on the first frame.

The plan now follows the script's functions: a call that reaches trigger(),
program(), hyperTriggers() or game(), directly or through other functions of
the script, is never hoisted and is an error at the call, with the way there
named. It covers a function called by name, an arrow kept in a const, one
handed to forEach, a method, a function from another file and one through a
namespace import, inside a program, a listener or a game() function. The same
function called at the top of a file is the script's, as before.

Fixes #89

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VEdp4VusnFfHbBoyhRPUTt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

program() or trigger() inside a function called from a program builds silently and runs from game start

1 participant