diff --git a/compiler/forget/src/HIR/BuildHIR.ts b/compiler/forget/src/HIR/BuildHIR.ts index 969998d275..c8b1ae8f51 100644 --- a/compiler/forget/src/HIR/BuildHIR.ts +++ b/compiler/forget/src/HIR/BuildHIR.ts @@ -1790,7 +1790,36 @@ function lowerAssignment( switch (lvalueNode.type) { case "Identifier": { const lvalue = lvaluePath as NodePath; - const place = lowerIdentifier(builder, lvalue); + const identifier = builder.resolveIdentifier(lvalue); + if (identifier == null) { + if (kind === InstructionKind.Reassign) { + // Trying to reassign a global is not allowed + builder.errors.push({ + reason: `(BuildHIR::lowerAssignment) Assigning to an identifier defined outside the function scope is not supported.`, + severity: ErrorSeverity.InvalidInput, + nodePath: lvalue, + }); + } else { + // Else its an internal error bc we couldn't find the binding + builder.errors.push({ + reason: `(BuildHIR::lowerAssignment) Could not find binding for declaration.`, + severity: ErrorSeverity.Invariant, + nodePath: lvalue, + }); + } + return { + kind: "UnsupportedNode", + loc: lvalue.node.loc ?? GeneratedSource, + node: lvalue.node, + }; + } + + const place: Place = { + kind: "Identifier", + identifier: identifier, + effect: Effect.Unknown, + loc: lvalue.node.loc ?? GeneratedSource, + }; builder.push({ id: makeInstructionId(0), lvalue: { place: { ...place }, kind }, diff --git a/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.expect.md b/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.expect.md index b92f21f70a..fed25d84e4 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.expect.md @@ -68,6 +68,9 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) { default: { } } + + // Cannot assign to globals + someUnknownGlobal = true; } ``` @@ -400,6 +403,14 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) { 59 | } 60 | case foo(): { 61 | } + +[ReactForget] InvalidInputError: (BuildHIR::lowerAssignment) Assigning to an identifier defined outside the function scope is not supported. + 67 | + 68 | // Cannot assign to globals +> 69 | someUnknownGlobal = true; + | ^^^^^^^^^^^^^^^^^ + 70 | } + 71 | ``` \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.js b/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.js index fd71c0cb2d..54696dc410 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.js +++ b/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.js @@ -64,4 +64,7 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) { default: { } } + + // Cannot assign to globals + someUnknownGlobal = true; }