···34163416 }
34173417 | Not_a_function
3418341834193419-exception Filter_arrow_failed of filter_arrow_failure
34203420-34213419type filtered_arrow =
34223420 { ty_param : type_expr;
34233421 ty_ret : type_expr;
···34443442 let t' = newty2 ~level (Tarrow (l, t1, t2, commu_ok)) in
34453443 t', t1, t2
34463444 in
34473447- let t =
34483448- try expand_head_trace env t
34493449- with Unify_trace trace ->
34453445+ match expand_head_trace env t with
34463446+ | t ->
34473447+ begin
34483448+ match get_desc t with
34493449+ | Tvar _ ->
34503450+ let t', ty_param, ty_ret = function_type (get_level t) in
34513451+ link_type t t';
34523452+ Ok { ty_param; ty_ret }
34533453+ | Tarrow(l', ty_param, ty_ret, _) ->
34543454+ if l = l' || !Clflags.classic && l = Nolabel && not (is_optional l')
34553455+ then Ok { ty_param; ty_ret }
34563456+ else Error (Label_mismatch
34573457+ { got = l; expected = l'; expected_type = t })
34583458+ | _ ->
34593459+ Error Not_a_function
34603460+ end
34613461+ | exception Unify_trace trace ->
34503462 let t', _, _ = function_type (get_level t) in
34513451- raise (Filter_arrow_failed
34523452- (Unification_error
34533453- (expand_to_unification_error
34543454- env
34553455- (Diff { got = t'; expected = t } :: trace))))
34563456- in
34573457- match get_desc t with
34583458- | Tvar _ ->
34593459- let t', ty_param, ty_ret = function_type (get_level t) in
34603460- link_type t t';
34613461- { ty_param; ty_ret }
34623462- | Tarrow(l', ty_param, ty_ret, _) ->
34633463- if l = l' || !Clflags.classic && l = Nolabel && not (is_optional l')
34643464- then { ty_param; ty_ret }
34653465- else raise (Filter_arrow_failed
34663466- (Label_mismatch
34673467- { got = l; expected = l'; expected_type = t }))
34683468- | _ ->
34693469- raise (Filter_arrow_failed Not_a_function)
34633463+ Error (Unification_error
34643464+ (expand_to_unification_error
34653465+ env
34663466+ (Diff { got = t'; expected = t } :: trace)))
3470346734713468let is_really_poly env ty =
34723469 let snap = Btype.snapshot () in
+10-12
typing/ctype.mli
···308308 ty_ret : type_expr;
309309 }
310310311311+type filter_arrow_failure =
312312+ | Unification_error of Errortrace.unification_error
313313+ | Label_mismatch of
314314+ { got : arg_label
315315+ ; expected : arg_label
316316+ ; expected_type : type_expr
317317+ }
318318+ | Not_a_function
319319+311320val filter_arrow: Env.t -> type_expr -> arg_label -> param_hole:bool ->
312312- filtered_arrow
321321+ (filtered_arrow, filter_arrow_failure) result
313322 (* A special case of unification with [l:'a -> 'b]. If [param_hole] is
314323 true then ['a] might be initialized with a [Tvar _] hole to be filled
315324 later by a [Tpoly _].
···340349 (* Replaces all the variables of a type by a univar. *)
341350342351(* Exceptions for special cases of unify *)
343343-344344-type filter_arrow_failure =
345345- | Unification_error of Errortrace.unification_error
346346- | Label_mismatch of
347347- { got : arg_label
348348- ; expected : arg_label
349349- ; expected_type : type_expr
350350- }
351351- | Not_a_function
352352-353353-exception Filter_arrow_failed of filter_arrow_failure
354352355353type filter_method_failure =
356354 | Unification_error of Errortrace.unification_error
+16-12
typing/typecore.ml
···33223322 | Some spat -> check_poly_constraint spat env label
33233323 in
33243324 let { ty_param; ty_ret } =
33253325- try filter_arrow env ty_expected label ~param_hole:has_poly
33263326- with Filter_arrow_failed err ->
33253325+ match filter_arrow env ty_expected label ~param_hole:has_poly with
33263326+ | Ok filtered_arrow -> filtered_arrow
33273327+ | Error err ->
33273328 let loc_fun, ty_fun = in_function in
33283329 let err =
33293330 error_of_filter_arrow_failure ~explanation:None ty_fun err ~first
···40854086 (* Assert that [ty] is a function, and return its return type. *)
40864087 let filter_ty_ret_exn ty arg_label ~param_hole =
40874088 match filter_arrow env ty arg_label ~param_hole with
40884088- | { ty_ret; _ } -> ty_ret
40894089- | exception (Filter_arrow_failed error) ->
40894089+ | Ok { ty_ret; _ } -> ty_ret
40904090+ | Error error ->
40904091 let trace =
40914092 match error with
40924093 | Unification_error trace -> trace
···52565257 with_local_level_generalize_structure_if separate begin fun () ->
52575258 (* If [has_poly] is true then we rely on the later call to type_pat to
52585259 enforce the invariant that the parameter type be a [Tpoly] node *)
52595259- try filter_arrow env (instance ty_expected) arg_label ~param_hole:has_poly
52605260- with Filter_arrow_failed err ->
52615261- let err =
52625262- error_of_filter_arrow_failure ~explanation ty_fun err ~first
52635263- in
52645264- raise (Error(loc, env, err))
52605260+ match
52615261+ filter_arrow env (instance ty_expected) arg_label ~param_hole:has_poly
52625262+ with
52635263+ | Ok filtered_arrow -> filtered_arrow
52645264+ | Error err ->
52655265+ let err =
52665266+ error_of_filter_arrow_failure ~explanation ty_fun err ~first
52675267+ in
52685268+ raise (Error(loc, env, err))
52655269 end
52665270 in
52675271 if !Clflags.principal
···60086012 let exception Filter_arrow_mono_failed in
60096013 let filter_arrow_mono env t l =
60106014 match filter_arrow env t l ~param_hole:false with
60116011- | exception Filter_arrow_failed _ -> raise Filter_arrow_mono_failed
60126012- | {ty_param; _} as farr ->
60156015+ | Error _ -> raise Filter_arrow_mono_failed
60166016+ | Ok ({ty_param; _} as farr) ->
60136017 match tpoly_get_mono_opt ty_param with
60146018 | None -> raise Filter_arrow_mono_failed
60156019 | Some ty_param -> { farr with ty_param }