Skip to content

Commit 73d4f00

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 fcdf7a9 commit 73d4f00

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
@@ -398,10 +398,8 @@ module E = struct
398398
| Pexp_for (p, e1, e2, d, e3) ->
399399
for_ ~loc ~attrs (sub.pat sub p) (sub.expr sub e1) (sub.expr sub e2) d
400400
(sub.expr sub e3)
401-
| Pexp_coerce (e, t1, t2) ->
402-
coerce ~loc ~attrs (sub.expr sub e)
403-
(map_opt (sub.typ sub) t1)
404-
(sub.typ sub t2)
401+
| Pexp_coerce (e, (), t2) ->
402+
coerce ~loc ~attrs (sub.expr sub e) (sub.typ sub t2)
405403
| Pexp_constraint (e, t) ->
406404
constraint_ ~loc ~attrs (sub.expr sub e) (sub.typ sub t)
407405
| 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
@@ -173,7 +173,7 @@ module Exp = struct
173173
let while_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_while (a, b))
174174
let for_ ?loc ?attrs a b c d e = mk ?loc ?attrs (Pexp_for (a, b, c, d, e))
175175
let constraint_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_constraint (a, b))
176-
let coerce ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_coerce (a, b, c))
176+
let coerce ?loc ?attrs a c = mk ?loc ?attrs (Pexp_coerce (a, (), c))
177177
let send ?loc ?attrs a b = mk ?loc ?attrs (Pexp_send (a, b))
178178
let new_ ?loc ?attrs a = mk ?loc ?attrs (Pexp_new a)
179179
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
@@ -147,7 +147,7 @@ module Exp:
147147
-> expression
148148
val for_: ?loc:loc -> ?attrs:attrs -> pattern -> expression -> expression
149149
-> direction_flag -> expression -> expression
150-
val coerce: ?loc:loc -> ?attrs:attrs -> expression -> core_type option
150+
val coerce: ?loc:loc -> ?attrs:attrs -> expression
151151
-> core_type -> expression
152152
val constraint_: ?loc:loc -> ?attrs:attrs -> expression -> core_type
153153
-> expression

jscomp/ml/ast_iterator.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ module E = struct
346346
| Pexp_for (p, e1, e2, _d, e3) ->
347347
sub.pat sub p; sub.expr sub e1; sub.expr sub e2;
348348
sub.expr sub e3
349-
| Pexp_coerce (e, t1, t2) ->
350-
sub.expr sub e; iter_opt (sub.typ sub) t1;
349+
| Pexp_coerce (e, (), t2) ->
350+
sub.expr sub e;
351351
sub.typ sub t2
352352
| Pexp_constraint (e, t) ->
353353
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
@@ -364,8 +364,8 @@ module E = struct
364364
| Pexp_for (p, e1, e2, d, e3) ->
365365
for_ ~loc ~attrs (sub.pat sub p) (sub.expr sub e1) (sub.expr sub e2) d
366366
(sub.expr sub e3)
367-
| Pexp_coerce (e, t1, t2) ->
368-
coerce ~loc ~attrs (sub.expr sub e) (map_opt (sub.typ sub) t1)
367+
| Pexp_coerce (e, (), t2) ->
368+
coerce ~loc ~attrs (sub.expr sub e)
369369
(sub.typ sub t2)
370370
| Pexp_constraint (e, t) ->
371371
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
@@ -245,9 +245,8 @@ let rec add_expr bv exp =
245245
| Pexp_while(e1, e2) -> add_expr bv e1; add_expr bv e2
246246
| Pexp_for( _, e1, e2, _, e3) ->
247247
add_expr bv e1; add_expr bv e2; add_expr bv e3
248-
| Pexp_coerce(e1, oty2, ty3) ->
248+
| Pexp_coerce(e1, (), ty3) ->
249249
add_expr bv e1;
250-
add_opt add_type bv oty2;
251250
add_type bv ty3
252251
| Pexp_constraint(e1, ty2) ->
253252
add_expr bv e1;

jscomp/ml/parser.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ let mkstrexp e attrs =
243243
let mkexp_constraint e (t1, t2) =
244244
match t1, t2 with
245245
| Some t, None -> ghexp(Pexp_constraint(e, t))
246-
| _, Some t -> ghexp(Pexp_coerce(e, t1, t))
246+
| _, Some t -> ghexp(Pexp_coerce(e, (), t))
247247
| None, None -> assert false
248248

249249
let mkexp_opt_constraint e = function
@@ -6559,7 +6559,7 @@ let yyact = [|
65596559
# 648 "ml/parser.mly"
65606560
( mkmod ~attrs:_3
65616561
(Pmod_unpack(
6562-
ghexp(Pexp_coerce(_4, Some(ghtyp(Ptyp_package _6)),
6562+
ghexp(Pexp_coerce(_4, (),
65636563
ghtyp(Ptyp_package _8))))) )
65646564
# 6565 "ml/parser.ml"
65656565
: 'paren_module_expr))
@@ -6571,7 +6571,7 @@ let yyact = [|
65716571
# 653 "ml/parser.mly"
65726572
( mkmod ~attrs:_3
65736573
(Pmod_unpack(
6574-
ghexp(Pexp_coerce(_4, None, ghtyp(Ptyp_package _6))))) )
6574+
ghexp(Pexp_coerce(_4, (), ghtyp(Ptyp_package _6))))) )
65756575
# 6576 "ml/parser.ml"
65766576
: 'paren_module_expr))
65776577
; (fun __caml_parser_env ->

jscomp/ml/parser.mly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ let mkstrexp e attrs =
137137
let mkexp_constraint e (t1, t2) =
138138
match t1, t2 with
139139
| Some t, None -> ghexp(Pexp_constraint(e, t))
140-
| _, Some t -> ghexp(Pexp_coerce(e, t1, t))
140+
| _, Some t -> ghexp(Pexp_coerce(e, (), t))
141141
| None, None -> assert false
142142

143143
let mkexp_opt_constraint e = function

jscomp/ml/parsetree.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,8 @@ and expression_desc =
310310
*)
311311
| Pexp_constraint of expression * core_type
312312
(* (E : T) *)
313-
| Pexp_coerce of expression * core_type option * core_type
313+
| Pexp_coerce of expression * unit * core_type
314314
(* (E :> T) (None, T)
315-
(E : T0 :> T) (Some T0, T)
316315
*)
317316
| Pexp_send of expression * label loc
318317
(* E # m *)

jscomp/ml/pprintast.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,8 @@ and simple_expr ctxt f x =
719719
pp f "@[<hov2>(%a)@]" (list (simple_expr ctxt) ~sep:",@;") l
720720
| Pexp_constraint (e, ct) ->
721721
pp f "(%a : %a)" (expression ctxt) e (core_type ctxt) ct
722-
| Pexp_coerce (e, cto1, ct) ->
723-
pp f "(%a%a :> %a)" (expression ctxt) e
724-
(option (core_type ctxt) ~first:" : " ~last:" ") cto1 (* no sep hint*)
722+
| Pexp_coerce (e, (), ct) ->
723+
pp f "(%a :> %a)" (expression ctxt) e
725724
(core_type ctxt) ct
726725
| Pexp_variant (l, None) -> pp f "`%s" l
727726
| Pexp_record (l, eo) ->

jscomp/ml/printast.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,9 @@ and expression i ppf x =
329329
line i ppf "Pexp_constraint\n";
330330
expression i ppf e;
331331
core_type i ppf ct;
332-
| Pexp_coerce (e, cto1, cto2) ->
332+
| Pexp_coerce (e, (), cto2) ->
333333
line i ppf "Pexp_coerce\n";
334334
expression i ppf e;
335-
option i core_type ppf cto1;
336335
core_type i ppf cto2;
337336
| Pexp_send (e, s) ->
338337
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
@@ -278,10 +278,9 @@ and expression_extra i ppf x attrs =
278278
line i ppf "Texp_constraint\n";
279279
attributes i ppf attrs;
280280
core_type i ppf ct;
281-
| Texp_coerce (cto1, cto2) ->
281+
| Texp_coerce cto2 ->
282282
line i ppf "Texp_coerce\n";
283283
attributes i ppf attrs;
284-
option i core_type ppf cto1;
285284
core_type i ppf cto2;
286285
| Texp_open (ovf, m, _, _) ->
287286
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
@@ -208,8 +208,8 @@ let expr sub x =
208208
let extra = function
209209
| Texp_constraint cty ->
210210
Texp_constraint (sub.typ sub cty)
211-
| Texp_coerce (cty1, cty2) ->
212-
Texp_coerce (opt (sub.typ sub) cty1, sub.typ sub cty2)
211+
| Texp_coerce cty2 ->
212+
Texp_coerce (sub.typ sub cty2)
213213
| Texp_open (ovf, path, loc, env) ->
214214
Texp_open (ovf, path, loc, sub.env sub env)
215215
| 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
@@ -249,8 +249,8 @@ module MakeIterator(Iter : IteratorArgument) : sig
249249
match cstr with
250250
Texp_constraint ct ->
251251
iter_core_type ct
252-
| Texp_coerce (cty1, cty2) ->
253-
option iter_core_type cty1; iter_core_type cty2
252+
| Texp_coerce cty2 ->
253+
iter_core_type cty2
254254
| Texp_open _ -> ()
255255
| Texp_poly cto -> option iter_core_type cto
256256
| Texp_newtype _ -> ())

jscomp/ml/typedtreeMap.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,8 @@ module MakeMap(Map : MapArgument) = struct
381381
match desc with
382382
| Texp_constraint ct ->
383383
Texp_constraint (map_core_type ct), loc, attrs
384-
| Texp_coerce (None, ct) ->
385-
Texp_coerce (None, map_core_type ct), loc, attrs
386-
| Texp_coerce (Some ct1, ct2) ->
387-
Texp_coerce (Some (map_core_type ct1),
388-
map_core_type ct2), loc, attrs
384+
| Texp_coerce ct ->
385+
Texp_coerce (map_core_type ct), loc, attrs
389386
| Texp_poly (Some ct) ->
390387
Texp_poly (Some ( map_core_type ct )), loc, attrs
391388
| Texp_newtype _

jscomp/ml/untypeast.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ let exp_extra sub (extra, loc, attrs) sexp =
313313
let attrs = sub.attributes sub attrs in
314314
let desc =
315315
match extra with
316-
Texp_coerce (cty1, cty2) ->
316+
Texp_coerce cty2 ->
317317
Pexp_coerce (sexp,
318-
map_opt (sub.typ sub) cty1,
318+
(),
319319
sub.typ sub cty2)
320320
| Texp_constraint cty ->
321321
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)