Repository navigation
awk: make next and nextfile in a function called from a special rule fatal - #199
Conversation
…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
left a comment
There was a problem hiding this comment.
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?
| } else { | ||
| "nextfile" | ||
| }; | ||
| io::Error::other(format!( |
There was a problem hiding this comment.
could this be a thiserror enum instead of io::Error::other(format!(...))?
|
Thank you. The parser already rejects |
|
Thanks for your PR |
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 aBEGIN' 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.