Skip to content

Commit 21a298d

Browse files
committed
tweak
1 parent 5163d89 commit 21a298d

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

jscomp/syntax/ast_external_process.ml

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,17 @@
2626
[@@@ocaml.warning "+9"]
2727
(* record pattern match complete checker*)
2828

29-
type field =
30-
| No_fields
31-
| Valid_fields
32-
| Invalid_field
33-
34-
let variant_can_bs_unwrap_fields (row_fields : Parsetree.row_field list) : bool =
35-
let validity =
36-
Ext_list.fold_left row_fields No_fields
37-
begin fun st row ->
38-
match st, row with
39-
| (* we've seen no fields or only valid fields so far *)
40-
(No_fields | Valid_fields),
41-
(* and this field has one constructor arg that we can unwrap to *)
42-
Rtag (label, attrs, false, ([ _ ]))
43-
->
44-
Valid_fields
45-
| (* otherwise, this field or a previous field was invalid *)
46-
_ ->
47-
Invalid_field
48-
end
49-
in
50-
validity = Valid_fields
29+
30+
let rec variant_can_unwrap_aux (row_fields : Parsetree.row_field list) : bool =
31+
match row_fields with
32+
| [] -> true
33+
| Rtag(_,_,false,[_]) :: rest -> variant_can_unwrap_aux rest
34+
| _ :: rest -> false
35+
36+
let variant_unwrap (row_fields : Parsetree.row_field list) : bool =
37+
match row_fields with
38+
| [] -> false (* impossible syntax *)
39+
| xs -> variant_can_unwrap_aux xs
5140

5241
(*
5342
TODO: [nolabel] is only used once turn Nothing into Unit, refactor later
@@ -77,7 +66,7 @@ let spec_of_ptyp
7766
| `Unwrap ->
7867
begin match ptyp_desc with
7968
| Ptyp_variant (row_fields, Closed, _)
80-
when variant_can_bs_unwrap_fields row_fields ->
69+
when variant_unwrap row_fields ->
8170
Unwrap
8271
| _ ->
8372
Bs_syntaxerr.err ptyp.ptyp_loc Invalid_bs_unwrap_type

jscomp/test/bs_unwrap_test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ function ff1(x, p) {
9393
return /* () */0;
9494
}
9595

96+
function test00(param) {
97+
return {
98+
a: 1,
99+
b: 2,
100+
x: /* () */0
101+
};
102+
}
103+
96104
var none_arg = undefined;
97105

98106
exports.arg_string = arg_string;
@@ -104,4 +112,5 @@ exports.dyn_log4 = dyn_log4;
104112
exports.f = f;
105113
exports.ff0 = ff0;
106114
exports.ff1 = ff1;
115+
exports.test00 = test00;
107116
/* Not a pure module */

jscomp/test/bs_unwrap_test.ml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ external log2 :
2727
let _ = log2 (`Unit ())
2828

2929
external log3 :
30-
req:([ `String of string
31-
| `Int of int
30+
req:([
31+
`String of string
32+
| `Int of int
3233
] [@bs.unwrap])
3334
-> ?opt:([ `String of string
3435
| `Bool of bool
@@ -88,4 +89,10 @@ external log5: ?h:([`A of int | `B of string] [@bs.unwrap]) -> int -> unit = "co
8889

8990
let ff0 x p = log5 ?h:x p
9091

91-
let ff1 x p = log5 ?h:(x ()) p
92+
let ff1 x p = log5 ?h:(x ()) p
93+
94+
95+
external ff : a:int -> b:int-> x: unit -> _ = "" [@@bs.obj]
96+
97+
98+
let test00 () = ff ~a:1 ~b:2 ~x:()

0 commit comments

Comments
 (0)