Skip to content

Commit ed4f755

Browse files
committed
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 #6828
1 parent 2a0f3ba commit ed4f755

23 files changed

+43
-107
lines changed

jscomp/frontend/bs_ast_mapper.ml

Lines changed: 2 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 8 additions & 31 deletions
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,13 +2571,13 @@ 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 ->
2578+
let (arg, ty',cty') =
2579+
match () with
2580+
| () ->
25812581
let (cty', force) =
25822582
Typetexp.transl_simple_type_delayed env sty'
25832583
in
@@ -2620,38 +2620,15 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg=Rejected) env sexp ty
26202620
Coercion_failure(ty', full_expand env ty', trace, b)))
26212621
end
26222622
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;
2639-
if separate then begin
2640-
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')
2623+
(arg, ty', cty')
26472624
in
26482625
rue {
26492626
exp_desc = arg.exp_desc;
26502627
exp_loc = arg.exp_loc;
26512628
exp_type = ty';
26522629
exp_attributes = arg.exp_attributes;
26532630
exp_env = env;
2654-
exp_extra = (Texp_coerce (cty, cty'), loc, sexp.pexp_attributes) ::
2631+
exp_extra = (Texp_coerce cty', loc, sexp.pexp_attributes) ::
26552632
arg.exp_extra;
26562633
}
26572634
| Pexp_send (e, {txt=met}) ->

jscomp/ml/typedtree.ml

Lines changed: 1 addition & 1 deletion
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 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

Lines changed: 2 additions & 3 deletions
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 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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 5 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 10 deletions
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"]

jscomp/syntax/src/res_comments_table.ml

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,27 +1005,12 @@ and walk_expression expr t comments =
10051005
attach t.leading expr.pexp_loc leading;
10061006
walk_expression expr t inside;
10071007
attach t.trailing expr.pexp_loc trailing
1008-
| Pexp_coerce (expr, opt_typexpr, typexpr) ->
1008+
| Pexp_coerce (expr, (), typexpr) ->
10091009
let leading, inside, trailing = partition_by_loc comments expr.pexp_loc in
10101010
attach t.leading expr.pexp_loc leading;
10111011
walk_expression expr t inside;
10121012
let after_expr, rest = partition_adjacent_trailing expr.pexp_loc trailing in
10131013
attach t.trailing expr.pexp_loc after_expr;
1014-
let rest =
1015-
match opt_typexpr with
1016-
| Some typexpr ->
1017-
let leading, inside, trailing =
1018-
partition_by_loc comments typexpr.ptyp_loc
1019-
in
1020-
attach t.leading typexpr.ptyp_loc leading;
1021-
walk_core_type typexpr t inside;
1022-
let after_typ, rest =
1023-
partition_adjacent_trailing typexpr.ptyp_loc trailing
1024-
in
1025-
attach t.trailing typexpr.ptyp_loc after_typ;
1026-
rest
1027-
| None -> rest
1028-
in
10291014
let leading, inside, trailing = partition_by_loc rest typexpr.ptyp_loc in
10301015
attach t.leading typexpr.ptyp_loc leading;
10311016
walk_core_type typexpr t inside;

jscomp/syntax/src/res_core.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,7 @@ and parse_coerced_expr ~(expr : Parsetree.expression) p =
18601860
Parser.expect ColonGreaterThan p;
18611861
let typ = parse_typ_expr p in
18621862
let loc = mk_loc expr.pexp_loc.loc_start p.prev_end_pos in
1863-
Ast_helper.Exp.coerce ~loc expr None typ
1863+
Ast_helper.Exp.coerce ~loc expr typ
18641864

18651865
and parse_constrained_or_coerced_expr p =
18661866
let expr = parse_expr p in

jscomp/syntax/src/res_printer.ml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,17 +3300,10 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
33003300
]
33013301
| Pexp_function cases ->
33023302
Doc.concat [Doc.text "x => switch x "; print_cases ~state cases cmt_tbl]
3303-
| Pexp_coerce (expr, typ_opt, typ) ->
3303+
| Pexp_coerce (expr, (), typ) ->
33043304
let doc_expr = print_expression_with_comments ~state expr cmt_tbl in
33053305
let doc_typ = print_typ_expr ~state typ cmt_tbl in
3306-
let of_type =
3307-
match typ_opt with
3308-
| None -> Doc.nil
3309-
| Some typ1 ->
3310-
Doc.concat [Doc.text ": "; print_typ_expr ~state typ1 cmt_tbl]
3311-
in
3312-
Doc.concat
3313-
[Doc.lparen; doc_expr; of_type; Doc.text " :> "; doc_typ; Doc.rparen]
3306+
Doc.concat [Doc.lparen; doc_expr; Doc.text " :> "; doc_typ; Doc.rparen]
33143307
| Pexp_send (parent_expr, label) ->
33153308
let parent_doc =
33163309
let doc = print_expression_with_comments ~state parent_expr cmt_tbl in

0 commit comments

Comments
 (0)