Skip to content

Constructing a Class-Instance in a Macro Fails #8115

Closed
@deusaquilus

Description

@deusaquilus

minimized code

I have a trivial class

package example
class MyClass { }

I want to construct it in a macro

package example
import scala.quoted._
import scala.quoted.matching._

object MyClassMaker {
  inline def make: MyClass = ${ makeImpl }
  def makeImpl(given qctx: QuoteContext): Expr[MyClass] = {
    '{
      new MyClass {  }  /* eventually I want to add properties inside */
    }
  }
}

Note that when I remove the { } from MyClass it works.

When I run it like this:

@main def makerTest() = {
  val typeclass = MyClassMaker.make
}

The following error happens:

[error]   |Exception occurred while executing macro expansion.
[error]   |dotty.tools.dotc.core.CyclicReference: 
[error]   |     at dotty.tools.dotc.core.CyclicReference$.apply(TypeErrors.scala:154)
[error]   |     at dotty.tools.dotc.core.SymDenotations$SymDenotation.completeFrom(SymDenotations.scala:256)
[error]   |     at dotty.tools.dotc.core.Denotations$Denotation.completeInfo$1(Denotations.scala:185)
[error]   |     at dotty.tools.dotc.core.Denotations$Denotation.info(Denotations.scala:187)
[error]   |     at dotty.tools.dotc.core.SymDenotations$SymDenotation.ensureCompleted(SymDenotations.scala:398)
[error]   |     at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readNewDef(TreeUnpickler.scala:808)
[error]   |     at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readIndexedDef(TreeUnpickler.scala:743)
[error]   |     at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readIndexedStat(TreeUnpickler.scala:938)
[error]   |     at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readIndexedStats$$anonfun$1(TreeUnpickler.scala:986)
[error]   |     at dotty.tools.tasty.TastyReader.until(TastyReader.scala:125)
[error]   |     at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readIndexedStats(TreeUnpickler.scala:986)
[error]   |     at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readStats(TreeUnpickler.scala:990)
[error]   |     at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readLengthTerm$1(TreeUnpickler.scala:1103)
[error]   |     at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readTerm(TreeUnpickler.scala:1212)
[error]   |     at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readLengthTerm$1(TreeUnpickler.scala:1115)
[error]   |     at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readTerm(TreeUnpickler.scala:1212)
[error]   |     at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readTerm(TreeUnpickler.scala:1006)
[error]   |     at dotty.tools.dotc.core.tasty.TreeUnpickler.unpickle(TreeUnpickler.scala:107)
[error]   |     at dotty.tools.dotc.core.tasty.DottyUnpickler.computeRootTrees(DottyUnpickler.scala:59)
[error]   |     at dotty.tools.dotc.ast.tpd$TreeProvider.rootTrees(tpd.scala:1107)
[error]   |     at dotty.tools.dotc.core.tasty.DottyUnpickler.rootTrees(DottyUnpickler.scala:41)
[error]   |     at dotty.tools.dotc.ast.tpd$TreeProvider.tree(tpd.scala:1111)
[error]   |     at dotty.tools.dotc.core.tasty.DottyUnpickler.tree(DottyUnpickler.scala:41)
[error]   |     at dotty.tools.dotc.core.quoted.PickledQuotes$.unpickle(PickledQuotes.scala:131)
[error]   |     at dotty.tools.dotc.core.quoted.PickledQuotes$.unpickleExpr(PickledQuotes.scala:66)
[error]   |     at dotty.tools.dotc.tastyreflect.ReflectionCompilerInterface.unpickleExpr(ReflectionCompilerInterface.scala:38)
[error]   |     at scala.runtime.quoted.Unpickler$.unpickleExpr$direct(Unpickler.scala:16)
[error]   |     at example.MyClassMaker$.makeImpl(TypeclassMaker.scala:17)

expectation

This should compile and produce an instance of MyClass.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions