aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-03 12:30:50 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-03 12:30:50 +0100
commit3b69ffa33d419ee31331582edac427393009e711 (patch)
tree97080371a416b2d2290c1e1f7d0fc45ff422b94e /src/test/java
parentf5545308284496c18366d850397dbef18424c008 (diff)
Check shadowing behaviour
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerValTests.java20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerValTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerValTests.java
index 1526d3aa..7967e442 100644
--- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerValTests.java
+++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerValTests.java
@@ -68,4 +68,24 @@ public class TypeCheckerValTests {
assertThat(error.variableName(), equalTo("x"));
}
+
+ @Test
+ public void whenValShadowsExistingVariableThenValCannotBeReferencedBeforeDefinition() {
+ var untyped = UntypedFunctionDefinitionNode.builder()
+ .addStatement(Untyped.expressionStatement(Untyped.reference("x")))
+ .addStatement(Untyped.val(Untyped.targetVariable("x"), Untyped.stringLiteral("hello")))
+ .build();
+ var context = TypeCheckerContext.EMPTY
+ .enterBlockWithDefinedVariables(P.map(
+ Map.entry("Unit", Types.metaType(Types.UNIT)),
+ Map.entry("x", Types.INT)
+ ));
+
+ var error = assertThrows(
+ ReferenceBeforeDefinitionError.class,
+ () -> TypeChecker.typeCheckNamespaceStatement(untyped, context)
+ );
+
+ assertThat(error.variableName(), equalTo("x"));
+ }
}