aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-10-29 18:22:31 +0000
committerMichael Williamson <mike@zwobble.org>2023-10-29 18:22:31 +0000
commit7bc7dbd5f5d73c1c070ed69af2ab143d7a1c199e (patch)
tree0f46cdd1dc61335d281ee4263ed8d1f1cc40c6cb /src
parent3b3040d8e199c9720a16f77dcc8fdedee56ef7cd (diff)
Support inferring return type from multiple return statements
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java15
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java40
2 files changed, 44 insertions, 11 deletions
diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
index 0ceca376..15623e98 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
@@ -682,14 +682,13 @@ public class TypeChecker {
);
}
case ReturnTypeState.ImplicitLowerBound implicitLowerBound -> {
- throw new UnsupportedOperationException("TODO");
-// var supertype = Types.commonSupertype(List.of(
-// implicitLowerBound.type(),
-// returnValueType
-// ));
-// context = context.updateReturnTypeState(
-// ReturnTypeState.implicitLowerBound(supertype)
-// );
+ var supertype = Types.commonSupertype(List.of(
+ implicitLowerBound.type(),
+ returnValueType
+ ));
+ context = context.updateReturnTypeState(
+ ReturnTypeState.implicitLowerBound(supertype)
+ );
}
case ReturnTypeState.NotInFunction notInFunction -> {
// This should be syntactically impossible, but since we only have
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"))