Skip to content
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

Cannot call a method from super class in a macro #13105

Open
adamw opened this issue Jul 19, 2021 · 2 comments
Open

Cannot call a method from super class in a macro #13105

adamw opened this issue Jul 19, 2021 · 2 comments
Assignees

Comments

@adamw
Copy link
Contributor

adamw commented Jul 19, 2021

Compiler version

3.0.1

Minimized code

I'm looking for a way to call a method defined in an extended trait from a macro. My first attempt was to use Ref() e.g.

// Main.scala

trait Instances {
  def s: String = "STRING"
}

object Test extends Instances {
  def sMacro = Macros.f()

  def main(args: Array[String]): Unit = {
    println(sMacro)
  }
}

// Macros.scala

import scala.quoted.*

object Macros {
  inline def f(): String = ${ fImpl }

  def fImpl(using q: Quotes): Expr[String] = {
    import q.reflect.*

    val classSymbol = Symbol.spliceOwner.owner.owner
    classSymbol.memberMethods.find(_.name == "s") match {
      case None => '{""}
      case Some(s) => Ref(s).asExprOf[String]
    }
  }
}

Output

exception while typing Instances.this of class class dotty.tools.dotc.ast.Trees$This # -1
exception while typing Instances.this.s of class class dotty.tools.dotc.ast.Trees$Select # -1
exception while typing Instances.this.s:String of class class dotty.tools.dotc.ast.Trees$Typed # -1
exception while typing Instances.this.s:String of class class dotty.tools.dotc.ast.Trees$Inlined # -1
exception while typing def sMacro: String = Instances.this.s:String of class class dotty.tools.dotc.ast.Trees$DefDef # -1
exception while typing @scala.annotation.internal.SourceFile("src/main/scala/Main.scala") final module 
  class
 Test$() extends Object(), Instances {
  private def writeReplace(): AnyRef = 
    new scala.runtime.ModuleSerializationProxy(classOf[Test.type])
  def sMacro: String = Instances.this.s:String
} of class class dotty.tools.dotc.ast.Trees$TypeDef # -1
exception while typing package <empty> {
  @scala.annotation.internal.SourceFile("src/main/scala/Main.scala") trait 
    Instances
  () extends Object {
    def s: String = "STRING"
  }
  final lazy module val Test: Test$ = new Test$()
  @scala.annotation.internal.SourceFile("src/main/scala/Main.scala") final 
    module
   class Test$() extends Object(), Instances {
    private def writeReplace(): AnyRef = 
      new scala.runtime.ModuleSerializationProxy(classOf[Test.type])
    def sMacro: String = Instances.this.s:String
  }
  final lazy module val Main$package: Main$package$ = new Main$package$()
  @scala.annotation.internal.SourceFile("src/main/scala/Main.scala") final 
    module
   class Main$package$() extends Object() {
    private def writeReplace(): AnyRef = 
      new scala.runtime.ModuleSerializationProxy(classOf[Main$package.type])
    @main() def main: Unit = 
      {
        println(Test.sMacro)
      }
  }
  @scala.annotation.internal.SourceFile("src/main/scala/Main.scala") final class
     
  main() extends Object() {
    <static> def main(args: Array[String]): Unit = 
      try main catch 
        {
          case error @ _:scala.util.CommandLineParser.ParseError => 
            scala.util.CommandLineParser.showError(error)
        }
  }
} of class class dotty.tools.dotc.ast.Trees$PackageDef # -1
java.lang.AssertionError: assertion failed: asTerm called on not-a-Term val <none> while compiling /Users/mborek/playground/sameFileError/samefileerror/src/main/scala/Main.scala, /Users/mborek/playground/sameFileError/samefileerror/src/main/scala/MainMacro.scala
[error] ## Exception when compiling 4 sources to /Users/mborek/playground/sameFileError/samefileerror/target/scala-3.0.1-RC2/classes
[error] java.lang.AssertionError: assertion failed: asTerm called on not-a-Term val <none>
[error] scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
[error] dotty.tools.dotc.core.Symbols$Symbol.asTerm(Symbols.scala:163)
[error] dotty.tools.dotc.transform.ExplicitOuter$.dotty$tools$dotc$transform$ExplicitOuter$$$outerParamAccessor(ExplicitOuter.scala:230)
[error] dotty.tools.dotc.transform.ExplicitOuter$OuterOps$.loop$1(ExplicitOuter.scala:424)
[error] dotty.tools.dotc.transform.ExplicitOuter$OuterOps$.path$extension(ExplicitOuter.scala:433)
[error] dotty.tools.dotc.transform.Erasure$Typer.typedThis(Erasure.scala:805)
[error] dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2696)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2758)
[error] dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:118)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2823)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2827)
[error] dotty.tools.dotc.transform.Erasure$Typer.typedSelect(Erasure.scala:696)
[error] dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2665)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2757)
[error] dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:118)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2823)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2820)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2827)
[error] dotty.tools.dotc.transform.Erasure$Typer.typedTyped(Erasure.scala:658)
[error] dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2700)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2758)
[error] dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:118)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2823)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2820)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2827)
[error] dotty.tools.dotc.typer.Typer.typedInlined(Typer.scala:1794)
[error] dotty.tools.dotc.transform.Erasure$Typer.typedInlined(Erasure.scala:899)
[error] dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2717)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2758)
[error] dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:118)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2823)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2827)
[error] dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:2943)
[error] dotty.tools.dotc.typer.Typer.$anonfun$34(Typer.scala:2168)
[error] dotty.tools.dotc.typer.PrepareInlineable$.dropInlineIfError(PrepareInlineable.scala:225)
[error] dotty.tools.dotc.typer.Typer.typedDefDef(Typer.scala:2168)
[error] dotty.tools.dotc.transform.Erasure$Typer.typedDefDef(Erasure.scala:955)
[error] dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2672)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2757)
[error] dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:118)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2823)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2827)
[error] dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:2849)
[error] dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:2899)
[error] dotty.tools.dotc.transform.Erasure$Typer.typedStats(Erasure.scala:1053)
[error] dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:2354)
[error] dotty.tools.dotc.transform.Erasure$Typer.typedClassDef(Erasure.scala:1043)
[error] dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$2(Typer.scala:2683)
[error] dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2687)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2757)
[error] dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:118)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2823)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2827)
[error] dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:2849)
[error] dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:2899)
[error] dotty.tools.dotc.transform.Erasure$Typer.typedStats(Erasure.scala:1053)
[error] dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:2477)
[error] dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2728)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2758)
[error] dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:118)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2823)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2827)
[error] dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:2943)
[error] dotty.tools.dotc.transform.Erasure.run(Erasure.scala:132)
[error] dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:303)
[error] scala.collection.immutable.List.map(List.scala:246)
[error] dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:304)
[error] dotty.tools.dotc.Run.runPhases$4$$anonfun$4(Run.scala:205)
[error] scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
[error] scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
[error] scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
[error] dotty.tools.dotc.Run.runPhases$5(Run.scala:216)
[error] dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:224)
[error] scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
[error] dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:67)
[error] dotty.tools.dotc.Run.compileUnits(Run.scala:231)
[error] dotty.tools.dotc.Run.compileUnits(Run.scala:172)
[error] dotty.tools.dotc.Driver.finish(Driver.scala:60)
[error] dotty.tools.dotc.Driver.doCompile(Driver.scala:40)
[error] dotty.tools.xsbt.CompilerBridgeDriver.run(CompilerBridgeDriver.java:88)
[error] dotty.tools.xsbt.CompilerBridge.run(CompilerBridge.java:22)
[error] sbt.internal.inc.AnalyzingCompiler.compile(AnalyzingCompiler.scala:91)
[error] sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$7(MixedAnalyzingCompiler.scala:186)
[error] scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
[error] sbt.internal.inc.MixedAnalyzingCompiler.timed(MixedAnalyzingCompiler.scala:241)
[error] sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4(MixedAnalyzingCompiler.scala:176)
[error] sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4$adapted(MixedAnalyzingCompiler.scala:157)
[error] sbt.internal.inc.JarUtils$.withPreviousJar(JarUtils.scala:239)
[error] sbt.internal.inc.MixedAnalyzingCompiler.compileScala$1(MixedAnalyzingCompiler.scala:157)
[error] sbt.internal.inc.MixedAnalyzingCompiler.compile(MixedAnalyzingCompiler.scala:204)
[error] sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1(IncrementalCompilerImpl.scala:528)
[error] sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1$adapted(IncrementalCompilerImpl.scala:528)
[error] sbt.internal.inc.Incremental$.$anonfun$apply$5(Incremental.scala:174)
[error] sbt.internal.inc.Incremental$.$anonfun$apply$5$adapted(Incremental.scala:172)
[error] sbt.internal.inc.Incremental$$anon$2.run(Incremental.scala:457)
[error] sbt.internal.inc.IncrementalCommon$CycleState.next(IncrementalCommon.scala:116)
[error] sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:56)
[error] sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:52)
[error] sbt.internal.inc.IncrementalCommon.cycle(IncrementalCommon.scala:261)
[error] sbt.internal.inc.Incremental$.$anonfun$incrementalCompile$8(Incremental.scala:412)
[error] sbt.internal.inc.Incremental$.withClassfileManager(Incremental.scala:499)
[error] sbt.internal.inc.Incremental$.incrementalCompile(Incremental.scala:399)
[error] sbt.internal.inc.Incremental$.apply(Incremental.scala:166)
[error] sbt.internal.inc.IncrementalCompilerImpl.compileInternal(IncrementalCompilerImpl.scala:528)
[error] sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileIncrementally$1(IncrementalCompilerImpl.scala:482)
[error] sbt.internal.inc.IncrementalCompilerImpl.handleCompilationError(IncrementalCompilerImpl.scala:332)
[error] sbt.internal.inc.IncrementalCompilerImpl.compileIncrementally(IncrementalCompilerImpl.scala:420)
[error] sbt.internal.inc.IncrementalCompilerImpl.compile(IncrementalCompilerImpl.scala:137)
[error] sbt.Defaults$.compileIncrementalTaskImpl(Defaults.scala:2346)
[error] sbt.Defaults$.$anonfun$compileIncrementalTask$2(Defaults.scala:2303)
[error] sbt.internal.io.Retry$.apply(Retry.scala:46)
[error] sbt.internal.io.Retry$.apply(Retry.scala:28)
[error] sbt.internal.io.Retry$.apply(Retry.scala:23)
[error] sbt.internal.server.BspCompileTask$.compute(BspCompileTask.scala:31)
[error] sbt.Defaults$.$anonfun$compileIncrementalTask$1(Defaults.scala:2299)
[error] scala.Function1.$anonfun$compose$1(Function1.scala:49)
[error] sbt.internal.util.$tilde$greater.$anonfun$$u2219$1(TypeFunctions.scala:62)
[error] sbt.std.Transform$$anon$4.work(Transform.scala:68)
[error] sbt.Execute.$anonfun$submit$2(Execute.scala:282)
[error] sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:23)
[error] sbt.Execute.work(Execute.scala:291)
[error] sbt.Execute.$anonfun$submit$1(Execute.scala:282)
[error] sbt.ConcurrentRestrictions$$anon$4.$anonfun$submitValid$1(ConcurrentRestrictions.scala:265)
[error] sbt.CompletionService$$anon$2.call(CompletionService.scala:64)
[error] java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
[error] java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
[error] java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
[error] java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
[error] java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
[error] java.base/java.lang.Thread.run(Thread.java:832)

Expectation

Successful compilation

This is a follow-up to #12966

@odersky
Copy link
Contributor

odersky commented Jul 19, 2021

I think in Main.scala it should be Macros.f, not TestMacro.f. With that change I could reproduce the problem. When running with -Ycheck I get

*** error while checking Main_2.scala after phase inlining ***
exception occurred while compiling Main_2.scala
java.lang.AssertionError: assertion failed: error while typing Instances.this, method sMacro is not contained in trait Instances while compiling Main_2.scala

and the checker stack looks like this:

exception while typing Instances.this of class class dotty.tools.dotc.ast.Trees$This # -1
exception while typing Instances.this.s of class class dotty.tools.dotc.ast.Trees$Select # -1
exception while typing Instances.this.s:String of class class dotty.tools.dotc.ast.Trees$Typed # -1
exception while typing Instances.this.s:String of class class dotty.tools.dotc.ast.Trees$Inlined # -1
exception while typing def sMacro: String = Instances.this.s:String of class class dotty.tools.dotc.ast.Trees$DefDef # -1

I am not sure whether this is a bug in Ref, or whether using Ref is wrong here and one should use something else.
@nicolasstucki can you weigh in?

@adamw
Copy link
Contributor Author

adamw commented Jul 19, 2021

@odersky yes, sorry, fixed the example :)

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