harn-parser 0.7.27

Parser, AST, and type checker for the Harn programming language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! Match exhaustiveness, `unknown`-narrowing exhaustiveness, match-pattern type checks.

use super::*;

#[test]
fn test_exhaustive_match_no_warning() {
    let warns = warnings(
        r#"pipeline t(task) {
  enum Color { Red, Green, Blue }
  let c = Color.Red
  match c.variant {
"Red" -> { log("r") }
"Green" -> { log("g") }
"Blue" -> { log("b") }
  }
}"#,
    );
    let exhaustive_warns: Vec<_> = warns
        .iter()
        .filter(|w| w.contains("Non-exhaustive"))
        .collect();
    assert!(exhaustive_warns.is_empty());
}

#[test]
fn test_non_exhaustive_match_errors() {
    let errs = errors(
        r#"pipeline t(task) {
  enum Color { Red, Green, Blue }
  let c = Color.Red
  match c.variant {
"Red" -> { log("r") }
"Green" -> { log("g") }
  }
}"#,
    );
    let exhaustive_errs: Vec<_> = errs
        .iter()
        .filter(|e| e.contains("Non-exhaustive"))
        .collect();
    assert_eq!(exhaustive_errs.len(), 1, "got: {:?}", errs);
    assert!(exhaustive_errs[0].contains("Blue"));
}

#[test]
fn test_non_exhaustive_multiple_missing_errors() {
    let errs = errors(
        r#"pipeline t(task) {
  enum Status { Active, Inactive, Pending }
  let s = Status.Active
  match s.variant {
"Active" -> { log("a") }
  }
}"#,
    );
    let exhaustive_errs: Vec<_> = errs
        .iter()
        .filter(|e| e.contains("Non-exhaustive"))
        .collect();
    assert_eq!(exhaustive_errs.len(), 1, "got: {:?}", errs);
    assert!(exhaustive_errs[0].contains("Inactive"));
    assert!(exhaustive_errs[0].contains("Pending"));
}

#[test]
fn test_tagged_shape_union_match_exhaustive_no_warning() {
    let warns = warnings(
        r#"type Msg = {kind: "ping", ttl: int} | {kind: "pong", latency_ms: int}

pipeline t(task) {
  fn handle(m: Msg) -> string {
    match m.kind {
      "ping" -> { return "ping" }
      "pong" -> { return "pong" }
    }
  }
}"#,
    );
    let exh: Vec<_> = warns
        .iter()
        .filter(|w| w.contains("Non-exhaustive"))
        .collect();
    assert!(
        exh.is_empty(),
        "no non-exhaustive warning expected, got: {:?}",
        warns
    );
}

#[test]
fn test_tagged_shape_union_match_missing_arm_errors() {
    let errs = errors(
        r#"type Msg = {kind: "ping", ttl: int} | {kind: "pong", latency_ms: int}

pipeline t(task) {
  fn handle(m: Msg) -> string {
    match m.kind {
      "ping" -> { return "ping" }
    }
  }
}"#,
    );
    let exh: Vec<_> = errs
        .iter()
        .filter(|e| e.contains("Non-exhaustive match on tagged shape union"))
        .collect();
    assert_eq!(exh.len(), 1, "got: {:?}", errs);
    assert!(
        exh[0].contains("\"pong\""),
        "expected missing pong, got: {}",
        exh[0]
    );
}

#[test]
fn test_literal_union_match_exhaustive_no_warning() {
    let warns = warnings(
        r#"type Verdict = "pass" | "fail" | "unclear"

pipeline t(task) {
  fn classify(v: Verdict) -> string {
    match v {
      "pass" -> { return "ok" }
      "fail" -> { return "no" }
      "unclear" -> { return "?" }
    }
  }
}"#,
    );
    let exh: Vec<_> = warns
        .iter()
        .filter(|w| w.contains("Non-exhaustive"))
        .collect();
    assert!(exh.is_empty(), "no warning expected, got: {:?}", warns);
}

#[test]
fn test_literal_union_match_missing_errors() {
    let errs = errors(
        r#"type Verdict = "pass" | "fail" | "unclear"

pipeline t(task) {
  fn classify(v: Verdict) -> string {
    match v {
      "pass" -> { return "ok" }
      "fail" -> { return "no" }
    }
  }
}"#,
    );
    let exh: Vec<_> = errs
        .iter()
        .filter(|e| e.contains("Non-exhaustive match on literal union"))
        .collect();
    assert_eq!(exh.len(), 1, "got: {:?}", errs);
    assert!(exh[0].contains("\"unclear\""));
}

#[test]
fn test_non_exhaustive_match_wildcard_silences_error() {
    // Wildcard `_` arm escapes the new exhaustiveness error.
    let errs = errors(
        r#"pipeline t(task) {
  enum Color { Red, Green, Blue }
  let c = Color.Red
  match c.variant {
    "Red" -> { log("r") }
    _ -> { log("?") }
  }
}"#,
    );
    let exhaustive_errs: Vec<_> = errs
        .iter()
        .filter(|e| e.contains("Non-exhaustive"))
        .collect();
    assert!(exhaustive_errs.is_empty(), "got: {:?}", errs);
}

#[test]
fn test_unknown_exhaustive_unreachable_happy_path() {
    // All nine concrete variants covered → no warning on unreachable().
    let source = r#"pipeline t(task) {
  fn describe(v: unknown) -> string {
if type_of(v) == "string"  { return "s" }
if type_of(v) == "int"     { return "i" }
if type_of(v) == "float"   { return "f" }
if type_of(v) == "bool"    { return "b" }
if type_of(v) == "nil"     { return "n" }
if type_of(v) == "list"    { return "l" }
if type_of(v) == "dict"    { return "d" }
if type_of(v) == "bytes"   { return "y" }
if type_of(v) == "closure" { return "c" }
unreachable("unknown type_of variant")
  }
  log(describe(1))
}"#;
    assert!(exhaustive_warns(source).is_empty());
}

#[test]
fn test_unknown_exhaustive_unreachable_incomplete_warns() {
    let source = r#"pipeline t(task) {
  fn describe(v: unknown) -> string {
if type_of(v) == "string" { return "s" }
if type_of(v) == "int"    { return "i" }
unreachable("unknown type_of variant")
  }
  log(describe(1))
}"#;
    let warns = exhaustive_warns(source);
    assert_eq!(warns.len(), 1, "expected one warning, got: {:?}", warns);
    let w = &warns[0];
    for missing in &["float", "bool", "nil", "list", "dict", "bytes", "closure"] {
        assert!(w.contains(missing), "missing {missing} in: {w}");
    }
    assert!(!w.contains("int"));
    assert!(!w.contains("string"));
    assert!(w.contains("unreachable"));
    assert!(w.contains("v: unknown"));
}

#[test]
fn test_unknown_incomplete_normal_return_no_warning() {
    // Normal `return` fallthrough is NOT an exhaustiveness claim.
    let source = r#"pipeline t(task) {
  fn describe(v: unknown) -> string {
if type_of(v) == "string" { return "s" }
if type_of(v) == "int"    { return "i" }
return "other"
  }
  log(describe(1))
}"#;
    assert!(exhaustive_warns(source).is_empty());
}

#[test]
fn test_unknown_exhaustive_throw_incomplete_warns() {
    let source = r#"pipeline t(task) {
  fn describe(v: unknown) -> string {
if type_of(v) == "string" { return "s" }
throw "unhandled"
  }
  log(describe("x"))
}"#;
    let warns = exhaustive_warns(source);
    assert_eq!(warns.len(), 1, "expected one warning, got: {:?}", warns);
    assert!(warns[0].contains("throw"));
    assert!(warns[0].contains("int"));
}

#[test]
fn test_unknown_throw_without_narrowing_no_warning() {
    // A bare throw with no preceding `type_of` narrowing is not
    // an exhaustiveness claim — stay silent.
    let source = r#"pipeline t(task) {
  fn crash(v: unknown) -> string {
throw "nope"
  }
  log(crash(1))
}"#;
    assert!(exhaustive_warns(source).is_empty());
}

#[test]
fn test_unknown_exhaustive_nested_branch() {
    // Nested if inside a single branch: inner exhaustiveness doesn't
    // escape to the outer scope, and incomplete outer coverage warns.
    let source = r#"pipeline t(task) {
  fn describe(v: unknown, x: int) -> string {
if type_of(v) == "string" {
  if x > 0 { return v.upper() } else { return "s" }
}
if type_of(v) == "int" { return "i" }
unreachable("unknown type_of variant")
  }
  log(describe(1, 1))
}"#;
    let warns = exhaustive_warns(source);
    assert_eq!(warns.len(), 1, "expected one warning, got: {:?}", warns);
    assert!(warns[0].contains("float"));
}

#[test]
fn test_unknown_exhaustive_negated_check() {
    // `type_of(v) != "T"` guards the happy path, so the then-branch
    // accumulates coverage on the truthy side via inversion.
    let source = r#"pipeline t(task) {
  fn describe(v: unknown) -> string {
if type_of(v) != "string" {
  // v still unknown here, but "string" is NOT ruled out on this path
  return "non-string"
}
// v: string here
return v.upper()
  }
  log(describe("x"))
}"#;
    // No unreachable/throw so no warning regardless.
    assert!(exhaustive_warns(source).is_empty());
}

#[test]
fn test_enum_construct_type_inference() {
    let errs = errors(
        r#"pipeline t(task) {
  enum Color { Red, Green, Blue }
  let c: Color = Color.Red
}"#,
    );
    assert!(errs.is_empty());
}

#[test]
fn test_nil_coalescing_strips_nil() {
    // After ??, nil should be stripped from the type
    let errs = errors(
        r#"pipeline t(task) {
  let x: string | nil = nil
  let y: string = x ?? "default"
}"#,
    );
    assert!(errs.is_empty());
}

#[test]
fn test_shape_mismatch_detail_missing_field() {
    let errs = errors(
        r#"pipeline t(task) {
  let x: {name: string, age: int} = {name: "hello"}
}"#,
    );
    assert_eq!(errs.len(), 1);
    assert!(
        errs[0].contains("missing field 'age'"),
        "expected detail about missing field, got: {}",
        errs[0]
    );
}

#[test]
fn test_shape_mismatch_detail_wrong_type() {
    let errs = errors(
        r#"pipeline t(task) {
  let x: {name: string, age: int} = {name: 42, age: 10}
}"#,
    );
    assert_eq!(errs.len(), 1);
    assert!(
        errs[0].contains("field 'name' has type int, expected string"),
        "expected detail about wrong type, got: {}",
        errs[0]
    );
}

#[test]
fn test_match_pattern_string_against_int() {
    let warns = warnings(
        r#"pipeline t(task) {
  let x: int = 42
  match x {
"hello" -> { log("bad") }
42 -> { log("ok") }
  }
}"#,
    );
    let pattern_warns: Vec<_> = warns
        .iter()
        .filter(|w| w.contains("Match pattern type mismatch"))
        .collect();
    assert_eq!(pattern_warns.len(), 1);
    assert!(pattern_warns[0].contains("matching int against string literal"));
}

#[test]
fn test_match_pattern_int_against_string() {
    let warns = warnings(
        r#"pipeline t(task) {
  let x: string = "hello"
  match x {
42 -> { log("bad") }
"hello" -> { log("ok") }
  }
}"#,
    );
    let pattern_warns: Vec<_> = warns
        .iter()
        .filter(|w| w.contains("Match pattern type mismatch"))
        .collect();
    assert_eq!(pattern_warns.len(), 1);
    assert!(pattern_warns[0].contains("matching string against int literal"));
}

#[test]
fn test_match_pattern_bool_against_int() {
    let warns = warnings(
        r#"pipeline t(task) {
  let x: int = 42
  match x {
true -> { log("bad") }
42 -> { log("ok") }
  }
}"#,
    );
    let pattern_warns: Vec<_> = warns
        .iter()
        .filter(|w| w.contains("Match pattern type mismatch"))
        .collect();
    assert_eq!(pattern_warns.len(), 1);
    assert!(pattern_warns[0].contains("matching int against bool literal"));
}

#[test]
fn test_match_pattern_float_against_string() {
    let warns = warnings(
        r#"pipeline t(task) {
  let x: string = "hello"
  match x {
3.14 -> { log("bad") }
"hello" -> { log("ok") }
  }
}"#,
    );
    let pattern_warns: Vec<_> = warns
        .iter()
        .filter(|w| w.contains("Match pattern type mismatch"))
        .collect();
    assert_eq!(pattern_warns.len(), 1);
    assert!(pattern_warns[0].contains("matching string against float literal"));
}

#[test]
fn test_match_pattern_int_against_float_ok() {
    // int and float are compatible for match patterns
    let warns = warnings(
        r#"pipeline t(task) {
  let x: float = 3.14
  match x {
42 -> { log("ok") }
_ -> { log("default") }
  }
}"#,
    );
    let pattern_warns: Vec<_> = warns
        .iter()
        .filter(|w| w.contains("Match pattern type mismatch"))
        .collect();
    assert!(pattern_warns.is_empty());
}

#[test]
fn test_match_pattern_float_against_int_ok() {
    // float and int are compatible for match patterns
    let warns = warnings(
        r#"pipeline t(task) {
  let x: int = 42
  match x {
3.14 -> { log("close") }
_ -> { log("default") }
  }
}"#,
    );
    let pattern_warns: Vec<_> = warns
        .iter()
        .filter(|w| w.contains("Match pattern type mismatch"))
        .collect();
    assert!(pattern_warns.is_empty());
}

#[test]
fn test_match_pattern_correct_types_no_warning() {
    let warns = warnings(
        r#"pipeline t(task) {
  let x: int = 42
  match x {
1 -> { log("one") }
2 -> { log("two") }
_ -> { log("other") }
  }
}"#,
    );
    let pattern_warns: Vec<_> = warns
        .iter()
        .filter(|w| w.contains("Match pattern type mismatch"))
        .collect();
    assert!(pattern_warns.is_empty());
}

#[test]
fn test_match_pattern_wildcard_no_warning() {
    let warns = warnings(
        r#"pipeline t(task) {
  let x: int = 42
  match x {
_ -> { log("catch all") }
  }
}"#,
    );
    let pattern_warns: Vec<_> = warns
        .iter()
        .filter(|w| w.contains("Match pattern type mismatch"))
        .collect();
    assert!(pattern_warns.is_empty());
}

#[test]
fn test_match_pattern_untyped_no_warning() {
    // When value has no known type, no warning should be emitted
    let warns = warnings(
        r#"pipeline t(task) {
  let x = some_unknown_fn()
  match x {
"hello" -> { log("string") }
42 -> { log("int") }
  }
}"#,
    );
    let pattern_warns: Vec<_> = warns
        .iter()
        .filter(|w| w.contains("Match pattern type mismatch"))
        .collect();
    assert!(pattern_warns.is_empty());
}

#[test]
fn test_or_pattern_covers_literal_union_exhaustive() {
    // An or-pattern that combines alternatives still contributes each
    // literal to the exhaustiveness coverage set. Three-member literal
    // union, two arms: one literal + one two-alternative or-pattern.
    let errs = errors(
        r#"pipeline t(task) {
  fn verdict(v: "pass" | "fail" | "skip") -> string {
    return match v {
      "pass" -> { "ok" }
      "fail" | "skip" -> { "not ok" }
    }
  }
}"#,
    );
    let exh: Vec<_> = errs
        .iter()
        .filter(|e| e.contains("Non-exhaustive"))
        .collect();
    assert!(exh.is_empty(), "expected no error, got: {:?}", errs);
}

#[test]
fn test_or_pattern_missing_alternative_errors() {
    // Or-pattern with only two alternatives on a three-member union
    // must still be flagged as non-exhaustive.
    let errs = errors(
        r#"pipeline t(task) {
  fn verdict(v: "pass" | "fail" | "skip") -> string {
    return match v {
      "pass" | "fail" -> { "ok" }
    }
  }
}"#,
    );
    let exh: Vec<_> = errs
        .iter()
        .filter(|e| e.contains("Non-exhaustive"))
        .collect();
    assert_eq!(exh.len(), 1, "got: {:?}", errs);
    assert!(exh[0].contains("\"skip\""));
}

#[test]
fn test_or_pattern_tagged_shape_union_exhaustive() {
    // Or-pattern on the discriminant of a tagged shape union satisfies
    // exhaustiveness when the alternatives cover every variant's tag.
    let errs = errors(
        r#"type Msg =
  {kind: "ping", ttl: int} |
  {kind: "pong", latency_ms: int} |
  {kind: "close", reason: string}

pipeline t(task) {
  fn handle(m: Msg) -> string {
    return match m.kind {
      "ping" -> { "p" }
      "pong" | "close" -> { "other" }
    }
  }
}"#,
    );
    let exh: Vec<_> = errs
        .iter()
        .filter(|e| e.contains("Non-exhaustive"))
        .collect();
    assert!(exh.is_empty(), "expected no error, got: {:?}", errs);
}

#[test]
fn test_or_pattern_tagged_shape_union_missing() {
    // Or-pattern that covers two of three variants must still report
    // the missing one.
    let errs = errors(
        r#"type Msg =
  {kind: "ping", ttl: int} |
  {kind: "pong", latency_ms: int} |
  {kind: "close", reason: string}

pipeline t(task) {
  fn handle(m: Msg) -> string {
    return match m.kind {
      "ping" | "pong" -> { "live" }
    }
  }
}"#,
    );
    let exh: Vec<_> = errs
        .iter()
        .filter(|e| e.contains("Non-exhaustive"))
        .collect();
    assert_eq!(exh.len(), 1, "got: {:?}", errs);
    assert!(exh[0].contains("\"close\""));
}

#[test]
fn test_or_pattern_enum_exhaustive() {
    // Or-pattern on `enum.variant` (string-literal match against a
    // known enum) covers each listed variant.
    let errs = errors(
        r#"pipeline t(task) {
  enum Color { Red, Green, Blue }
  let c = Color.Red
  match c.variant {
"Red" | "Green" -> { log("warm-ish") }
"Blue" -> { log("cool") }
  }
}"#,
    );
    let exh: Vec<_> = errs
        .iter()
        .filter(|e| e.contains("Non-exhaustive"))
        .collect();
    assert!(exh.is_empty(), "expected no error, got: {:?}", errs);
}