Skip to content

Commit f3a283a

Browse files
committed
Error messages for @optional.
1 parent acc5fc2 commit f3a283a

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

jscomp/ml/includecore.ml

+21-6
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ type type_mismatch =
136136
| Field_arity of Ident.t
137137
| Field_names of int * string * string
138138
| Field_missing of bool * Ident.t
139-
| Record_representation of bool (* true means second one is unboxed float *)
139+
| Record_representation of record_representation * record_representation
140140
| Unboxed_representation of bool (* true means second one is unboxed *)
141141
| Immediate
142142

@@ -161,10 +161,25 @@ let report_type_mismatch0 first second decl ppf err =
161161
| Field_missing (b, s) ->
162162
pr "The field %s is only present in %s %s"
163163
(Ident.name s) (if b then second else first) decl
164-
| Record_representation b ->
165-
pr "Their internal representations differ:@ %s %s %s"
166-
(if b then second else first) decl
167-
"uses @@obj representation"
164+
| Record_representation (rep1, rep2) ->
165+
let default () = pr "Their internal representations differ" in
166+
( match rep1, rep2 with
167+
| Record_optional_labels lbls1, Record_optional_labels lbls2 ->
168+
let onlyInLhs =
169+
Ext_list.find_first lbls1 (fun l -> not (Ext_list.mem_string lbls2 l)) in
170+
let onlyInRhs =
171+
Ext_list.find_first lbls2 (fun l -> not (Ext_list.mem_string lbls1 l)) in
172+
(match onlyInLhs, onlyInRhs with
173+
| Some l, _ ->
174+
pr "@optional label %s only in %s" l second
175+
| _, Some l ->
176+
pr "@optional label %s only in %s" l first
177+
| None, None -> default ())
178+
| _ ->
179+
default ()
180+
)
181+
(* pr "Their internal representations differ:@ rep1:%s rep2:%s decl:%s"
182+
first second decl *)
168183
| Unboxed_representation b ->
169184
pr "Their internal representations differ:@ %s %s %s"
170185
(if b then second else first) decl
@@ -314,7 +329,7 @@ let type_declarations ?(equality = false) ~loc env name decl1 id decl2 =
314329
let err = compare_records ~loc env decl1.type_params decl2.type_params
315330
1 labels1 labels2 in
316331
if err <> [] || rep1 = rep2 then err else
317-
[Record_representation (rep2 = Record_object)]
332+
[Record_representation (rep1, rep2)]
318333
| (Type_open, Type_open) -> []
319334
| (_, _) -> [Kind]
320335
in

jscomp/ml/includecore.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type type_mismatch =
3232
| Field_arity of Ident.t
3333
| Field_names of int * string * string
3434
| Field_missing of bool * Ident.t
35-
| Record_representation of bool
35+
| Record_representation of record_representation * record_representation
3636
| Unboxed_representation of bool
3737
| Immediate
3838

jscomp/test/record_regression.res

+11-11
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ 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+
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+
}

0 commit comments

Comments
 (0)