Skip to content

Commit dd0a803

Browse files
committed
Implement SearchResult.show
1 parent 801554c commit dd0a803

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Texts._, Types._, Flags._, Names._, Symbols._, NameOps._, Constants._, De
66
import Contexts.Context, Scopes.Scope, Denotations.Denotation, Annotations.Annotation
77
import StdNames.{nme, tpnme}
88
import ast.Trees._, ast._
9+
import typer.Implicits._
910
import config.Config
1011
import java.lang.Integer.toOctalString
1112
import config.Config.summarizeDepth
@@ -484,6 +485,23 @@ class PlainPrinter(_ctx: Context) extends Printer {
484485
}
485486
}.close // todo: override in refined printer
486487

488+
def toText(result: SearchResult): Text = result match {
489+
case result: SearchSuccess =>
490+
"SearchSuccess: " ~ toText(result.ref) ~ " via " ~ toText(result.tree)
491+
case result: NonMatchingImplicit =>
492+
"NoImplicitMatches"
493+
case result: DivergingImplicit =>
494+
"Diverging Implicit"
495+
case result: ShadowedImplicit =>
496+
"Shadowed Implicit"
497+
case result: FailedImplicit =>
498+
"Failed Implicit"
499+
case result: AmbiguousImplicits =>
500+
"Ambiguous Implicit: " ~ toText(result.alt1) ~ " and " ~ toText(result.alt2)
501+
case _ =>
502+
"?Unknown Implicit Result?"
503+
}
504+
487505
private var maxSummarized = Int.MaxValue
488506

489507
def summarized[T](depth: Int)(op: => T): T = {

compiler/src/dotty/tools/dotc/printing/Printer.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import core._
55
import Texts._, ast.Trees._
66
import Types.Type, Symbols.Symbol, Contexts.Context, Scopes.Scope, Constants.Constant,
77
Names.Name, Denotations._, Annotations.Annotation
8+
import typer.Implicits.SearchResult
89

910
/** The base class of all printers
1011
*/
@@ -94,7 +95,10 @@ abstract class Printer {
9495
/** Textual representation of tree */
9596
def toText[T >: Untyped](tree: Tree[T]): Text
9697

97-
/** Perform string or text-producing operation `op` so that only a
98+
/** Textual representation of implicit search result */
99+
def toText(result: SearchResult): Text
100+
101+
/** Perform string or text-producing operation `op` so that only a
98102
* summarized text with given recursion depth is shown
99103
*/
100104
def summarized[T](depth: Int)(op: => T): T

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import core._
66
import ast.{Trees, untpd, tpd, TreeInfo}
77
import util.Positions._
88
import util.Stats.{track, record, monitored}
9-
import printing.Showable
9+
import printing.{Showable, Printer}
10+
import printing.Texts._
1011
import Contexts._
1112
import Types._
1213
import Flags._
@@ -219,14 +220,16 @@ object Implicits {
219220
}
220221

221222
/** The result of an implicit search */
222-
abstract class SearchResult
223+
abstract class SearchResult extends Showable {
224+
def toText(printer: Printer): Text = printer.toText(this)
225+
}
223226

224227
/** A successful search
225228
* @param ref The implicit reference that succeeded
226229
* @param tree The typed tree that needs to be inserted
227230
* @param ctx The context after the implicit search
228231
*/
229-
case class SearchSuccess(tree: tpd.Tree, ref: TermRef, level: Int, tstate: TyperState) extends SearchResult {
232+
case class SearchSuccess(tree: tpd.Tree, ref: TermRef, level: Int, tstate: TyperState) extends SearchResult with Showable {
230233
override def toString = s"SearchSuccess($tree, $ref, $level)"
231234
}
232235

@@ -256,7 +259,7 @@ object Implicits {
256259
}
257260

258261
/** An ambiguous implicits failure */
259-
class AmbiguousImplicits(alt1: TermRef, alt2: TermRef, val pt: Type, val argument: tpd.Tree) extends ExplainedSearchFailure {
262+
class AmbiguousImplicits(val alt1: TermRef, val alt2: TermRef, val pt: Type, val argument: tpd.Tree) extends ExplainedSearchFailure {
260263
def explanation(implicit ctx: Context): String =
261264
em"both ${err.refStr(alt1)} and ${err.refStr(alt2)} $qualify"
262265
override def postscript(implicit ctx: Context) =
@@ -604,6 +607,7 @@ trait Implicits { self: Typer =>
604607
result match {
605608
case result: SearchSuccess =>
606609
result.tstate.commit()
610+
implicits.println(i"committing ${result.tstate.constraint} yielding ${ctx.typerState.constraint} ${ctx.typerState.hashesStr}")
607611
result
608612
case result: AmbiguousImplicits =>
609613
val deepPt = pt.deepenProto

0 commit comments

Comments
 (0)