Skip to content

Commit

Permalink
Fix parenthesization of ascribed function types with single tuple arg…
Browse files Browse the repository at this point in the history
…ument
  • Loading branch information
Win Wang committed Oct 1, 2019
1 parent be11120 commit b8fad2b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
7 changes: 7 additions & 0 deletions scalafix/input/src/main/scala/rsc/tests/BetterRscCompat.scala
Expand Up @@ -283,4 +283,11 @@ object BetterRscCompat_Test {

class H extends V
}

object FunctionTypes {

val f1 = (_: (Int, Int)) => ()

def f2 = (_: (Int, Int), _: Int) => ()
}
}
Expand Up @@ -283,4 +283,11 @@ object BetterRscCompat_Test {

class H extends V
}

object FunctionTypes {

val f1: ((Int, Int)) => Unit = (_: (Int, Int)) => ()

def f2: ((Int, Int), Int) => Unit = (_: (Int, Int), _: Int) => ()
}
}
Expand Up @@ -30,13 +30,22 @@ class SemanticdbPrinter(
rep(args, ", ")(normal)
str(")")
} else if (sym.startsWith("scala/Function")) {
var params :+ ret = args
val params :+ ret = args
val hasByNameArg = params.exists(_.isInstanceOf[s.ByNameType])
val hasFunctionArg = params.exists {
case s.TypeRef(pre, sym, args) if sym.startsWith("scala/Function") => true
case _ => false
}
val needsExtraParens = hasFunctionArg || hasByNameArg || (params.length != 1)
val singleTupleArg = params.length == 1 && (params.head match {
case s.TypeRef(_, argSym, _) => argSym.startsWith("scala/Tuple")
case _ => false
})

val needsExtraParens = hasFunctionArg ||
hasByNameArg ||
singleTupleArg ||
(params.length != 1)

if (needsExtraParens) str("(")
rep(params, ", ") { normal }
if (needsExtraParens) str(")")
Expand Down

0 comments on commit b8fad2b

Please sign in to comment.