match arms: accept brace-block bodies#226
Merged
Merged
Conversation
`~v:{stmt1;stmt2;final-expr}` is now valid alongside the existing
`~v:stmt1;stmt2;final-expr` inline form. Mirrors `=cond{block}` grammar
so the arm boundary is unambiguous when the body contains call-shapes
that look like patterns to a tired model.
In parse_arm_body, when the post-colon token is LBrace and not a
destructure-pattern start, delegate to parse_brace_body. AST is
unchanged: MatchArm.body stays Vec<Spanned<Stmt>>, last stmt is the
arm value, every backend already iterates that way.
Seven tests across tree, VM, and Cranelift covering: - single-expr arms (existing behaviour preserved) - brace block with a local binding inside an Ok arm - brace block on the Err arm - nested match inside a brace-block arm - bool match with brace blocks on both branches - inline ;-separated arm body still works (additive change) - match-as-expression on RHS of a binding with brace-block arm The single-expr and inline-semi cases anchor that the brace-block path is purely additive and the old shapes can't silently regress.
Engine-asserted example demonstrating brace-block arm bodies on Ok and Err arms, nested match inside a block, and bool match with blocks on both branches. Picked up by tests/examples_engines.rs so the shape is exercised on every backend.
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
~v:{stmt1;stmt2;final-expr}is now a valid match arm body, alongside the existing inline~v:stmt1;stmt2;final-exprform. Mirrors the existing=cond{block}grammar so the arm boundary is unambiguous when the body contains call-shapes that resemble patterns.The manifesto framing: every Result-handling site that wants more than a single expression in an arm currently either uses the inline
;form (which works but is visually noisy and easy for a tired model to mis-segment) or extracts a helper function. Personas reported the helper tax repeatedly (ilo_assessment_feedback.mdlines 830, 851, 928). Brace blocks give a visually clear, unambiguous shape that matches what the agent already knows about=cond{block}.Repro
Before:
After:
What's in the diff
parse_arm_body, when the post-colon token isLBraceand it is not a destructure-pattern start, delegate toparse_brace_body. AST shape unchanged:MatchArm.bodystaysVec<Spanned<Stmt>>, last stmt is the arm value. Every backend already iterates that way, so no verifier / interpreter / VM / Cranelift change needed.tests/regression_match_block.rswith seven tests across tree / VM / Cranelift: single-expr (existing), brace block with local in Ok arm, brace block on Err arm, nested match in block, bool match with blocks on both branches, inline;-separated still works, match-as-expression with brace-block arm.examples/match-block.ilo— engine-asserted example picked up bytests/examples_engines.rsso the shape is exercised on every backend.Test plan
cargo fmtcargo clippy --release --features cranelift --all-targets -- -D warningscargo test --release --features cranelift— full suite greenregression_match_blocktests all pass on tree, VM, and Cranelift;arm bodies still work (anchored by the first and sixth tests)Follow-ups
None for this PR. The inline-
;form remains valid and is the more token-minimal shape for short arm bodies; brace blocks are the readable escape hatch when you need locals or want the boundary to be unmistakable.