Support refutable sub-patterns in tuple and struct destructuring#600
Merged
Support refutable sub-patterns in tuple and struct destructuring#600
Conversation
…et/matches Transform literal sub-patterns (int, bool, char, string) inside tuple and struct patterns into guard conditions during the lower phase (TIR→TIR). For example, `[a, 10] => body` becomes `[a, __lit_N] && __lit_N == 10 => body`. - Parser: extend parse_let_pattern() with literal patterns, rest (..) in tuples, and mut bindings; recognize [bracket as variant pattern start for if-let - Lower: add extract_literal_sub_patterns() to replace literals with temp bindings and build equality guards; handle both Match and IfLet uniformly - String equality uses String^Eq::eq method call; primitives use binary == - Remove float literal test cases (float patterns explicitly rejected by design) Resolves TODO tests: struct_destruct_literal, tuple_destruct_literal https://claude.ai/code/session_01TnBQtMkKForC5jzEJ6Z3Yy
Extend the refutable sub-pattern extraction in the lower phase to handle
variant and enum patterns inside tuple (and struct) match arms, not just
literals. This enables patterns like `match [a, b] { [Bool(x), Bool(y)] => ... }`.
- Rename extract_literal_sub_patterns → extract_refutable_sub_patterns
- Add variant sub-pattern extraction with VariantTest guards and payload binding
- Add enum sub-pattern extraction with discriminant comparison guards
- Handle ref-peeling (deref) for variant/enum sub-patterns in reference contexts
- Add e2e test for variant patterns in tuple destructuring
- Simplify core:json_value Value::eq using the new tuple variant pattern syntax
https://claude.ai/code/session_01TnBQtMkKForC5jzEJ6Z3Yy
There was a problem hiding this comment.
⚠️ Performance Alert ⚠️
Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.
| Benchmark suite | Current: d75ccb5 | Previous: 3b14015 | Ratio |
|---|---|---|---|
sieve (-O1) |
207 ms |
157 ms |
1.32 |
This comment was automatically generated by workflow using github-action-benchmark.
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
This PR adds support for refutable sub-patterns (literals, variants, and enums) within tuple and struct destructuring patterns in match expressions and if-let statements. Previously, only irrefutable patterns like bindings and wildcards were supported in nested positions.
Key Changes
Pattern Extraction Logic (
wado-compiler/src/lower/pattern.rs):pattern_has_refutable_sub_patterns()to detect when a tuple/struct pattern contains refutable sub-patternsextract_refutable_sub_patterns()to transform refutable sub-patterns into guard conditions and body prefix statementsextract_refutable_sub_pattern()to handle individual refutable patterns (literals, variants, enums) by:literal_eq_condition(),get_case_index(), andget_struct_fields()to support pattern extractionParser Updates (
wado-compiler/src/parser.rs):is_pattern_start()to recognize tuple patterns starting with[parse_let_pattern()to support literal patterns in match/if-let contexts (previously only allowed in let bindings)Test Fixtures:
tuple_destruct_variant.wado- tests tuple destructuring with variant sub-patternstuple_destruct_literal.wado- tests tuple destructuring with literal sub-patternsstruct_destruct_literal.wado- tests struct destructuring with literal sub-patternstuple_destruct_literal.wadoandstruct_destruct_literal.wadoto remove unsupported float literal patternsDocumentation Updates:
docs/spec.mdto document nested sub-patterns in tuples and structsdocs/cheatsheet.mdwith examples of nested pattern matchingLibrary Updates (
lib/core/json_value.wado):Value::eq()implementation to use tuple destructuring with variant sub-patternsImplementation Details
The transformation works by:
&&This approach maintains the semantics of pattern matching while enabling more expressive destructuring patterns.
https://claude.ai/code/session_01TnBQtMkKForC5jzEJ6Z3Yy