Skip to content

Commit

Permalink
Improve error message for CyclicReference in macros (#16749)
Browse files Browse the repository at this point in the history
Closes #16582
  • Loading branch information
nicolasstucki authored Feb 16, 2023
2 parents 0e5d922 + ad4abba commit 221bae8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
9 changes: 8 additions & 1 deletion compiler/src/dotty/tools/dotc/quoted/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import dotty.tools.dotc.typer.ImportInfo.withRootImports
import dotty.tools.dotc.util.SrcPos
import dotty.tools.dotc.reporting.Message
import dotty.tools.repl.AbstractFileClassLoader
import dotty.tools.dotc.core.CyclicReference

/** Tree interpreter for metaprogramming constructs */
class Interpreter(pos: SrcPos, classLoader0: ClassLoader)(using Context):
Expand Down Expand Up @@ -252,8 +253,14 @@ class Interpreter(pos: SrcPos, classLoader0: ClassLoader)(using Context):
}
val shortStackTrace = targetException.getStackTrace.take(end + 1)
targetException.setStackTrace(shortStackTrace)
targetException.printStackTrace(new PrintWriter(sw))

targetException match
case _: CyclicReference => sw.write("\nSee full stack trace using -Ydebug")
case _ =>
} else {
targetException.printStackTrace(new PrintWriter(sw))
}
targetException.printStackTrace(new PrintWriter(sw))
sw.write("\n")
throw new StopInterpretation(sw.toString.toMessage, pos)
}
Expand Down
15 changes: 15 additions & 0 deletions tests/neg-macros/i16582.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

-- Error: tests/neg-macros/i16582/Test_2.scala:5:27 --------------------------------------------------------------------
5 | val o2 = ownerDoesNotWork(2) // error
| ^^^^^^^^^^^^^^^^^^^
| Exception occurred while executing macro expansion.
| dotty.tools.dotc.core.CyclicReference: Recursive value o2 needs type
|
| See full stack trace using -Ydebug
|---------------------------------------------------------------------------------------------------------------------
|Inline stack trace
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|This location contains code that was inlined from Macro_1.scala:7
7 | ${ownerWorksImpl('in)}
| ^^^^^^^^^^^^^^^^^^^^^^
---------------------------------------------------------------------------------------------------------------------
29 changes: 29 additions & 0 deletions tests/neg-macros/i16582/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import scala.quoted.*

inline def ownerWorks(in: Int): Any =
${ownerWorksImpl('in)}

transparent inline def ownerDoesNotWork(in: Int): Any =
${ownerWorksImpl('in)}

def ownerWorksImpl(in: Expr[Int])(using Quotes): Expr[String] =
import quotes.reflect.*
val position = Position.ofMacroExpansion
val file = position.sourceFile
val owner0 = Symbol.spliceOwner.maybeOwner
println("owner0 = " + owner0)
val ownerName = owner0.tree match {
case ValDef(name, _, _) =>
name
case DefDef(name, _, _, _) =>
name
case t => report.errorAndAbort(s"unexpected tree shape: ${t.show}")
}
val path = file.path
val line = position.startLine
val column = position.startColumn
val v = in.valueOrAbort
val out = Expr(s"val $ownerName $v: $file @ ${position.startLine}")
out


6 changes: 6 additions & 0 deletions tests/neg-macros/i16582/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def test=
val o1 = ownerWorks(1)
println(o1)

val o2 = ownerDoesNotWork(2) // error
println(o2)

0 comments on commit 221bae8

Please sign in to comment.