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/main/java | |
| parent | 71a8ca11b03331a92bb271cbd1fbcb877e587300 (diff) | |
Check call to function without effects has no effects
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java | 8 | ||||
| -rw-r--r-- | src/main/java/org/shedlang/compiler/typechecker/errors/FunctionHasNoEffectsError.java | 10 |
2 files changed, 17 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); + } +} |
