Skip to content

Commit

Permalink
fix: class definition bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Mar 22, 2024
1 parent 1caebdb commit ccb2cce
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
8 changes: 5 additions & 3 deletions crates/erg_compiler/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2352,9 +2352,11 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
let Some(hir::Expr::Call(call)) = hir_def.body.block.first() else {
return unreachable_error!(LowerErrors, LowerError, self);
};
if let Some(sup_type) = call.args.get_left_or_key("Super") {
if let Err(err) = self.check_inheritable(type_obj, sup_type, &hir_def.sig) {
self.errs.extend(err);
if hir_def.def_kind().is_inherit() {
if let Some(sup_type) = call.args.get_left_or_key("Super") {
if let Err(err) = self.check_inheritable(type_obj, sup_type, &hir_def.sig) {
self.errs.extend(err);
}
}
}
let Some(constructor) =
Expand Down
4 changes: 4 additions & 0 deletions crates/erg_parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5553,6 +5553,10 @@ impl DefKind {
matches!(self, Self::Class | Self::Inherit)
}

pub const fn is_inherit(&self) -> bool {
matches!(self, Self::Inherit)
}

pub const fn is_class_or_trait(&self) -> bool {
self.is_class() || self.is_trait()
}
Expand Down
21 changes: 21 additions & 0 deletions tests/should_ok/recursive_class.er
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
F = Class((obj: Obj) -> F)

greedy(obj) =
log obj
F.new greedy

_ = greedy(1)::base(1)::base(1)

Leaf = Class Int
Leaf|<: Show|.
__str__ ref self = "Leaf(\{self::base})"
Node = Class { left = Tree; right = Tree }
Node|<: Show|.
__str__ ref self = "Node{ left = \{self::left}; right = \{self::right} }"
Tree = Class Node or Leaf
Tree|<: Show|.
__str__ ref self = match self::base:
(l: Leaf) -> l.__str__()
(n: Node)-> n.__str__()

print! Node.new { left = Tree.new Leaf.new 1; right = Tree.new Leaf.new 2 }
5 changes: 5 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ fn exec_record_err() -> Result<(), ()> {
expect_failure("tests/should_err/record.er", 0, 1)
}

#[test]
fn exec_recursive_class() -> Result<(), ()> {
expect_success("tests/should_ok/recursive_class.er", 0)
}

#[test]
fn exec_refinement() -> Result<(), ()> {
expect_success("tests/should_ok/refinement.er", 0)
Expand Down

0 comments on commit ccb2cce

Please sign in to comment.