From 4d21d8e94e9f17548ddde560aadb6f4dd650af77 Mon Sep 17 00:00:00 2001 From: Bruder John Date: Thu, 27 Jun 2024 15:17:05 +0200 Subject: [PATCH] fix method overlaoding semantic check --- src/main/java/semantic/SemanticAnalyzer.java | 6 ++--- .../SelectWrongMethodCauseParameter.java | 22 +++++++++++++++++++ .../typedAstFeaturesTests/CorrectTest.java | 2 +- .../SelectRightOverloadedMethod.java | 21 ++++++++++++++++++ 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 src/test/resources/input/typedAstExceptionsTests/SelectWrongMethodCauseParameter.java create mode 100644 src/test/resources/input/typedAstFeaturesTests/SelectRightOverloadedMethod.java diff --git a/src/main/java/semantic/SemanticAnalyzer.java b/src/main/java/semantic/SemanticAnalyzer.java index 1f318f2..db9cac0 100644 --- a/src/main/java/semantic/SemanticAnalyzer.java +++ b/src/main/java/semantic/SemanticAnalyzer.java @@ -214,9 +214,7 @@ public class SemanticAnalyzer implements SemanticVisitor { @Override public TypeCheckResult analyze(ParameterNode toCheck) { - - - return new TypeCheckResult(true, null); + return new TypeCheckResult(true, toCheck.type); } @Override @@ -517,7 +515,7 @@ public class SemanticAnalyzer implements SemanticVisitor { for(int i = 0; i < method.getParameters().size(); i++){ var result1 = method.getParameters().get(i).accept(this); var result2 = toCheck.parameters.get(i).accept(this); - if (Objects.equals(result1.getType(), result2.getType())) { + if (!Objects.equals(result1.getType(), result2.getType())) { same = false; } } diff --git a/src/test/resources/input/typedAstExceptionsTests/SelectWrongMethodCauseParameter.java b/src/test/resources/input/typedAstExceptionsTests/SelectWrongMethodCauseParameter.java new file mode 100644 index 0000000..f65e757 --- /dev/null +++ b/src/test/resources/input/typedAstExceptionsTests/SelectWrongMethodCauseParameter.java @@ -0,0 +1,22 @@ +// @expected: TypeMismatchException +public class Test{ + + public int i; + public boolean b; + + public int test(){ + + return this.test(i); + + } + + public void test(int a){ + + } + + public int test(boolean bool){ + int ret = 1; + return ret; + } + +} \ No newline at end of file diff --git a/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java b/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java index e5219be..bcb6b9c 100644 --- a/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java +++ b/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java @@ -5,7 +5,7 @@ public class Test{ public int test(){ - return i; + return this.test(b); } diff --git a/src/test/resources/input/typedAstFeaturesTests/SelectRightOverloadedMethod.java b/src/test/resources/input/typedAstFeaturesTests/SelectRightOverloadedMethod.java new file mode 100644 index 0000000..bcb6b9c --- /dev/null +++ b/src/test/resources/input/typedAstFeaturesTests/SelectRightOverloadedMethod.java @@ -0,0 +1,21 @@ +public class Test{ + + public int i; + public boolean b; + + public int test(){ + + return this.test(b); + + } + + public void test(int a){ + + } + + public int test(boolean bool){ + int ret = 1; + return ret; + } + +} \ No newline at end of file