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 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.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/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 -> diff --git a/jscomp/test/as_inline_record_test.js b/jscomp/test/as_inline_record_test.js index 73f692f953..c0de1fbe9d 100644 --- a/jscomp/test/as_inline_record_test.js +++ b/jscomp/test/as_inline_record_test.js @@ -2,10 +2,21 @@ 'use strict'; +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 a401cfbe83..1e9e751f2c 100644 --- a/jscomp/test/as_inline_record_test.res +++ b/jscomp/test/as_inline_record_test.res @@ -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 + }