Skip to content

Accept next inside user-defined functions, unwinding calls at runtime - #582

Merged
bertysentry merged 1 commit into
mainfrom
580-next-inside-a-user-defined-function-is-rejected-at-compile-time
Aug 18, 2026
Merged

Accept next inside user-defined functions, unwinding calls at runtime#582
bertysentry merged 1 commit into
mainfrom
580-next-inside-a-user-defined-function-is-rejected-at-compile-time

Conversation

@bertysentry

Copy link
Copy Markdown
Contributor

Fixes #580.

Problem

Jawk rejected a next statement inside a user-defined function at compile time (SemanticException: cannot next; not within any input rules), while gawk (including --posix), mawk, and BWK awk accept it and skip to the next input record when the function is called from an input rule.

Change

Mirrors the existing nextfile design:

  • AwkParser: NextStatementAst emits a dedicated EXEC_NEXT opcode when no NEXTABLE ancestor exists, instead of throwing. The main input loop's next-record entry point (nextAddress) is registered as a property of the tuple stream, like the per-file addresses. Direct uses in rules keep today's behavior: a plain jump in input rules, compile-time rejection in special rules.
  • Opcode: new EXEC_NEXT opcode, appended at the end of the enum so existing ordinals stay stable for precompiled tuple files.
  • AwkTuples: nextAddress property (setter/getter, optimizer remap, dead-code reachability seed) and execNext(); serialVersionUID bumped to 7.
  • AVM: EXEC_NEXT performs the gawk runtime rule checks — fatal error when called from a BEGIN, END, BEGINFILE, or ENDFILE rule, with gawk's exact messages (verified against gawk 5.0) — then unwinds the function calls via resetCallState() (which also settles the --profile timing stack, see --profile: function timing stack leaks on next/nextfile inside a function #557) and jumps to the main input loop's next-record address. A new mainInputLoopStarted flag (set by CONSUME_INPUT/CONSUME_FILE_INPUT) distinguishes BEGIN-time calls from input-rule calls.
$ printf 'a\nb\n' | jawk 'function f() { next } { f(); print "unreached" } END { print "end" }'
end
$ printf 'x\n' | jawk 'function f() { next } { } END { f() }'
AwkRuntimeException (line 1): `next' cannot be called from a `END' rule

Tests

New NextStatementTest covers: record skipping, nested call unwinding, later-rule bypass, per-file loop interplay with BEGINFILE/ENDFILE, runtime fatals from BEGIN/END/BEGINFILE/ENDFILE callers (including a program with no main input loop), and the unchanged compile-time rejection of direct uses in special rules. CliOptionTest gains a --profile test asserting functions unwound by next are recorded correctly (companion to the #557 nextfile test).

Documentation

  • behavior-changes.md: bullet under Unreleased.
  • cli.md: updated the next/nextfile/getline restrictions paragraph.

🤖 Generated with Claude Code

`next` inside a user-defined function was rejected at compile time with
"SemanticException: cannot next; not within any input rules", while gawk
(including --posix), mawk, and BWK awk accept it and skip to the next
input record when the function is called from an input rule.

Mirror the existing nextfile design: NextStatementAst emits a dedicated
EXEC_NEXT opcode when no NEXTABLE ancestor exists (instead of throwing),
and the interpreter performs the gawk runtime rule checks (fatal error
from BEGIN, END, BEGINFILE, and ENDFILE callers), unwinds the call state
via resetCallState(), and jumps to the main input loop's next-record
address, which the tuple stream carries as a property like the per-file
addresses. Direct uses in rules keep today's behavior: a plain jump in
input rules, compile-time rejection in special rules.

Fixes #580

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@bertysentry bertysentry linked an issue Aug 18, 2026 that may be closed by this pull request
@bertysentry

Copy link
Copy Markdown
Contributor Author

@codex please review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: c81bf8208b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@bertysentry
bertysentry merged commit 4628dbd into main Aug 18, 2026
5 checks passed
@bertysentry
bertysentry deleted the 580-next-inside-a-user-defined-function-is-rejected-at-compile-time branch August 18, 2026 22:03
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.

next inside a user-defined function is rejected at compile time

1 participant