diff options
| author | Thomas Refis <thomas.refis@gmail.com> | 2020-02-20 09:39:10 +0100 |
|---|---|---|
| committer | Thomas Refis <thomas.refis@gmail.com> | 2020-02-20 09:56:44 +0100 |
| commit | b96eef9f9ee4af3f710d084ef3e1fd0b7f177650 (patch) | |
| tree | be82a17aba78a6a893da78b7aa7a3da2ed7714b0 | |
| parent | 5b0357fc0f5605cc844c0040ae25ccc88706dc84 (diff) | |
| download | ocaml-flatten-pattern.tar.gz | |
matching: try => match with exceptionflatten-pattern
This makes it clearer where the exception comes from.
| -rw-r--r-- | lambda/matching.ml | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/lambda/matching.ml b/lambda/matching.ml index e2a2891908..b409df4247 100644 --- a/lambda/matching.ml +++ b/lambda/matching.ml @@ -3601,35 +3601,38 @@ let do_for_multiple_match loc paraml pat_act_list partial = } ) in try - try - (* Once for checking that compilation is possible *) - let next, nexts = split_and_precompile ~arg_id:None pm1 in - let size = List.length paraml - and idl = List.map (fun _ -> Ident.create_local "*match*") paraml in - let args = List.map (fun id -> (Lvar id, Alias)) idl in - let flat_next = flatten_precompiled size args next - and flat_nexts = - List.map (fun (e, pm) -> (e, flatten_precompiled size args pm)) nexts - in - let lam, total = - comp_match_handlers (compile_flattened repr) partial - (Context.start size) flat_next flat_nexts - in - List.fold_right2 (bind Strict) idl paraml - ( match partial with - | Partial -> check_total total lam raise_num (partial_function loc) + match split_and_precompile ~arg_id:None pm1 with + | exception Cannot_flatten -> ( + (* One pattern binds the whole tuple, flattening is not possible. + We need to allocate the scrutinee. *) + let lambda, total = compile_match None partial (Context.start 1) pm1 in + match partial with + | Partial -> check_total total lambda raise_num (partial_function loc) | Total -> assert (Jumps.is_empty total); - lam - ) - with Cannot_flatten -> ( - let lambda, total = compile_match None partial (Context.start 1) pm1 in - match partial with - | Partial -> check_total total lambda raise_num (partial_function loc) - | Total -> - assert (Jumps.is_empty total); - lambda - ) + lambda + ) + | next, nexts -> ( + let size = List.length paraml + and idl = List.map (fun _ -> Ident.create_local "*match*") paraml in + let args = List.map (fun id -> (Lvar id, Alias)) idl in + (* This will not raise, since the PM is well typed. *) + let flat_next = flatten_precompiled size args next + and flat_nexts = + List.map (fun (e, pm) -> (e, flatten_precompiled size args pm)) nexts + in + let lam, total = + comp_match_handlers (compile_flattened repr) partial + (Context.start size) flat_next flat_nexts + in + List.fold_right2 (bind Strict) idl paraml + ( match partial with + | Partial -> check_total total lam raise_num (partial_function loc) + | Total -> + assert (Jumps.is_empty total); + lam + ) + ) with Unused -> assert false (* ; partial_function loc () *) |
