Repository: scalameta/scalameta (scala)

Issue:
Lazy given
## Configuration (required) ##

```
version = 3.7.17
runner.dialect = scala3
```

## Command-line parameters (required) ##

When I run scalafmt via CLI like this: `scalafmt -c .scalafmt.conf Given.scala`

## Steps

Given code like this:
```scala
lazy given endpoint: DcEndpoint = TestChatServer.start()
```

## Problem

Scalafmt formats code like this:
```scala
error: [dialect scala3] lazy not allowed here. Only vals can be lazy
lazy given endpoint: DcEndpoint = TestChatServer.start()
```

## Expectation

I would like the formatted output to look like this:
```scala
lazy given endpoint: DcEndpoint = TestChatServer.start()
```

## Workaround

Exclude files like this.

Pull request:
Fixes #3530.

Patch:
diff --git a/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala b/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala
index 716b588a41..b9b7ae7860 100644
--- a/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala
+++ b/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala
@@ -509,14 +509,16 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser =>
 
   def summonClassifierFunc[A, B](implicit v: Classifier[A, B]): A => Boolean = v.apply
 
-  def onlyAcceptMod[M <: Mod: ClassTag, T: TokenClassifier](
+  def onlyAcceptMod[M <: Mod: ClassTag, T](
       mods: List[Mod],
       errorMsg: String
-  )(implicit classifier: Classifier[Mod, M]) = {
-    if (token.isNot[T]) {
+  )(implicit classifier: TokenClassifier[T]) =
+    onlyAcceptMod[M](classifier.apply)(mods)(errorMsg)
+
+  def onlyAcceptMod[M <: Mod: ClassTag](ok: Token => Boolean)(mods: List[Mod])(errorMsg: String) =
+    if (!ok(token)) {
       mods.first[M].foreach(m => syntaxError(errorMsg, at = m))
     }
-  }
 
   class InvalidModCombination[M1 <: Mod, M2 <: Mod](m1: M1, m2: M2) {
     def errorMessage: String = Messages.IllegalCombinationModifiers(m1, m2)
@@ -3569,7 +3571,9 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser =>
       enumCaseAllowed: Boolean = false,
       secondaryConstructorAllowed: Boolean = false
   ): Stat = {
-    onlyAcceptMod[Mod.Lazy, KwVal](mods, "lazy not allowed here. Only vals can be lazy")
+    onlyAcceptMod[Mod.Lazy](_.isAny[KwVal, KwGiven])(mods)(
+      "lazy not allowed here. Only val/given can be lazy."
+    )
     onlyAcceptMod[Mod.Opaque, KwType](mods, "opaque not allowed here. Only types can be opaque.")
     token match {
       case KwVal() | KwVar() =>


Test patch:
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/GivenUsingSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/GivenUsingSuite.scala
index 275841ccc0..008791e997 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/GivenUsingSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/GivenUsingSuite.scala
@@ -1152,4 +1152,11 @@ class GivenUsingSuite extends BaseDottySuite {
       )
     )
   }
+
+  test("lazy given") {
+    val code = "lazy given foo: Foo = ???"
+    val tree = Defn.GivenAlias(List(Mod.Lazy()), tname("foo"), None, pname("Foo"), tname("???"))
+    runTestAssert[Stat](code)(tree)
+  }
+
 }
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/MinorDottySuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/MinorDottySuite.scala
index 97ef11f328..e13b823d7e 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/MinorDottySuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/MinorDottySuite.scala
@@ -553,7 +553,7 @@ class MinorDottySuite extends BaseDottySuite {
     )
     runTestError[Stat](
       "trait Foo { protected[this] lazy var from: Int }",
-      "lazy not allowed here. Only vals can be lazy"
+      "lazy not allowed here. Only val/given can be lazy"
     )
   }