Diagnostic and cause
LinkRefutable let pattern
- Family
- topaz
- Phase
- check
- Code
TPZ5026
Cause and conditions
A let destructuring pattern must match every value of the initializer's type. Use match or if let when a constructor pattern can fail.
Related rule: Refutable let patternResponse
Use if let or match to handle both outcomes, or choose an irrefutable pattern.
Source evidence
- compiler/crates/topaz_diag/src/explain.rs at
151141d7bb44· 748-760 - compiler/crates/topaz_self_frontend/topaz/checker/statements.tpz at
151141d7bb44· 251-251 - compiler/crates/topaz_self_frontend/topaz/checker/statements.tpz at
151141d7bb44· 235-255 - compiler/crates/topaz_self_frontend/topaz/checker/patterns.tpz at
151141d7bb44· 126-135 - compiler/crates/topaz_self_frontend/topaz/checker/patterns.tpz at
151141d7bb44· 305-316
Compiler phase: check.
Compiler explanation: A let pattern could fail to match at runtime.
Message templates. The marker ⟨value⟩ stands for text computed for the reported program.
- refutable pattern in
let: it does not match every value of the type — useif letto handle the non-matching case Source
A let destructuring pattern must match every value of the initializer's type. Use match or if let when a constructor pattern can fail.
Refutable let pattern
A let destructuring pattern must match every value of the initializer's type. Use match or if let when a constructor pattern can fail.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/statements.tpz at
151141d7bb44· 235-255 - compiler/crates/topaz_self_frontend/topaz/checker/patterns.tpz at
151141d7bb44· 126-135 - compiler/crates/topaz_self_frontend/topaz/checker/patterns.tpz at
151141d7bb44· 305-316
Response
LinkUse if let or match to handle both outcomes, or choose an irrefutable pattern.
Reproduction and verification
LinkRejected program
Linklet maybe: Option<int> = None()let Some(value) = maybe
No stdout.
Expected result
Expected result
Rejection during check. Nonzero exit.
TPZ5026diagnostics-refutable-let-pattern atexample.tpz:2:5
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/statements.tpz at
151141d7bb44· 235-255 - compiler/crates/topaz_self_frontend/topaz/checker/patterns.tpz at
151141d7bb44· 126-135 - compiler/crates/topaz_self_frontend/topaz/checker/patterns.tpz at
151141d7bb44· 305-316
Repaired program
Linklet maybe: Option<int> = None()let value: int = match (maybe) {case Some(number) => numbercase None() => 0}
Expected result
Expected result
Checking accepts the program. Exit code 0.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/statements.tpz at
151141d7bb44· 235-255 - compiler/crates/topaz_self_frontend/topaz/checker/patterns.tpz at
151141d7bb44· 126-135 - compiler/crates/topaz_self_frontend/topaz/checker/patterns.tpz at
151141d7bb44· 305-316