Skip to content

Commit eddfbb7

Browse files
authored
Record definition spread issues (#6154)
1 parent a668cbe commit eddfbb7

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#### :bug: Bug Fix
1616

1717
- Fix broken formatting in uncurried mode for functions with _ placeholder args. https://github.com/rescript-lang/rescript-compiler/pull/6148
18+
- Fix issue where spreading record types with optional labels would not have their labels preserved as optional. https://github.com/rescript-lang/rescript-compiler/pull/6154
1819

1920
# 11.0.0-alpha.3
2021

jscomp/ml/typedecl.ml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -439,13 +439,6 @@ let transl_declaration ~typeRecordAsObject env sdecl id =
439439
else typ in
440440
{lbl with pld_type = typ }) in
441441
let lbls, lbls' = transl_labels env true lbls in
442-
let rep =
443-
if unbox then Record_unboxed false
444-
else
445-
if optionalLabels <> []
446-
then Record_optional_labels optionalLabels
447-
else Record_regular
448-
in
449442
let lbls_opt = match lbls, lbls' with
450443
| {ld_name = {txt = "..."}; ld_type} :: _, _ :: _ ->
451444
let rec extract t = match t.desc with
@@ -479,7 +472,15 @@ let transl_declaration ~typeRecordAsObject env sdecl id =
479472
(match lbls_opt with
480473
| Some (lbls, lbls') ->
481474
check_duplicates lbls StringSet.empty;
482-
Ttype_record lbls, Type_record(lbls', rep), sdecl
475+
let optionalLabels =
476+
Ext_list.filter_map lbls (fun lbl ->
477+
if has_optional lbl.ld_attributes then Some lbl.ld_name.txt else None)
478+
in
479+
Ttype_record lbls, Type_record(lbls', if unbox then
480+
Record_unboxed false
481+
else if optionalLabels <> [] then
482+
Record_optional_labels optionalLabels
483+
else Record_regular), sdecl
483484
| None ->
484485
(* Could not find record type decl for ...t: assume t is an object type and this is syntax ambiguity *)
485486
typeRecordAsObject := true;

jscomp/test/DotDotDot.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ var v2 = {
2222
w: 2.0
2323
};
2424

25+
var x = {
26+
name: "test",
27+
x: "test"
28+
};
29+
2530
exports.v = v;
2631
exports.v2 = v2;
32+
exports.x = x;
2733
exports.MultipleDotDotDots = MultipleDotDotDots;
2834
/* No side effect */

jscomp/test/DotDotDot.res

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ type svgProps = {
3535
y?: string,
3636
}
3737

38+
let x: svgProps = {x: "test", name: "test"}
39+
40+
// uncomment this to reveal a parser error
41+
// type copiedSvgProps = {...svgProps}
42+
3843
module MultipleDotDotDots = {
3944
type t1 = {x: int}
4045
type t2 = {y: string}

0 commit comments

Comments
 (0)