Skip to content

Unresolved symbols: when pickling file containing an anonymous class. #8651

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
SimonCommits opened this issue Apr 1, 2020 · 4 comments · Fixed by #8796
Closed

Unresolved symbols: when pickling file containing an anonymous class. #8651

SimonCommits opened this issue Apr 1, 2020 · 4 comments · Fixed by #8796
Assignees

Comments

@SimonCommits
Copy link

SimonCommits commented Apr 1, 2020

Hello,

In some file I defined an abstract class

abstract class Coroutine[+T] {
  def continue: Option[T] 
}

In another file of the same package I created an anonymous implementation of Coroutine. The continue implementation references a variable state defined within the anonymous class

object Macros {
  
 import scala.quoted._
 import scala.quoted.matching._ 
    

 inline def coroutine[T](inline body: Any): Coroutine[T] = ${ coroutineImpl('{body}) }

 def coroutineImpl[T: Type](expr: Expr[_ <: Any])(implicit qtx: QuoteContext): Expr[Coroutine[T]] = {
   import qtx.tasty.{_, given _}

   '{
     new Coroutine[T] { 
       var state: Int = 0 

       def continue: Option[T] = ${
         '{
           state = 1 //if this line is commented there are no compile errors anymore!!!
           None
         }
       }
       
     }
   }

 }
}

My aim was initially to create a function continue which when called would go to the next state (by increasing the state variable which is the root of the problem here) of the coroutine class and return the value yielded at the current state. I minimized the code so it does not make any semantical sense any more (the value returned is None and the state only gets updated to 1). Also with this minimized code an additional warning spawns on the compiler log.

Compiling those two files provokes an error. This error disappears whenever the reference to the state variable within the expression built in the continue method is removed.
So this reference seems to be the root of the error:
|Canceled quote directly inside a splice. ${ '{ XYZ } } is equivalent to XYZ

Initially I referenced the state variable within a foldLeft that was within the splice of the continue method. This caused the same error(without the warning) whenever I referenced state within the foldLeft.

Here is the truncated error output (ignore the warning):

sbt:dotty-simple> compile
[info] Compiling 1 Scala source to /home/snif/hackerspace/semester/bugReproduction/target/scala-0.22/classes ...
[warn] -- Warning: /home/snif/hackerspace/semester/bugReproduction/src/main/scala/Macros.scala:18:34 
[warn] 18 |        def continue: Option[T] = ${
[warn]    |                                  ^
[warn]    |Canceled quote directly inside a splice. ${ '{ XYZ } } is equivalent to XYZ.
[warn] 19 |          '{
[warn] 20 |            state = 1
[warn] 21 |            None
[warn] 22 |          }
[warn] 23 |        }
[info] exception occurred while compiling /home/snif/hackerspace/semester/bugReproduction/src/main/scala/Macros.scala
java.lang.AssertionError: assertion failed: unresolved symbols: anonymous class coroutines.Coroutine[T] {...}(line 14) when pickling /home/snif/hackerspace/semester/bugReproduc
tion/src/main/scala/Macros.scala while compiling /home/snif/hackerspace/semester/bugReproduction/src/main/scala/Macros.scala
[error] ## Exception when compiling 2 sources to /home/snif/hackerspace/semester/bugReproduction/target/scala-0.22/classes
[error] java.lang.AssertionError: assertion failed: unresolved symbols: anonymous class coroutines.Coroutine[T] {...}(line 14) when pickling /home/snif/hackerspace/semester/bug
Reproduction/src/main/scala/Macros.scala
[error] dotty.DottyPredef$.assertFail(DottyPredef.scala:17)
[error] dotty.tools.dotc.core.tasty.TreePickler.pickle(TreePickler.scala:715)
[error] dotty.tools.dotc.core.quoted.PickledQuotes$.pickle(PickledQuotes.scala:102)
[error] dotty.tools.dotc.core.quoted.PickledQuotes$.pickleQuote(PickledQuotes.scala:34)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.pickleAsTasty$1(ReifyQuotes.scala:236)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.pickledQuote(ReifyQuotes.scala:248)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.transformQuotation(ReifyQuotes.scala:196)
[error] dotty.tools.dotc.transform.TreeMapWithStages.op$1(TreeMapWithStages.scala:94)
[error] dotty.tools.dotc.transform.TreeMapWithStages.transform(TreeMapWithStages.scala:120)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.op$1(ReifyQuotes.scala:415)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.transform(ReifyQuotes.scala:417)
[error] dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1251)
[error] dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:117)
[error] dotty.tools.dotc.transform.TreeMapWithStages.mapOverTree$1(TreeMapWithStages.scala:77)
[error] dotty.tools.dotc.transform.TreeMapWithStages.op$1(TreeMapWithStages.scala:118)
[error] dotty.tools.dotc.transform.TreeMapWithStages.transform(TreeMapWithStages.scala:120)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.op$1(ReifyQuotes.scala:415)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.transform(ReifyQuotes.scala:417)
[error] dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1257)
[error] dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:117)
[error] dotty.tools.dotc.transform.TreeMapWithStages.mapOverTree$1(TreeMapWithStages.scala:77)
[error] dotty.tools.dotc.transform.TreeMapWithStages.op$1(TreeMapWithStages.scala:118)
[error] dotty.tools.dotc.transform.TreeMapWithStages.transform(TreeMapWithStages.scala:120)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.op$1(ReifyQuotes.scala:415)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.transform(ReifyQuotes.scala:417)
[error] dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:97)
[error] dotty.tools.dotc.transform.TreeMapWithStages.mapOverTree$1(TreeMapWithStages.scala:77)
[error] dotty.tools.dotc.transform.TreeMapWithStages.op$1(TreeMapWithStages.scala:118)
[error] dotty.tools.dotc.transform.TreeMapWithStages.transform(TreeMapWithStages.scala:120)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.op$1(ReifyQuotes.scala:415)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.transform(ReifyQuotes.scala:417)
[error] dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform$$anonfun$2(Trees.scala:1347)
[error] scala.collection.immutable.List.mapConserve(List.scala:444)
[error] dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1347)
[error] dotty.tools.dotc.ast.Trees$Instance$TreeMap.transformStats(Trees.scala:1345)
[error] dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1271)
[error] dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:89)
[error] dotty.tools.dotc.transform.TreeMapWithStages.mapOverTree$1(TreeMapWithStages.scala:77)
[error] dotty.tools.dotc.transform.TreeMapWithStages.op$1(TreeMapWithStages.scala:106)
[error] dotty.tools.dotc.transform.TreeMapWithStages.transform(TreeMapWithStages.scala:120)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.op$1(ReifyQuotes.scala:415)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.transform(ReifyQuotes.scala:417)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.transformWithCapturer(ReifyQuotes.scala:355)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.body$3(ReifyQuotes.scala:329)
[error] dotty.tools.dotc.transform.ReifyQuotes$QuoteReifier.$anonfun$1(ReifyQuotes.scala:339)
...
@liufengyun
Copy link
Contributor

Does the error persist if you change continue to the following:

       def continue: Option[T] = {
           state = 1
           None
       }

@liufengyun
Copy link
Contributor

Just tried, with the code above, the crash does not happen. So the issue is with the nested quote which refers to a local definition in an outer quoted block.

/cc: @nicolasstucki

@liufengyun
Copy link
Contributor

liufengyun commented Apr 2, 2020

@LeDevDuDimanche You may workaround the issue with the following change:

abstract class Coroutine[+T] {
  protected var state: Int
  def continue: Option[T]
}

object Macros {

 import scala.quoted._
 import scala.quoted.matching._


 inline def coroutine[T](inline body: Any): Coroutine[T] = ${ coroutineImpl('{body}) }

 def coroutineImpl[T: Type](expr: Expr[_ <: Any])(implicit qtx: QuoteContext): Expr[Coroutine[T]] = {
   import qtx.tasty._

   '{
     new Coroutine[T] {
       def continue: Option[T] = {
         val self: Coroutine[T] = this
         ${
           '{
             self.state = 1
             None
            }
          }
       }
     }
   }
 }
}

@nicolasstucki
Copy link
Contributor

Minimized to

import scala.quoted._
def coroutineImpl(using QuoteContext): Expr[Any] =
  '{
    new {
      def state: Int = 0
      ${'state}
    }
  }

We seem to not be identifying this.state as a capture nor its level and then the following tests fails
https://github.com/lampepfl/dotty/blob/fd7ff85c4296c4a72d238a2bad6d187200052db7/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala#L311

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Apr 29, 2020
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Apr 29, 2020
Fix scala#6140, fix scala#6772, fix scala#7030, fix scala#7892, fix scala#7997, fix scala#8651 and improve scala#8100.

Differences with previous implementation
* Only track and check levels within quotes or splices
* Track levels of all symbols not at level 0
* Split level checking into specialized variants for types and terms (healType/healTermType)
* Detect inconsistent types rather than try to detect consistent ones
* Check/heal term inconsistencies only on leaf nodes (TypeTree, RefTree, Ident, This)
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue May 4, 2020
Fix scala#6140, fix scala#6772, fix scala#7030, fix scala#7892, fix scala#7997, fix scala#8651 and improve scala#8100.

Differences with previous implementation
* Only track and check levels within quotes or splices
* Track levels of all symbols not at level 0
* Split level checking into specialized variants for types and terms (healType/healTermType)
* Detect inconsistent types rather than try to detect consistent ones
* Check/heal term inconsistencies only on leaf nodes (TypeTree, RefTree, Ident, This)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants