Repository: scalameta/scalameta (scala)

Issue:
Regression crash when nested fors combined with given and braceless lambda passing
For scalafmt 3.8.6, following code:
```scala
import scala.util.Random
def foo(n: String)(i: Seq[Int]) = i

@main def m =
  given Random(1)
  for _ <- 1 to 10 do
    foo("Hello"):
      for i <- 0 to 10 yield i
```

Causes this error:
```
error: [dialect scala3 [with overrides]] `identifier` expected but `for` found
      for i <- 0 to 10 yield i
      ^
```

Works in scalafmt 3.8.3

Pull request:
Fixes #4190.

Patch:
diff --git a/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScannerTokens.scala b/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScannerTokens.scala
index aae9130053..2bcd6ccd95 100644
--- a/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScannerTokens.scala
+++ b/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScannerTokens.scala
@@ -686,6 +686,7 @@ final class ScannerTokens(val tokens: Tokens)(implicit dialect: Dialect) {
           case RegionTry :: xs
               if !next.isAny[KwCatch, KwFinally] && canBeLeadingInfix != LeadingInfix.Yes &&
                 !isIndented(xs, nextIndent) => strip(xs)
+          case (_: RegionGivenDecl) :: xs => strip(xs)
           case Nil | (_: CanProduceLF) :: _ => Some(rs)
           case _ => None
         }


Test patch:
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/GivenSyntax36Suite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/GivenSyntax36Suite.scala
index 3583e4685b..2f901e7483 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/GivenSyntax36Suite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/GivenSyntax36Suite.scala
@@ -1000,4 +1000,43 @@ class GivenSyntax36Suite extends BaseDottySuite {
     runTestAssert[Stat](code, layout)(tree)
   }
 
+  test("#4190 given") {
+    val code =
+      """|def m =
+         |  given Random(1)
+         |  for _ <- 1 to 10 do
+         |    foo("Hello"):
+         |      for i <- 0 to 10 yield i
+         |""".stripMargin
+    val layout =
+      """|def m = {
+         |  given Random(1)
+         |  for (_ <- 1 to 10) foo("Hello") {
+         |    for (i <- 0 to 10) yield i
+         |  }
+         |}
+         |""".stripMargin
+    val tree = Defn.Def(
+      Nil,
+      "m",
+      Nil,
+      None,
+      blk(
+        Defn.Given(Nil, anon, Nil, tpl(List(init("Random", List(lit(1)))), tplBody())),
+        Term.For(
+          Term.EnumeratorsBlock(List(Enumerator.Generator(patwildcard, tinfix(lit(1), "to", lit(10))))),
+          tapply(
+            tapply("foo", lit("Hello")),
+            blk(Term.ForYield(
+              Term
+                .EnumeratorsBlock(List(Enumerator.Generator(patvar("i"), tinfix(lit(0), "to", lit(10))))),
+              tname("i")
+            ))
+          )
+        )
+      )
+    )
+    runTestAssert[Stat](code, layout)(tree)
+  }
+
 }