scalameta / scalafmt

Code formatter for Scala
http://scalameta.org/scalafmt
Apache License 2.0
1.44k stars 276 forks source link

`PositionExceptionImpl` complaining about expected `)` when encountering valid `<-` in for comprehension #3911

Closed mdedetrich closed 4 months ago

mdedetrich commented 7 months ago

Configuration (required)

Please paste the contents of your .scalafmt.conf file here:

version = 3.8.1
runner.dialect = scala212source3
maxColumn = 120
project.git = true

# http://docs.scala-lang.org/style/scaladoc.html recommends the JavaDoc style.
# scala/scala is written that way too https://github.com/scala/scala/blob/v2.12.2/src/library/scala/Predef.scala
docstrings.style = Asterisk
literals.hexDigits = upper
project.layout = StandardConvention

Command-line parameters (required)

When I run scalafmt via CLI like this: scalafmt

Steps

Given code like this:

// #slickQueryImplicitJoin
val slick2 =
  // #slickQueryImplicitJoin
  // or equivalent for-expression:
  (for (
    p <- people
    a <- addresses if p.addressId === a.id
  ) yield (p.name, a.city)).result
// #slickQueryImplicitJoin
val ((sqlRes, slickRes), slick2Res) = Await.result(db.run(sql zip slick zip slick2), Duration.Inf)
assert(sqlRes == slickRes)
assert(slickRes == slick2Res)
assert(sqlRes.size > 0)

Problem

Scalafmt fails with

org.scalafmt.dynamic.exceptions.PositionExceptionImpl: /Users/mdedetrich/github/slick/doc/code/SqlToSlick.scala:287: error: [dialect scala3] `)` expected but `<-` found
            p <- people58 source files formatted
              ^: /Users/mdedetrich/github/slick/doc/code/SqlToSlick.scala

Expectation

I would expect scalafmt to not fail

Workaround

Turning the formatting off with // format: off works

Notes

Changing the dialect with different dialects using fileOverride { "glob:**/doc/**.scala" = scala212 } doesn't have any effect, so it seems unrelated to dialect.

See original PR at https://github.com/slick/slick/pull/2909

kitbellew commented 5 months ago

@mdedetrich where is this syntax defined as valid (i.e., specifying multiple enumerators without semicolons within parens)? i tried it in scastie, and the compiler complains: https://scastie.scala-lang.org/BzMsTlvTQsOIY6dG3ITRSA

my earlier fix for your reported problem is causing a regression: scalameta/scalameta#3715.

kitbellew commented 5 months ago

ok, looks like this is a matter of incorrect bug report :(

your code DOES have the semicolon: https://github.com/slick/slick/blob/main/doc/code/SqlToSlick.scala#L280