diff --git a/src/main/java/de/hsrm/compiler/Klang/ContextAnalysis.java b/src/main/java/de/hsrm/compiler/Klang/ContextAnalysis.java index f2478c3..9dc5fe6 100644 --- a/src/main/java/de/hsrm/compiler/Klang/ContextAnalysis.java +++ b/src/main/java/de/hsrm/compiler/Klang/ContextAnalysis.java @@ -851,10 +851,24 @@ public class ContextAnalysis extends KlangBaseVisitor { var parameterName = ctx.IDENT().getText(); var parameterType = Type.getByName(ctx.type_annotation().type().getText()); + if (structDefs.containsKey(parameterName)) { + var line = ctx.start.getLine(); + var col = ctx.start.getCharPositionInLine(); + var error = "Parameter name " + parameterName + " duplicates a struct of the same name."; + throw new RuntimeException(Helper.getErrorPrefix(line, col) + error); + } + + if (enumDefs.containsKey(parameterName)) { + var line = ctx.start.getLine(); + var col = ctx.start.getCharPositionInLine(); + var error = "Parameter name " + parameterName + " duplicates an enum of the same name."; + throw new RuntimeException(Helper.getErrorPrefix(line, col) + error); + } + if (!parameterType.isPrimitiveType() && !structDefs.containsKey(parameterType.getName()) && !enumDefs.containsKey(parameterType.getName())) { var line = ctx.type_annotation().start.getLine(); var col = ctx.type_annotation().start.getCharPositionInLine(); - String error = "Type " + parameterType.getName() + " not defined."; + var error = "Type " + parameterType.getName() + " not defined."; throw new RuntimeException(Helper.getErrorPrefix(line, col) + error); } diff --git a/src/test/java/FunctionDefinitionTest.java b/src/test/java/FunctionDefinitionTest.java index 85295be..b6086fc 100644 --- a/src/test/java/FunctionDefinitionTest.java +++ b/src/test/java/FunctionDefinitionTest.java @@ -77,4 +77,19 @@ public class FunctionDefinitionTest { var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree)); assertEquals("Error in line 1:20 Function foo has to return something of type int.", e.getMessage()); } + + @Test + void shouldThrowExceptionIfParameterNameMatchesEnumName() { + // given + var tree = Helper.prepareParser("enum Bar { A, B } function foo(Bar: int): int { return 1; } foo(1);"); + var ctxAnal = new ContextAnalysis( + Helper.getFuncs(tree), + Helper.getStructs(tree), + Helper.getEnums(tree) + ); + + // when / then + var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree)); + assertEquals("Error in line 1:31 Parameter name Bar duplicates an enum of the same name.", e.getMessage()); + } } \ No newline at end of file