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 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- Fix build error where JSX v4 transformation of the discouraged forwardRef in uncurried mode https://github.com/rescript-lang/rescript-compiler/pull/6447
- Fix printing of exotic JSX names https://github.com/rescript-lang/rescript-compiler/pull/6451
- Fix locations when code with `await` fails to compile (all locations would point to the internal function `unsafe_await`) https://github.com/rescript-lang/rescript-compiler/pull/6452
- Fix renaming fields (with @as) in inline records doesn't work when destructuring https://github.com/rescript-lang/rescript-compiler/pull/6456

#### :house: Internal

Expand Down
13 changes: 1 addition & 12 deletions jscomp/core/record_attributes_check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,7 @@

type label = Types.label_description

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

let find_name_with_loc (attr : Parsetree.attribute) : string Asttypes.loc option
=
Expand Down
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
3 changes: 3 additions & 0 deletions jscomp/ml/matching.mli
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
open Typedtree
open Lambda

val find_name :
Parsetree.attribute -> Asttypes.label option

val call_switcher_variant_constant :
(Location.t ->
Lambda.lambda option ->
Expand Down
13 changes: 12 additions & 1 deletion jscomp/test/as_inline_record_test.js

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

13 changes: 12 additions & 1 deletion jscomp/test/as_inline_record_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ type user =
| User({
@as("renamed")
name: string,
age: int,
})

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

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

let getAge = t =>
switch t {
| User({age}) => age
}