Skip to content

Commit

Permalink
raise error when no cases matched in pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
butterunderflow committed Aug 23, 2024
1 parent da9b3d9 commit c18560b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/back/closure_translator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ let ff_match_constr = C.VARIABLE "ff_match_constr"

let ff_match_tuple = C.VARIABLE "ff_match_tuple"

let ff_match_fail = C.VARIABLE "ff_match_fail"

let ff_match_fail_stmt = C.(COMPUTATION (CALL (ff_match_fail, [])))

let header =
{|
#include "fun_rt.hpp"
Expand Down Expand Up @@ -315,7 +319,8 @@ and trans_expr ctx e =
( res,
[
C.DOWHILE
(C.CONSTANT (C.CONST_INT "0"), make_stmt_seq (e_stmts @ branches));
( C.CONSTANT (C.CONST_INT "0"),
make_stmt_seq (e_stmts @ branches @ [ ff_match_fail_stmt ]) );
] )
| ECmp (op, e0, e1) ->
let is_eq_v = create_decl "is_eq" ctx in
Expand Down
2 changes: 2 additions & 0 deletions runtime/include/fun_rt_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ bool ff_match_tuple(ff_obj_t cond, ff_obj_t* const (&payloads)[N]) {
return true;
}

void ff_match_fail();

ff_obj_t ff_is_equal(ff_obj_t x, ff_obj_t y);

bool ff_is_equal_aux(const ff_obj_t& x, const ff_obj_t& y);
Expand Down
4 changes: 4 additions & 0 deletions runtime/src/fun_rt_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ bool ff_match_constr(int64_t id, ff_obj_t cond, ff_obj_t* payload) {
return true;
}

void ff_match_fail() {
throw std::runtime_error("Match failure!");
}

bool ff_is_equal_aux(const ff_obj_t& x, const ff_obj_t& y) {
if (x.tag != y.tag) {
return false;
Expand Down
4 changes: 4 additions & 0 deletions tests/cram/test_dirs/interval_functor.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
match_res_5 = app_res_17;
break;
}
ff_match_fail();
}
while(0);
return match_res_5;
Expand Down Expand Up @@ -346,6 +347,7 @@
}
}
}
ff_match_fail();
}
while(0);
return match_res_11;
Expand Down Expand Up @@ -469,6 +471,7 @@
break;
}
}
ff_match_fail();
}
while(0);
return match_res_4;
Expand Down Expand Up @@ -496,6 +499,7 @@
match_res_3 = temp_7;
break;
}
ff_match_fail();
}
while(0);
return match_res_3;
Expand Down
7 changes: 6 additions & 1 deletion tests/cram/test_dirs/match.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
break;
}
}
ff_match_fail();
}
while(0);
m_28 = match_res_21;
Expand All @@ -96,6 +97,7 @@
match_res_29 = tu_31;
break;
}
ff_match_fail();
}
while(0);
tu_32 = match_res_29;
Expand All @@ -109,6 +111,7 @@
match_res_33 = app_res_38;
break;
}
ff_match_fail();
}
while(0);
m_39 = match_res_33;
Expand Down Expand Up @@ -144,6 +147,7 @@
match_res_4 = app_res_8;
break;
}
ff_match_fail();
}
while(0);
return match_res_4;
Expand Down Expand Up @@ -200,6 +204,7 @@
match_res_3 = app_res_5;
break;
}
ff_match_fail();
}
while(0);
__7 = match_res_3;
Expand All @@ -225,4 +230,4 @@
}

$ ./test_unhandled.fun.out
Runtime error: Assertion failed!
Runtime error: Match failure!

0 comments on commit c18560b

Please sign in to comment.