Skip to content

Commit 5d5a624

Browse files
committed
Unify type variables introduced in patterns, and fix 2 type checker bugs
One related to incomplete occurs check, and the other one related to incomplete "find" in the union find part.
1 parent 1412670 commit 5d5a624

9 files changed

+335
-84
lines changed

src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ pub mod expression {
12861286

12871287
// Identifiers
12881288

1289-
type IdentifierName = StringSymbol;
1289+
pub type IdentifierName = StringSymbol;
12901290

12911291
#[derive(PartialEq, Debug, Clone)]
12921292
pub struct CapitalizedIdentifier {
@@ -1308,7 +1308,7 @@ pub mod expression {
13081308
}
13091309
}
13101310

1311-
#[derive(PartialEq, Debug, Clone)]
1311+
#[derive(PartialEq, Debug, Copy, Clone)]
13121312
pub struct Identifier {
13131313
pub name: IdentifierName,
13141314
pub span: span::Index,

src/compiler/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ main =
312312
1 as b | 2 as b | b -> b
313313
_ -> id
314314
test = when Nil is
315-
Cons a _ |
316-
Cons (Cons a _) _ |
317-
Cons (Cons (Cons a _) _) _ -> a
315+
Cons a Nil |
316+
Cons _ (Cons a Nil) |
317+
Cons _ (Cons _ (Cons a Nil)) -> a
318318
test = when Pair None (Some 3) is
319319
Pair (Some (Some (5 as a | 7 as a) | Some (1 as a))) (Some (3 as b | b)) ->
320320
True

src/compiler/snapshots/alma__compiler__tests__Type patterns and pattern matching.snap

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
source: src/compiler/mod.rs
33
assertion_line: 284
4-
expression: "compile(&[r#\"\nmodule Test exposing (main, Option(Some, None))\n\ntype Option a = Some a | None\n\ntype Id = Id Float\n\ntype List a = Cons a (List a) | Nil\n\ntype Pair a b = Pair a b\n\nmain =\n let\n Id id = Id 1\n test = when Id 5 is\n Id id -> id\n test = when \"test\" is\n \"banana\" -> id\n \"phone\" -> id\n test = when 5 is\n 5 -> id\n _ -> id\n test = when Some(Id(1)) is\n Some (Id id) -> id\n test = when 5 is\n 5 as a -> a\n 1 as b | 2 as b | b -> b\n _ -> id\n test = when Nil is\n Cons a _ |\n Cons (Cons a _) _ |\n Cons (Cons (Cons a _) _) _ -> a\n test = when Pair None (Some 3) is\n Pair (Some (Some (5 as a | 7 as a) | Some (1 as a))) (Some (3 as b | b)) ->\n True\n Some id\n\n \"#])"
4+
expression: "compile(&[r#\"\nmodule Test exposing (main, Option(Some, None))\n\ntype Option a = Some a | None\n\ntype Id = Id Float\n\ntype List a = Cons a (List a) | Nil\n\ntype Pair a b = Pair a b\n\nmain =\n let\n Id id = Id 1\n test = when Id 5 is\n Id id -> id\n test = when \"test\" is\n \"banana\" -> id\n \"phone\" -> id\n test = when 5 is\n 5 -> id\n _ -> id\n test = when Some(Id(1)) is\n Some (Id id) -> id\n test = when 5 is\n 5 as a -> a\n 1 as b | 2 as b | b -> b\n _ -> id\n test = when Nil is\n Cons a Nil |\n Cons _ (Cons a Nil) |\n Cons _ (Cons _ (Cons a Nil)) -> a\n test = when Pair None (Some 3) is\n Pair (Some (Some (5 as a | 7 as a) | Some (1 as a))) (Some (3 as b | b)) ->\n True\n Some id\n\n \"#])"
55
---
66
// alma_out/Alma.ffi.js
77

@@ -198,8 +198,8 @@ let main = function() {
198198
let { _0: id } = Id(1)
199199
let test = function () {
200200
let __result = Id(5)
201-
if (__result.__tag === "Id") {
202-
let { _0: id } = __result
201+
let id
202+
if (__result.__tag === "Id" && (id = __result._0, true)) {
203203
return id
204204
}
205205

@@ -227,21 +227,20 @@ let main = function() {
227227
}()
228228
let test = function () {
229229
let __result = Some(Id(1))
230-
if (__result.__tag === "Some" && __result._0.__tag === "Id") {
231-
let { _0: { _0: id } } = __result
230+
let id
231+
if (__result.__tag === "Some" && __result._0.__tag === "Id" && (id = __result._0._0, true)) {
232232
return id
233233
}
234234

235235
throw new Error("Incomplete pattern match")
236236
}()
237237
let test = function () {
238238
let __result = 5
239-
if (__result === 5) {
240-
let a = __result
239+
let a, b
240+
if (((__result === 5) ? (a = __result, true) : false)) {
241241
return a
242242
}
243-
if (((__result === 1) || (__result === 2))) {
244-
let b, b, b = __result
243+
if (((((__result === 1) ? (b = __result, true) : false)) || (((__result === 2) ? (b = __result, true) : false)) || ((b = __result, true)))) {
245244
return b
246245
}
247246
return id
@@ -250,17 +249,17 @@ let main = function() {
250249
}()
251250
let test = function () {
252251
let __result = Nil
253-
if (((__result.__tag === "Cons") || (__result.__tag === "Cons" && __result._0.__tag === "Cons") || (__result.__tag === "Cons" && __result._0.__tag === "Cons" && __result._0._0.__tag === "Cons"))) {
254-
let { _0: a }, { _0: { _0: a } }, { _0: { _0: { _0: a } } } = __result
252+
let a
253+
if (((__result.__tag === "Cons" && (a = __result._0, true) && __result._1.__tag === "Nil") || (__result.__tag === "Cons" && __result._1.__tag === "Cons" && (a = __result._1._0, true) && __result._1._1.__tag === "Nil") || (__result.__tag === "Cons" && __result._1.__tag === "Cons" && __result._1._1.__tag === "Cons" && (a = __result._1._1._0, true) && __result._1._1._1.__tag === "Nil"))) {
255254
return a
256255
}
257256

258257
throw new Error("Incomplete pattern match")
259258
}()
260259
let test = function () {
261260
let __result = Pair(None, Some(3))
262-
if (__result.__tag === "Pair" && __result._0.__tag === "Some" && ((__result._0._0.__tag === "Some" && ((__result._0._0._0 === 5) || (__result._0._0._0 === 7))) || (__result._0._0.__tag === "Some" && __result._0._0._0 === 1)) && __result._1.__tag === "Some" && ((__result._1._0 === 3))) {
263-
let { _0: { _0: { _0: a, a }, { _0: a } }, _1: { _0: b, b } } = __result
261+
let a, b
262+
if (__result.__tag === "Pair" && __result._0.__tag === "Some" && ((__result._0._0.__tag === "Some" && ((((__result._0._0._0 === 5) ? (a = __result._0._0._0, true) : false)) || (((__result._0._0._0 === 7) ? (a = __result._0._0._0, true) : false)))) || (__result._0._0.__tag === "Some" && ((__result._0._0._0 === 1) ? (a = __result._0._0._0, true) : false))) && __result._1.__tag === "Some" && ((((__result._1._0 === 3) ? (b = __result._1._0, true) : false)) || ((b = __result._1._0, true)))) {
264263
return True
265264
}
266265

0 commit comments

Comments
 (0)