Forbid use of automatic types in lambdas

This commit is contained in:
Mattia Giambirtone 2023-03-07 11:41:37 +01:00
parent 61f554d563
commit 32ae21d143
1 changed files with 3 additions and 43 deletions

View File

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