Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,172 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/compiler/scala/tools/nsc/typechecker/splain/Colors.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala.tools.nsc | ||
package typechecker | ||
package splain | ||
|
||
trait StringColor | ||
{ | ||
def color(s: String, col: String): String | ||
} | ||
|
||
object StringColors | ||
{ | ||
implicit val noColor = | ||
new StringColor { | ||
def color(s: String, col: String) = s | ||
} | ||
|
||
implicit val color = | ||
new StringColor { | ||
import Console.RESET | ||
|
||
def color(s: String, col: String) = col + s + RESET | ||
} | ||
} | ||
|
||
object StringColor | ||
{ | ||
implicit class StringColorOps(s: String)(implicit sc: StringColor) | ||
{ | ||
import Console._ | ||
def red = sc.color(s, RED) | ||
def green = sc.color(s, GREEN) | ||
def yellow = sc.color(s, YELLOW) | ||
def blue = sc.color(s, BLUE) | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/compiler/scala/tools/nsc/typechecker/splain/SplainData.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala.tools.nsc | ||
package typechecker | ||
package splain | ||
|
||
import scala.util.matching.Regex | ||
|
||
trait SplainData { self: Analyzer => | ||
|
||
import global._ | ||
|
||
sealed trait ImplicitErrorSpecifics | ||
|
||
object ImplicitErrorSpecifics | ||
{ | ||
case class NotFound(param: Symbol) | ||
extends ImplicitErrorSpecifics | ||
|
||
case class NonconformantBounds(targs: List[Type], tparams: List[Symbol], originalError: Option[AbsTypeError]) | ||
extends ImplicitErrorSpecifics | ||
} | ||
|
||
object ImplicitErrors | ||
{ | ||
var stack: List[Type] = Nil | ||
|
||
var errors: List[ImplicitError] = Nil | ||
|
||
def push(error: ImplicitError): Unit = errors = error :: errors | ||
|
||
def nesting: Int = stack.length - 1 | ||
|
||
def nested: Boolean = stack.nonEmpty | ||
|
||
def removeErrorsFor(tpe: Type): Unit = errors = errors.dropWhile(_.tpe == tpe) | ||
|
||
def startSearch(expectedType: Type): Unit = { | ||
if (!nested) errors = List() | ||
stack = expectedType :: stack | ||
} | ||
|
||
def finishSearch(success: Boolean, expectedType: Type): Unit = { | ||
if (success) removeErrorsFor(expectedType) | ||
stack = stack.drop(1) | ||
} | ||
} | ||
|
||
case class ImplicitError(tpe: Type, candidate: Tree, nesting: Int, specifics: ImplicitErrorSpecifics) | ||
{ | ||
override def equals(other: Any) = other match { | ||
case o: ImplicitError => | ||
o.tpe.toString == tpe.toString && ImplicitError.candidateName(this) == ImplicitError.candidateName(o) | ||
case _ => false | ||
} | ||
|
||
override def hashCode = (tpe.toString.hashCode, ImplicitError.candidateName(this).hashCode).hashCode | ||
|
||
override def toString: String = | ||
s"NotFound(${ImplicitError.shortName(tpe.toString)}, ${ImplicitError.shortName(candidate.toString)}), $nesting, $specifics)" | ||
} | ||
|
||
object ImplicitError | ||
{ | ||
def notFound(tpe: Type, candidate: Tree, nesting: Int)(param: Symbol): ImplicitError = | ||
ImplicitError(tpe, candidate, nesting, ImplicitErrorSpecifics.NotFound(param)) | ||
|
||
def nonconformantBounds | ||
(tpe: Type, candidate: Tree, nesting: Int) | ||
(targs: List[Type], tparams: List[Symbol], originalError: Option[AbsTypeError]) | ||
: ImplicitError = | ||
ImplicitError(tpe, candidate, nesting, ImplicitErrorSpecifics.NonconformantBounds(targs, tparams, originalError)) | ||
|
||
def unapplyCandidate(e: ImplicitError): Tree = | ||
e.candidate match { | ||
case TypeApply(name, _) => name | ||
case a => a | ||
} | ||
|
||
def candidateName(e: ImplicitError): String = | ||
unapplyCandidate(e) match { | ||
case Select(_, name) => name.toString | ||
case Ident(name) => name.toString | ||
case a => a.toString | ||
} | ||
|
||
val candidateRegex: Regex = """.*\.this\.(.*)""".r | ||
|
||
def cleanCandidate(e: ImplicitError): String = | ||
unapplyCandidate(e).toString match { | ||
case candidateRegex(suf) => suf | ||
case a => a | ||
} | ||
|
||
def shortName(ident: String): String = ident.split('.').toList.lastOption.getOrElse(ident) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/compiler/scala/tools/nsc/typechecker/splain/SplainDiagnostics.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala.tools.nsc | ||
package typechecker | ||
package splain | ||
|
||
import scala.util.control.NonFatal | ||
|
||
trait SplainDiagnostics | ||
extends SplainFormatting | ||
{ self: Analyzer with SplainData => | ||
import global._ | ||
|
||
def showStats[A](desc: String, run: => A): A = { | ||
val ret = run | ||
if (sys.env.contains("SPLAIN_CACHE_STATS")) | ||
reporter.echo(s"$desc entries/hits: $cacheStats") | ||
ret | ||
} | ||
|
||
def foundReqMsgShort(found: Type, req: Type): Option[TypeRepr] = | ||
try { | ||
Some(showStats("foundreq", showFormattedL(formatDiff(found, req, true), true))) | ||
} catch { | ||
case NonFatal(e) => | ||
None | ||
} | ||
|
||
def splainFoundReqMsg(found: Type, req: Type): Option[String] = | ||
if (!settings.splainSettingEnable || settings.splainSettingNoFoundReq) None | ||
else foundReqMsgShort(found, req).map(a => ";\n" + a.indent.joinLines) | ||
} |
Oops, something went wrong.