diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-10-29 18:22:31 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-10-29 18:22:31 +0000 |
| commit | 7bc7dbd5f5d73c1c070ed69af2ab143d7a1c199e (patch) | |
| tree | 0f46cdd1dc61335d281ee4263ed8d1f1cc40c6cb /src/test/java | |
| parent | 3b3040d8e199c9720a16f77dcc8fdedee56ef7cd (diff) | |
Support inferring return type from multiple return statements
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java index 5e80c9a0..8b7bc4f6 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java @@ -217,7 +217,7 @@ public class TypeCheckerFunctionBodyTests { } @Test - public void givenNoExplicitReturnTypeWhenNoExplicitReturnsThenReturnTypeIsUnit() { + public void givenNoExplicitReturnTypeWhenNoReturnStatementsThenReturnTypeIsUnit() { var untyped = Untyped.block(List.of( Untyped.expressionStatement(Untyped.stringLiteral("")) )); @@ -236,7 +236,7 @@ public class TypeCheckerFunctionBodyTests { } @Test - public void givenNoExplicitReturnTypeWhenThereAreExplicitReturnsThenAllBranchesMustReturn() { + public void givenNoExplicitReturnTypeWhenThereAreNonUnitReturnStatementsThenAllBranchesMustReturn() { var untyped = Untyped.block(List.of( Untyped.expressionStatement(Untyped.if_( List.of( @@ -268,7 +268,7 @@ public class TypeCheckerFunctionBodyTests { } @Test - public void givenNoExplicitReturnTypeWhenThereIsOneExplicitReturnThenReturnTypeIsTypeOfReturnExpression() { + public void givenNoExplicitReturnTypeWhenThereIsOneReturnStatementThenReturnTypeIsTypeOfReturnExpression() { var untyped = Untyped.block(List.of( Untyped.functionReturn(Untyped.stringLiteral("")) )); @@ -287,6 +287,40 @@ public class TypeCheckerFunctionBodyTests { } @Test + public void givenNoExplicitReturnTypeWhenThereAreMultipleReturnStatementsThenReturnTypeIsCommonSupertypeOfReturnExpressions() { + var untyped = Untyped.block(List.of( + Untyped.expressionStatement(Untyped.if_( + List.of( + Untyped.conditionalBranch( + Untyped.reference("c"), + Untyped.block(List.of( + Untyped.functionReturn(Untyped.stringLiteral("")) + )) + ) + ), + Untyped.block(List.of( + Untyped.functionReturn(Untyped.intLiteral(0)) + )) + )) + )); + var signature = TypedFunctionSignatureNode.builder() + .withoutReturnType() + .build(); + var context = TypeCheckerContext.EMPTY + .enterGlobal(P.map( + Map.entry("c", Types.BOOL) + )); + + var typed = typeCheckFunctionBody(untyped, signature, context); + + assertThat( + typed.value(), + isTypedFunctionBodyNode() + .withType(Types.TOP) + ); + } + + @Test public void freeVariablesIncludeVariablesReferencedInFunctionButDefinedOutsideOfFunction() { var untyped = Untyped.block(List.of( Untyped.expressionStatement(Untyped.reference("y")) |
