Skip to content

Commit

Permalink
Simplify forSome statements into wildcard existentials (fix twitter#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
Win Wang committed Oct 1, 2019
1 parent be11120 commit e68af7d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
14 changes: 14 additions & 0 deletions scalafix/input/src/main/scala/rsc/tests/BetterRscCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,18 @@ object BetterRscCompat_Test {

class H extends V
}

object ExistentialTypes {
class Box[A]
class Box2[A, B]
trait T
class A extends T
class B extends T

val box = null.asInstanceOf[Box[_]]

val boxes1 = Seq(new Box[A], new Box[B])

val boxes2 = Seq(new Box2[A, B], new Box2[B, A])
}
}
14 changes: 14 additions & 0 deletions scalafix/output/src/main/scala/rsc/tests/BetterRscCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,18 @@ object BetterRscCompat_Test {

class H extends V
}

object ExistentialTypes {
class Box[A]
class Box2[A, B]
trait T
class A extends T
class B extends T

val box: Box[_] = null.asInstanceOf[Box[_]]

val boxes1: Seq[Box[_]] = Seq(new Box[A], new Box[B])

val boxes2: Seq[Box2[_, _]] = Seq(new Box2[A, B], new Box2[B, A])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class SemanticdbPrinter(
}
val needsExtraParens = hasFunctionArg || hasByNameArg || (params.length != 1)
if (needsExtraParens) str("(")
rep(params, ", ") { normal }
rep(params, ", ") {
normal
}
if (needsExtraParens) str(")")
str(" => ")
normal(ret)
Expand All @@ -53,6 +55,7 @@ class SemanticdbPrinter(
case d.TypeParameter(value) => Some(n.TypeName(value))
case other => None
}

def printPrettyPrefix: Unit = {
val prettyPre = if (pre == s.NoType) sym.trivialPrefix(env) else pre
prettyPre match {
Expand All @@ -66,6 +69,7 @@ class SemanticdbPrinter(
str("#")
}
}

if (config.better) {
name.map(fullEnv.lookup) match {
case Some(x) if !symbols.sameOrTypeAlias(x, sym) =>
Expand Down Expand Up @@ -142,9 +146,15 @@ class SemanticdbPrinter(
rep(" ", anns, " ", "")(pprint)
}
case s.ExistentialType(utpe, decls) =>
decls.infos.foreach(symbols.append)
opt(utpe)(normal)
rep(" forSome { ", decls.infos, "; ", " }")(pprint)
if (config.better) {
val wildcardInfos = decls.infos.map(_.withDisplayName("_"))
wildcardInfos.foreach(symbols.append)
opt(utpe)(normal)
} else {
decls.infos.foreach(symbols.append)
opt(utpe)(normal)
rep(" forSome { ", decls.infos, "; ", " }")(pprint)
}
case s.UniversalType(tparams, utpe) =>
// FIXME: https://github.com/twitter/rsc/issues/150
str("({ type λ")
Expand Down Expand Up @@ -189,7 +199,9 @@ class SemanticdbPrinter(
val displayName = info.displayName
if (displayName == "") {
sys.error(s"unsupported symbol: $sym")
} else if (displayName == "_" || displayName.startsWith("?")) {
} else if (displayName == "_" && !config.better) {
gensymCache.getOrElseUpdate(sym, gensym("T"))
} else if (displayName.startsWith("?")) {
gensymCache.getOrElseUpdate(sym, gensym("T"))
} else {
displayName
Expand All @@ -200,9 +212,10 @@ class SemanticdbPrinter(
else sym
}
}
if (keywords.containsKey(printableName)) str("`")
val needsBackticks = keywords.containsKey(printableName) && printableName != "_"
if (needsBackticks) str("`")
str(printableName)
if (keywords.containsKey(printableName)) str("`")
if (needsBackticks) str("`")
}

private def pprint(info: s.SymbolInformation): Unit = {
Expand Down

0 comments on commit e68af7d

Please sign in to comment.