Skip to content

Commit b019a65

Browse files
zeptometerKordyjan
authored andcommitted
Introduce a new compiler option -Ydebug-macros
This compiler option shows debug information when quote pattern matching fails. This commmit also fix a bug of current imiplementation of debug print [Cherry-picked be0c98a]
1 parent 8454909 commit b019a65

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,5 +397,7 @@ private sealed trait YSettings:
397397
val YinstrumentDefs: Setting[Boolean] = BooleanSetting("-Yinstrument-defs", "Add instrumentation code that counts method calls; needs -Yinstrument to be set, too.")
398398

399399
val YforceInlineWhileTyping: Setting[Boolean] = BooleanSetting("-Yforce-inline-while-typing", "Make non-transparent inline methods inline when typing. Emulates the old inlining behavior of 3.0.0-M3.")
400+
401+
val YdebugMacros: Setting[Boolean] = BooleanSetting("-Ydebug-macros", "Show debug info when quote pattern match fails")
400402
end YSettings
401403

compiler/src/scala/quoted/runtime/impl/QuoteMatcher.scala

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,9 @@ import dotty.tools.dotc.util.optional
102102
*
103103
* ```
104104
*/
105-
object QuoteMatcher {
105+
class QuoteMatcher(debug: Boolean) {
106106
import tpd.*
107107

108-
// TODO use flag from Context. Maybe -debug or add -debug-macros
109-
private inline val debug = false
110-
111108
/** Sequence of matched expressions.
112109
* These expressions are part of the scrutinee and will be bound to the quote pattern term splices.
113110
*/
@@ -134,7 +131,6 @@ object QuoteMatcher {
134131
given Env = Map.empty
135132
scrutinee =?= pat1
136133
}.map { matchings =>
137-
import QuoteMatcher.MatchResult.*
138134
lazy val spliceScope = SpliceScope.getCurrent
139135
// After matching and doing all subtype checks, we have to approximate all the type bindings
140136
// that we have found, seal them in a quoted.Type and add them to the result
@@ -234,7 +230,7 @@ object QuoteMatcher {
234230
case _ => None
235231
end TypeTreeTypeTest
236232

237-
val res = pattern match
233+
def runMatch(): optional[MatchingExprs] = pattern match
238234

239235
/* Term hole */
240236
// Match a scala.internal.Quoted.patternHole typed as a repeated argument and return the scrutinee tree
@@ -426,24 +422,32 @@ object QuoteMatcher {
426422
// No Match
427423
case _ =>
428424
notMatched
425+
end runMatch
426+
427+
if debug then
428+
try {
429+
runMatch()
430+
} catch {
431+
case e: util.boundary.Break[?] =>
432+
val quotes = QuotesImpl()
433+
println(
434+
s""">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
435+
|Scrutinee
436+
| ${scrutinee.show}
437+
|did not match pattern
438+
| ${pattern.show}
439+
|
440+
|with environment: ${summon[Env]}
441+
|
442+
|Scrutinee: ${quotes.reflect.Printer.TreeStructure.show(scrutinee.asInstanceOf)}
443+
|Pattern: ${quotes.reflect.Printer.TreeStructure.show(pattern.asInstanceOf)}
444+
|
445+
|""".stripMargin)
446+
throw e
447+
}
448+
else
449+
runMatch()
429450

430-
if (debug && res == notMatched)
431-
val quotes = QuotesImpl()
432-
println(
433-
s""">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
434-
|Scrutinee
435-
| ${scrutinee.show}
436-
|did not match pattern
437-
| ${pattern.show}
438-
|
439-
|with environment: ${summon[Env]}
440-
|
441-
|Scrutinee: ${quotes.reflect.Printer.TreeStructure.show(scrutinee.asInstanceOf)}
442-
|Pattern: ${quotes.reflect.Printer.TreeStructure.show(pattern.asInstanceOf)}
443-
|
444-
|""".stripMargin)
445-
446-
res
447451
end =?=
448452

449453
end extension

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ object QuotesImpl {
3939
class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler, QuoteMatching:
4040

4141
private val xCheckMacro: Boolean = ctx.settings.XcheckMacros.value
42+
private val yDebugMacro: Boolean = ctx.settings.YdebugMacros.value
4243

4344
extension [T](self: scala.quoted.Expr[T])
4445
def show: String =
4546
reflect.Printer.TreeCode.show(reflect.asTerm(self))
4647

4748
def matches(that: scala.quoted.Expr[Any]): Boolean =
48-
QuoteMatcher.treeMatch(reflect.asTerm(self), reflect.asTerm(that)).nonEmpty
49+
QuoteMatcher(yDebugMacro).treeMatch(reflect.asTerm(self), reflect.asTerm(that)).nonEmpty
4950

5051
def valueOrAbort(using fromExpr: FromExpr[T]): T =
5152
def reportError =
@@ -3155,14 +3156,14 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
31553156
def unapply[TypeBindings, Tup <: Tuple](scrutinee: scala.quoted.Expr[Any])(using pattern: scala.quoted.Expr[Any]): Option[Tup] =
31563157
val scrutineeTree = reflect.asTerm(scrutinee)
31573158
val patternTree = reflect.asTerm(pattern)
3158-
QuoteMatcher.treeMatch(scrutineeTree, patternTree).asInstanceOf[Option[Tup]]
3159+
QuoteMatcher(yDebugMacro).treeMatch(scrutineeTree, patternTree).asInstanceOf[Option[Tup]]
31593160
end ExprMatch
31603161

31613162
object TypeMatch extends TypeMatchModule:
31623163
def unapply[TypeBindings, Tup <: Tuple](scrutinee: scala.quoted.Type[?])(using pattern: scala.quoted.Type[?]): Option[Tup] =
31633164
val scrutineeTree = reflect.TypeTree.of(using scrutinee)
31643165
val patternTree = reflect.TypeTree.of(using pattern)
3165-
QuoteMatcher.treeMatch(scrutineeTree, patternTree).asInstanceOf[Option[Tup]]
3166+
QuoteMatcher(yDebugMacro).treeMatch(scrutineeTree, patternTree).asInstanceOf[Option[Tup]]
31663167
end TypeMatch
31673168

31683169
end QuotesImpl

0 commit comments

Comments
 (0)