Skip to content
This repository was archived by the owner on Jul 12, 2024. It is now read-only.

fix: Add tests for virtual dispatch + fix bug for abstract class #14

Merged
merged 2 commits into from
Mar 11, 2024

Conversation

tanishiking
Copy link
Owner

We had sevelral problems with the vtable implementations

  • We'd been generating a global vtable even for an abstract class, but it's not needed because we can't instantiate abstract class
  • For calculating the vtable (both it's type and global instance), we filtered out the abstract methods. Therefore, we couldn't resolve a abstract method call from vtable.

For example,

class A extends B:
  def a = 1

class B extends C:
  def b: Int = 1
  override def c: Int = 1

abstract class C:
  def c: Int

The vtable type for C will be an empty table, because there's no concrete methods. Therefore, when we have x: C, x.c wond't resolve the implementation of C because vtable type doesn't have a slot for c.

The root cause is that we generated both of the following from one (in-memory) vtable object that doesn't have abstract methods.

  • vtable type (for declaring the vtable type, and resolve methods by name at compile time), and
  • global vtable instance (for method lookup at runtime)

The former should include abstract methods like C.c, and the former should not.

We had sevelral problems with the vtable implementations

- We'd been generating a global vtable even for an abstract class, but it's not needed because we can't instantiate abstract class
- For calculating the vtable (both it's type and global instance), we filtered out the abstract methods. Therefore, we couldn't resolve a abstract method call from vtable.

For example,

```scala
class A extends B:
  def a = 1

class B extends C:
  def b: Int = 1
  override def c: Int = 1

abstract class C:
  def c: Int
```

The vtable type for C will be an empty table, because there's no concrete methods. Therefore, when we have `x: C`, `x.c` wond't resolve the implementation of `C` because vtable type doesn't have a slot for `c`.

The root cause is that we generated both of the following from one (in-memory) vtable object that doesn't have abstract methods.

- vtable type (for declaring the vtable type, and resolve methods by name at compile time), and
- global vtable instance (for method lookup at runtime)

The former should include abstract methods like `C.c`, and the former should not.
@tanishiking tanishiking force-pushed the fix-virtual-dispatch branch from 4c7298b to af2d6ad Compare March 11, 2024 07:00
@tanishiking tanishiking merged commit e956e49 into main Mar 11, 2024
1 check passed
@tanishiking tanishiking deleted the fix-virtual-dispatch branch March 11, 2024 07:03
@sjrd
Copy link
Collaborator

sjrd commented Mar 11, 2024

Cool. That also fixed an issue I had with dispatch on java.lang.Number methods in #13. I could add more tests thanks to this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants