Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pure functions and capture checking #4582

Merged
merged 2 commits into from
Nov 22, 2024
Merged
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
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
144 changes: 144 additions & 0 deletions scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -7696,3 +7696,147 @@ object a:
b.c match
case d if e => f
// comment
<<< implicit parameter type conversion with "into" modifier 1
runner.dialect = scala3future
===
object a:
def ++ (elems: into IterableOnce[A]): List[A]
>>>
object a:
def ++(elems: into IterableOnce[A]): List[A]
<<< implicit parameter type conversion with "into" modifier 2
runner.dialect = scala3future
===
object a:
def ++ (elems: => into IterableOnce[A]): List[A]
>>>
object a:
def ++(elems: => into IterableOnce[A]): List[A]
<<< implicit parameter type conversion with "into" modifier 3
runner.dialect = scala3future
===
object a:
def flatMap[B](f: into A => IterableOnce[B]): List[B]
>>>
object a:
def flatMap[B](f: into A => IterableOnce[B]): List[B]
<<< implicit parameter type conversion with "into" modifier 4
runner.dialect = scala3future
===
object a:
def flatMap[B](f: A => into IterableOnce[B]): List[B]
>>>
object a:
def flatMap[B](f: A => into IterableOnce[B]): List[B]
<<< implicit parameter type conversion with "into" modifier 5
runner.dialect = scala3future
===
object a:
def concatAll(xss: (into IterableOnce[Char])*): List[Char]
>>>
object a:
def concatAll(xss: (into IterableOnce[Char])*): List[Char]
<<< pure type functions 1
runner.dialect = scala3future
===
object a:
val func: A -> B = foo
>>>
object a:
val func: A -> B = foo
<<< pure type functions 2
runner.dialect = scala3future
===
object a:
def func(f: A -> B): Unit
>>>
object a:
def func(f: A -> B): Unit
<<< pure type functions 3
runner.dialect = scala3future
===
object a:
def map[T <: (A -> B)](f: T): A -> B = ???
>>>
object a:
def map[T <: (A -> B)](f: T): A -> B = ???
<<< pure type functions 4
runner.dialect = scala3future
===
object a:
val func: A ?-> B = foo
>>>
object a:
val func: A ?-> B = foo
<<< pure type functions 5
runner.dialect = scala3future
===
object a:
def func(f: A ?-> B): Unit
>>>
object a:
def func(f: A ?-> B): Unit
<<< pure type functions 6
runner.dialect = scala3future
===
object a:
def map[T <: (A ?-> B)](f: T): A ?-> B = ???
>>>
object a:
def map[T <: (A ?-> B)](f: T): A ?-> B = ???
<<< pure type functions 7
runner.dialect = scala3future
===
object a:
def func(f: -> B): Unit
>>>
object a:
def func(f: -> B): Unit
<<< capability capture checking 1
runner.dialect = scala3future
===
object a:
class Logger(fs: FileSystem^)
>>>
object a:
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)
<<< capability capture checking 3
runner.dialect = scala3future
===
object a:
def tail: LazyList[A]^{this}
>>>
object a:
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)
<<< 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 = ???
<<< 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
Loading