Skip to content

Constructing a Class-Instance in a Macro Fails #8115

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

Closed
deusaquilus opened this issue Jan 29, 2020 · 3 comments
Closed

Constructing a Class-Instance in a Macro Fails #8115

deusaquilus opened this issue Jan 29, 2020 · 3 comments
Assignees

Comments

@deusaquilus
Copy link
Contributor

deusaquilus commented Jan 29, 2020

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.

@deusaquilus
Copy link
Contributor Author

The reason I am exploring this approach is to be able to solve this issue:
https://contributors.scala-lang.org/t/need-a-way-to-pass-constructor-arg-values-into-macros-for-quill/3997

@nicolasstucki
Copy link
Contributor

Have you tried the following?

'{
  class MyClass2 extends MyClass { }   /* eventually I want to add properties inside */
  new MyClass2
}

@nicolasstucki
Copy link
Contributor

It looks like it is already fixed

liufengyun added a commit that referenced this issue Jan 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants