Skip to content

Commit e4152b7

Browse files
author
Florent Ferrari
committed
Merge remote-tracking branch 'luyu-fork/pattern-improvements' into pattern-improvements
2 parents 2e395f4 + 63d18ee commit e4152b7

File tree

2 files changed

+16
-33
lines changed

2 files changed

+16
-33
lines changed

hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Desugarer.scala

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,10 @@ class Desugarer(val elaborator: Elaborator)
9090
def ++(fallback: Split): Split =
9191
if fallback == Split.End then
9292
split
93-
else if split.isFull then
94-
raise:
95-
ErrorReport:
96-
msg"The following branches are unreachable." -> fallback.toLoc ::
97-
msg"Because the previous split is full." -> split.toLoc :: Nil
98-
split
9993
else (split match
10094
case Split.Cons(head, tail) => Split.Cons(head, tail ++ fallback)
10195
case Split.Let(name, term, tail) => Split.Let(name, term, tail ++ fallback)
102-
case Split.Else(_) /* impossible */ | Split.End => fallback)
96+
case Split.Else(_) | Split.End => fallback)
10397

10498
private val subScrutineeMap = HashMap.empty[BlockLocalSymbol, ScrutineeData]
10599
private val fieldScrutineeMap = HashMap.empty[BlockLocalSymbol, ScrutineeData]
@@ -466,9 +460,7 @@ class Desugarer(val elaborator: Elaborator)
466460
if pat.patternParams.size > 0 then
467461
error(
468462
msg"Pattern `${pat.nme}` expects ${"pattern argument".pluralize(pat.patternParams.size, true)}" ->
469-
pat.patternParams.foldLeft[Opt[Loc]](N):
470-
case (N, param) => param.sym.toLoc
471-
case (S(loc), param) => S(loc ++ param.sym.toLoc),
463+
Loc(pat.patternParams.iterator.map(_.sym)),
472464
msg"But no arguments were given" -> ctor.toLoc)
473465
fallback
474466
else
@@ -515,12 +507,8 @@ class Desugarer(val elaborator: Elaborator)
515507
if pat.patternParams.size != patArgs.size then
516508
error(
517509
msg"Pattern `${pat.nme}` expects ${"pattern argument".pluralize(pat.patternParams.size, true)}" ->
518-
pat.patternParams.foldLeft[Opt[Loc]](N):
519-
case (N, param) => param.sym.toLoc
520-
case (S(loc), param) => S(loc ++ param.sym.toLoc),
521-
msg"But ${"pattern argument".pluralize(patArgs.size, true)} were given" -> args.foldLeft[Opt[Loc]](N):
522-
case (N, arg) => arg.toLoc
523-
case (S(loc), arg) => S(loc ++ arg.toLoc))
510+
Loc(pat.patternParams.iterator.map(_.sym)),
511+
msg"But ${"pattern argument".pluralize(patArgs.size, true)} were given" -> Loc(args))
524512
fallback
525513
else
526514
Branch(ref, Pattern.Synonym(pat, patArgs.zip(args)), sequel(ctx)) ~: fallback

hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,22 @@ if A is
1515
else 3
1616
//│ = 2
1717

18+
:e
1819
if A is
1920
A & A & B then 0
2021
B & A & B then 1
2122
A & A & A then 2
2223
else 3
2324
//│ = 2
25+
//│ FAILURE: Unexpected lack of type error
2426

2527
:sjs
26-
:todo
27-
if A is
28-
A & y then y
28+
fun foo(v) =
29+
if v is
30+
A & B then 1
2931
else 0
30-
//│ ╔══[ERROR] The following branches are unreachable.
31-
//│ ║ l.29: else 0
32-
//│ ║ ^
33-
//│ ╟── Because the previous split is full.
34-
//│ ║ l.28: A & y then y
35-
//│ ╙── ^
3632
//│ JS (unsanitized):
37-
//│ let scrut2, y; scrut2 = A1; if (scrut2 instanceof A1.class) { y = scrut2; y } else { 0 }
38-
//│ = A
33+
//│ let foo; foo = function foo(v) { if (v instanceof A1.class) { return 0 } else { return 0 } };
3934

4035
fun range(i, j) =
4136
if i > j then Nil
@@ -65,18 +60,18 @@ fun foo(x) = if x is
6560
else 30
6661
else 40
6762
//│ ╔══[ERROR] The following branches are unreachable.
68-
//│ ║ l.66: else 40
63+
//│ ║ l.61: else 40
6964
//│ ║ ^^
7065
//│ ╟── Because the previous split is full.
71-
//│ ║ l.62: {a:_ & a} & C and
66+
//│ ║ l.57: {a:_ & a} & C and
7267
//│ ║ ^^^^^
73-
//│ ║ l.63: a is 1 then 10
68+
//│ ║ l.58: a is 1 then 10
7469
//│ ║ ^^^^^^^^^^^^^^^^^^^^^^
75-
//│ ║ l.64: a is 2 then 20
70+
//│ ║ l.59: a is 2 then 20
7671
//│ ║ ^^^^^^^^^^^^^^^^^^^^^^
77-
//│ ║ l.65: else 30
72+
//│ ║ l.60: else 30
7873
//│ ║ ^^^^^^^^^^^^^^^
79-
//│ ║ l.66: else 40
74+
//│ ║ l.61: else 40
8075
//│ ╙── ^^^^^^^^^^^
8176

8277
foo(C(1))

0 commit comments

Comments
 (0)