Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
Class can't inherit itself: typechecking error
Browse files Browse the repository at this point in the history
  • Loading branch information
anmolkabra committed May 14, 2019
1 parent 12c8623 commit db0d94d
Show file tree
Hide file tree
Showing 17 changed files with 85 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/main/java/kc875/ast/visit/TypeCheckVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,13 @@ private void collectTauClasses(List<? extends ClassXi> cs) {
// Initial pass for duplicate classes, fields and methods in this file
Set<String> 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)) {
Expand Down
1 change: 1 addition & 0 deletions tests/pa7-typecheck-tests/a.ixi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class A {
a: int
ma()
}

fa()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3:1 error:Different methods in declaration and definition of class A
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use a

// A defines an extra method
class A {
a: int
ma() {}
extra_method() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7:5 error:Function ma already defined
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use a

// A provides duplicate methods
class A {
a: int
ma() {}
ma() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6:5 error:Mismatch signatures of overriden function with name ma
Original file line number Diff line number Diff line change
@@ -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) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7:1 error:Declaration and definition for class A don't inherit the same class
10 changes: 10 additions & 0 deletions tests/pa7-typecheck-tests/class_decl_defn_dont_match_error3.xi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use a

class B {
}

// A inherits a class but the declaration doesn't
class A extends B {
a: int
ma() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7:1 error:Declaration and definition for class B don't inherit the same class
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use b

class C {
}

// B's def inherits C but decl inherits A
class B extends C {
b: int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1:1 error:Class can't inherit itself
2 changes: 2 additions & 0 deletions tests/pa7-typecheck-tests/class_inherit_itself_error.xi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class B extends B {
}
6 changes: 6 additions & 0 deletions tests/pa7-typecheck-tests/class_nested_interface_imports.xi
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
6 changes: 6 additions & 0 deletions tests/pa7-typecheck-tests/xthScript
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit db0d94d

Please sign in to comment.