Description
PatternContext::new branches on string literals ("match pattern", "for-loop pattern"), making the context selector brittle and prone to errors.
Details
Location:
src/parser/expression/pattern_collection.rs:51
Current implementation:
PatternContext::new uses string literal matching to determine context type:
- "match pattern"
- "for-loop pattern"
Problems:
- String literals are error-prone (typos, case sensitivity)
- No compile-time exhaustiveness checking
- Unreachable string branches can slip through
- Difficult to refactor or extend with new context types
Impact
- Type safety: No compiler guarantees for valid context types
- Maintainability: Adding new contexts requires string coordination across call sites
- Reliability: Typos in string literals lead to runtime failures
- Refactoring: IDE refactoring tools cannot help with string-based dispatch
Actionable Fix
Replace the string-based context selector with an enum:
- Introduce
PatternContextKind enum with variants:
MatchPattern
ForLoopPattern
- Replace
PatternContext::new string parameter with PatternContextKind
- Use
match over enum variants instead of string comparisons
- Remove unreachable string branch
Benefits:
- Compile-time exhaustiveness checking
- Type-safe context selection
- Better IDE support and refactoring
- Self-documenting code
Priority
Medium – Type safety and maintainability issue, but not blocking current functionality.
References
Requested by
@leynos
Description
PatternContext::newbranches on string literals ("match pattern", "for-loop pattern"), making the context selector brittle and prone to errors.Details
Location:
src/parser/expression/pattern_collection.rs:51Current implementation:
PatternContext::newuses string literal matching to determine context type:Problems:
Impact
Actionable Fix
Replace the string-based context selector with an enum:
PatternContextKindenum with variants:MatchPatternForLoopPatternPatternContext::newstring parameter withPatternContextKindmatchover enum variants instead of string comparisonsBenefits:
Priority
Medium – Type safety and maintainability issue, but not blocking current functionality.
References
@leynosRequested by
@leynos