@@ -678,13 +678,19 @@ let extract_option_type env ty =
678
678
Tconstr (path , [ty ], _ ) when Path. same path Predef. path_option -> ty
679
679
| _ -> assert false
680
680
681
+ let protect_expansion env ty =
682
+ if Env. has_local_constraints env then generic_instance ty else ty
683
+
681
684
type record_extraction_result =
682
685
| Record_type of Path .t * Path .t * Types .label_declaration list * record_representation
683
686
| Not_a_record_type
684
687
| Maybe_a_record_type
685
688
689
+ let extract_concrete_typedecl_protected env ty =
690
+ extract_concrete_typedecl env (protect_expansion env ty)
691
+
686
692
let extract_concrete_record env ty =
687
- match extract_concrete_typedecl env ty with
693
+ match extract_concrete_typedecl_protected env ty with
688
694
| Typedecl (p0 , p , {type_kind =Type_record (fields , repres )} ) ->
689
695
Record_type (p0, p, fields, repres)
690
696
| Has_no_typedecl | Typedecl (_ , _ , _ ) -> Not_a_record_type
@@ -696,7 +702,7 @@ type variant_extraction_result =
696
702
| Maybe_a_variant_type
697
703
698
704
let extract_concrete_variant env ty =
699
- match extract_concrete_typedecl env ty with
705
+ match extract_concrete_typedecl_protected env ty with
700
706
| Typedecl (p0 , p , {type_kind =Type_variant (cstrs , _ )} ) ->
701
707
Variant_type (p0, p, cstrs)
702
708
| Typedecl (p0 , p , {type_kind =Type_open } ) ->
@@ -3342,6 +3348,12 @@ let collect_unknown_apply_args env funct ty_fun mode_fun rev_args sargs ret_tvar
3342
3348
let collect_apply_args env funct ignore_labels ty_fun ty_fun0 mode_fun sargs ret_tvar =
3343
3349
let warned = ref false in
3344
3350
let rec loop ty_fun ty_fun0 mode_fun rev_args sargs =
3351
+ let type_unknown_args () =
3352
+ (* We're not looking at a *known* function type anymore, or there are no
3353
+ arguments left. *)
3354
+ collect_unknown_apply_args env funct ty_fun0 mode_fun rev_args sargs ret_tvar
3355
+ in
3356
+ if sargs = [] then type_unknown_args () else
3345
3357
let ty_fun' = expand_head env ty_fun in
3346
3358
match get_desc ty_fun', get_desc (expand_head env ty_fun0), sargs with
3347
3359
| Tarrow (ad, ty_arg, ty_ret, com),
@@ -3426,9 +3438,7 @@ let collect_apply_args env funct ignore_labels ty_fun ty_fun0 mode_fun sargs ret
3426
3438
in
3427
3439
loop ty_ret ty_ret0 mode_ret ((l, arg) :: rev_args) remaining_sargs
3428
3440
| _ ->
3429
- (* We're not looking at a *known* function type anymore, or there are no
3430
- arguments left. *)
3431
- collect_unknown_apply_args env funct ty_fun0 mode_fun rev_args sargs ret_tvar
3441
+ type_unknown_args ()
3432
3442
in
3433
3443
loop ty_fun ty_fun0 mode_fun [] sargs
3434
3444
@@ -4537,7 +4547,7 @@ and type_expect_
4537
4547
| Pexp_constant (Pconst_string (str , _ , _ ) as cst ) ->
4538
4548
let cst = constant_or_raise env loc cst in
4539
4549
(* Terrible hack for format strings *)
4540
- let ty_exp = expand_head env ty_expected in
4550
+ let ty_exp = expand_head env (protect_expansion env ty_expected) in
4541
4551
let fmt6_path =
4542
4552
Path. (Pdot (Pident (Ident. create_persistent " CamlinternalFormatBasics" ),
4543
4553
" format6" ))
@@ -4973,10 +4983,11 @@ and type_expect_
4973
4983
sarg ty_expected_explained sexp.pexp_attributes
4974
4984
| Pexp_variant (l , sarg ) ->
4975
4985
(* Keep sharing *)
4986
+ let ty_expected1 = protect_expansion env ty_expected in
4976
4987
let ty_expected0 = instance ty_expected in
4977
4988
let argument_mode = mode_subcomponent expected_mode in
4978
4989
begin try match
4979
- sarg, get_desc (expand_head env ty_expected ),
4990
+ sarg, get_desc (expand_head env ty_expected1 ),
4980
4991
get_desc (expand_head env ty_expected0)
4981
4992
with
4982
4993
| Some sarg , Tvariant row , Tvariant row0 ->
@@ -5746,7 +5757,7 @@ and type_expect_
5746
5757
| Pexp_poly (sbody , sty ) ->
5747
5758
if ! Clflags. principal then begin_def () ;
5748
5759
let ty, cty =
5749
- match sty with None -> ty_expected, None
5760
+ match sty with None -> protect_expansion env ty_expected, None
5750
5761
| Some sty ->
5751
5762
let sty = Ast_helper.Typ. force_poly sty in
5752
5763
let cty =
@@ -5796,7 +5807,8 @@ and type_expect_
5796
5807
match get_desc (Ctype. expand_head env (instance ty_expected)) with
5797
5808
Tpackage (p , fl ) ->
5798
5809
if ! Clflags. principal &&
5799
- get_level (Ctype. expand_head env ty_expected)
5810
+ get_level (Ctype. expand_head env
5811
+ (protect_expansion env ty_expected))
5800
5812
< Btype. generic_level
5801
5813
then
5802
5814
Location. prerr_warning loc
@@ -6684,7 +6696,7 @@ and type_argument ?explanation ?recarg env (mode : expected_mode) sarg
6684
6696
(lv <> generic_level || get_level ty_fun' <> generic_level)
6685
6697
and ty_fun = instance ty_fun' in
6686
6698
let marg, ty_arg, mret, ty_res =
6687
- match get_desc (expand_head env ty_expected' ) with
6699
+ match get_desc (expand_head env ty_expected) with
6688
6700
Tarrow ((Nolabel,marg ,mret ),ty_arg ,ty_res ,_ ) ->
6689
6701
marg, ty_arg, mret, ty_res
6690
6702
| _ -> assert false
@@ -7182,6 +7194,8 @@ and type_cases
7182
7194
) half_typed_cases;
7183
7195
(* type bodies *)
7184
7196
let in_function = if List. length caselist = 1 then in_function else None in
7197
+ let ty_res' = instance ty_res in
7198
+ if ! Clflags. principal then begin_def () ;
7185
7199
let cases =
7186
7200
List. map
7187
7201
(fun { typed_pat = pat ; branch_env = ext_env ;
@@ -7200,14 +7214,8 @@ and type_cases
7200
7214
~check_as: (fun s -> Warnings. Unused_var s)
7201
7215
in
7202
7216
let ext_env = add_module_variables ext_env mvs in
7203
- let ty_res' =
7204
- if ! Clflags. principal then begin
7205
- begin_def () ;
7206
- let ty = instance ~partial: true ty_res in
7207
- end_def () ;
7208
- generalize_structure ty; ty
7209
- end
7210
- else if contains_gadt then
7217
+ let ty_expected =
7218
+ if contains_gadt && not ! Clflags. principal then
7211
7219
(* allow propagation from preceding branches *)
7212
7220
correct_levels ty_res
7213
7221
else ty_res in
@@ -7221,20 +7229,17 @@ and type_cases
7221
7229
in
7222
7230
let exp =
7223
7231
type_expect ?in_function ext_env emode
7224
- pc_rhs (mk_expected ?explanation ty_res' )
7232
+ pc_rhs (mk_expected ?explanation ty_expected )
7225
7233
in
7226
7234
{
7227
7235
c_lhs = pat;
7228
7236
c_guard = guard;
7229
- c_rhs = {exp with exp_type = instance ty_res'}
7237
+ c_rhs = {exp with exp_type = ty_res'}
7230
7238
}
7231
7239
)
7232
7240
half_typed_cases
7233
7241
in
7234
- if ! Clflags. principal || does_contain_gadt then begin
7235
- let ty_res' = instance ty_res in
7236
- List. iter (fun c -> unify_exp env c.c_rhs ty_res') cases
7237
- end ;
7242
+ if ! Clflags. principal then end_def () ;
7238
7243
let do_init = may_contain_gadts || needs_exhaust_check in
7239
7244
let ty_arg_check =
7240
7245
if do_init then
@@ -7281,7 +7286,7 @@ and type_cases
7281
7286
if create_inner_level then begin
7282
7287
end_def () ;
7283
7288
(* Ensure that existential types do not escape *)
7284
- unify_exp_types loc env (instance ty_res)
7289
+ unify_exp_types loc env ty_res'
7285
7290
(newvar (Layout. any ~why: Dummy_layout ));
7286
7291
end ;
7287
7292
cases, partial
0 commit comments