diff --git a/src/frontend/compiler.nim b/src/frontend/compiler.nim index 801a9ac..dee2be0 100644 --- a/src/frontend/compiler.nim +++ b/src/frontend/compiler.nim @@ -1212,14 +1212,13 @@ proc endScope(self: Compiler) = for name in self.names: if name.depth > self.depth: names.add(name) - if not name.resolved and not self.replMode: - self.warning(UnusedName, &"'{name.ident.token.lexeme}' is declared but not used") - #continue if name.owner != self.currentModule and self.depth > -1: # Names coming from other modules only go out of scope # when the global scope is closed (i.e. at the end of # the module) continue + if not name.resolved and not self.replMode: + self.warning(UnusedName, &"'{name.ident.token.lexeme}' is declared but not used") if name.kind == NameKind.Var: inc(popCount) elif name.kind == NameKind.Argument: @@ -1752,8 +1751,16 @@ proc blockStmt(self: Compiler, node: BlockStmt) = ## Compiles block statements, which create a new ## local scope self.beginScope() + var last: Declaration for decl in node.code: + if not last.isNil(): + case last.kind: + of breakStmt, continueStmt: + self.warning(UnreachableCode, &"code after '{last.token.lexeme}' statement is unreachable") + else: + discard self.declaration(decl) + last = decl self.endScope() @@ -2014,7 +2021,6 @@ proc deferStmt(self: Compiler, node: DeferStmt) = proc returnStmt(self: Compiler, node: ReturnStmt) = ## Compiles return statements - if not node.value.isNil(): self.expression(node.value) self.emitByte(OpCode.SetResult, node.token.line) @@ -2282,9 +2288,14 @@ proc funDecl(self: Compiler, node: FunDecl, name: Name) = # the try/finally block with the deferred # code var deferStart = self.deferred.len() + var last: Declaration self.beginScope() for decl in BlockStmt(node.body).code: + if not last.isNil(): + if last.kind == returnStmt: + self.warning(UnreachableCode, "code after 'return' statement is unreachable") self.declaration(decl) + last = decl let typ = self.currentFunction.valueType.returnType var hasVal: bool = false case self.currentFunction.valueType.fun.kind: