aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-10-29 18:02:04 +0000
committerMichael Williamson <mike@zwobble.org>2023-10-29 18:02:04 +0000
commit9d15f1601242707953864e687a261932a6bf84ea (patch)
treec2470b56785e6243e05af24d87c8458cf628fdbb /src/test/java
parentd19e7debea80e9216923935ef03c8607d7c1d9ec (diff)
Support inferring return type from single return statement
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java19
1 files changed, 19 insertions, 0 deletions
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
@@ -268,6 +268,25 @@ public class TypeCheckerFunctionBodyTests {
}
@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(
Untyped.expressionStatement(Untyped.reference("y"))