Skip to content

Commit

Permalink
Support pure functions and capture checking
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 22, 2024
1 parent 4530bbe commit 2056384
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ class FormatOps(
// invoked on closing paren, part of ParamClause
@tailrec
final def defnSiteLastToken(t: Tree): Option[FT] = t match {
case _: Term.ParamClause | _: Type.FuncParamClause | _: Type.FunctionType |
_: Member.ParamClauseGroup => t.parent match {
case _: Member.SyntaxValuesClause | _: Member.ParamClauseGroup |
_: Type.ParamFunctionType => t.parent match {
case Some(p) => defnSiteLastToken(p)
case _ => None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ class Router(formatOps: FormatOps) {

// { ... } Blocks
case FT(_: T.LeftBrace, _: T.RightBrace, _) => Seq(Split(NoSplit, 0))

// Capture checking
case FT(_, _: T.LeftBrace, _) if isCapturingBrace(rightOwner) =>
Seq(Split(NoSplit, 0))
case FT(_: T.LeftBrace, _, _) if isCapturingBrace(leftOwner) =>
val close = matchingLeft(ft)
Seq(Split(NoSplit, 0).withIndent(style.indent.main, close, Before))

case FT(_: T.LeftBrace, right, _) =>
val close = matchingLeft(ft)
val isSelfAnnotationNL = style.optIn.selfAnnotationNewline &&
Expand Down Expand Up @@ -355,7 +363,7 @@ class Router(formatOps: FormatOps) {
else getSingleLineLambdaDecisionOpt
}

val noSplitMod = xmlSpace(leftOwner)
val noSplitMod = braceSpace(leftOwner)
val (slbMod, slbParensExclude) =
if (singleLineDecisionOpt.isEmpty) (noSplitMod, None)
else getBracesToParensMod(close, noSplitMod, isWithinBraces = true)
Expand Down Expand Up @@ -717,7 +725,7 @@ class Router(formatOps: FormatOps) {
}

case FT(_, _: T.RightBrace, _) =>
Seq(Split(if (ft.hasBlankLine) Newline2x else xmlSpace(rightOwner), 0))
Seq(Split(if (ft.hasBlankLine) Newline2x else braceSpace(rightOwner), 0))

case FT(_: T.KwPackage, _, _) if leftOwner.is[Pkg] => Seq(Split(Space, 0))
// Opening [ with no leading space.
Expand Down Expand Up @@ -1586,9 +1594,8 @@ class Router(formatOps: FormatOps) {
.exists(x => isTokenLastOrAfter(x.left, roPos))
}
} =>
val mod =
getBracesToParensMod(matchingRight(ft), Space, isWithinBraces = false)
._1
val rb = matchingRight(ft)
val mod = getBracesToParensMod(rb, Space, isWithinBraces = false)._1
Seq(Split(mod, 0))

// Delim
Expand Down Expand Up @@ -2549,6 +2556,9 @@ class Router(formatOps: FormatOps) {
if rightOwner.isAny[Tree.Repeated, Pat.SeqWildcard] =>
Seq(Split(NoSplit, 0))

case FT(_, T.Ident("^"), _) if rightOwner.is[Type.Capturing] =>
Seq(Split(NoSplit, 0))

case FT(
_: T.Ident | _: T.Literal | _: T.Interpolation.End | _: T.Xml.End,
_: T.Ident | _: T.Literal,
Expand Down Expand Up @@ -2658,7 +2668,8 @@ class Router(formatOps: FormatOps) {
case FT(Reserved(), _, _) => Seq(Split(Space, 0))

case FT(_, _: T.Symbolic, _) => Seq(Split(Space, 0))
case FT(_: T.RightArrow, _, _) if leftOwner.is[Type.ByName] =>
case FT(_: T.RightArrow | soft.KwPureFunctionArrow(), _, _)
if leftOwner.is[Type.ByNameType] =>
val mod = Space(style.spaces.inByNameTypes)
Seq(Split(mod, 0))
case FT(_: T.Colon, _, _) if style.spaces.notAfterColon(leftOwner) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ object TreeSyntacticGroup {
case _: Type.Singleton => g.Type.SimpleTyp
case _: Type.Apply => g.Type.SimpleTyp
case t: Type.ApplyInfix => g.Type.InfixTyp(t.op.value)
case _: Type.FunctionType => g.Type.Typ
case _: Type.ParamFunctionType => g.Type.Typ
case _: Type.PolyFunction => g.Type.Typ
case _: Type.Tuple => g.Type.SimpleTyp
case _: Type.With => g.Type.WithTyp
Expand All @@ -64,7 +64,7 @@ object TreeSyntacticGroup {
case _: Type.Wildcard => g.Type.SimpleTyp
case _: Type.Bounds => g.Path // ???
case _: Type.Repeated => g.Type.ParamTyp
case _: Type.ByName => g.Type.ParamTyp
case _: Type.ByNameType => g.Type.ParamTyp
case _: Type.Var => g.Type.ParamTyp
case _: Type.Param => g.Path // ???
case _: Type.Match => g.Type.Typ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class RedundantParens(implicit val ftoks: FormatTokens)
case _ if numParens >= 2 => true

case _: Term.AnonymousFunction | _: Term.Param => false
case _: Type.FunctionType => false
case _: Type.ParamFunctionType => false
case _: Init => false

case t: Member.ArgClause => okToReplaceArgClause(t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -911,11 +911,19 @@ object TreeOps {

def isParentAnApply(t: Tree): Boolean = t.parent.is[Term.Apply]

def isTreeOrBlockParent(owner: Tree)(pred: Tree => Boolean): Boolean =
if (owner.is[Term.Block]) owner.parent.exists(pred) else pred(owner)
def isCapturingBrace(owner: Tree): Boolean = owner match {
case _: Type.Capturing => true
case t: Type.FunctionLikeType => t.parent.is[Type.Capturing]
case _ => false
}

def xmlSpace(owner: Tree): Modification =
Space(!isTreeOrBlockParent(owner)(_.isAny[Term.Xml, Pat.Xml]))
def braceSpace(owner: Tree): Modification = Space {
def isXml(t: Tree) = t.isAny[Term.Xml, Pat.Xml]
owner match {
case t: Term.Block => !t.parent.exists(isXml)
case t => !isXml(t) && !isCapturingBrace(t)
}
}

def isEmptyTree(tree: Tree): Boolean = tree match {
case t: Term.Block => t.stats.isEmpty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7799,44 +7799,44 @@ object a:
class Logger(fs: FileSystem^)
>>>
object a:
class Logger(fs: FileSystem ^)
class Logger(fs: FileSystem^)
<<< capability capture checking 2
runner.dialect = scala3future
===
object a:
val l: Logger^{fs} = Logger(fs)
>>>
object a:
val l: Logger ^ { fs } = Logger(fs)
val l: Logger^{fs} = Logger(fs)
<<< capability capture checking 3
runner.dialect = scala3future
===
object a:
def tail: LazyList[A]^{this}
>>>
object a:
def tail: LazyList[A] ^ { this }
def tail: LazyList[A]^{this}
<<< capability capture checking 4
runner.dialect = scala3future
===
object a:
def p: Pair[Int ->{ct} String, Logger^{fs}] = Pair(x, y)
>>>
object a:
def p: Pair[Int -> { ct } String, Logger ^ { fs }] = Pair(x, y)
def p: Pair[Int ->{ct} String, Logger^{fs}] = Pair(x, y)
<<< capability capture checking 5
runner.dialect = scala3future
===
object a:
def map[T <: (A ?->{a, c} B)](f: T): A ?-> B = ???
>>>
object a:
def map[T <: (A ?-> { a, c } B)](f: T): A ?-> B = ???
def map[T <: (A ?->{a, c} B)](f: T): A ?-> B = ???
<<< capability capture checking 6
runner.dialect = scala3future
===
object a:
def func(f: ->{a, b, c} B): Unit
>>>
object a:
def func(f: -> { a, b, c } B): Unit
def func(f: ->{a, b, c} B): Unit
Original file line number Diff line number Diff line change
Expand Up @@ -7498,44 +7498,44 @@ object a:
class Logger(fs: FileSystem^)
>>>
object a:
class Logger(fs: FileSystem ^)
class Logger(fs: FileSystem^)
<<< capability capture checking 2
runner.dialect = scala3future
===
object a:
val l: Logger^{fs} = Logger(fs)
>>>
object a:
val l: Logger ^ { fs } = Logger(fs)
val l: Logger^{fs} = Logger(fs)
<<< capability capture checking 3
runner.dialect = scala3future
===
object a:
def tail: LazyList[A]^{this}
>>>
object a:
def tail: LazyList[A] ^ { this }
def tail: LazyList[A]^{this}
<<< capability capture checking 4
runner.dialect = scala3future
===
object a:
def p: Pair[Int ->{ct} String, Logger^{fs}] = Pair(x, y)
>>>
object a:
def p: Pair[Int -> { ct } String, Logger ^ { fs }] = Pair(x, y)
def p: Pair[Int ->{ct} String, Logger^{fs}] = Pair(x, y)
<<< capability capture checking 5
runner.dialect = scala3future
===
object a:
def map[T <: (A ?->{a, c} B)](f: T): A ?-> B = ???
>>>
object a:
def map[T <: (A ?-> { a, c } B)](f: T): A ?-> B = ???
def map[T <: (A ?->{a, c} B)](f: T): A ?-> B = ???
<<< capability capture checking 6
runner.dialect = scala3future
===
object a:
def func(f: ->{a, b, c} B): Unit
>>>
object a:
def func(f: -> { a, b, c } B): Unit
def func(f: ->{a, b, c} B): Unit
Original file line number Diff line number Diff line change
Expand Up @@ -7823,44 +7823,44 @@ object a:
class Logger(fs: FileSystem^)
>>>
object a:
class Logger(fs: FileSystem ^)
class Logger(fs: FileSystem^)
<<< capability capture checking 2
runner.dialect = scala3future
===
object a:
val l: Logger^{fs} = Logger(fs)
>>>
object a:
val l: Logger ^ { fs } = Logger(fs)
val l: Logger^{fs} = Logger(fs)
<<< capability capture checking 3
runner.dialect = scala3future
===
object a:
def tail: LazyList[A]^{this}
>>>
object a:
def tail: LazyList[A] ^ { this }
def tail: LazyList[A]^{this}
<<< capability capture checking 4
runner.dialect = scala3future
===
object a:
def p: Pair[Int ->{ct} String, Logger^{fs}] = Pair(x, y)
>>>
object a:
def p: Pair[Int -> { ct } String, Logger ^ { fs }] = Pair(x, y)
def p: Pair[Int ->{ct} String, Logger^{fs}] = Pair(x, y)
<<< capability capture checking 5
runner.dialect = scala3future
===
object a:
def map[T <: (A ?->{a, c} B)](f: T): A ?-> B = ???
>>>
object a:
def map[T <: (A ?-> { a, c } B)](f: T): A ?-> B = ???
def map[T <: (A ?->{a, c} B)](f: T): A ?-> B = ???
<<< capability capture checking 6
runner.dialect = scala3future
===
object a:
def func(f: ->{a, b, c} B): Unit
>>>
object a:
def func(f: -> { a, b, c } B): Unit
def func(f: ->{a, b, c} B): Unit
Original file line number Diff line number Diff line change
Expand Up @@ -8117,65 +8117,44 @@ object a:
class Logger(fs: FileSystem^)
>>>
object a:
class Logger(fs: FileSystem ^)
class Logger(fs: FileSystem^)
<<< capability capture checking 2
runner.dialect = scala3future
===
object a:
val l: Logger^{fs} = Logger(fs)
>>>
object a:
val l: Logger ^ {
fs
} = Logger(fs)
val l: Logger^{fs} = Logger(fs)
<<< capability capture checking 3
runner.dialect = scala3future
===
object a:
def tail: LazyList[A]^{this}
>>>
object a:
def tail: LazyList[A] ^ {
this
}
def tail: LazyList[A]^{this}
<<< capability capture checking 4
runner.dialect = scala3future
===
object a:
def p: Pair[Int ->{ct} String, Logger^{fs}] = Pair(x, y)
>>>
object a:
def p: Pair[
Int -> {
ct
} String,
Logger ^ {
fs
}
] = Pair(x, y)
def p: Pair[Int ->{ct} String, Logger^{fs}] = Pair(x, y)
<<< capability capture checking 5
runner.dialect = scala3future
===
object a:
def map[T <: (A ?->{a, c} B)](f: T): A ?-> B = ???
>>>
object a:
def map[
T <: (
A ?-> {
a, c
} B
)
](f: T): A ?-> B = ???
def map[T <: (A ?->{a, c} B)](f: T): A ?-> B = ???
<<< capability capture checking 6
runner.dialect = scala3future
===
object a:
def func(f: ->{a, b, c} B): Unit
>>>
object a:
def func(
f: -> {
a, b, c
} B
): Unit
def func(f: ->{a, b, c} B): Unit
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
val explored = Debug.explored.get()
logger.debug(s"Total explored: $explored")
if (!onlyUnit && !onlyManual)
assertEquals(explored, 1089084, "total explored")
assertEquals(explored, 1089000, "total explored")
val results = debugResults.result()
// TODO(olafur) don't block printing out test results.
// I don't want to deal with scalaz's Tasks :'(
Expand Down

0 comments on commit 2056384

Please sign in to comment.