Skip to content

Commit 70d2c55

Browse files
committed
No coercion from from mandatory to optional field
1 parent 65e9888 commit 70d2c55

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

jscomp/ml/ctype.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3969,8 +3969,8 @@ let rec subtype_rec env trace t1 t2 cstrs =
39693969
let label_decl_sub (acc1, acc2) ld2 =
39703970
match Ext_list.find_first fields1 (fun ld1 -> ld1.ld_id.name = ld2.ld_id.name) with
39713971
| Some ld1 ->
3972-
if field_is_optional ld1.ld_id repr1 && not (field_is_optional ld2.ld_id repr2) then
3973-
(* optional field can't be cast to non-optional one *)
3972+
if field_is_optional ld1.ld_id repr1 <> (field_is_optional ld2.ld_id repr2) then
3973+
(* optional field can't be modified *)
39743974
violation := true;
39753975
let get_as (({txt}, payload) : Parsetree.attribute) =
39763976
if txt = "as" then Ast_payload.is_single_string payload

jscomp/test/RecordCoercion.res

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ type r3 = {a?: int}
66

77
type r4 = {}
88

9-
let _ = (x: r1) => (x :> r2) // Convert a from mandatory to optional
9+
// let _ = (x: r1) => (x :> r2) // Convert a from mandatory to optional NOT ALLOWED
1010
// let _ = (x: r2) => (x :> r1) can't turn an optional field to a mandatory one
1111
let _ = (x: r2) => (x :> r3) // can omit field
12-
let _ = (x: r1) => (x :> r3) // omit field and convert from mandatory to optional
12+
// let _ = (x: r1) => (x :> r3) // omit field and convert from mandatory to optional NOT ALLOWED
1313
let _ = (x: r3) => (x :> r4) // omit everything
1414

15-
type nested1 = {n: r1, extra: int}
16-
type nested2 = {n: r2}
15+
type nested1 = {n: r2, extra: int}
16+
type nested2 = {n: r3}
1717
let _ = (x: nested1) => (x :> nested2)
1818

1919
module TestInlining = {

0 commit comments

Comments
 (0)