From 32ae21d14358a68b0358ccf32ffc7d665d69808b Mon Sep 17 00:00:00 2001 From: Mattia Giambirtone Date: Tue, 7 Mar 2023 11:41:37 +0100 Subject: [PATCH] Forbid use of automatic types in lambdas --- .../compiler/targets/bytecode/target.nim | 46 ++----------------- 1 file changed, 3 insertions(+), 43 deletions(-) diff --git a/src/frontend/compiler/targets/bytecode/target.nim b/src/frontend/compiler/targets/bytecode/target.nim index 1ded887..c2c9224 100644 --- a/src/frontend/compiler/targets/bytecode/target.nim +++ b/src/frontend/compiler/targets/bytecode/target.nim @@ -937,45 +937,6 @@ proc prepareAutoFunction(self: BytecodeCompiler, fn: Name, args: seq[tuple[name: return fn -proc prepareAutoLambda(self: BytecodeCompiler, fn: Type, args: seq[tuple[name: string, kind: Type, default: Expression]]): Type = - ## "Prepares" an automatic lambda function - ## by instantiating a concrete version of it - ## along with its arguments - let idx = self.stackIndex - self.stackIndex = 1 - var default: Expression - var node = LambdaExpr(fn.fun) - var fn = deepCopy(fn) - fn.isAuto = false - fn.compiled = false - # We now declare and typecheck the function's - # arguments - for (argument, val) in zip(node.arguments, args): - if self.names.high() > 16777215: - self.error("cannot declare more than 16777215 variables at a time") - inc(self.stackIndex) - self.names.add(Name(depth: self.depth + 1, - isPrivate: true, - owner: self.currentModule, - file: self.file, - isConst: false, - ident: argument.name, - valueType: val.kind, - codePos: 0, - isLet: false, - line: argument.name.token.line, - belongsTo: self.currentFunction, - kind: NameKind.Argument, - node: argument.name, - position: self.stackIndex, - isReal: true - )) - fn.args = args - fn.location = self.stackIndex - self.stackIndex = idx - return fn - - proc generateCall(self: BytecodeCompiler, fn: Name, args: seq[Expression], line: int) = ## Small wrapper that abstracts emitting a call instruction ## for a given function @@ -1392,10 +1353,7 @@ method call(self: BytecodeCompiler, node: CallExpr, compile: bool = true): Type of NodeKind.lambdaExpr: var node = LambdaExpr(node.callee) var impl = self.lambdaExpr(node, compile=compile) - if impl.isAuto: - impl = self.prepareAutoLambda(impl, args) - result = impl - result = result.returnType + result = impl.returnType if compile: self.generateCall(impl, argExpr, node.token.line) else: @@ -1483,6 +1441,8 @@ method lambdaExpr(self: BytecodeCompiler, node: LambdaExpr, compile: bool = true node: argument.name, position: self.stackIndex ) + if name.valueType.kind == Auto: + self.error("due to current compiler limitations, automatic types cannot be used in lambdas", name.ident) if compile: self.names.add(name) inc(self.stackIndex)