Skip to content

Commit ce43416

Browse files
authored
Merge pull request #6305 from rescript-lang/fix-record-spread-not-in-1st-pos
fix issue where record type spreads would not be picked up
2 parents 6a9fe4a + 51e9ff4 commit ce43416

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

jscomp/ml/typedecl.ml

+11-5
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,18 @@ let transl_declaration ~typeRecordAsObject env sdecl id =
425425
else typ in
426426
{lbl with pld_type = typ }) in
427427
let lbls, lbls' = transl_labels env true lbls in
428-
let lbls_opt = match lbls, lbls' with
429-
| {ld_name = {txt = "..."}; ld_type} :: _, _ :: _ ->
428+
let has_spread =
429+
lbls
430+
|> List.exists (fun l ->
431+
match l with
432+
| {ld_name = {txt = "..."}} -> true
433+
| _ -> false) in
434+
let lbls_opt = match has_spread with
435+
| true ->
430436
let rec extract t = match t.desc with
431437
| Tpoly(t, []) -> extract t
432438
| _ -> Ctype.repr t in
433-
let mkLbl (l: Types.label_declaration) : Typedtree.label_declaration =
439+
let mkLbl (l: Types.label_declaration) (ld_type: Typedtree.core_type) : Typedtree.label_declaration =
434440
{ ld_id = l.ld_id;
435441
ld_name = {txt = Ident.name l.ld_id; loc = l.ld_loc};
436442
ld_mutable = l.ld_mutable;
@@ -441,14 +447,14 @@ let transl_declaration ~typeRecordAsObject env sdecl id =
441447
| {ld_name = {txt = "..."}; ld_type} :: rest, _ :: rest' ->
442448
(match Ctype.extract_concrete_typedecl env (extract ld_type.ctyp_type) with
443449
(_p0, _p, {type_kind=Type_record (fields, _repr)}) ->
444-
process_lbls (fst acc @ (fields |> List.map mkLbl), snd acc @ fields) rest rest'
450+
process_lbls (fst acc @ (fields |> List.map (fun l -> mkLbl l ld_type)), snd acc @ fields) rest rest'
445451
| _ -> assert false
446452
| exception _ -> None)
447453
| lbl::rest, lbl'::rest' -> process_lbls (fst acc @ [lbl], snd acc @ [lbl']) rest rest'
448454
| _ -> Some acc
449455
in
450456
process_lbls ([], []) lbls lbls'
451-
| _ -> Some (lbls, lbls') in
457+
| false -> Some (lbls, lbls') in
452458
let rec check_duplicates loc (lbls : Typedtree.label_declaration list) seen = match lbls with
453459
| [] -> ()
454460
| lbl::rest ->

jscomp/test/build.ninja

+2-1
Large diffs are not rendered by default.

jscomp/test/record_type_spread.js

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/record_type_spread.res

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type x = {x: int}
2+
3+
type y = {
4+
y: int,
5+
...x,
6+
}
7+
8+
let getY = (v: y) => v.y
9+
let getX = (v: y) => v.x
10+
11+
let v: y = {y: 3, x: 3}

0 commit comments

Comments
 (0)