Skip to content

fix issue where record type spreads would not be picked up #6305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions jscomp/ml/typedecl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,18 @@ let transl_declaration ~typeRecordAsObject env sdecl id =
else typ in
{lbl with pld_type = typ }) in
let lbls, lbls' = transl_labels env true lbls in
let lbls_opt = match lbls, lbls' with
| {ld_name = {txt = "..."}; ld_type} :: _, _ :: _ ->
let has_spread =
lbls
|> List.exists (fun l ->
match l with
| {ld_name = {txt = "..."}} -> true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to have the treatment of spread somewhere (e.g. dedicated file)? E.g. no mention of "..." outside that file, or the has_spread functionality.
It might, or might not, be useful. What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be useful, yeah. Might make sense to do it together with the next "feature" for type spreads, should we decide to do something like support type arguments.

| _ -> false) in
let lbls_opt = match has_spread with
| true ->
let rec extract t = match t.desc with
| Tpoly(t, []) -> extract t
| _ -> Ctype.repr t in
let mkLbl (l: Types.label_declaration) : Typedtree.label_declaration =
let mkLbl (l: Types.label_declaration) (ld_type: Typedtree.core_type) : Typedtree.label_declaration =
{ ld_id = l.ld_id;
ld_name = {txt = Ident.name l.ld_id; loc = l.ld_loc};
ld_mutable = l.ld_mutable;
Expand All @@ -441,14 +447,14 @@ let transl_declaration ~typeRecordAsObject env sdecl id =
| {ld_name = {txt = "..."}; ld_type} :: rest, _ :: rest' ->
(match Ctype.extract_concrete_typedecl env (extract ld_type.ctyp_type) with
(_p0, _p, {type_kind=Type_record (fields, _repr)}) ->
process_lbls (fst acc @ (fields |> List.map mkLbl), snd acc @ fields) rest rest'
process_lbls (fst acc @ (fields |> List.map (fun l -> mkLbl l ld_type)), snd acc @ fields) rest rest'
| _ -> assert false
| exception _ -> None)
| lbl::rest, lbl'::rest' -> process_lbls (fst acc @ [lbl], snd acc @ [lbl']) rest rest'
| _ -> Some acc
in
process_lbls ([], []) lbls lbls'
| _ -> Some (lbls, lbls') in
| false -> Some (lbls, lbls') in
let rec check_duplicates loc (lbls : Typedtree.label_declaration list) seen = match lbls with
| [] -> ()
| lbl::rest ->
Expand Down
3 changes: 2 additions & 1 deletion jscomp/test/build.ninja

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions jscomp/test/record_type_spread.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions jscomp/test/record_type_spread.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type x = {x: int}

type y = {
y: int,
...x,
}

let getY = (v: y) => v.y
let getX = (v: y) => v.x

let v: y = {y: 3, x: 3}