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

Improve error message for CyclicReference in macros #16749

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, classLoader: ClassLoader)(using Context):
Expand Down Expand Up @@ -253,8 +254,14 @@ class Interpreter(pos: SrcPos, classLoader: 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-Y is supposed to be for internal compiler flags. Ideally we would use -X -or -V which is meant for things that just increase verbosity I think)

|---------------------------------------------------------------------------------------------------------------------
|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)