From 39d1eab2343e29a50c5b1fa59c8018fe605ee482 Mon Sep 17 00:00:00 2001 From: Mattia Giambirtone Date: Sun, 10 Jul 2022 15:06:43 +0200 Subject: [PATCH] Temporary fix for not closing over function arguments --- src/frontend/compiler.nim | 12 ++++++++---- tests/closures.pn | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/frontend/compiler.nim b/src/frontend/compiler.nim index c5984da..938a565 100644 --- a/src/frontend/compiler.nim +++ b/src/frontend/compiler.nim @@ -450,9 +450,13 @@ proc detectClosureVariable(self: Compiler, name: var Name, depth: int = self.sco if self.closedOver.len() >= 16777216: self.error("too many consecutive closed-over variables (max is 16777215)") name.isClosedOver = true - self.chunk.code[name.codePos] = StoreClosure.uint8() - for i, b in self.closedOver.high().toTriple(): - self.chunk.code[name.codePos + i + 1] = b + if not name.isFunctionArgument: + # We handle closed-over function arguments later + self.chunk.code[name.codePos] = StoreClosure.uint8() + for i, b in self.closedOver.high().toTriple(): + self.chunk.code[name.codePos + i + 1] = b + else: + self.error("it is currently not possible to close over function arguments") proc compareTypes(self: Compiler, a, b: Type): bool = @@ -1006,7 +1010,7 @@ proc generateCall(self: Compiler, fn: Name, args: seq[Expression]) = for argument in reversed(args): # We pass the arguments in reverse # because of how stack semantics - # work. They'll be fixed at runtime + # work. They'll be fixed at runtime self.expression(argument) # Creates a new call frame and jumps # to the function's first instruction diff --git a/tests/closures.pn b/tests/closures.pn index 4ab2777..d98ec01 100644 --- a/tests/closures.pn +++ b/tests/closures.pn @@ -1,5 +1,5 @@ fn makeClosure(n: int): fn: int { - var n = n; + # let n = n; # Workaround fn inner: int { return n; }