Skip to content

Commit 0f80890

Browse files
committed
compiler: forbid cascading match cases
Erlang's `case` expression does not have an equivalent that we can translate this to idiomatically, so we're forbidding it for now. We'll revisit later on. Close #9
1 parent d933a9b commit 0f80890

File tree

5 files changed

+34
-13
lines changed

5 files changed

+34
-13
lines changed

src/compiler/ocaml_to_erlang/error.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ let ppf = Format.err_formatter
1111
let file_a_bug =
1212
{| If you think this is a bug, please file an issue here: https://github.com/AbstractMachinesLab/caramel/issues/new |}
1313

14+
let unsupported_fallthrough_cases () =
15+
Format.fprintf ppf
16+
{|We have found a case expression that falls through to the next case, like:
17+
18+
match x with
19+
| 0 | 1 -> true <--- this branch falls through
20+
| _ -> false
21+
22+
Since these patterns are not possible in Erlang, Caramel does not support them
23+
at the moment.
24+
\n
25+
|};
26+
exit 1
27+
1428
let unsupported_let_rec_inside_of_function_body () =
1529
Format.fprintf ppf
1630
{|We have found a let rec binding within a function.

src/compiler/ocaml_to_erlang/fun.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ and mk_pattern :
125125
let value = mk_pattern expr ~var_names in
126126
Pat.tuple [ tag; value ]
127127
| Tpat_constant const -> Erlang.Ast.Pattern_match (const_to_literal const)
128+
| Tpat_or (_, _, _) -> Error.unsupported_fallthrough_cases ()
128129
(* NOTE: here's where the translation of pattern
129130
* matching at the function level should happen. *)
130131
| _ -> Erlang.Ast.Pattern_ignore

tests/compiler/expressions.t/match.ml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,3 @@ let match_tuples () =
3737

3838
let match_atoms () =
3939
match `Hello with `Xavier -> true | `Joe -> true | _ -> false
40-
41-
let match_fall_through () =
42-
match 0 with
43-
| 1 | 2 -> true
44-
| _ -> false
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let match_fall_through () =
2+
match 0 with
3+
| 1 | 2 -> true
4+
| _ -> false

tests/compiler/expressions.t/run.t

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
list.ml
99
literals.ml
1010
match.ml
11+
match_fallthrough.ml
1112
names.ml
1213
names_primes.ml
1314
record_update.ml
@@ -381,7 +382,6 @@
381382
-export_type([int_pair/0]).
382383

383384
-export([match_atoms/0]).
384-
-export([match_fall_through/0]).
385385
-export([match_ignore/0]).
386386
-export([match_int/0]).
387387
-export([match_list/0]).
@@ -459,13 +459,6 @@
459459
_ -> false
460460
end.
461461

462-
-spec match_fall_through() -> boolean().
463-
match_fall_through() ->
464-
case 0 of
465-
_ -> true;
466-
_ -> false
467-
end.
468-
469462

470463
$ caramelc compile names.ml
471464
File "names.ml", line 15, characters 2-13:
@@ -629,3 +622,17 @@
629622
}.
630623

631624

625+
$ caramelc compile match_fallthrough.ml
626+
We have found a case expression that falls through to the next case, like:
627+
628+
match x with
629+
| 0 | 1 -> true <--- this branch falls through
630+
| _ -> false
631+
632+
Since these patterns are not possible in Erlang, Caramel does not support them
633+
at the moment.
634+
\n
635+
[1]
636+
$ cat match_fallthrough.erl
637+
cat: match_fallthrough.erl: No such file or directory
638+
[1]

0 commit comments

Comments
 (0)