From 83408981fcd9525c5840a852b3dffdbbadd26c7c Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Sun, 29 Oct 2023 21:23:42 +0800 Subject: [PATCH 1/4] Get field name from attribute when pattern matching --- jscomp/ml/matching.ml | 16 +++++++++++++++- jscomp/test/as_inline_record_test.js | 5 +++++ jscomp/test/as_inline_record_test.res | 5 +++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/jscomp/ml/matching.ml b/jscomp/ml/matching.ml index 575e118911..d79b00f0c4 100644 --- a/jscomp/ml/matching.ml +++ b/jscomp/ml/matching.ml @@ -26,6 +26,19 @@ open Printf let dbg = false +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 + (* See Peyton-Jones, ``The Implementation of functional programming languages'', chapter 5. *) (* @@ -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 diff --git a/jscomp/test/as_inline_record_test.js b/jscomp/test/as_inline_record_test.js index 73f692f953..5dd58cb6a2 100644 --- a/jscomp/test/as_inline_record_test.js +++ b/jscomp/test/as_inline_record_test.js @@ -2,10 +2,15 @@ 'use strict'; +function getName(t) { + return t.renamed; +} + var user = { TAG: "User", renamed: "Corentin" }; exports.user = user; +exports.getName = getName; /* No side effect */ diff --git a/jscomp/test/as_inline_record_test.res b/jscomp/test/as_inline_record_test.res index a401cfbe83..1b76f41342 100644 --- a/jscomp/test/as_inline_record_test.res +++ b/jscomp/test/as_inline_record_test.res @@ -5,3 +5,8 @@ type user = }) let user = User({name: "Corentin"}) + +let getName = t => + switch t { + | User({name}) => name + } From a5afe91b52fa9b9318db780a7fcf7d57df165179 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Mon, 30 Oct 2023 15:27:08 +0800 Subject: [PATCH 2/4] Move find_name to matching so it can be shared --- jscomp/core/record_attributes_check.ml | 13 +------------ jscomp/ml/matching.mli | 3 +++ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/jscomp/core/record_attributes_check.ml b/jscomp/core/record_attributes_check.ml index c77757b493..60865b275b 100644 --- a/jscomp/core/record_attributes_check.ml +++ b/jscomp/core/record_attributes_check.ml @@ -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 = diff --git a/jscomp/ml/matching.mli b/jscomp/ml/matching.mli index 16fda89bf5..c26f3b7838 100644 --- a/jscomp/ml/matching.mli +++ b/jscomp/ml/matching.mli @@ -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 -> From 75fe6b1014868a8bba72f769e94bf4ac53352bfa Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Mon, 30 Oct 2023 15:28:57 +0800 Subject: [PATCH 3/4] Add addtional prop to the test --- jscomp/test/as_inline_record_test.js | 8 +++++++- jscomp/test/as_inline_record_test.res | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/jscomp/test/as_inline_record_test.js b/jscomp/test/as_inline_record_test.js index 5dd58cb6a2..c0de1fbe9d 100644 --- a/jscomp/test/as_inline_record_test.js +++ b/jscomp/test/as_inline_record_test.js @@ -6,11 +6,17 @@ function getName(t) { return t.renamed; } +function getAge(t) { + return t.age; +} + var user = { TAG: "User", - renamed: "Corentin" + renamed: "Corentin", + age: 35 }; exports.user = user; exports.getName = getName; +exports.getAge = getAge; /* No side effect */ diff --git a/jscomp/test/as_inline_record_test.res b/jscomp/test/as_inline_record_test.res index 1b76f41342..1e9e751f2c 100644 --- a/jscomp/test/as_inline_record_test.res +++ b/jscomp/test/as_inline_record_test.res @@ -2,11 +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 + } From a37ea17d0e05b4ac26543aafe193a8b37c4ea9ec Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Mon, 30 Oct 2023 16:07:50 +0800 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 101f1d4469..806e17c452 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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