Skip to content

Commit

Permalink
type_approx_function bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rajgodse committed Aug 9, 2023
1 parent d882ad6 commit d9d25f0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
11 changes: 11 additions & 0 deletions ocaml/testsuite/tests/pattern-guards/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,14 @@ Line 6, characters 9-10:
Warning 18 [not-principal]: this type-based constructor disambiguation is not principal.
val disambiguation_from_guard : Foo.t option -> string = <fun>
|}];;

(* Test approximate typechecking of guards under function. *)

let rec f = function
| x :: xs when x match Some y -> y + f xs
| _ :: xs -> f xs
| [] -> 0
;;
[%%expect{|
val f : int option list -> int = <fun>
|}];;
11 changes: 6 additions & 5 deletions ocaml/typing/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3748,7 +3748,7 @@ let rec type_function_approx env loc label spato rhs in_function ty_expected =
| Some spat -> type_pattern_approx env spat ty_arg
end;
let in_function = Some (loc_fun, ty_fun) in
type_approx_aux env rhs in_function ty_ret
type_approx_aux_case_rhs env rhs in_function ty_ret

and type_approx_aux env sexp in_function ty_expected =
match Jane_syntax.Expression.of_ast sexp with
Expand All @@ -3757,11 +3757,12 @@ and type_approx_aux env sexp in_function ty_expected =
match sexp.pexp_desc with
| Pexp_let (_, _, e) -> type_approx_aux env e None ty_expected
| Pexp_fun (l, _, p, e) ->
type_function_approx env sexp.pexp_loc l (Some p) e in_function
ty_expected
| Pexp_function ({pc_rhs}::_) ->
type_function_approx env sexp.pexp_loc Nolabel None pc_rhs
type_function_approx env sexp.pexp_loc l (Some p) (Psimple_rhs e)
in_function ty_expected
| Pexp_function (case::_) ->
let rhs = parsed_case_rhs_of_case case in
type_function_approx env sexp.pexp_loc Nolabel None rhs in_function
ty_expected
| Pexp_match (_, cases) -> type_approx_aux_cases env cases None ty_expected
| Pexp_try (e, _) -> type_approx_aux env e None ty_expected
| Pexp_tuple l ->
Expand Down

0 comments on commit d9d25f0

Please sign in to comment.