diff --git a/src/frontend/compiler.nim b/src/frontend/compiler.nim index 82e3064..5176326 100644 --- a/src/frontend/compiler.nim +++ b/src/frontend/compiler.nim @@ -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) diff --git a/src/frontend/meta/ast.nim b/src/frontend/meta/ast.nim index 1311dc3..6838439 100644 --- a/src/frontend/meta/ast.nim +++ b/src/frontend/meta/ast.nim @@ -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) diff --git a/tests/generics2.pn b/tests/generics2.pn index 0b9f660..2264253 100644 --- a/tests/generics2.pn +++ b/tests/generics2.pn @@ -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); }