From 5f40a1bf751459341b61de757a4eee203eb04172 Mon Sep 17 00:00:00 2001 From: Jan Kassens Date: Mon, 14 Nov 2022 11:35:47 -0500 Subject: [PATCH] [bug] fix codegen for LVals with memberPath (#772) --- compiler/forget/src/HIR/Codegen.ts | 10 +++++++++- .../fixtures/hir/assignment-variations.expect.md | 4 ++-- .../hir/mutable-lifetime-with-aliasing.expect.md | 2 +- .../fixtures/hir/property-assignment.expect.md | 2 +- .../src/__tests__/fixtures/hir/ssa-property.expect.md | 2 +- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/compiler/forget/src/HIR/Codegen.ts b/compiler/forget/src/HIR/Codegen.ts index 16b6a95de6..7249f65102 100644 --- a/compiler/forget/src/HIR/Codegen.ts +++ b/compiler/forget/src/HIR/Codegen.ts @@ -745,7 +745,15 @@ function codegenJsxElement( } function codegenLVal(lval: LValue): t.LVal { - return convertIdentifier(lval.place.identifier); + const expr = convertIdentifier(lval.place.identifier); + const memberPath = lval.place.memberPath; + return memberPath == null + ? expr + : memberPath.reduceRight( + (path: t.Identifier | t.MemberExpression, member) => + t.memberExpression(path, t.identifier(member)), + expr + ); } function codegenValue( diff --git a/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md b/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md index 2817fd9b69..b5baec5f97 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md @@ -98,8 +98,8 @@ flowchart TB ```javascript function g$0(a$4) { - a$4 = a$4.b.c + 1; - a$4 = a$4.b.c * 2; + a$4.c.b = a$4.b.c + 1; + a$4.c.b = a$4.b.c * 2; return; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-with-aliasing.expect.md b/compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-with-aliasing.expect.md index 180b99ba2e..e2c8ea2be1 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-with-aliasing.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-with-aliasing.expect.md @@ -156,7 +156,7 @@ function Component$0(props$10) { c: c$13, }; const x$15 = {}; - x$15 = b$12; + x$15.b = b$12; const y$16 = mutate$8(x$15, d$14); a$11; b$12; diff --git a/compiler/forget/src/__tests__/fixtures/hir/property-assignment.expect.md b/compiler/forget/src/__tests__/fixtures/hir/property-assignment.expect.md index 1f534c8745..bb39788d38 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/property-assignment.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/property-assignment.expect.md @@ -53,7 +53,7 @@ flowchart TB function Component$0(props$6) { const x$7 = {}; const y$8 = []; - x$7 = y$8; + x$7.y = y$8; const child$9 = ; x$7.y.push(props$6.p0); return {child$9}; diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-property.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-property.expect.md index 2f2802774b..9d5f790b60 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-property.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-property.expect.md @@ -45,7 +45,7 @@ flowchart TB function foo$0() { const x$3 = []; const y$4 = {}; - y$4 = x$3; + y$4.x = x$3; return y$4; }