fix(given/when): implicit break and skip smartmatch on boolean exprs#589
Merged
fix(given/when): implicit break and skip smartmatch on boolean exprs#589
Conversation
The given/when transformation in StatementParser had two bugs that caused t/cardinals.t in DateTime::Calendar::Discordian to fail 21/73 subtests (e.g. "1st" coming out as "1stth"): 1. After a successful `when` match, Perl implicitly breaks out of the enclosing `given`. We now append `last;` to every parsed when-block and mark the synthesized given BlockNode as isLoop=true so that `last` exits the given instead of escaping to an outer loop. 2. Per perlsyn, when(EXPR) does NOT smart-match against $_ when EXPR is a comparison (==, !=, <, >, <=, >=, <=>, eq, ne, lt, gt, le, ge, cmp), boolean combinator (&&, ||, //, and, or, xor), regex bind (=~, !~), negation (!, not), or defined/exists. We now detect these forms and use the expression's value directly as a boolean instead of wrapping it in `$_ ~~ EXPR`. After this fix, DateTime::Calendar::Discordian passes 271/271 tests under `jcpan -t`. Generated with [Devin](https://cli.devin.ai/docs) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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.
Summary
Fix two bugs in PerlOnJava's
given/whentransformation that causedDateTime::Calendar::Discordianto fail 21/73 subtests int/cardinals.tunderjcpan -t(e.g.1stcame out as1stth).The failing test pattern:
Bug 1 — missing implicit
lastPer
perlsyn, after awhenblock runs, control implicitly breaks out of the enclosinggiven. PerlOnJava just expandedwhen (COND) { BLOCK }intoif ($_ ~~ COND) { BLOCK }, so everywhen(and thedefault) ran in turn, producing things like1stth.Fix: append a synthetic
last;to every parsed when-block and mark the synthesizedgivenBlockNodeasisLoop = trueso thatlastexits thegiveninstead of escaping to an outer loop / the program.Bug 2 —
whenalways smart-matched against$_Per
perlsyn,when(EXPR)does not smart-match against$_whenEXPRis one of:==,!=,<,>,<=,>=,<=>,eq,ne,lt,gt,le,ge,cmp&&,||,//,and,or,xor=~,!~!,notdefined/existsPerlOnJava unconditionally wrapped, so e.g.
when ($i % 10 == 2 && $i != 12)for$i = 2became2 ~~ 1(false) instead of using the boolean1directly.Fix: add
whenIsBoolean(...)to detect those exceptional forms and use the expression's value directly as a boolean instead of wrapping with$_ ~~.Test plan
jcpan -t DateTime::Calendar::Discordian—Files=16, Tests=271 ... Result: PASS(was 21/73 failures int/cardinals.t)make— full unit-test suite green, no regressionswhensemantics matchperlfor both smart-match and boolean casesGenerated with Devin