From 6f0a3754bd74ab6de956585fb41f56a3a26dfb25 Mon Sep 17 00:00:00 2001 From: nitrix Date: Mon, 3 Feb 2020 22:36:24 +0100 Subject: [PATCH] check whether the body of a function guarantees a return value and throw an exception if not --- .../java/de/hsrm/compiler/Klang/ContextAnalysis.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/hsrm/compiler/Klang/ContextAnalysis.java b/src/main/java/de/hsrm/compiler/Klang/ContextAnalysis.java index 34713cc..518439f 100644 --- a/src/main/java/de/hsrm/compiler/Klang/ContextAnalysis.java +++ b/src/main/java/de/hsrm/compiler/Klang/ContextAnalysis.java @@ -488,6 +488,8 @@ public class ContextAnalysis extends KlangBaseVisitor { @Override public Node visitFunctionDef(KlangParser.FunctionDefContext ctx) { String name = ctx.funcName.getText(); + int line = ctx.start.getLine(); + int col = ctx.start.getCharPositionInLine(); Type returnType = Type.getByName(ctx.returnType.type().getText()); this.currentDeclaredReturnType = returnType; @@ -509,9 +511,12 @@ public class ContextAnalysis extends KlangBaseVisitor { this.vars.put(param.name, var); } - // Visit the block, make sure the types are matching + // Visit the block, make sure that a return value is guaranteed Node block = this.visit(ctx.braced_block()); - block.type.combine(returnType); + if (block.type == null) { + String error = "Function " + name +" has to return something of type " + returnType.getName() +"."; + throw new RuntimeException(Helper.getErrorPrefix(line, col) + error); + } FunctionDefinition result = new FunctionDefinition(name, params, (Block) block); result.type = returnType;