aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-03-02 15:39:10 +0000
committerMichael Williamson <mike@zwobble.org>2024-03-02 15:39:10 +0000
commitf1089538f04062f71f530fa27396f6f446a0fc47 (patch)
tree974fb1a56f4ee4ed5b95bc900232ee9c5e922f5d /src
parent36aad08599b78b8ec034f567ee41ec7fa3dc5a02 (diff)
Support implicit effect args
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java9
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java48
2 files changed, 53 insertions, 4 deletions
diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
index cff54be0..223323d3 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
@@ -401,20 +401,21 @@ public class TypeChecker {
var typeSubstitutionMap = argsResult.value().typeSubstitutionMap;
context = argsResult.context();
- var receiverHasEffects = !receiverType.effect().equals(Effects.NONE);
+ var receiverEffect = TypeSubstitution.substituteEffect(receiverType.effect(), typeSubstitutionMap);
+ var receiverHasEffects = !receiverEffect.equals(Effects.NONE);
if (receiverHasEffects && !untyped.hasEffects()) {
- throw new CallIsMissingEffectsError(receiverType.effect(), untyped.source());
+ throw new CallIsMissingEffectsError(receiverEffect, untyped.source());
}
if (!receiverHasEffects && untyped.hasEffects()) {
throw new FunctionHasNoEffectsError(untyped.source());
}
- if (!isSubEffect(receiverType.effect(), context.effect(), context.toTypeInfos())) {
+ if (!isSubEffect(receiverEffect, context.effect(), context.toTypeInfos())) {
throw new UnexpectedEffectError(
context.effect(),
- receiverType.effect(),
+ receiverEffect,
untyped.source()
);
}
diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java
index 1dcfc36a..c7bbafaf 100644
--- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java
+++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java
@@ -465,4 +465,52 @@ public class TypeCheckerCallTests {
.withType(Types.constructedType(typeConstructor, List.of(Types.STRING)))
);
}
+
+ @Test
+ public void canCallGenericFunctionWithImplicitEffectArg() {
+ var untyped = UntypedCallNode.builder()
+ .withReceiver(Untyped.reference("twice"))
+ .withHasEffects(true)
+ .addPositionalArg(Untyped.reference("f"))
+ .build();
+ var twiceType = FunctionType.builder()
+ .withTypeLevelParams(List.of(
+ EffectParam.forFunction(NamespaceName.of("Example"), "twice", "E")
+ ))
+ .addPositionalParam(
+ FunctionType.builder()
+ .withEffect(EffectParam.forFunction(NamespaceName.of("Example"), "twice", "E"))
+ .build()
+ )
+ .withEffect(EffectParam.forFunction(NamespaceName.of("Example"), "twice", "E"))
+ .build();
+ var context = TypeCheckerContext.EMPTY
+ .enterFunction(
+ P.map(
+ Map.entry(
+ "twice",
+ Variable.defined(twiceType, VariableType.VAL)
+ ),
+ Map.entry(
+ "f",
+ Variable.defined(
+ FunctionType.builder()
+ .withEffect(Effects.IO)
+ .build(),
+ VariableType.VAL
+ )
+ )
+ ),
+ Effects.IO,
+ Types.UNIT
+ );
+
+ var typed = typeCheckExpression(untyped, context);
+
+ assertThat(
+ typed.value(),
+ isTypedCallNode()
+ .withHasEffects(true)
+ );
+ }
}