From 9d15f1601242707953864e687a261932a6bf84ea Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 29 Oct 2023 18:02:04 +0000 Subject: Support inferring return type from single return statement --- .../typechecker/TypeCheckerFunctionBodyTests.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java index 5bc73325..5e80c9a0 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java @@ -267,6 +267,25 @@ public class TypeCheckerFunctionBodyTests { assertThat(error.actual(), equalTo(Types.UNIT)); } + @Test + public void givenNoExplicitReturnTypeWhenThereIsOneExplicitReturnThenReturnTypeIsTypeOfReturnExpression() { + var untyped = Untyped.block(List.of( + Untyped.functionReturn(Untyped.stringLiteral("")) + )); + var signature = TypedFunctionSignatureNode.builder() + .withoutReturnType() + .build(); + var context = TypeCheckerContext.EMPTY; + + var typed = typeCheckFunctionBody(untyped, signature, context); + + assertThat( + typed.value(), + isTypedFunctionBodyNode() + .withType(Types.STRING) + ); + } + @Test public void freeVariablesIncludeVariablesReferencedInFunctionButDefinedOutsideOfFunction() { var untyped = Untyped.block(List.of( -- cgit v1.2.3