Skip to content
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

Get field name from attribute when pattern matching #6456

Merged
merged 4 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 15 additions & 1 deletion jscomp/ml/matching.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ open Printf

let dbg = false

let find_name (attr : Parsetree.attribute) =
Copy link
Member Author

Choose a reason for hiding this comment

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

This is similar to find_name in jscomp/core/record_attributes_check.ml. I prefer if this can be extracted to a common place where both places can use the same function, not sure where though.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks identical. Can't you use the one from record_attributes_check.ml?

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks identical. Can't you use the one from record_attributes_check.ml?

I tried, but record_attributes_check.ml is in the core package, and trying to import it from ml causes a circular import.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried, but record_attributes_check.ml is in the core package, and trying to import it from ml causes a circular import.

Sorry I'm an idiot, I can reverse the dependency 🤦

match attr with
| ( { txt = "bs.as" | "as" },
PStr
[
{
pstr_desc =
Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (s, _)) }, _);
};
] ) ->
Some s
| _ -> None

(* See Peyton-Jones, ``The Implementation of functional programming
languages'', chapter 5. *)
(*
Expand Down Expand Up @@ -1601,7 +1614,8 @@ let make_record_matching loc all_labels def = function
| Record_regular | Record_optional_labels _ ->
Lprim (Pfield (lbl.lbl_pos, !Lambda.fld_record lbl), [arg], loc)
| Record_inlined _ ->
Lprim (Pfield (lbl.lbl_pos, Fld_record_inline {name = lbl.lbl_name}), [arg], loc)
let name = Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name in
Lprim (Pfield (lbl.lbl_pos, Fld_record_inline {name}), [arg], loc)
| Record_unboxed _ -> arg
| Record_extension -> Lprim (Pfield (lbl.lbl_pos + 1, Fld_record_extension {name = lbl.lbl_name}), [arg], loc)
in
Expand Down
5 changes: 5 additions & 0 deletions jscomp/test/as_inline_record_test.js

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

5 changes: 5 additions & 0 deletions jscomp/test/as_inline_record_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ type user =
})

let user = User({name: "Corentin"})

let getName = t =>
switch t {
| User({name}) => name
}