Forbid use of automatic types in lambdas
This commit is contained in:
parent
61f554d563
commit
32ae21d143
|
@ -937,45 +937,6 @@ proc prepareAutoFunction(self: BytecodeCompiler, fn: Name, args: seq[tuple[name:
|
||||||
return fn
|
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) =
|
proc generateCall(self: BytecodeCompiler, fn: Name, args: seq[Expression], line: int) =
|
||||||
## Small wrapper that abstracts emitting a call instruction
|
## Small wrapper that abstracts emitting a call instruction
|
||||||
## for a given function
|
## for a given function
|
||||||
|
@ -1392,10 +1353,7 @@ method call(self: BytecodeCompiler, node: CallExpr, compile: bool = true): Type
|
||||||
of NodeKind.lambdaExpr:
|
of NodeKind.lambdaExpr:
|
||||||
var node = LambdaExpr(node.callee)
|
var node = LambdaExpr(node.callee)
|
||||||
var impl = self.lambdaExpr(node, compile=compile)
|
var impl = self.lambdaExpr(node, compile=compile)
|
||||||
if impl.isAuto:
|
result = impl.returnType
|
||||||
impl = self.prepareAutoLambda(impl, args)
|
|
||||||
result = impl
|
|
||||||
result = result.returnType
|
|
||||||
if compile:
|
if compile:
|
||||||
self.generateCall(impl, argExpr, node.token.line)
|
self.generateCall(impl, argExpr, node.token.line)
|
||||||
else:
|
else:
|
||||||
|
@ -1483,6 +1441,8 @@ method lambdaExpr(self: BytecodeCompiler, node: LambdaExpr, compile: bool = true
|
||||||
node: argument.name,
|
node: argument.name,
|
||||||
position: self.stackIndex
|
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:
|
if compile:
|
||||||
self.names.add(name)
|
self.names.add(name)
|
||||||
inc(self.stackIndex)
|
inc(self.stackIndex)
|
||||||
|
|
Loading…
Reference in New Issue