Skip to content

Commit 560b220

Browse files
Merge pull request #6809 from dotty-staging/fix-inner-object-macro-implimentation
Fix inner object macro implementations
2 parents bd93937 + 312822d commit 560b220

File tree

7 files changed

+73
-6
lines changed

7 files changed

+73
-6
lines changed

compiler/src/dotty/tools/dotc/transform/Splicer.scala

+10-5
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ object Splicer {
143143
loadModule(fn.moduleClass)
144144

145145
protected def interpretNew(fn: Symbol, args: => List[Result])(implicit env: Env): Object = {
146-
val clazz = loadClass(fn.owner.fullName)
146+
val clazz = loadClass(fn.owner.fullName.toString)
147147
val constr = clazz.getConstructor(paramsSig(fn): _*)
148148
constr.newInstance(args: _*).asInstanceOf[Object]
149149
}
@@ -154,11 +154,16 @@ object Splicer {
154154
private def loadModule(sym: Symbol): Object = {
155155
if (sym.owner.is(Package)) {
156156
// is top level object
157-
val moduleClass = loadClass(sym.fullName)
157+
val moduleClass = loadClass(sym.fullName.toString)
158158
moduleClass.getField(str.MODULE_INSTANCE_FIELD).get(null)
159159
} else {
160160
// nested object in an object
161-
val clazz = loadClass(sym.fullNameSeparated(FlatName))
161+
val className = {
162+
val pack = sym.topLevelClass.owner
163+
if (pack == defn.RootPackage || pack == defn.EmptyPackageClass) sym.flatName.toString
164+
else pack.showFullName + "." + sym.flatName
165+
}
166+
val clazz = loadClass(className)
162167
clazz.getConstructor().newInstance().asInstanceOf[Object]
163168
}
164169
}
@@ -168,8 +173,8 @@ object Splicer {
168173
lineClassloader.loadClass(moduleClass.name.firstPart.toString)
169174
}
170175

171-
private def loadClass(name: Name): Class[_] = {
172-
try classLoader.loadClass(name.toString)
176+
private def loadClass(name: String): Class[_] = {
177+
try classLoader.loadClass(name)
173178
catch {
174179
case _: ClassNotFoundException =>
175180
val msg = s"Could not find class $name in classpath$extraMsg"

tests/pos-macros/i6803b/Macro_1.scala

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package blah
2+
3+
import scala.language.implicitConversions
4+
import scala.quoted._
5+
import scala.quoted.autolift._
6+
7+
object AsObject {
8+
final class LineNo(val lineNo: Int)
9+
object LineNo {
10+
def unsafe(i: Int): LineNo = new LineNo(i)
11+
inline delegate x for LineNo = ${impl}
12+
private def impl given (qctx: QuoteContext): Expr[LineNo] = {
13+
import qctx.tasty._
14+
'{unsafe(${rootPosition.startLine})}
15+
}
16+
}
17+
}

tests/pos-macros/i6803b/Test_2.scala

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import blah._
2+
3+
object Test {
4+
def main(args: Array[String]): Unit = {
5+
6+
def testO(): Unit = {
7+
import AsObject.LineNo
8+
the[LineNo] given LineNo.x
9+
}
10+
}
11+
}

tests/run-macros/inline-case-objects.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ scala.None$
33
Bar$
44
Bar.Baz$
55
foo.Bar$
6-
Bar.Baz$
6+
foo.Bar.Baz$
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
A.f
2+
A.B.f
3+
A.B.C.f
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package blah
2+
3+
import scala.quoted._
4+
5+
object A {
6+
inline def f: Unit = ${impl}
7+
private def impl given (qctx: QuoteContext): Expr[Unit] = {
8+
'{println("A.f")}
9+
}
10+
object B {
11+
inline def f: Unit = ${impl}
12+
private def impl given (qctx: QuoteContext): Expr[Unit] = {
13+
'{println("A.B.f")}
14+
}
15+
object C {
16+
inline def f: Unit = ${impl}
17+
private def impl given (qctx: QuoteContext): Expr[Unit] = {
18+
'{println("A.B.C.f")}
19+
}
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import blah._
2+
3+
object Test {
4+
def main(args: Array[String]): Unit = {
5+
A.f
6+
A.B.f
7+
A.B.C.f
8+
}
9+
}

0 commit comments

Comments
 (0)