parser: reject reserved stmt keywords as fn decl starts#235
Merged
Conversation
is_fn_decl_start treated any Ident followed by > as a zero-param fn
header, which mis-fires on wh >cond{...} mid-body: parse_body_with's
top-level boundary heuristic terminates the outer body at wh and
parses wh >v 0{v=- v 1} as a fresh fn decl named wh returning v.
Symptom is ILO-T008 "return type mismatch: expected n, got _" plus a
cascade of undefined-variable errors in function 'wh'. Hit by both
gis-analyst and routing-tsp persona reruns against v0.11.1.
wh, ret, brk, and cnt are intercepted by parse_stmt as control-flow
forms and can never start a fn decl. Short-circuit is_fn_decl_start
to false for these four names. Mirrors the existing ILO-P011
reserved-keyword precedent in parse_decl.
Regression test covering the gis-analyst repro (wh >v 0 mid-body), wh >cond followed by a sibling fn decl, the GreaterEq variant, and a zero-param fn sanity check. All four shapes exercised across tree, vm, and cranelift. examples/wh-gt-condition.ilo demonstrates the now-correct shape and flows through tests/examples_engines.rs as an extra cross-engine regression layer.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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
Both gis-analyst and routing-tsp personas hit the same parser trap on v0.11.1:
wh >cond{...}mid-body gets re-parsed as a new function declaration namedwhreturningcond, surfacing as a misleadingILO-T008 return type mismatch: expected n, got _plus a cascade of "undefined variable" errors in functionwh. Same root cause as the v0.10wh >len xs 0{...}friction entry — it was neverlen-specific, justwh > ...in any position.Manifesto framing: the workaround (
cnd=>v 0;wh cnd{...;cnd=>v 0}) adds two extra statements per loop, exactly the kind of recurring token tax the language exists to avoid. The fix dissolves the friction at the parser layer; the correctedwh >v 0{...}form is 1 token cheaper per loop than any workaround.Repro
Before:
After:
What's in the diff
Commit 1 —
parser: reject reserved stmt keywords as fn decl starts.is_fn_decl_startpreviously accepted anyIdent Greater ...sequence as a zero-param fn header. After the first;of an outer body,parse_body_with's top-level boundary heuristic fires onwh >v 0{...}and treatswhas a fresh fn decl. Short-circuitis_fn_decl_starttofalsewhen the leading ident is one of the four reserved statement-keyword identifiers (wh/ret/brk/cnt) thatparse_stmtintercepts as control-flow forms. Mirrors the existing ILO-P011 reserved-keyword precedent inparse_decl.Commit 2 —
test: cross-engine coverage for wh >cond parser trap. Newtests/regression_wh_gt_trap.rscovers four shapes across tree, vm, and cranelift:wh >v 0{...}after a let binding (the gis-analyst repro)wh >cond{...}followed by a sibling fn decl (boundary heuristic must still find the real boundary)wh >=v 1{...}(GreaterEq prefix, same family)name>return;bodystill parses)examples/wh-gt-condition.ilodemonstrates the now-correct shape and rides throughtests/examples_engines.rsas an extra cross-engine regression layer.Test plan
cargo test --release --features cranelift— 4253 passed, 0 failedregression_wh_gt_traptests pass on tree/vm/craneliftexamples_enginespicks upwh-gt-condition.iloand runs it across all enginescargo fmt --checkcleancargo clippy --release --features cranelift --all-targets -- -D warningscleanfoo s:n>n;v=+s 0;wh >v 0{v=- v 1};+v 0returns0forfoo 5under all three engines