-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Emit object method calls via method dispatch (#3750)
- Loading branch information
1 parent
e2dd6ef
commit 3c77dc4
Showing
2 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
nscplugin/src/test/scala/scala/scalanative/compiler/MethodCallsTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package scala.scalanative.compiler | ||
|
||
import org.junit.Test | ||
import org.junit.Assert._ | ||
|
||
import scala.scalanative.nir._ | ||
import scala.scalanative.util.Scope | ||
import scala.scalanative.linker.compileAndLoad | ||
import scala.scalanative.api.CompilationFailedException | ||
import scala.scalanative.NIRCompiler | ||
|
||
class MethodCallTest { | ||
|
||
@Test def emitsMethodDispatchForAbstractMethods(): Unit = { | ||
compileAndLoad( | ||
"Test.scala" -> | ||
""" | ||
|trait Foo{ | ||
| def bar(v1: String, v2: Option[String] = None): String | ||
|} | ||
|object Foo extends Foo{ | ||
| override def bar(v1: String, v2: Option[String] = None): String = ??? | ||
|} | ||
|class FooCls extends Foo{ | ||
| override def bar(v1: String, v2: Option[String] = None): String = ??? | ||
|} | ||
|object Test{ | ||
| def testModule(): Unit = { | ||
| Foo.bar("object") | ||
| Foo.bar("object", Some("opt")) | ||
| } | ||
| def testClass(): Unit = { | ||
| val foo = new FooCls() | ||
| foo.bar("cls") | ||
| foo.bar("cls", Some("opt")) | ||
| } | ||
|}""".stripMargin | ||
) { defns => | ||
val TestModule = Global.Top("Test$") | ||
val TestModuleMethod = | ||
TestModule.member(Sig.Method("testModule", Seq(Type.Unit))) | ||
val TestClassMethod = | ||
TestModule.member(Sig.Method("testClass", Seq(Type.Unit))) | ||
val FooModule = Type.Ref(Global.Top("Foo$")) | ||
val FooClass = Type.Ref(Global.Top("FooCls")) | ||
|
||
val expected: Seq[Global] = | ||
Seq(TestModule, TestModuleMethod, TestClassMethod) | ||
assertEquals(Set.empty, expected.diff(defns.map(_.name)).toSet) | ||
|
||
defns.foreach { | ||
case defn: Defn.Define if defn.name == TestModuleMethod => | ||
defn.insts.collect { | ||
case Inst.Let( | ||
_, | ||
op @ Op.Call(Type.Function(Seq(FooModule, _*), _), fn, _), | ||
_ | ||
) => | ||
assert(fn.isInstanceOf[Val.Local], op.show) | ||
} | ||
case defn: Defn.Define if defn.name == TestClassMethod => | ||
defn.insts.collect { | ||
case Inst.Let( | ||
_, | ||
op @ Op.Call(Type.Function(Seq(FooClass, _*), _), fn, _), | ||
_ | ||
) => | ||
fn match { | ||
case Val.Global(Global.Member(FooClass.name, sig), _) => | ||
assert(sig.isCtor) | ||
case _ => assert(fn.isInstanceOf[Val.Local], op.show) | ||
} | ||
} | ||
case _ => () | ||
} | ||
} | ||
} | ||
|
||
} |