From db0d94d0e1f070ebfcbcb7a7a11f0265d87072b1 Mon Sep 17 00:00:00 2001 From: anmolkabra Date: Mon, 13 May 2019 21:42:05 -0400 Subject: [PATCH] Class can't inherit itself: typechecking error --- .idea/inspectionProfiles/Project_Default.xml | 15 +++++++++++++++ .../java/kc875/ast/visit/TypeCheckVisitor.java | 7 +++++++ tests/pa7-typecheck-tests/a.ixi | 1 + .../class_decl_defn_dont_match_error0.typedsol | 1 + .../class_decl_defn_dont_match_error0.xi | 8 ++++++++ .../class_decl_defn_dont_match_error1.typedsol | 1 + .../class_decl_defn_dont_match_error1.xi | 8 ++++++++ .../class_decl_defn_dont_match_error2.typedsol | 1 + .../class_decl_defn_dont_match_error2.xi | 7 +++++++ .../class_decl_defn_dont_match_error3.typedsol | 1 + .../class_decl_defn_dont_match_error3.xi | 10 ++++++++++ .../class_decl_defn_dont_match_error4.typedsol | 1 + .../class_decl_defn_dont_match_error4.xi | 9 +++++++++ .../class_inherit_itself_error.typedsol | 1 + .../class_inherit_itself_error.xi | 2 ++ .../class_nested_interface_imports.xi | 6 ++++++ tests/pa7-typecheck-tests/xthScript | 6 ++++++ 17 files changed, 85 insertions(+) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 tests/pa7-typecheck-tests/class_decl_defn_dont_match_error0.typedsol create mode 100644 tests/pa7-typecheck-tests/class_decl_defn_dont_match_error0.xi create mode 100644 tests/pa7-typecheck-tests/class_decl_defn_dont_match_error1.typedsol create mode 100644 tests/pa7-typecheck-tests/class_decl_defn_dont_match_error1.xi create mode 100644 tests/pa7-typecheck-tests/class_decl_defn_dont_match_error2.typedsol create mode 100644 tests/pa7-typecheck-tests/class_decl_defn_dont_match_error2.xi create mode 100644 tests/pa7-typecheck-tests/class_decl_defn_dont_match_error3.typedsol create mode 100644 tests/pa7-typecheck-tests/class_decl_defn_dont_match_error3.xi create mode 100644 tests/pa7-typecheck-tests/class_decl_defn_dont_match_error4.typedsol create mode 100644 tests/pa7-typecheck-tests/class_decl_defn_dont_match_error4.xi create mode 100644 tests/pa7-typecheck-tests/class_inherit_itself_error.typedsol create mode 100644 tests/pa7-typecheck-tests/class_inherit_itself_error.xi diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..5ae1be7 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,15 @@ + + + + \ No newline at end of file diff --git a/src/main/java/kc875/ast/visit/TypeCheckVisitor.java b/src/main/java/kc875/ast/visit/TypeCheckVisitor.java index 0c3b4c2..f5375ad 100644 --- a/src/main/java/kc875/ast/visit/TypeCheckVisitor.java +++ b/src/main/java/kc875/ast/visit/TypeCheckVisitor.java @@ -1040,6 +1040,13 @@ private void collectTauClasses(List cs) { // Initial pass for duplicate classes, fields and methods in this file Set classNames = new HashSet<>(); for (ClassXi c : cs) { + // Class can't inherit itself + c.getSuperClass().thenDo(sc -> { + if (sc.equals(c.getName())) + throw new SemanticError( + "Class can't inherit itself", c.getLocation() + ); + }); // Duplicate classes not allowed String cName = c.getName(); if (classNames.contains(cName)) { diff --git a/tests/pa7-typecheck-tests/a.ixi b/tests/pa7-typecheck-tests/a.ixi index 6c1adca..90cea54 100644 --- a/tests/pa7-typecheck-tests/a.ixi +++ b/tests/pa7-typecheck-tests/a.ixi @@ -1,5 +1,6 @@ class A { a: int + ma() } fa() diff --git a/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error0.typedsol b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error0.typedsol new file mode 100644 index 0000000..15c2cc2 --- /dev/null +++ b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error0.typedsol @@ -0,0 +1 @@ +3:1 error:Different methods in declaration and definition of class A diff --git a/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error0.xi b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error0.xi new file mode 100644 index 0000000..423456c --- /dev/null +++ b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error0.xi @@ -0,0 +1,8 @@ +use a + +// A defines an extra method +class A { + a: int + ma() {} + extra_method() {} +} diff --git a/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error1.typedsol b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error1.typedsol new file mode 100644 index 0000000..5ef1b98 --- /dev/null +++ b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error1.typedsol @@ -0,0 +1 @@ +7:5 error:Function ma already defined diff --git a/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error1.xi b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error1.xi new file mode 100644 index 0000000..f37afa0 --- /dev/null +++ b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error1.xi @@ -0,0 +1,8 @@ +use a + +// A provides duplicate methods +class A { + a: int + ma() {} + ma() {} +} diff --git a/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error2.typedsol b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error2.typedsol new file mode 100644 index 0000000..1437e86 --- /dev/null +++ b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error2.typedsol @@ -0,0 +1 @@ +6:5 error:Mismatch signatures of overriden function with name ma diff --git a/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error2.xi b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error2.xi new file mode 100644 index 0000000..aba7d87 --- /dev/null +++ b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error2.xi @@ -0,0 +1,7 @@ +use a + +// A defines ma with a different signature than the declaration in module a +class A { + a: int + ma(p: int) {} +} diff --git a/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error3.typedsol b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error3.typedsol new file mode 100644 index 0000000..67d0f8a --- /dev/null +++ b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error3.typedsol @@ -0,0 +1 @@ +7:1 error:Declaration and definition for class A don't inherit the same class diff --git a/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error3.xi b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error3.xi new file mode 100644 index 0000000..c0404d8 --- /dev/null +++ b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error3.xi @@ -0,0 +1,10 @@ +use a + +class B { +} + +// A inherits a class but the declaration doesn't +class A extends B { + a: int + ma() {} +} diff --git a/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error4.typedsol b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error4.typedsol new file mode 100644 index 0000000..26dc1af --- /dev/null +++ b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error4.typedsol @@ -0,0 +1 @@ +7:1 error:Declaration and definition for class B don't inherit the same class diff --git a/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error4.xi b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error4.xi new file mode 100644 index 0000000..1078c66 --- /dev/null +++ b/tests/pa7-typecheck-tests/class_decl_defn_dont_match_error4.xi @@ -0,0 +1,9 @@ +use b + +class C { +} + +// B's def inherits C but decl inherits A +class B extends C { + b: int +} diff --git a/tests/pa7-typecheck-tests/class_inherit_itself_error.typedsol b/tests/pa7-typecheck-tests/class_inherit_itself_error.typedsol new file mode 100644 index 0000000..bd32422 --- /dev/null +++ b/tests/pa7-typecheck-tests/class_inherit_itself_error.typedsol @@ -0,0 +1 @@ +1:1 error:Class can't inherit itself diff --git a/tests/pa7-typecheck-tests/class_inherit_itself_error.xi b/tests/pa7-typecheck-tests/class_inherit_itself_error.xi new file mode 100644 index 0000000..d00d499 --- /dev/null +++ b/tests/pa7-typecheck-tests/class_inherit_itself_error.xi @@ -0,0 +1,2 @@ +class B extends B { +} diff --git a/tests/pa7-typecheck-tests/class_nested_interface_imports.xi b/tests/pa7-typecheck-tests/class_nested_interface_imports.xi index e00c2fb..3ebffcc 100644 --- a/tests/pa7-typecheck-tests/class_nested_interface_imports.xi +++ b/tests/pa7-typecheck-tests/class_nested_interface_imports.xi @@ -13,4 +13,10 @@ fb() {} main(args:int[][]) { fa()// should've been imported by b fb() + c: C = new C + c.ma() + d: D = new D + d.ma() + a: A = new A + a.ma() } diff --git a/tests/pa7-typecheck-tests/xthScript b/tests/pa7-typecheck-tests/xthScript index 3cc739a..5f2906c 100644 --- a/tests/pa7-typecheck-tests/xthScript +++ b/tests/pa7-typecheck-tests/xthScript @@ -36,6 +36,11 @@ xic ("Test --typecheck") "-libpath $(testpath) --typecheck" { break_creates_unreachable_stmt.xi; break_in_loop_multiple_places.xi; break_outside_loop.xi; + class_decl_defn_dont_match_error0.xi; + class_decl_defn_dont_match_error1.xi; + class_decl_defn_dont_match_error2.xi; + class_decl_defn_dont_match_error3.xi; + class_decl_defn_dont_match_error4.xi; class_dup_def.xi; class_field_init_by_methods.xi; class_field_not_found.xi; @@ -44,6 +49,7 @@ xic ("Test --typecheck") "-libpath $(testpath) --typecheck" { class_implements_interface.xi; class_incorrect_override_diff_params.xi; class_incorrect_override_diff_ret.xi; + class_inherit_itself_error.xi; class_method_not_found.xi; class_method_shadow_by_method.xi; class_method_use_super_method.xi;