plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
799b9c8
— Peter John
2026-11-10T19:55:15+05:30
Checker: a match on literals or with guards needs a catch-all arm
compiler/checker/src/matchcheck.rs
CHANGED
|
@@ -214,11 +214,13 @@ pub(crate) fn checkMatch(
|
|
|
214
214
|
// arm it doesn't count is the user's, written wrong).
|
|
215
215
|
let mut exhaustive_at = None;
|
|
216
216
|
let mut bad_pattern = false;
|
|
217
|
+
let mut trait_reported = false;
|
|
217
218
|
if let [subject_ty] = subject_types.as_slice() {
|
|
218
219
|
let subject = resolveRecorded(subject_ty);
|
|
219
220
|
if isTraitObject(&subject)
|
|
220
221
|
&& !m.cases.iter().any(|c| c.guard.is_none() && c.patterns.len() == 1 && patCatchAll(&c.patterns[0]))
|
|
221
222
|
{
|
|
223
|
+
trait_reported = true;
|
|
222
224
|
errors.push(CheckError::at(
|
|
223
225
|
fn_pos.0,
|
|
224
226
|
fn_pos.1,
|
|
@@ -228,15 +230,18 @@ pub(crate) fn checkMatch(
|
|
|
228
230
|
),
|
|
229
231
|
));
|
|
230
232
|
}
|
|
233
|
+
}
|
|
234
|
+
// Every subject at once: the arms without a guard, one pattern per subject.
|
|
231
|
-
|
|
235
|
+
let rows: Vec<Vec<&ast::CasePattern>> = m
|
|
232
|
-
|
|
236
|
+
.cases
|
|
233
|
-
|
|
237
|
+
.iter()
|
|
234
|
-
|
|
238
|
+
.filter(|c| c.guard.is_none() && c.patterns.len() == subject_types.len())
|
|
235
|
-
|
|
239
|
+
.map(|c| c.patterns.iter().collect())
|
|
236
|
-
|
|
240
|
+
.collect();
|
|
241
|
+
let subjects: Vec<PlumType> = subject_types.iter().map(resolveRecorded).collect();
|
|
242
|
+
match coverRows(rows, &subjects, ctx) {
|
|
243
|
+
Ok(()) => {}
|
|
237
|
-
|
|
244
|
+
Err(CoverFail::Reported(label, missing)) => {
|
|
238
|
-
coverRows(rows, std::slice::from_ref(subject_ty), ctx)
|
|
239
|
-
{
|
|
240
245
|
exhaustive_at = Some(errors.len());
|
|
241
246
|
errors.push(CheckError::at(
|
|
242
247
|
fn_pos.0,
|
|
@@ -249,6 +254,22 @@ pub(crate) fn checkMatch(
|
|
|
249
254
|
),
|
|
250
255
|
));
|
|
251
256
|
}
|
|
257
|
+
// Literals, guards or a type with no variants to list (`Int`, `Str`): only a
|
|
258
|
+
// catch-all arm covers the rest.
|
|
259
|
+
Err(CoverFail::Opaque) if !trait_reported && !subjects.is_empty() => {
|
|
260
|
+
exhaustive_at = Some(errors.len());
|
|
261
|
+
let shown: Vec<String> = subjects.iter().map(|t| t.to_string()).collect();
|
|
262
|
+
errors.push(CheckError::at(
|
|
263
|
+
fn_pos.0,
|
|
264
|
+
fn_pos.1,
|
|
265
|
+
format!(
|
|
266
|
+
"fn '{}': non-exhaustive match on {}: add a `_ =>` arm for the values no arm matches",
|
|
267
|
+
fn_name,
|
|
268
|
+
shown.join(", ")
|
|
269
|
+
),
|
|
270
|
+
));
|
|
271
|
+
}
|
|
272
|
+
Err(CoverFail::Opaque) => {}
|
|
252
273
|
}
|
|
253
274
|
|
|
254
275
|
for case in &m.cases {
|
compiler/checker/tests/checker/review_tests.rs
CHANGED
|
@@ -52,6 +52,32 @@ fn a_type_name_alone_is_not_a_value() {
|
|
|
52
52
|
errorWith("enum C = Red | Green\n\nfun k() -> Int =\n x := C\n 1", "'C' is a type, not a value");
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
#[test]
|
|
56
|
+
fn a_literal_match_needs_a_catch_all() {
|
|
57
|
+
let e = errorWith("fun k(n: Int) -> Int = match n\n 0 => 1\n 1 => 2", "non-exhaustive match on Int");
|
|
58
|
+
assert!(e.message.contains("`_ =>`"), "{e:?}");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
#[test]
|
|
62
|
+
fn a_guarded_match_needs_a_catch_all() {
|
|
63
|
+
errorWith("fun k(n: Int) -> Int = match n\n x when x > 0 => 1\n x when x <= 0 => 2", "non-exhaustive match on Int");
|
|
64
|
+
ok("fun k(n: Int) -> Int = match n\n x when x > 0 => 1\n _ => 2");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
#[test]
|
|
68
|
+
fn a_str_match_needs_a_catch_all() {
|
|
69
|
+
errorWith("fun k(s: Str) -> Int = match s\n \"a\" => 1", "non-exhaustive match on Str");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
#[test]
|
|
73
|
+
fn a_match_on_two_subjects_is_checked_too() {
|
|
74
|
+
errorWith(
|
|
75
|
+
"enum C = Red | Green\n\nfun k(a: C, b: C) -> Int = match a, b\n Red, Red => 1\n Green, _ => 2",
|
|
76
|
+
"missing Green",
|
|
77
|
+
);
|
|
78
|
+
ok("enum C = Red | Green\n\nfun k(a: C, b: C) -> Int = match a, b\n Red, Red => 1\n Red, Green => 3\n Green, _ => 2");
|
|
79
|
+
}
|
|
80
|
+
|
|
55
81
|
fn stdErrors(name: &str, body: &str) -> Vec<String> {
|
|
56
82
|
plum_test_support::checkErrors(name, &format!("import std/Int\nimport std/Str\nimport std/Option\nimport std/collections/List\n\n{body}\n"))
|
|
57
83
|
}
|
compiler/wasm-codegen/tests/wasm_codegen/codegen_tests.rs
CHANGED
|
@@ -370,7 +370,7 @@ fun bad(n: Int) -> Int =
|
|
|
370
370
|
"non-exhaustive match in value position must be a clear error, not invalid wasm",
|
|
371
371
|
);
|
|
372
372
|
assert!(
|
|
373
|
-
err.contains("
|
|
373
|
+
err.contains("non-exhaustive match on Int"),
|
|
374
374
|
"got: {}",
|
|
375
375
|
err
|
|
376
376
|
);
|