aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-11-07 20:02:01 +0000
committerMichael Williamson <mike@zwobble.org>2023-11-07 20:02:01 +0000
commit704531beb6a43e4f515e2a4dd30c65c08bba5aea (patch)
tree06dc7e58cc509657ace63f8a07855f758fe929d7 /src
parentc42698c0245d37f24cc00f8ced5c0ad6fc02a1b0 (diff)
Use explicit resume type
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java27
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerTryTests.java38
2 files changed, 62 insertions, 3 deletions
diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
index 3bc3e088..c45b770b 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
@@ -1737,9 +1737,30 @@ public class TypeChecker {
contextForReturnType
));
- var partialSignature = inferFunctionPartialSignature(typedSignature, Optional.of(effectFieldType))
- // TODO: use explicit return type if present
- .withReturnTypeState(ReturnTypeState.effectHandler(effectFieldType.returnType()));
+ var partialSignature = inferFunctionPartialSignature(typedSignature, Optional.of(effectFieldType));
+
+ // TODO: Find a nicer way of doing this (we know the return type state should only be one of two variants).
+ var effectHandlerReturnTypeState = switch (partialSignature.returnTypeState()) {
+ case ReturnTypeState.EffectHandler effectHandler ->
+ throw new IllegalStateException();
+
+ case ReturnTypeState.Explicit explicit ->
+ // TODO: do we actually want to allow this? Potentially unclear
+ // whether this refers to the resume type or the exit type?
+ ReturnTypeState.effectHandler(explicit.type());
+
+ case ReturnTypeState.ImplicitLowerBound implicitLowerBound ->
+ throw new IllegalStateException();
+
+ case ReturnTypeState.ImplicitUnknown implicitUnknown ->
+ ReturnTypeState.effectHandler(effectFieldType.returnType());
+
+ case ReturnTypeState.NotInFunction notInFunction ->
+ throw new IllegalStateException();
+
+ };
+
+ partialSignature = partialSignature.withReturnTypeState(effectHandlerReturnTypeState);
if (typedSignature.effect().isEmpty()) {
partialSignature = partialSignature.withEffect(context.effect());
diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerTryTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerTryTests.java
index 4469979e..f62780d4 100644
--- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerTryTests.java
+++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerTryTests.java
@@ -352,6 +352,44 @@ public class TypeCheckerTryTests {
}
@Test
+ public void whenHandlerMethodTypeHasNarrowerReturnTypeThanEffectMethodThenNarrowerTypeIsUsedToTypeCheckBody() {
+ var handledEffect = Effects.userDefined(NAMESPACE_NAME, "HandledEffect");
+ var untyped = UntypedTryNode.builder()
+ .withHandledEffect(Untyped.typeLevelReference(handledEffect.name()))
+ .addHandlerMethod(
+ UntypedFunctionDefinitionNode.builder()
+ .addStatement(Untyped.resume(Untyped.intLiteral(0)))
+ .withReturnType(Untyped.typeLevelReference("String"))
+ .withName("mark")
+ .build()
+ )
+ .build();
+ var context = TypeCheckerContext.EMPTY
+ .addUserDefinedEffectInfo(
+ handledEffect,
+ UserDefinedEffectInfo.builder()
+ .addField("mark", FunctionType.builder().withReturnType(Types.TOP).build())
+ .build()
+ )
+ .enterFunction(
+ P.map(
+ Map.entry("String", Variable.defined(Types.metaType(Types.STRING), VariableType.CONSTANT)),
+ Map.entry(handledEffect.name(), Variable.defined(Types.effectType(handledEffect), VariableType.CONSTANT))
+ ),
+ Effects.NONE,
+ Optional.empty()
+ );
+
+ var error = assertThrows(
+ UnexpectedTypeError.class,
+ () -> typeCheckExpression(untyped, context)
+ );
+
+ assertThat(error.expected(), equalTo(TypeSets.type(Types.STRING)));
+ assertThat(error.actual(), equalTo(Types.INT));
+ }
+
+ @Test
public void whenHandlerMethodTypeHasEffectThenEffectOfExpressionScopeIsIgnored() {
var handledEffect = Effects.userDefined(NAMESPACE_NAME, "HandledEffect");
var untyped = UntypedTryNode.builder()