Skip to content

Fix inner object macro implementations #6809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ object Splicer {
loadModule(fn.moduleClass)

protected def interpretNew(fn: Symbol, args: => List[Result])(implicit env: Env): Object = {
val clazz = loadClass(fn.owner.fullName)
val clazz = loadClass(fn.owner.fullName.toString)
val constr = clazz.getConstructor(paramsSig(fn): _*)
constr.newInstance(args: _*).asInstanceOf[Object]
}
Expand All @@ -152,11 +152,16 @@ object Splicer {
private def loadModule(sym: Symbol): Object = {
if (sym.owner.is(Package)) {
// is top level object
val moduleClass = loadClass(sym.fullName)
val moduleClass = loadClass(sym.fullName.toString)
moduleClass.getField(str.MODULE_INSTANCE_FIELD).get(null)
} else {
// nested object in an object
val clazz = loadClass(sym.fullNameSeparated(FlatName))
val className = {
val pack = sym.topLevelClass.owner
if (pack == defn.RootPackage || pack == defn.EmptyPackageClass) sym.flatName.toString
else pack.showFullName + "." + sym.flatName
}
val clazz = loadClass(className)
clazz.getConstructor().newInstance().asInstanceOf[Object]
}
}
Expand All @@ -166,8 +171,8 @@ object Splicer {
lineClassloader.loadClass(moduleClass.name.firstPart.toString)
}

private def loadClass(name: Name): Class[_] = {
try classLoader.loadClass(name.toString)
private def loadClass(name: String): Class[_] = {
try classLoader.loadClass(name)
catch {
case _: ClassNotFoundException =>
val msg = s"Could not find class $name in classpath$extraMsg"
Expand Down
17 changes: 17 additions & 0 deletions tests/pos-macros/i6803b/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package blah

import scala.language.implicitConversions
import scala.quoted._
import scala.quoted.autolift._

object AsObject {
final class LineNo(val lineNo: Int)
object LineNo {
def unsafe(i: Int): LineNo = new LineNo(i)
inline delegate x for LineNo = ${impl}
private def impl given (qctx: QuoteContext): Expr[LineNo] = {
import qctx.tasty._
'{unsafe(${rootPosition.startLine})}
}
}
}
11 changes: 11 additions & 0 deletions tests/pos-macros/i6803b/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import blah._

object Test {
def main(args: Array[String]): Unit = {

def testO(): Unit = {
import AsObject.LineNo
the[LineNo] given LineNo.x
}
}
}
2 changes: 1 addition & 1 deletion tests/run-macros/inline-case-objects.check
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ scala.None$
Bar$
Bar.Baz$
foo.Bar$
Bar.Baz$
foo.Bar.Baz$
3 changes: 3 additions & 0 deletions tests/run-macros/inline-macro-inner-object.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A.f
A.B.f
A.B.C.f
22 changes: 22 additions & 0 deletions tests/run-macros/inline-macro-inner-object/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package blah

import scala.quoted._

object A {
inline def f: Unit = ${impl}
private def impl given (qctx: QuoteContext): Expr[Unit] = {
'{println("A.f")}
}
object B {
inline def f: Unit = ${impl}
private def impl given (qctx: QuoteContext): Expr[Unit] = {
'{println("A.B.f")}
}
object C {
inline def f: Unit = ${impl}
private def impl given (qctx: QuoteContext): Expr[Unit] = {
'{println("A.B.C.f")}
}
}
}
}
9 changes: 9 additions & 0 deletions tests/run-macros/inline-macro-inner-object/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import blah._

object Test {
def main(args: Array[String]): Unit = {
A.f
A.B.f
A.B.C.f
}
}