scala-ide / scalariform

Scala source code formatter
http://scala-ide.github.com/scalariform/
MIT License
527 stars 148 forks source link

Stack Overflow with complex expressions #265

Closed KenCoder closed 6 years ago

KenCoder commented 6 years ago

We occasionally get a StackOverflowError using ScalaStyle in our builds. The end of the stack is shown below, but I believe this is due to the fact that groupBy is implemented using recursion but not @tailrec.

Since the stack trace is cut off, I don't actually have the sample code that generates the problem. However, I can generate the problem with the following valid (albeit unlikely) unit test. The Scala compiler handles this expression fine.

  "Parser" should "handle deep complex expressions" in {
    parseExpression("o.p(a" + (",a" * 2000) + ")")
  }

I'm happy to submit a PR to fix this -- give me a day or two to put it together. I'll add tests for the updated groupBy (since I'm changing it) as well as the above.

The stack trace:

   java.lang.StackOverflowError
      at scalariform.utils.Utils$$anonfun$4.apply(Utils.scala:61)
      at scalariform.utils.Utils$$anonfun$4.apply(Utils.scala:61)
      at scala.collection.immutable.List.span(List.scala:369)
      at scalariform.utils.Utils$.groupBy(Utils.scala:61)
      at scalariform.utils.Utils$.groupBy(Utils.scala:62)
      at scalariform.utils.Utils$.groupBy(Utils.scala:62)
      at scalariform.utils.Utils$.groupBy(Utils.scala:62)
      at scalariform.utils.Utils$.groupBy(Utils.scala:62)
      at scalariform.utils.Utils$.groupBy(Utils.scala:62)
      at scalariform.utils.Utils$.groupBy(Utils.scala:62)
    ... and groupBy all the way down.
godenji commented 6 years ago

@KenCoder by all means, please submit a PR; if tests pass we'll get it merged.

bhmiller commented 6 years ago

@godenji what sort of release cadence should i expect for scalariform (and thus picking up this bug fix downstream)? thanks.

crakjie commented 5 years ago

Thanks, @KenCoder. I agree with @bhmiller, it's could be nice to have a release with these kinds of bugfix.

idserda commented 5 years ago

+1 for releasing this fix.

biaoma-ty commented 4 years ago

occurred in scalastyle version 1.0.0, how was this going on?