From 28b744b0039e381e197f46cea0e5e87ac4b8bab2 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 3 Jul 2023 12:35:23 +0100 Subject: Check for duplicate variable names --- .../compiler/typechecker/TypeCheckerValTests.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerValTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerValTests.java index 7967e442..f09c73e9 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerValTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerValTests.java @@ -3,6 +3,7 @@ package org.shedlang.compiler.typechecker; import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.untyped.Untyped; import org.shedlang.compiler.ast.untyped.UntypedFunctionDefinitionNode; +import org.shedlang.compiler.typechecker.errors.DuplicateVariableName; import org.shedlang.compiler.typechecker.errors.ReferenceBeforeDefinitionError; import org.shedlang.compiler.types.Types; import org.shedlang.compiler.util.P; @@ -88,4 +89,23 @@ public class TypeCheckerValTests { assertThat(error.variableName(), equalTo("x")); } + + @Test + public void duplicateVariableNamesAreNotAllowed() { + var untyped = UntypedFunctionDefinitionNode.builder() + .addStatement(Untyped.val(Untyped.targetVariable("x"), Untyped.stringLiteral("hello"))) + .addStatement(Untyped.val(Untyped.targetVariable("x"), Untyped.stringLiteral("hello"))) + .build(); + var context = TypeCheckerContext.EMPTY + .enterBlockWithDefinedVariables(P.map( + Map.entry("Unit", Types.metaType(Types.UNIT)) + )); + + var error = assertThrows( + DuplicateVariableName.class, + () -> TypeChecker.typeCheckNamespaceStatement(untyped, context) + ); + + assertThat(error.variableName(), equalTo("x")); + } } -- cgit v1.2.3