Fixed bug with return statement and updates tests

This commit is contained in:
Mattia Giambirtone 2022-11-28 19:03:08 +01:00
parent 58b5d07dd6
commit d9570d4393
3 changed files with 6 additions and 2 deletions

View File

@ -2323,6 +2323,10 @@ proc deferStmt(self: Compiler, node: DeferStmt) =
proc returnStmt(self: Compiler, node: ReturnStmt) =
## Compiles return statements
if self.currentFunction.valueType.returnType.isNil() and not node.value.isNil():
self.error("cannot return a value from a void function", node.value)
elif not self.currentFunction.valueType.returnType.isNil() and node.value.isNil():
self.error("bare return statement is only allowed in void functions", node)
if not node.value.isNil():
self.expression(node.value)
self.emitByte(OpCode.SetResult, node.token.line)

View File

@ -798,7 +798,7 @@ proc getRelativeBoundaries*(self: ASTNode): tuple[start, stop: int] =
if self.pragmas.len() > 0:
stop = getRelativeBoundaries(self.pragmas[^1]).stop
result = (start, stop)
of breakStmt:
of breakStmt, returnStmt, continueStmt:
result = self.token.relPos
of importStmt:
result = (self.token.relPos.start, getRelativeBoundaries(ImportStmt(self).moduleName).stop)

View File

@ -7,6 +7,6 @@ fn identity(x: int32): int32 {
# No call is necessary for the compiler to detect the error:
# generics are type checked at declaration time!
fn nope[T: int32 | int16](x: T) {
fn nope[T: int32 | int16](x: T): T {
return identity(x);
}