Accept next inside user-defined functions, unwinding calls at runtime - #582
Merged
bertysentry merged 1 commit intoAug 18, 2026
Conversation
`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>
Contributor
Author
|
@codex please review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
deleted the
580-next-inside-a-user-defined-function-is-rejected-at-compile-time
branch
August 18, 2026 22:03
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #580.
Problem
Jawk rejected a
nextstatement 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
nextfiledesign:AwkParser:NextStatementAstemits a dedicatedEXEC_NEXTopcode when noNEXTABLEancestor 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: newEXEC_NEXTopcode, appended at the end of the enum so existing ordinals stay stable for precompiled tuple files.AwkTuples:nextAddressproperty (setter/getter, optimizer remap, dead-code reachability seed) andexecNext();serialVersionUIDbumped to 7.AVM:EXEC_NEXTperforms the gawk runtime rule checks — fatal error when called from aBEGIN,END,BEGINFILE, orENDFILErule, with gawk's exact messages (verified against gawk 5.0) — then unwinds the function calls viaresetCallState()(which also settles the--profiletiming 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 newmainInputLoopStartedflag (set byCONSUME_INPUT/CONSUME_FILE_INPUT) distinguishes BEGIN-time calls from input-rule calls.Tests
New
NextStatementTestcovers: record skipping, nested call unwinding, later-rule bypass, per-file loop interplay withBEGINFILE/ENDFILE, runtime fatals fromBEGIN/END/BEGINFILE/ENDFILEcallers (including a program with no main input loop), and the unchanged compile-time rejection of direct uses in special rules.CliOptionTestgains a--profiletest asserting functions unwound bynextare recorded correctly (companion to the #557nextfiletest).Documentation
behavior-changes.md: bullet underUnreleased.cli.md: updated thenext/nextfile/getlinerestrictions paragraph.🤖 Generated with Claude Code