Skip to content

Commit 221bae8

Browse files
Improve error message for CyclicReference in macros (#16749)
Closes #16582
2 parents 0e5d922 + ad4abba commit 221bae8

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

compiler/src/dotty/tools/dotc/quoted/Interpreter.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import dotty.tools.dotc.typer.ImportInfo.withRootImports
3030
import dotty.tools.dotc.util.SrcPos
3131
import dotty.tools.dotc.reporting.Message
3232
import dotty.tools.repl.AbstractFileClassLoader
33+
import dotty.tools.dotc.core.CyclicReference
3334

3435
/** Tree interpreter for metaprogramming constructs */
3536
class Interpreter(pos: SrcPos, classLoader0: ClassLoader)(using Context):
@@ -252,8 +253,14 @@ class Interpreter(pos: SrcPos, classLoader0: ClassLoader)(using Context):
252253
}
253254
val shortStackTrace = targetException.getStackTrace.take(end + 1)
254255
targetException.setStackTrace(shortStackTrace)
256+
targetException.printStackTrace(new PrintWriter(sw))
257+
258+
targetException match
259+
case _: CyclicReference => sw.write("\nSee full stack trace using -Ydebug")
260+
case _ =>
261+
} else {
262+
targetException.printStackTrace(new PrintWriter(sw))
255263
}
256-
targetException.printStackTrace(new PrintWriter(sw))
257264
sw.write("\n")
258265
throw new StopInterpretation(sw.toString.toMessage, pos)
259266
}

tests/neg-macros/i16582.check

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
-- Error: tests/neg-macros/i16582/Test_2.scala:5:27 --------------------------------------------------------------------
3+
5 | val o2 = ownerDoesNotWork(2) // error
4+
| ^^^^^^^^^^^^^^^^^^^
5+
| Exception occurred while executing macro expansion.
6+
| dotty.tools.dotc.core.CyclicReference: Recursive value o2 needs type
7+
|
8+
| See full stack trace using -Ydebug
9+
|---------------------------------------------------------------------------------------------------------------------
10+
|Inline stack trace
11+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
12+
|This location contains code that was inlined from Macro_1.scala:7
13+
7 | ${ownerWorksImpl('in)}
14+
| ^^^^^^^^^^^^^^^^^^^^^^
15+
---------------------------------------------------------------------------------------------------------------------

tests/neg-macros/i16582/Macro_1.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import scala.quoted.*
2+
3+
inline def ownerWorks(in: Int): Any =
4+
${ownerWorksImpl('in)}
5+
6+
transparent inline def ownerDoesNotWork(in: Int): Any =
7+
${ownerWorksImpl('in)}
8+
9+
def ownerWorksImpl(in: Expr[Int])(using Quotes): Expr[String] =
10+
import quotes.reflect.*
11+
val position = Position.ofMacroExpansion
12+
val file = position.sourceFile
13+
val owner0 = Symbol.spliceOwner.maybeOwner
14+
println("owner0 = " + owner0)
15+
val ownerName = owner0.tree match {
16+
case ValDef(name, _, _) =>
17+
name
18+
case DefDef(name, _, _, _) =>
19+
name
20+
case t => report.errorAndAbort(s"unexpected tree shape: ${t.show}")
21+
}
22+
val path = file.path
23+
val line = position.startLine
24+
val column = position.startColumn
25+
val v = in.valueOrAbort
26+
val out = Expr(s"val $ownerName $v: $file @ ${position.startLine}")
27+
out
28+
29+

tests/neg-macros/i16582/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def test=
2+
val o1 = ownerWorks(1)
3+
println(o1)
4+
5+
val o2 = ownerDoesNotWork(2) // error
6+
println(o2)

0 commit comments

Comments
 (0)