Skip to content

Commit c531f6e

Browse files
committed
Complete @optional.
1 parent 86e7c25 commit c531f6e

File tree

4 files changed

+72
-32
lines changed

4 files changed

+72
-32
lines changed

jscomp/test/record_regression.res

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,15 @@ type partiallyOptional = {
8585

8686
let po = {aa: 3, bb: Some(4)}
8787

88-
module M: {
89-
type partiallyOptional = {
90-
@optional aa: int,
91-
bb: option<int>,
92-
}
93-
} = {
94-
type partiallyOptional = {
95-
@optional aa: int,
96-
@optional bb: int,
97-
}
98-
}
88+
// Trigger representation mismatch error.
89+
// module M: {
90+
// type partiallyOptional = {
91+
// @optional aa: int,
92+
// bb: option<int>,
93+
// }
94+
// } = {
95+
// type partiallyOptional = {
96+
// @optional aa: int,
97+
// @optional bb: int,
98+
// }
99+
// }

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34171,7 +34171,7 @@ type type_mismatch =
3417134171
| Field_arity of Ident.t
3417234172
| Field_names of int * string * string
3417334173
| Field_missing of bool * Ident.t
34174-
| Record_representation of bool
34174+
| Record_representation of record_representation * record_representation
3417534175
| Unboxed_representation of bool
3417634176
| Immediate
3417734177

@@ -34337,7 +34337,7 @@ type type_mismatch =
3433734337
| Field_arity of Ident.t
3433834338
| Field_names of int * string * string
3433934339
| Field_missing of bool * Ident.t
34340-
| Record_representation of bool (* true means second one is unboxed float *)
34340+
| Record_representation of record_representation * record_representation
3434134341
| Unboxed_representation of bool (* true means second one is unboxed *)
3434234342
| Immediate
3434334343

@@ -34362,10 +34362,23 @@ let report_type_mismatch0 first second decl ppf err =
3436234362
| Field_missing (b, s) ->
3436334363
pr "The field %s is only present in %s %s"
3436434364
(Ident.name s) (if b then second else first) decl
34365-
| Record_representation b ->
34366-
pr "Their internal representations differ:@ %s %s %s"
34367-
(if b then second else first) decl
34368-
"uses @@obj representation"
34365+
| Record_representation (rep1, rep2) ->
34366+
let default () = pr "Their internal representations differ" in
34367+
( match rep1, rep2 with
34368+
| Record_optional_labels lbls1, Record_optional_labels lbls2 ->
34369+
let onlyInLhs =
34370+
Ext_list.find_first lbls1 (fun l -> not (Ext_list.mem_string lbls2 l)) in
34371+
let onlyInRhs =
34372+
Ext_list.find_first lbls2 (fun l -> not (Ext_list.mem_string lbls1 l)) in
34373+
(match onlyInLhs, onlyInRhs with
34374+
| Some l, _ ->
34375+
pr "@optional label %s only in %s" l second
34376+
| _, Some l ->
34377+
pr "@optional label %s only in %s" l first
34378+
| None, None -> default ())
34379+
| _ ->
34380+
default ()
34381+
)
3436934382
| Unboxed_representation b ->
3437034383
pr "Their internal representations differ:@ %s %s %s"
3437134384
(if b then second else first) decl
@@ -34515,7 +34528,7 @@ let type_declarations ?(equality = false) ~loc env name decl1 id decl2 =
3451534528
let err = compare_records ~loc env decl1.type_params decl2.type_params
3451634529
1 labels1 labels2 in
3451734530
if err <> [] || rep1 = rep2 then err else
34518-
[Record_representation (rep2 = Record_object)]
34531+
[Record_representation (rep1, rep2)]
3451934532
| (Type_open, Type_open) -> []
3452034533
| (_, _) -> [Kind]
3452134534
in

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34171,7 +34171,7 @@ type type_mismatch =
3417134171
| Field_arity of Ident.t
3417234172
| Field_names of int * string * string
3417334173
| Field_missing of bool * Ident.t
34174-
| Record_representation of bool
34174+
| Record_representation of record_representation * record_representation
3417534175
| Unboxed_representation of bool
3417634176
| Immediate
3417734177

@@ -34337,7 +34337,7 @@ type type_mismatch =
3433734337
| Field_arity of Ident.t
3433834338
| Field_names of int * string * string
3433934339
| Field_missing of bool * Ident.t
34340-
| Record_representation of bool (* true means second one is unboxed float *)
34340+
| Record_representation of record_representation * record_representation
3434134341
| Unboxed_representation of bool (* true means second one is unboxed *)
3434234342
| Immediate
3434334343

@@ -34362,10 +34362,23 @@ let report_type_mismatch0 first second decl ppf err =
3436234362
| Field_missing (b, s) ->
3436334363
pr "The field %s is only present in %s %s"
3436434364
(Ident.name s) (if b then second else first) decl
34365-
| Record_representation b ->
34366-
pr "Their internal representations differ:@ %s %s %s"
34367-
(if b then second else first) decl
34368-
"uses @@obj representation"
34365+
| Record_representation (rep1, rep2) ->
34366+
let default () = pr "Their internal representations differ" in
34367+
( match rep1, rep2 with
34368+
| Record_optional_labels lbls1, Record_optional_labels lbls2 ->
34369+
let onlyInLhs =
34370+
Ext_list.find_first lbls1 (fun l -> not (Ext_list.mem_string lbls2 l)) in
34371+
let onlyInRhs =
34372+
Ext_list.find_first lbls2 (fun l -> not (Ext_list.mem_string lbls1 l)) in
34373+
(match onlyInLhs, onlyInRhs with
34374+
| Some l, _ ->
34375+
pr "@optional label %s only in %s" l second
34376+
| _, Some l ->
34377+
pr "@optional label %s only in %s" l first
34378+
| None, None -> default ())
34379+
| _ ->
34380+
default ()
34381+
)
3436934382
| Unboxed_representation b ->
3437034383
pr "Their internal representations differ:@ %s %s %s"
3437134384
(if b then second else first) decl
@@ -34515,7 +34528,7 @@ let type_declarations ?(equality = false) ~loc env name decl1 id decl2 =
3451534528
let err = compare_records ~loc env decl1.type_params decl2.type_params
3451634529
1 labels1 labels2 in
3451734530
if err <> [] || rep1 = rep2 then err else
34518-
[Record_representation (rep2 = Record_object)]
34531+
[Record_representation (rep1, rep2)]
3451934532
| (Type_open, Type_open) -> []
3452034533
| (_, _) -> [Kind]
3452134534
in

lib/4.06.1/whole_compiler.ml

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208702,7 +208702,7 @@ type type_mismatch =
208702208702
| Field_arity of Ident.t
208703208703
| Field_names of int * string * string
208704208704
| Field_missing of bool * Ident.t
208705-
| Record_representation of bool
208705+
| Record_representation of record_representation * record_representation
208706208706
| Unboxed_representation of bool
208707208707
| Immediate
208708208708

@@ -208868,7 +208868,7 @@ type type_mismatch =
208868208868
| Field_arity of Ident.t
208869208869
| Field_names of int * string * string
208870208870
| Field_missing of bool * Ident.t
208871-
| Record_representation of bool (* true means second one is unboxed float *)
208871+
| Record_representation of record_representation * record_representation
208872208872
| Unboxed_representation of bool (* true means second one is unboxed *)
208873208873
| Immediate
208874208874

@@ -208893,10 +208893,23 @@ let report_type_mismatch0 first second decl ppf err =
208893208893
| Field_missing (b, s) ->
208894208894
pr "The field %s is only present in %s %s"
208895208895
(Ident.name s) (if b then second else first) decl
208896-
| Record_representation b ->
208897-
pr "Their internal representations differ:@ %s %s %s"
208898-
(if b then second else first) decl
208899-
"uses @@obj representation"
208896+
| Record_representation (rep1, rep2) ->
208897+
let default () = pr "Their internal representations differ" in
208898+
( match rep1, rep2 with
208899+
| Record_optional_labels lbls1, Record_optional_labels lbls2 ->
208900+
let onlyInLhs =
208901+
Ext_list.find_first lbls1 (fun l -> not (Ext_list.mem_string lbls2 l)) in
208902+
let onlyInRhs =
208903+
Ext_list.find_first lbls2 (fun l -> not (Ext_list.mem_string lbls1 l)) in
208904+
(match onlyInLhs, onlyInRhs with
208905+
| Some l, _ ->
208906+
pr "@optional label %s only in %s" l second
208907+
| _, Some l ->
208908+
pr "@optional label %s only in %s" l first
208909+
| None, None -> default ())
208910+
| _ ->
208911+
default ()
208912+
)
208900208913
| Unboxed_representation b ->
208901208914
pr "Their internal representations differ:@ %s %s %s"
208902208915
(if b then second else first) decl
@@ -209046,7 +209059,7 @@ let type_declarations ?(equality = false) ~loc env name decl1 id decl2 =
209046209059
let err = compare_records ~loc env decl1.type_params decl2.type_params
209047209060
1 labels1 labels2 in
209048209061
if err <> [] || rep1 = rep2 then err else
209049-
[Record_representation (rep2 = Record_object)]
209062+
[Record_representation (rep1, rep2)]
209050209063
| (Type_open, Type_open) -> []
209051209064
| (_, _) -> [Kind]
209052209065
in

0 commit comments

Comments
 (0)