Skip to content

Commit 83ef108

Browse files
authored
Remove coercion with 2 types, which is only supported in ml syntax. (rescript-lang#6829)
* Remove coercion with 2 types, which is only supported in ml syntax. There are two forms of type coercion: 1 `e: t0 :> t` 2 `e :> t` The first form was never supported in .res syntax, and is now removed from parsed and typed tree. That said, coercion 1 is the only one that ever supported coercion with free variables. So this is subject to more investigation. See rescript-lang#6828 * Cleanup and indent. * Make Texp_coerce compatible with old runtime representation. By adding a unit argument to the payload.
1 parent 2a0f3ba commit 83ef108

24 files changed

+82
-147
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
- Refactor uppercase exotic ident handling. https://github.com/rescript-lang/rescript-compiler/pull/6779
4343
- Fix `-nostdlib` internal compiler option. https://github.com/rescript-lang/rescript-compiler/pull/6824
4444
- Remove a number of ast nodes never populated by the .res parser, and resulting dead code. https://github.com/rescript-lang/rescript-compiler/pull/6830
45+
- Remove coercion with 2 types from internal representation. Coercion `e : t1 :> t2` was only supported in `.ml` syntax and never by the `.res` parser. https://github.com/rescript-lang/rescript-compiler/pull/6829
4546

4647
#### :nail_care: Polish
4748

jscomp/frontend/bs_ast_mapper.ml

+2-4
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,8 @@ module E = struct
349349
| Pexp_for (p, e1, e2, d, e3) ->
350350
for_ ~loc ~attrs (sub.pat sub p) (sub.expr sub e1) (sub.expr sub e2) d
351351
(sub.expr sub e3)
352-
| Pexp_coerce (e, t1, t2) ->
353-
coerce ~loc ~attrs (sub.expr sub e)
354-
(map_opt (sub.typ sub) t1)
355-
(sub.typ sub t2)
352+
| Pexp_coerce (e, (), t2) ->
353+
coerce ~loc ~attrs (sub.expr sub e) (sub.typ sub t2)
356354
| Pexp_constraint (e, t) ->
357355
constraint_ ~loc ~attrs (sub.expr sub e) (sub.typ sub t)
358356
| Pexp_send (e, s) -> send ~loc ~attrs (sub.expr sub e) (map_loc sub s)

jscomp/ml/ast_helper.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ module Exp = struct
171171
let while_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_while (a, b))
172172
let for_ ?loc ?attrs a b c d e = mk ?loc ?attrs (Pexp_for (a, b, c, d, e))
173173
let constraint_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_constraint (a, b))
174-
let coerce ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_coerce (a, b, c))
174+
let coerce ?loc ?attrs a c = mk ?loc ?attrs (Pexp_coerce (a, (), c))
175175
let send ?loc ?attrs a b = mk ?loc ?attrs (Pexp_send (a, b))
176176
let new_ ?loc ?attrs a = mk ?loc ?attrs (Pexp_new a)
177177
let setinstvar ?loc ?attrs a b = mk ?loc ?attrs (Pexp_setinstvar (a, b))

jscomp/ml/ast_helper.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ module Exp:
146146
-> expression
147147
val for_: ?loc:loc -> ?attrs:attrs -> pattern -> expression -> expression
148148
-> direction_flag -> expression -> expression
149-
val coerce: ?loc:loc -> ?attrs:attrs -> expression -> core_type option
149+
val coerce: ?loc:loc -> ?attrs:attrs -> expression
150150
-> core_type -> expression
151151
val constraint_: ?loc:loc -> ?attrs:attrs -> expression -> core_type
152152
-> expression

jscomp/ml/ast_iterator.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ module E = struct
303303
| Pexp_for (p, e1, e2, _d, e3) ->
304304
sub.pat sub p; sub.expr sub e1; sub.expr sub e2;
305305
sub.expr sub e3
306-
| Pexp_coerce (e, t1, t2) ->
307-
sub.expr sub e; iter_opt (sub.typ sub) t1;
306+
| Pexp_coerce (e, (), t2) ->
307+
sub.expr sub e;
308308
sub.typ sub t2
309309
| Pexp_constraint (e, t) ->
310310
sub.expr sub e; sub.typ sub t

jscomp/ml/ast_mapper.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ module E = struct
314314
| Pexp_for (p, e1, e2, d, e3) ->
315315
for_ ~loc ~attrs (sub.pat sub p) (sub.expr sub e1) (sub.expr sub e2) d
316316
(sub.expr sub e3)
317-
| Pexp_coerce (e, t1, t2) ->
318-
coerce ~loc ~attrs (sub.expr sub e) (map_opt (sub.typ sub) t1)
317+
| Pexp_coerce (e, (), t2) ->
318+
coerce ~loc ~attrs (sub.expr sub e)
319319
(sub.typ sub t2)
320320
| Pexp_constraint (e, t) ->
321321
constraint_ ~loc ~attrs (sub.expr sub e) (sub.typ sub t)

jscomp/ml/depend.ml

+1-2
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,8 @@ let rec add_expr bv exp =
218218
| Pexp_while(e1, e2) -> add_expr bv e1; add_expr bv e2
219219
| Pexp_for( _, e1, e2, _, e3) ->
220220
add_expr bv e1; add_expr bv e2; add_expr bv e3
221-
| Pexp_coerce(e1, oty2, ty3) ->
221+
| Pexp_coerce(e1, (), ty3) ->
222222
add_expr bv e1;
223-
add_opt add_type bv oty2;
224223
add_type bv ty3
225224
| Pexp_constraint(e1, ty2) ->
226225
add_expr bv e1;

jscomp/ml/parser.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ let mkstrexp e attrs =
241241
let mkexp_constraint e (t1, t2) =
242242
match t1, t2 with
243243
| Some t, None -> ghexp(Pexp_constraint(e, t))
244-
| _, Some t -> ghexp(Pexp_coerce(e, t1, t))
244+
| _, Some t -> ghexp(Pexp_coerce(e, (), t))
245245
| None, None -> assert false
246246

247247
let mkexp_opt_constraint e = function
@@ -6554,7 +6554,7 @@ let yyact = [|
65546554
# 648 "ml/parser.mly"
65556555
( mkmod ~attrs:_3
65566556
(Pmod_unpack(
6557-
ghexp(Pexp_coerce(_4, Some(ghtyp(Ptyp_package _6)),
6557+
ghexp(Pexp_coerce(_4, (),
65586558
ghtyp(Ptyp_package _8))))) )
65596559
# 6565 "ml/parser.ml"
65606560
: 'paren_module_expr))
@@ -6566,7 +6566,7 @@ let yyact = [|
65666566
# 653 "ml/parser.mly"
65676567
( mkmod ~attrs:_3
65686568
(Pmod_unpack(
6569-
ghexp(Pexp_coerce(_4, None, ghtyp(Ptyp_package _6))))) )
6569+
ghexp(Pexp_coerce(_4, (), ghtyp(Ptyp_package _6))))) )
65706570
# 6576 "ml/parser.ml"
65716571
: 'paren_module_expr))
65726572
; (fun __caml_parser_env ->

jscomp/ml/parser.mly

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ let mkstrexp e attrs =
135135
let mkexp_constraint e (t1, t2) =
136136
match t1, t2 with
137137
| Some t, None -> ghexp(Pexp_constraint(e, t))
138-
| _, Some t -> ghexp(Pexp_coerce(e, t1, t))
138+
| _, Some t -> ghexp(Pexp_coerce(e, (), t))
139139
| None, None -> assert false
140140

141141
let mkexp_opt_constraint e = function

jscomp/ml/parsetree.ml

+1-2
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,8 @@ and expression_desc =
307307
*)
308308
| Pexp_constraint of expression * core_type
309309
(* (E : T) *)
310-
| Pexp_coerce of expression * core_type option * core_type
310+
| Pexp_coerce of expression * unit * core_type
311311
(* (E :> T) (None, T)
312-
(E : T0 :> T) (Some T0, T)
313312
*)
314313
| Pexp_send of expression * label loc
315314
(* E # m *)

jscomp/ml/pprintast.ml

+2-3
Original file line numberDiff line numberDiff line change
@@ -707,9 +707,8 @@ and simple_expr ctxt f x =
707707
pp f "@[<hov2>(%a)@]" (list (simple_expr ctxt) ~sep:",@;") l
708708
| Pexp_constraint (e, ct) ->
709709
pp f "(%a : %a)" (expression ctxt) e (core_type ctxt) ct
710-
| Pexp_coerce (e, cto1, ct) ->
711-
pp f "(%a%a :> %a)" (expression ctxt) e
712-
(option (core_type ctxt) ~first:" : " ~last:" ") cto1 (* no sep hint*)
710+
| Pexp_coerce (e, (), ct) ->
711+
pp f "(%a :> %a)" (expression ctxt) e
713712
(core_type ctxt) ct
714713
| Pexp_variant (l, None) -> pp f "`%s" l
715714
| Pexp_record (l, eo) ->

jscomp/ml/printast.ml

+1-2
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,9 @@ and expression i ppf x =
321321
line i ppf "Pexp_constraint\n";
322322
expression i ppf e;
323323
core_type i ppf ct;
324-
| Pexp_coerce (e, cto1, cto2) ->
324+
| Pexp_coerce (e, (), cto2) ->
325325
line i ppf "Pexp_coerce\n";
326326
expression i ppf e;
327-
option i core_type ppf cto1;
328327
core_type i ppf cto2;
329328
| Pexp_send (e, s) ->
330329
line i ppf "Pexp_send \"%s\"\n" s.txt;

jscomp/ml/printtyped.ml

+1-2
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,9 @@ and expression_extra i ppf x attrs =
270270
line i ppf "Texp_constraint\n";
271271
attributes i ppf attrs;
272272
core_type i ppf ct;
273-
| Texp_coerce (cto1, cto2) ->
273+
| Texp_coerce ((), cto2) ->
274274
line i ppf "Texp_coerce\n";
275275
attributes i ppf attrs;
276-
option i core_type ppf cto1;
277276
core_type i ppf cto2;
278277
| Texp_open (ovf, m, _, _) ->
279278
line i ppf "Texp_open %a \"%a\"\n" fmt_override_flag ovf fmt_path m;

jscomp/ml/tast_mapper.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ let expr sub x =
190190
let extra = function
191191
| Texp_constraint cty ->
192192
Texp_constraint (sub.typ sub cty)
193-
| Texp_coerce (cty1, cty2) ->
194-
Texp_coerce (opt (sub.typ sub) cty1, sub.typ sub cty2)
193+
| Texp_coerce ((), cty2) ->
194+
Texp_coerce ((), (sub.typ sub cty2))
195195
| Texp_open (ovf, path, loc, env) ->
196196
Texp_open (ovf, path, loc, sub.env sub env)
197197
| Texp_newtype _ as d -> d

jscomp/ml/typecore.ml

+46-71
Original file line numberDiff line numberDiff line change
@@ -1709,13 +1709,13 @@ let rec type_approx env sexp =
17091709
raise(Error(sexp.pexp_loc, env, Expr_type_clash (trace, None)))
17101710
end;
17111711
ty1
1712-
| Pexp_coerce (e, sty1, sty2) ->
1712+
| Pexp_coerce (e, (), sty2) ->
17131713
let approx_ty_opt = function
17141714
| None -> newvar ()
17151715
| Some sty -> approx_type env sty
17161716
in
17171717
let ty = type_approx env e
1718-
and ty1 = approx_ty_opt sty1
1718+
and ty1 = approx_ty_opt None
17191719
and ty2 = approx_type env sty2 in
17201720
begin try unify env ty ty1 with Unify trace ->
17211721
raise(Error(sexp.pexp_loc, env, Expr_type_clash (trace, None)))
@@ -2571,87 +2571,62 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg=Rejected) env sexp ty
25712571
exp_extra =
25722572
(Texp_constraint cty, loc, sexp.pexp_attributes) :: arg.exp_extra;
25732573
}
2574-
| Pexp_coerce(sarg, sty, sty') ->
2574+
| Pexp_coerce(sarg, (), sty') ->
25752575
let separate = true in (* always separate, 1% slowdown for lablgtk *)
25762576
(* Also see PR#7199 for a problem with the following:
25772577
let separate = Env.has_local_constraints env in*)
2578-
let (arg, ty',cty,cty') =
2579-
match sty with
2580-
| None ->
2581-
let (cty', force) =
2582-
Typetexp.transl_simple_type_delayed env sty'
2583-
in
2584-
let ty' = cty'.ctyp_type in
2585-
if separate then begin_def ();
2586-
let arg = type_exp env sarg in
2587-
let gen =
2588-
if separate then begin
2589-
end_def ();
2590-
let tv = newvar () in
2591-
let gen = generalizable tv.level arg.exp_type in
2592-
(try unify_var env tv arg.exp_type with Unify trace ->
2593-
raise(Error(arg.exp_loc, env, Expr_type_clash (trace, type_clash_context))));
2594-
gen
2595-
end else true
2596-
in
2597-
begin match arg.exp_desc, !self_coercion, (repr ty').desc with
2598-
| _ when free_variables ~env arg.exp_type = []
2599-
&& free_variables ~env ty' = [] ->
2600-
if not gen && (* first try a single coercion *)
2601-
let snap = snapshot () in
2602-
let ty, _b = enlarge_type env ty' in
2603-
try
2604-
force (); Ctype.unify env arg.exp_type ty; true
2605-
with Unify _ ->
2606-
backtrack snap; false
2607-
then ()
2608-
else begin try
2609-
let force' = subtype env arg.exp_type ty' in
2610-
force (); force' ();
2611-
with Subtype (tr1, tr2) ->
2612-
(* prerr_endline "coercion failed"; *)
2613-
raise(Error(loc, env, Not_subtype(tr1, tr2)))
2614-
end;
2615-
| _ ->
2616-
let ty, b = enlarge_type env ty' in
2617-
force ();
2618-
begin try Ctype.unify env arg.exp_type ty with Unify trace ->
2619-
raise(Error(sarg.pexp_loc, env,
2620-
Coercion_failure(ty', full_expand env ty', trace, b)))
2621-
end
2622-
end;
2623-
(arg, ty', None, cty')
2624-
| Some sty ->
2625-
if separate then begin_def ();
2626-
let (cty, force) =
2627-
Typetexp.transl_simple_type_delayed env sty
2628-
and (cty', force') =
2629-
Typetexp.transl_simple_type_delayed env sty'
2630-
in
2631-
let ty = cty.ctyp_type in
2632-
let ty' = cty'.ctyp_type in
2633-
begin try
2634-
let force'' = subtype env ty ty' in
2635-
force (); force' (); force'' ()
2636-
with Subtype (tr1, tr2) ->
2637-
raise(Error(loc, env, Not_subtype(tr1, tr2)))
2638-
end;
2578+
let (arg, ty',cty') =
2579+
let (cty', force) =
2580+
Typetexp.transl_simple_type_delayed env sty'
2581+
in
2582+
let ty' = cty'.ctyp_type in
2583+
if separate then begin_def ();
2584+
let arg = type_exp env sarg in
2585+
let gen =
26392586
if separate then begin
26402587
end_def ();
2641-
generalize_structure ty;
2642-
generalize_structure ty';
2643-
(type_argument env sarg ty (instance env ty),
2644-
instance env ty', Some cty, cty')
2645-
end else
2646-
(type_argument env sarg ty ty, ty', Some cty, cty')
2588+
let tv = newvar () in
2589+
let gen = generalizable tv.level arg.exp_type in
2590+
(try unify_var env tv arg.exp_type with Unify trace ->
2591+
raise(Error(arg.exp_loc, env, Expr_type_clash (trace, type_clash_context))));
2592+
gen
2593+
end else true
2594+
in
2595+
begin match arg.exp_desc, !self_coercion, (repr ty').desc with
2596+
| _ when free_variables ~env arg.exp_type = []
2597+
&& free_variables ~env ty' = [] ->
2598+
if not gen && (* first try a single coercion *)
2599+
let snap = snapshot () in
2600+
let ty, _b = enlarge_type env ty' in
2601+
try
2602+
force (); Ctype.unify env arg.exp_type ty; true
2603+
with Unify _ ->
2604+
backtrack snap; false
2605+
then ()
2606+
else begin try
2607+
let force' = subtype env arg.exp_type ty' in
2608+
force (); force' ();
2609+
with Subtype (tr1, tr2) ->
2610+
(* prerr_endline "coercion failed"; *)
2611+
raise(Error(loc, env, Not_subtype(tr1, tr2)))
2612+
end;
2613+
| _ ->
2614+
let ty, b = enlarge_type env ty' in
2615+
force ();
2616+
begin try Ctype.unify env arg.exp_type ty with Unify trace ->
2617+
raise(Error(sarg.pexp_loc, env,
2618+
Coercion_failure(ty', full_expand env ty', trace, b)))
2619+
end
2620+
end;
2621+
(arg, ty', cty')
26472622
in
26482623
rue {
26492624
exp_desc = arg.exp_desc;
26502625
exp_loc = arg.exp_loc;
26512626
exp_type = ty';
26522627
exp_attributes = arg.exp_attributes;
26532628
exp_env = env;
2654-
exp_extra = (Texp_coerce (cty, cty'), loc, sexp.pexp_attributes) ::
2629+
exp_extra = (Texp_coerce ((), cty'), loc, sexp.pexp_attributes) ::
26552630
arg.exp_extra;
26562631
}
26572632
| Pexp_send (e, {txt=met}) ->

jscomp/ml/typedtree.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ and expression =
6868

6969
and exp_extra =
7070
| Texp_constraint of core_type
71-
| Texp_coerce of core_type option * core_type
71+
| Texp_coerce of unit * core_type
7272
| Texp_open of override_flag * Path.t * Longident.t loc * Env.t
7373
| Texp_poly of core_type option
7474
| Texp_newtype of string

jscomp/ml/typedtree.mli

+2-3
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ and expression =
119119
and exp_extra =
120120
| Texp_constraint of core_type
121121
(** E : T *)
122-
| Texp_coerce of core_type option * core_type
123-
(** E :> T [Texp_coerce (None, T)]
124-
E : T0 :> T [Texp_coerce (Some T0, T)]
122+
| Texp_coerce of unit * core_type
123+
(** E :> T [Texp_coerce T]
125124
*)
126125
| Texp_open of override_flag * Path.t * Longident.t loc * Env.t
127126
(** let open[!] M in [Texp_open (!, P, M, env)]

jscomp/ml/typedtreeIter.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ module MakeIterator(Iter : IteratorArgument) : sig
236236
match cstr with
237237
Texp_constraint ct ->
238238
iter_core_type ct
239-
| Texp_coerce (cty1, cty2) ->
240-
option iter_core_type cty1; iter_core_type cty2
239+
| Texp_coerce ((), cty2) ->
240+
iter_core_type cty2
241241
| Texp_open _ -> ()
242242
| Texp_poly cto -> option iter_core_type cto
243243
| Texp_newtype _ -> ())

jscomp/ml/typedtreeMap.ml

+2-5
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,8 @@ module MakeMap(Map : MapArgument) = struct
362362
match desc with
363363
| Texp_constraint ct ->
364364
Texp_constraint (map_core_type ct), loc, attrs
365-
| Texp_coerce (None, ct) ->
366-
Texp_coerce (None, map_core_type ct), loc, attrs
367-
| Texp_coerce (Some ct1, ct2) ->
368-
Texp_coerce (Some (map_core_type ct1),
369-
map_core_type ct2), loc, attrs
365+
| Texp_coerce ((), ct) ->
366+
Texp_coerce ((), map_core_type ct), loc, attrs
370367
| Texp_poly (Some ct) ->
371368
Texp_poly (Some ( map_core_type ct )), loc, attrs
372369
| Texp_newtype _

jscomp/ml/untypeast.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ let exp_extra sub (extra, loc, attrs) sexp =
305305
let attrs = sub.attributes sub attrs in
306306
let desc =
307307
match extra with
308-
Texp_coerce (cty1, cty2) ->
308+
Texp_coerce ((), cty2) ->
309309
Pexp_coerce (sexp,
310-
map_opt (sub.typ sub) cty1,
310+
(),
311311
sub.typ sub cty2)
312312
| Texp_constraint cty ->
313313
Pexp_constraint (sexp, sub.typ sub cty)

jscomp/syntax/src/res_ast_debugger.ml

+2-10
Original file line numberDiff line numberDiff line change
@@ -678,16 +678,8 @@ module SexpAst = struct
678678
| Pexp_constraint (expr, typexpr) ->
679679
Sexp.list
680680
[Sexp.atom "Pexp_constraint"; expression expr; core_type typexpr]
681-
| Pexp_coerce (expr, opt_typ, typexpr) ->
682-
Sexp.list
683-
[
684-
Sexp.atom "Pexp_coerce";
685-
expression expr;
686-
(match opt_typ with
687-
| None -> Sexp.atom "None"
688-
| Some typ -> Sexp.list [Sexp.atom "Some"; core_type typ]);
689-
core_type typexpr;
690-
]
681+
| Pexp_coerce (expr, (), typexpr) ->
682+
Sexp.list [Sexp.atom "Pexp_coerce"; expression expr; core_type typexpr]
691683
| Pexp_send _ -> Sexp.list [Sexp.atom "Pexp_send"]
692684
| Pexp_new _ -> Sexp.list [Sexp.atom "Pexp_new"]
693685
| Pexp_setinstvar _ -> Sexp.list [Sexp.atom "Pexp_setinstvar"]

0 commit comments

Comments
 (0)