diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-02 12:23:41 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-02 12:23:41 +0100 |
| commit | ff0b422cddead23865f0f5662bd9a4d2abef462d (patch) | |
| tree | d02d8164b7a9a2ab3ebe841584ad3809d9116630 /src | |
| parent | 71a8ca11b03331a92bb271cbd1fbcb877e587300 (diff) | |
Check call to function without effects has no effects
Diffstat (limited to 'src')
3 files changed, 41 insertions, 1 deletions
diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java index 4d2b7326..591d4810 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java @@ -142,10 +142,16 @@ public class TypeChecker { var typedArgs = argsResult.value(); context = argsResult.context(); - if (!receiverType.effect().equals(Effects.NONE) && !untyped.hasEffects()) { + var receiverHasEffects = !receiverType.effect().equals(Effects.NONE); + + if (receiverHasEffects && !untyped.hasEffects()) { throw new CallIsMissingEffectsError(receiverType.effect(), untyped.source()); } + if (!receiverHasEffects && untyped.hasEffects()) { + throw new FunctionHasNoEffectsError(untyped.source()); + } + return context.toResult(new TypedCallNode( typedReceiver, typedArgs, diff --git a/src/main/java/org/shedlang/compiler/typechecker/errors/FunctionHasNoEffectsError.java b/src/main/java/org/shedlang/compiler/typechecker/errors/FunctionHasNoEffectsError.java new file mode 100644 index 00000000..162109a0 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/typechecker/errors/FunctionHasNoEffectsError.java @@ -0,0 +1,10 @@ +package org.shedlang.compiler.typechecker.errors; + +import org.shedlang.compiler.errors.SourceError; +import org.shedlang.compiler.sources.Source; + +public class FunctionHasNoEffectsError extends SourceError { + public FunctionHasNoEffectsError(Source source) { + super("Call has effect, but function has no effects", source); + } +} diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java index ad1ab07e..ffbc3c29 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java @@ -2,6 +2,9 @@ package org.shedlang.compiler.typechecker; import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.untyped.Untyped; +import org.shedlang.compiler.ast.untyped.UntypedArgsNode; +import org.shedlang.compiler.ast.untyped.UntypedCallNode; +import org.shedlang.compiler.sources.Sources; import org.shedlang.compiler.typechecker.errors.*; import org.shedlang.compiler.typechecker.typesets.TypeSets; import org.shedlang.compiler.types.Effects; @@ -274,4 +277,25 @@ public class TypeCheckerCallTests { assertThat(error.effect(), equalTo(Effects.IO)); } + + @Test + public void givenFunctionHasNoEffectWhenCallingFunctionWithExplicitEffectsThenErrorIsThrown() { + var untyped = new UntypedCallNode( + Untyped.reference("f"), + new UntypedArgsNode(List.of(), List.of(), Sources.NULL), + true, + Sources.NULL + ); + var functionType = FunctionType.builder() + .withEffect(Effects.NONE) + .withReturnType(Types.UNIT) + .build(); + var context = TypeCheckerContext.EMPTY + .enter(P.map(Map.entry("f", functionType))); + + assertThrows( + FunctionHasNoEffectsError.class, + () -> TypeChecker.typeCheckExpression(untyped, context) + ); + } } |
