Skip to content

awk: make next and nextfile in a function called from a special rule fatal - #199

Merged
sylvestre merged 3 commits into
uutils:mainfrom
VanshajPoonia:awk-next-in-begin-178
Nov 25, 2026
Merged

sylvestre merged 3 commits into
uutils:mainfrom
VanshajPoonia:awk-next-in-begin-178

Conversation

@VanshajPoonia

Copy link
Copy Markdown
Contributor

Fixes #178.

next or nextfile directly in BEGIN, END, BEGINFILE or ENDFILE is rejected by the parser. Inside a function called from one of them it reached an unreachable!() in src/event.rs and panicked. The four event loops now return an error that names the rule. awk prints fatal: next' cannot be called from a BEGIN' rule and exits 2 like gawk.

nextfile in BEGINFILE is still allowed since it skips the file. That is what gawk does.

Tests: next_and_nextfile_from_a_function_in_a_special_rule_are_fatal fails on main. Two guards pass on both: nextfile in BEGINFILE skips the file and next in a normal rule skips the record.

gawk also prints the file and line before fatal. We print only fatal: because the signal carries no span. Adding one would mean changing Signal::Terminal so I left it out.

…fatal

A function called from BEGIN, END, BEGINFILE or ENDFILE could run next or nextfile and reach an unreachable!(). It now exits with 2 and a gawk-style message naming the rule.

@Alonely0 Alonely0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice this is not a runtime issue, but a parser one. gawk already fails for gawk 'BEGIN { if (0) next; }'. We already do this in the parser for some contextual keywords like continue, so perhaps you could replicate it for next and nextfile?

Comment thread src/event.rs Outdated
} else {
"nextfile"
};
io::Error::other(format!(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be a thiserror enum instead of io::Error::other(format!(...))?

@VanshajPoonia

Copy link
Copy Markdown
Contributor Author

Thank you. The parser already rejects next in BEGIN and inside an if, so BEGIN { if (0) next; } fails before it runs, which is the same as gawk. This PR is for a next inside a function that BEGIN calls, like function g() { next } BEGIN { g() }. gawk only catches that at run time (fatal, exit 2), and before this change we panicked. I also changed the error to a thiserror enum as sylvestre asked.

@sylvestre
sylvestre merged commit b47c9e6 into uutils:main Nov 25, 2026
15 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

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.

awk: next in a function called from BEGIN panics

3 participants