aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-11-05 12:45:05 +0000
committerMichael Williamson <mike@zwobble.org>2023-11-05 12:45:05 +0000
commit38f3a0a74a6f6b8dd4a3341d76a4e3db7872e084 (patch)
tree1e559a1f0b07f350ba344f2a0d16f4f998a06fb8 /src/test/java
parent1ed30cb57b8ae48ac60576aee807d5dfc86d8646 (diff)
Prevent capturing of var
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java23
1 files changed, 23 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 37c5d6dd..c6abd22a 100644
--- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java
+++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java
@@ -3,11 +3,13 @@ package org.shedlang.compiler.typechecker;
import org.junit.jupiter.api.Test;
import org.shedlang.compiler.ast.untyped.Untyped;
import org.shedlang.compiler.ast.untyped.UntypedCallNode;
+import org.shedlang.compiler.typechecker.errors.CannotCaptureVarError;
import org.shedlang.compiler.typechecker.errors.DuplicateVariableNameError;
import org.shedlang.compiler.typechecker.errors.UnexpectedTypeError;
import org.shedlang.compiler.typechecker.typesets.TypeSets;
import org.shedlang.compiler.types.Effects;
import org.shedlang.compiler.types.FunctionType;
+import org.shedlang.compiler.types.NamespaceName;
import org.shedlang.compiler.types.Types;
import org.shedlang.compiler.util.P;
@@ -450,6 +452,27 @@ public class TypeCheckerFunctionBodyTests {
}
@Test
+ public void varsCannotBeCaptured() {
+ var untyped = Untyped.block(List.of(
+ Untyped.expressionStatement(Untyped.reference("x"))
+ ));
+ var signature = PartialFunctionSignature.builder()
+ .withReturnType(Types.UNIT)
+ .build();
+ var context = TypeCheckerContext.EMPTY
+ .enterNamespace(NamespaceName.of("Example"), P.map(
+ Map.entry("x", Variable.defined(Types.STRING, VariableType.VAR))
+ ));
+
+ var error = assertThrows(
+ CannotCaptureVarError.class,
+ () -> typeCheckFunctionBody(untyped, signature, context)
+ );
+
+ assertThat(error.name(), equalTo("x"));
+ }
+
+ @Test
public void boundVariablesAreSet() {
var untyped = Untyped.block(List.of(
Untyped.val("local1", Untyped.intLiteral(1)),