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 501a480
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
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

0 comments on commit 501a480

Please sign in to comment.