Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test case for wildcard bounds? For example “Box[_ <: Path]”


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("_"))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are cases where it’s not safe to convert existentials into wildcards, for example “Map[T, T] forSome { type T }”. I think the rule is that existential types can only be converted into wildcards if they’re referenced once.

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