diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileErrors.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileErrors.java index 66fefc23d..87045f94f 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileErrors.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileErrors.java @@ -571,4 +571,8 @@ public static CompileError invalidPropertyPair(TypeID type, String name) { public static CompileError invalidLambdaHeader(FunctionHeader header) { return new CompileError(CompileExceptionCode.LAMBDA_HEADER_INVALID, "Invalid lambda header " + header); } + + public static CompileError unreachableStatement() { + return new CompileError(CompileExceptionCode.UNREACHABLE_STATEMENT, "Unreachable statement"); + } } diff --git a/ScriptingEngineTester/src/main/resources/zencode_tests/return/return_5.zc b/ScriptingEngineTester/src/main/resources/zencode_tests/return/return_5.zc index b1a295876..d0dd4ef60 100644 --- a/ScriptingEngineTester/src/main/resources/zencode_tests/return/return_5.zc +++ b/ScriptingEngineTester/src/main/resources/zencode_tests/return/return_5.zc @@ -1,4 +1,3 @@ -#disabled: Not part of the refactor #error: 10:UNREACHABLE_STATEMENT function test(check: bool): string { diff --git a/Validator/src/main/java/org/openzen/zenscript/validator/visitors/ReturnStatementValidator.java b/Validator/src/main/java/org/openzen/zenscript/validator/visitors/ReturnStatementValidator.java index 76535e17b..b898ed2d6 100644 --- a/Validator/src/main/java/org/openzen/zenscript/validator/visitors/ReturnStatementValidator.java +++ b/Validator/src/main/java/org/openzen/zenscript/validator/visitors/ReturnStatementValidator.java @@ -6,10 +6,14 @@ import org.openzen.zenscript.codemodel.type.TypeID; import org.openzen.zenscript.validator.Validator; -import java.util.Arrays; - public class ReturnStatementValidator implements StatementVisitor { + private final Validator validator; + + public ReturnStatementValidator(Validator validator) { + this.validator = validator; + } + /** * Validates whether a given statement appropriately returns a value as required by its return type. * @@ -24,7 +28,7 @@ public static void validate(TypeID returnType, Statement statement, Validator va return; } - ReturnStatementValidator.ReturnKind returnKind = statement.accept(new ReturnStatementValidator()); + ReturnStatementValidator.ReturnKind returnKind = statement.accept(new ReturnStatementValidator(validator)); switch (returnKind) { case ALWAYS_RETURNS: return; @@ -129,7 +133,17 @@ public ReturnKind visitWhile(WhileStatement statement) { } private ReturnKind mergeInsideBlock(Statement[] statements) { - return Arrays.stream(statements).map(it -> it.accept(this)).reduce(ReturnKind.NEVER_RETURNS, ReturnKind::max); + ReturnKind result = ReturnKind.NEVER_RETURNS; + for (Statement statement : statements) { + if (result == ReturnKind.ALWAYS_RETURNS) { + validator.logError(statement.position, CompileErrors.unreachableStatement()); + break; + } + + result = ReturnKind.max(result, statement.accept(this)); + } + + return result; } public enum ReturnKind {