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

State: keep explicit penalty to detect overflow #3014

Merged
merged 2 commits into from
Dec 28, 2021
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 @@ -197,8 +197,9 @@ private class BestFirstSearch private (
if (null == nextNextState) null
else traverseSameLine(nextNextState, depth)
if (null != furtherState) {
val delta = furtherState.totalCost - nextNextState.totalCost
if (delta > Constants.ExceedColumnPenalty)
val overflow =
furtherState.appliedPenalty > nextNextState.appliedPenalty
if (overflow)
Q.enqueue(nextNextState)
else {
optimalNotFound = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scala.meta.tokens.Token

import org.scalafmt.util.LoggerOps

class PolicySummary(val policies: Seq[Policy]) {
class PolicySummary(val policies: Seq[Policy]) extends AnyVal {
import LoggerOps._

@inline def noDequeue = policies.exists(_.noDequeue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final case class State(
pushes: Seq[ActualIndent],
column: Int,
allAltAreNL: Boolean,
appliedPenalty: Int, // penalty applied from overflow
delayedPenalty: Int // apply if positive, ignore otherwise
) {

Expand Down Expand Up @@ -117,6 +118,7 @@ final case class State(
nextIndents,
nextStateColumn,
nextAllAltAreNL,
appliedPenalty + penalty,
nextDelayedPenalty
)
}
Expand Down Expand Up @@ -258,7 +260,6 @@ final case class State(
}
}

def totalCost: Int = cost + math.max(0, delayedPenalty)
}

object State {
Expand All @@ -273,6 +274,7 @@ object State {
Seq.empty,
0,
false,
0,
0
)

Expand Down
3 changes: 2 additions & 1 deletion scalafmt-tests/src/test/resources/default/String.stat
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ object a {
>>>
object a {
intercept[TestException] {
val ct = Thread.currentThread() // Ensure that the thunk is executed in the tests thread
val ct =
Thread.currentThread() // Ensure that the thunk is executed in the tests thread
val ct =
Thread.currentThread() // Ensure that the thunk is executed in the tests thread
}
Expand Down
39 changes: 39 additions & 0 deletions scalafmt-tests/src/test/resources/scalajs/Apply.stat
Original file line number Diff line number Diff line change
Expand Up @@ -915,3 +915,42 @@ object a {
case foo bar (baz @ qux) =>
}
}
<<< [from scala-js] changed with incorect overflow detection
preset = default
maxColumn = 74
binPack.preset = true
newlines.source = keep
newlines.avoidForSimpleOverflow = [tooLong, punct, slc]
binPack.unsafeCallSite = oneline
===
object a {
@tailrec
def constructOptimized(revAlts: List[(js.Tree, js.Tree)],
elsep: js.Tree): js.Tree = {
revAlts match {
case foo =>
// cannot use flatMap due to tailrec
foo match {
case bar =>
constructOptimized(revAltsRest,
js.If(cond, newBody, elsep)(tpe)(cond.pos))
}
}
}
}
>>>
object a {
@tailrec
def constructOptimized(revAlts: List[(js.Tree, js.Tree)],
elsep: js.Tree): js.Tree = {
revAlts match {
case foo =>
// cannot use flatMap due to tailrec
foo match {
case bar =>
constructOptimized(revAltsRest,
js.If(cond, newBody, elsep)(tpe)(cond.pos))
}
}
}
}