From 2bff632883780c71c78da4639bb1084611f518be Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Tue, 16 May 2023 05:49:39 +0200 Subject: [PATCH 1/2] Fix issue with inlining records in the presence of record coercion Fixes https://github.com/rescript-lang/rescript-compiler/issues/6253 --- CHANGELOG.md | 3 +++ jscomp/core/lam_util.ml | 13 ++++++++++++- jscomp/test/RecordCoercion.js | 16 +++++++++++++++- jscomp/test/RecordCoercion.res | 16 ++++++++++++++++ jscomp/test/record_with_test.js | 4 +++- jscomp/test/update_record_test.js | 20 +++++++++++--------- 6 files changed, 60 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 463ee16fa6..75a48f414c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ #### :nail_care: Polish - Removed duplicate Super_error implementation in syntax https://github.com/rescript-lang/rescript-compiler/pull/6246 +#### :bug: Bug Fix +- Fix issue with inlining records in the presence of record coercion https://github.com/rescript-lang/rescript-compiler/pull/6256 + # 11.0.0-alpha.6 #### :boom: Breaking Change diff --git a/jscomp/core/lam_util.ml b/jscomp/core/lam_util.ml index 2e1589989d..04754dde66 100644 --- a/jscomp/core/lam_util.ml +++ b/jscomp/core/lam_util.ml @@ -193,7 +193,18 @@ let field_flatten_get | SimpleForm l -> l | exception _ -> lam () end - | Some (Constant (Const_block (_,_,ls))) -> + | Some (Constant (Const_block (_, Blk_record {fields}, ls))) -> + (match info with + | Fld_record {name} -> + let found = ref None in + for i = 1 to Array.length fields - 1 do + if fields.(i) = name then found := Ext_list.nth_opt ls i done; + (match !found with + | Some c -> Lam.const c + | None -> lam()) + | _ -> lam () + ) + | Some (Constant (Const_block (_,_,ls))) -> begin match Ext_list.nth_opt ls i with | None -> lam () | Some x -> Lam.const x diff --git a/jscomp/test/RecordCoercion.js b/jscomp/test/RecordCoercion.js index d856702bfe..ea6d174265 100644 --- a/jscomp/test/RecordCoercion.js +++ b/jscomp/test/RecordCoercion.js @@ -1,2 +1,16 @@ // Generated by ReScript, PLEASE EDIT WITH CARE -/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */ +'use strict'; + + +var TestInlining_a = { + number: 42, + name: "a" +}; + +var TestInlining = { + a: TestInlining_a, + name: "a" +}; + +exports.TestInlining = TestInlining; +/* No side effect */ diff --git a/jscomp/test/RecordCoercion.res b/jscomp/test/RecordCoercion.res index f577989ed7..c57cc11a6d 100644 --- a/jscomp/test/RecordCoercion.res +++ b/jscomp/test/RecordCoercion.res @@ -15,3 +15,19 @@ let _ = (x: r3) => (x :> r4) // omit everything type nested1 = {n: r1, extra: int} type nested2 = {n: r2} let _ = (x: nested1) => (x :> nested2) + +module TestInlining = { + type a = { + number: int, + name: string, + } + + type b = {name: string} + + let a: a = { + number: 42, + name: "a", + } + + let name = (a :> b).name +} diff --git a/jscomp/test/record_with_test.js b/jscomp/test/record_with_test.js index e1b315c7ec..b3b3b8aec2 100644 --- a/jscomp/test/record_with_test.js +++ b/jscomp/test/record_with_test.js @@ -14,8 +14,10 @@ var v = { extends: 0 }; +var u_v_syntax = v.syntax; + var u_v = { - syntax: undefined, + syntax: u_v_syntax, imports: 0, file_options: 0, package: 0, diff --git a/jscomp/test/update_record_test.js b/jscomp/test/update_record_test.js index d02cbf8854..79ccb9ca76 100644 --- a/jscomp/test/update_record_test.js +++ b/jscomp/test/update_record_test.js @@ -45,14 +45,16 @@ function f(x) { }; } -eq("File \"update_record_test.res\", line 28, characters 5-12", 1, f({ - a0: 0, - a1: 0, - a2: 0, - a3: 0, - a4: 0, - a5: 0 - }).a0); +var v = { + a0: 0, + a1: 0, + a2: 0, + a3: 0, + a4: 0, + a5: 0 +}; + +eq("File \"update_record_test.res\", line 28, characters 5-12", v.a0 + 1 | 0, f(v).a0); var val0 = { "invalid_js_id'": 3, @@ -68,7 +70,7 @@ function fff(x) { var val1 = fff(val0); -eq("File \"update_record_test.res\", line 41, characters 5-12", 3, 3); +eq("File \"update_record_test.res\", line 41, characters 5-12", val0["invalid_js_id'"], 3); eq("File \"update_record_test.res\", line 42, characters 5-12", val1["invalid_js_id'"], 5); From d7ca713246616c2a88899d4948e66b3cb5bd0f8f Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Tue, 16 May 2023 11:20:39 +0200 Subject: [PATCH 2/2] embarassing... --- jscomp/core/lam_util.ml | 2 +- jscomp/test/record_with_test.js | 4 +--- jscomp/test/update_record_test.js | 20 +++++++++----------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/jscomp/core/lam_util.ml b/jscomp/core/lam_util.ml index 04754dde66..36fc31bc1a 100644 --- a/jscomp/core/lam_util.ml +++ b/jscomp/core/lam_util.ml @@ -197,7 +197,7 @@ let field_flatten_get (match info with | Fld_record {name} -> let found = ref None in - for i = 1 to Array.length fields - 1 do + for i = 0 to Array.length fields - 1 do if fields.(i) = name then found := Ext_list.nth_opt ls i done; (match !found with | Some c -> Lam.const c diff --git a/jscomp/test/record_with_test.js b/jscomp/test/record_with_test.js index b3b3b8aec2..e1b315c7ec 100644 --- a/jscomp/test/record_with_test.js +++ b/jscomp/test/record_with_test.js @@ -14,10 +14,8 @@ var v = { extends: 0 }; -var u_v_syntax = v.syntax; - var u_v = { - syntax: u_v_syntax, + syntax: undefined, imports: 0, file_options: 0, package: 0, diff --git a/jscomp/test/update_record_test.js b/jscomp/test/update_record_test.js index 79ccb9ca76..d02cbf8854 100644 --- a/jscomp/test/update_record_test.js +++ b/jscomp/test/update_record_test.js @@ -45,16 +45,14 @@ function f(x) { }; } -var v = { - a0: 0, - a1: 0, - a2: 0, - a3: 0, - a4: 0, - a5: 0 -}; - -eq("File \"update_record_test.res\", line 28, characters 5-12", v.a0 + 1 | 0, f(v).a0); +eq("File \"update_record_test.res\", line 28, characters 5-12", 1, f({ + a0: 0, + a1: 0, + a2: 0, + a3: 0, + a4: 0, + a5: 0 + }).a0); var val0 = { "invalid_js_id'": 3, @@ -70,7 +68,7 @@ function fff(x) { var val1 = fff(val0); -eq("File \"update_record_test.res\", line 41, characters 5-12", val0["invalid_js_id'"], 3); +eq("File \"update_record_test.res\", line 41, characters 5-12", 3, 3); eq("File \"update_record_test.res\", line 42, characters 5-12", val1["invalid_js_id'"], 5);