Skip to content

Commit ab0e9e8

Browse files
authored
Get field name from attribute when pattern matching (#6456)
* Get field name from attribute when pattern matching * Move find_name to matching so it can be shared * Add addtional prop to the test * Update changelog
1 parent 8e1be10 commit ab0e9e8

6 files changed

+44
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- Fix build error where JSX v4 transformation of the discouraged forwardRef in uncurried mode https://github.com/rescript-lang/rescript-compiler/pull/6447
2828
- Fix printing of exotic JSX names https://github.com/rescript-lang/rescript-compiler/pull/6451
2929
- 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
30+
- Fix renaming fields (with @as) in inline records doesn't work when destructuring https://github.com/rescript-lang/rescript-compiler/pull/6456
3031

3132
#### :house: Internal
3233

jscomp/core/record_attributes_check.ml

+1-12
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,7 @@
2424

2525
type label = Types.label_description
2626

27-
let find_name (attr : Parsetree.attribute) =
28-
match attr with
29-
| ( { txt = "bs.as" | "as" },
30-
PStr
31-
[
32-
{
33-
pstr_desc =
34-
Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (s, _)) }, _);
35-
};
36-
] ) ->
37-
Some s
38-
| _ -> None
27+
let find_name = Matching.find_name
3928

4029
let find_name_with_loc (attr : Parsetree.attribute) : string Asttypes.loc option
4130
=

jscomp/ml/matching.ml

+15-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ open Printf
2626

2727
let dbg = false
2828

29+
let find_name (attr : Parsetree.attribute) =
30+
match attr with
31+
| ( { txt = "bs.as" | "as" },
32+
PStr
33+
[
34+
{
35+
pstr_desc =
36+
Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (s, _)) }, _);
37+
};
38+
] ) ->
39+
Some s
40+
| _ -> None
41+
2942
(* See Peyton-Jones, ``The Implementation of functional programming
3043
languages'', chapter 5. *)
3144
(*
@@ -1601,7 +1614,8 @@ let make_record_matching loc all_labels def = function
16011614
| Record_regular | Record_optional_labels _ ->
16021615
Lprim (Pfield (lbl.lbl_pos, !Lambda.fld_record lbl), [arg], loc)
16031616
| Record_inlined _ ->
1604-
Lprim (Pfield (lbl.lbl_pos, Fld_record_inline {name = lbl.lbl_name}), [arg], loc)
1617+
let name = Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name in
1618+
Lprim (Pfield (lbl.lbl_pos, Fld_record_inline {name}), [arg], loc)
16051619
| Record_unboxed _ -> arg
16061620
| Record_extension -> Lprim (Pfield (lbl.lbl_pos + 1, Fld_record_extension {name = lbl.lbl_name}), [arg], loc)
16071621
in

jscomp/ml/matching.mli

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
open Typedtree
1919
open Lambda
2020

21+
val find_name :
22+
Parsetree.attribute -> Asttypes.label option
23+
2124
val call_switcher_variant_constant :
2225
(Location.t ->
2326
Lambda.lambda option ->

jscomp/test/as_inline_record_test.js

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

jscomp/test/as_inline_record_test.res

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ type user =
22
| User({
33
@as("renamed")
44
name: string,
5+
age: int,
56
})
67

7-
let user = User({name: "Corentin"})
8+
let user = User({name: "Corentin", age: 35})
9+
10+
let getName = t =>
11+
switch t {
12+
| User({name}) => name
13+
}
14+
15+
let getAge = t =>
16+
switch t {
17+
| User({age}) => age
18+
}

0 commit comments

Comments
 (0)