Temporary fix for not closing over function arguments

This commit is contained in:
Mattia Giambirtone 2022-07-10 15:06:43 +02:00
parent 13cc641e7b
commit 39d1eab234
2 changed files with 9 additions and 5 deletions

View File

@ -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

View File

@ -1,5 +1,5 @@
fn makeClosure(n: int): fn: int {
var n = n;
# let n = n; # Workaround
fn inner: int {
return n;
}