Skip to content

Commit

Permalink
add Vtype-def-position option, guarded by a test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
tribbloid committed Oct 6, 2023
1 parent 140e6e9 commit 05b5586
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -493,44 +493,62 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with
}
}

case class DefPosition(
element: Formatted,
msg: String
) extends Based {

def index(): Unit = {

if (pluginSettings.showTypeDefPosition)
Based += FormattedIndex(element) -> this
}

override protected def formattedHeader_Body(break: Boolean): (String, Seq[TypeRepr]) = {

s"(defined at)" -> Seq(BrokenType(List(msg)))
}
}

def formatTypeRaw(tpe: Type, top: Boolean): Formatted = {
formatWithInfix(tpe, extractArgs(tpe), top)(formatType)
}

override def formatTypeImpl(tpe: Type, top: Boolean): Formatted = {

if (pluginSettings.TypeDiffsDetail.disambiguation) {
tpe.typeArgs match {
case List(t1, t2) =>
val result =
if (pluginSettings.TypeDiffsDetail.disambiguation) {

withDisambiguation(Nil, t1, t2) {
formatTypeImplNoDisambiguation(tpe, top)
}
} else {

tpe.typeArgs match {
case List(t1, t2) =>
val result = withDisambiguation(Nil, t1, t2) {
formatTypeImplNoDisambiguation(tpe, top)
}

result match {
case Infix(ii, left, right, _) =>
val noApparentDiff = (left == right) && (t1 != t2)

if (noApparentDiff || pluginSettings.TypeDiffsDetail.builtInMsgAlways) {
result match {
case Infix(ii, left, right, _) =>
val noApparentDiff = (left == right) && (t1 != t2)

BuiltInDiffMsg(
result,
TypeDiffView(t1, t2).builtInDiffMsg,
Some(ii)
).index()
}
case _ =>
}
if (noApparentDiff || pluginSettings.TypeDiffsDetail.builtInMsgAlways) {

result
case _ =>
formatTypeImplNoDisambiguation(tpe, top)
}

} else {
BuiltInDiffMsg(
result,
TypeDiffView(t1, t2).builtInDiffMsg,
Some(ii)
).index()
}
case _ =>
}

formatTypeImplNoDisambiguation(tpe, top)
result
case _ =>
formatTypeImplNoDisambiguation(tpe, top)
}

}

protected def formatTypeImplNoDisambiguation(tpe: Type, top: Boolean): Formatted = {
Expand All @@ -548,6 +566,13 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with

val result = results.last

TypeView(tpe).defPositionOpt.foreach { v =>
DefPosition(
result,
v.shortText
).index()
}

result
}

Expand Down
30 changes: 27 additions & 3 deletions core/src/main/scala-2.13.7+/latest/splain/TyperCompatViews.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package splain

import scala.reflect.internal.util.{NoSourceFile, Position}

trait TyperCompatViews {
self: SplainAnalyzer =>

Expand Down Expand Up @@ -66,26 +68,48 @@ trait TyperCompatViews {
}
}

object _DefPosition {

lazy val value: Position = definingSymbol.pos

lazy val noSource: Boolean = value.source == NoSourceFile

lazy val shortText: String = {

val prefix = value.source.file.path + ":"

val result = s"$prefix${value.line}:${value.column}"
result
}

lazy val formattedText: String = {

Position.formatMessage(value, "", shortenFile = false)
}
}

def defPositionOpt: Option[_DefPosition.type] = Option(_DefPosition).filterNot(_.noSource)

def typeToString: String = {

val detailLvl = pluginSettings.typeDetail
val typeDetail = pluginSettings.typeDetail

def short = self.safeToString

def long = scala.util.Try(self.toLongString).getOrElse(short)

def maybeContext = scala.util.Try(existentialContext(self)).toOption

def maybeAlias = scala.util.Try(explainAlias(self)).toOption

detailLvl match {
typeDetail match {
case i if i <= 1 => short
case 2 => long
case 3 =>
(Seq(long) ++ maybeContext).mkString("")

case i if i >= 4 =>
(Seq(long) ++ maybeContext ++ maybeAlias).mkString("")

}
}
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/splain/PluginSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ case class PluginSettings(pluginOpts: mutable.Map[String, String]) {

def showTypeReduction: Boolean = boolean(PluginSettings.Key.typeReduction)

def showTypeDefPosition: Boolean = boolean(PluginSettings.Key.typeDefPosition)

def typeDiffsDetail: Int = int(PluginSettings.Key.typeDiffsDetail)

object TypeDiffsDetail {
Expand All @@ -66,6 +68,8 @@ object PluginSettings {

val typeReduction = "Vtype-reduction"

val typeDefPosition = "Vtype-def-position"

val typeDetail = "Vtype-detail"

val typeDiffsDetail = "Vtype-diffs-detail"
Expand All @@ -79,6 +83,7 @@ object PluginSettings {
Key.implicitDiverging -> "false",
Key.implicitDivergingMaxDepth -> "100",
Key.typeReduction -> "false",
Key.typeDefPosition -> "false",
Key.typeDetail -> "1",
Key.typeDiffsDetail -> "1",
Key.debug -> "false"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
newSource1.scala:12: error: implicit error;
!I e: Diff.e1.VV =:= String
Cannot prove that Diff.e1.VV =:= String.

implicitly[e1.VV =:= String]
^
newSource1.scala:13: error: type mismatch;
String|Diff.e1.VV
val x: e1.VV = ??? : String
^
newSource1.scala:12: error: implicit error;
!I e: Diff.e1.VV (defined at) { newSource1.scala:9:10 } =:= String
Cannot prove that Diff.e1.VV =:= String.

implicitly[e1.VV =:= String]
^
newSource1.scala:13: error: type mismatch;
String|Diff.e1.VV (defined at) { newSource1.scala:9:10 }
val x: e1.VV = ??? : String
^
31 changes: 31 additions & 0 deletions core/src/test/scala/splain/plugin/VTypeDefPositionSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package splain.plugin

import splain.SpecBase

class VTypeDefPositionSpec extends SpecBase.Direct {

final val diff =
"""
object Diff {
class Example {
type VV
}
val e1 = new Example {
type VV <: Int
}
implicitly[e1.VV =:= String]
val x: e1.VV = ??? : String
}
"""

describe("#44") {

check(diff, numberOfErrors = 2)

check(diff, profile = "-P:splain:Vtype-def-position", numberOfErrors = 2)
}
}
8 changes: 5 additions & 3 deletions core/src/testFixtures/scala/splain/TestHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package splain
import org.scalatest.exceptions.TestFailedException
import org.scalatest.{Assertion, Suite}
import org.slf4j.LoggerFactory
import splain.test.TryCompile
import splain.test.{Issue, TryCompile}

import java.nio.file.{FileSystems, Files, Path, Paths}
import java.util.concurrent.atomic.AtomicInteger
Expand Down Expand Up @@ -197,7 +197,7 @@ trait TestHelpers extends Suite {
case class DirectRunner() {

case class ParseGroundTruths(
startsWith: String = "newSource1.scala:",
startsWith: String = Issue.defaultSrcName,
fName: Option[String] = None
) {

Expand All @@ -208,9 +208,11 @@ trait TestHelpers extends Suite {
}

lazy val cases: Seq[String] = {
val regex = s"(^|\n)$startsWith"

val result = raw
.split(
startsWith
regex
)
.toSeq
.filter(_.nonEmpty)
Expand Down

0 comments on commit 05b5586

Please sign in to comment.