Repository: scalameta/scalameta (scala)

Issue:
Abstract givens in trait cannot be anonymous error with scala 3.7.1 and scalafmt 3.9.7
Hello

and thank you very much for your nice work.

The following trait is ok for scala 3.7.1 but causes scalafmt to complain: 
`error: [dialect scala3] abstract givens cannot be anonymous`

```
  trait AbstractGivenTest {
    given ExecutionContext
  }
```

Best regards,
  Sven

Pull request:
The `name` field of Decl.Given should have been a Name, not Term.Name, but can't change it now. Fixes #4268.

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 9d95d688d8..cbbb08180a 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
@@ -3455,6 +3455,8 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) {
       case _: Comma | _: LeftBrace | _: Colon if newSyntaxOK => getDefnGiven()
       case _ => sig.name match {
           case n: Term.Name => Decl.Given(mods, n, sig.pcg, decltype)
+          case n: Name.Anonymous if dialect.allowImprovedTypeClassesSyntax =>
+            Decl.GivenAnonymous(mods, n, sig.pcg, decltype)
           case n => syntaxError("abstract givens cannot be anonymous", at = n)
         }
     }
diff --git a/scalameta/trees/shared/src/main/scala/scala/meta/Trees.scala b/scalameta/trees/shared/src/main/scala/scala/meta/Trees.scala
index 9bf2df5df0..8adc3af468 100644
--- a/scalameta/trees/shared/src/main/scala/scala/meta/Trees.scala
+++ b/scalameta/trees/shared/src/main/scala/scala/meta/Trees.scala
@@ -1046,6 +1046,21 @@ object Decl {
     @replacedField("4.6.0")
     final def tparams: List[sm.Type.Param] = tparamClause.values
   }
+
+  @branch
+  trait GivenLike extends Decl with Stat.GivenLike with Member with Tree.WithDeclTpe {
+    def mods: List[Mod]
+    def name: Name
+    def paramClauseGroups: List[Member.ParamClauseGroup]
+    def decltpe: sm.Type
+  }
+  @ast
+  class GivenAnonymous(
+      mods: List[Mod],
+      name: Name.Anonymous,
+      paramClauseGroups: List[Member.ParamClauseGroup],
+      decltpe: sm.Type
+  ) extends GivenLike
   @ast
   class Given(
       mods: List[Mod],
@@ -1054,11 +1069,7 @@ object Decl {
       @replacesFields("4.12.0", Member.ParamClauseGroupsCtor)
       paramClauseGroups: List[Member.ParamClauseGroup],
       decltpe: sm.Type
-  ) extends Decl
-      with Stat.GivenLike
-      with Member.Term
-      with Tree.WithParamClauseGroup
-      with Tree.WithDeclTpe {
+  ) extends GivenLike with Member.Term with Tree.WithParamClauseGroup {
     @replacedField("4.6.0", pos = 2)
     final def tparams: List[sm.Type.Param] = Member.ParamClauseGroup.toTparams(paramClauseGroups)
     @replacedField("4.6.0", pos = 3)
@@ -1066,6 +1077,7 @@ object Decl {
     @replacedField("4.12.0")
     final def paramClauseGroup: Option[Member.ParamClauseGroup] = paramClauseGroups.headOption
   }
+
 }
 
 @branch
diff --git a/scalameta/trees/shared/src/main/scala/scala/meta/internal/prettyprinters/TreeSyntax.scala b/scalameta/trees/shared/src/main/scala/scala/meta/internal/prettyprinters/TreeSyntax.scala
index ebf9d1a289..7ac96c49c9 100644
--- a/scalameta/trees/shared/src/main/scala/scala/meta/internal/prettyprinters/TreeSyntax.scala
+++ b/scalameta/trees/shared/src/main/scala/scala/meta/internal/prettyprinters/TreeSyntax.scala
@@ -809,7 +809,7 @@ object TreeSyntax {
       case t: Decl.Type => s(w(t.mods, " "), kw("type"), " ", t.name, t.tparamClause, t.bounds)
       case t: Decl.Def =>
         s(w(t.mods, " "), kw("def "), t.name, t.paramClauseGroups, kw(": "), t.decltpe)
-      case t: Decl.Given =>
+      case t: Decl.GivenLike =>
         val sig = givenSig(t.name, t.paramClauseGroups)
         r(" ")(t.mods, sig, givenDeclTpe(t.decltpe))
       case t: Defn.Val =>
diff --git a/scalameta/trees/shared/src/main/scala/scala/meta/internal/trees/ParentChecks.scala b/scalameta/trees/shared/src/main/scala/scala/meta/internal/trees/ParentChecks.scala
index 350113ccae..dfc000d5e7 100644
--- a/scalameta/trees/shared/src/main/scala/scala/meta/internal/trees/ParentChecks.scala
+++ b/scalameta/trees/shared/src/main/scala/scala/meta/internal/trees/ParentChecks.scala
@@ -68,7 +68,7 @@ object ParentChecks {
   def NameAnonymous(tree: Name.Anonymous, parent: Tree, destination: String): Boolean = parent match {
     case _: Ctor | _: Init | _: Self | _: Term.Param | _: Type.Param => destination == "name"
     case _: Mod.Private | _: Mod.Protected => destination == "within"
-    case _: Term.This | _: Term.Super | _: Defn.Given | _: Defn.GivenAlias => true
+    case _: Term.This | _: Term.Super | _: Stat.GivenLike => true
     case _: Defn.ExtensionGroup | _: Defn.RepeatedEnumCase => true
     case _ => false
   }


Test patch:
diff --git a/tests-semanticdb/src/test/scala-2.13/scala/meta/tests/api/SurfaceSuite.scala b/tests-semanticdb/src/test/scala-2.13/scala/meta/tests/api/SurfaceSuite.scala
index c245591cb1..b26ed91652 100644
--- a/tests-semanticdb/src/test/scala-2.13/scala/meta/tests/api/SurfaceSuite.scala
+++ b/tests-semanticdb/src/test/scala-2.13/scala/meta/tests/api/SurfaceSuite.scala
@@ -296,6 +296,8 @@ class SurfaceSuite extends FunSuite {
          |scala.meta.Decl
          |scala.meta.Decl.Def
          |scala.meta.Decl.Given
+         |scala.meta.Decl.GivenAnonymous
+         |scala.meta.Decl.GivenLike
          |scala.meta.Decl.Type
          |scala.meta.Decl.Val
          |scala.meta.Decl.Var
diff --git a/tests/jvm/src/test/scala-2.13/scala/meta/tests/trees/ReflectionSuite.scala b/tests/jvm/src/test/scala-2.13/scala/meta/tests/trees/ReflectionSuite.scala
index a06134807b..1213ec6b33 100644
--- a/tests/jvm/src/test/scala-2.13/scala/meta/tests/trees/ReflectionSuite.scala
+++ b/tests/jvm/src/test/scala-2.13/scala/meta/tests/trees/ReflectionSuite.scala
@@ -24,7 +24,7 @@ class ReflectionSuite extends TreeSuiteBase {
     val sym = symbolOf[scala.meta.Tree]
     assert(sym.isRoot)
     val root = sym.asRoot
-    assertEquals((root.allBranches.length, root.allLeafs.length), (71, 471))
+    assertEquals((root.allBranches.length, root.allLeafs.length), (72, 474))
   }
 
   test("If") {
@@ -124,6 +124,7 @@ class ReflectionSuite extends TreeSuiteBase {
          |scala.meta.Ctor.Primary
          |scala.meta.Init
          |scala.meta.Name
+         |scala.meta.Name.Anonymous
          |scala.meta.Pat
          |scala.meta.Pat.ArgClause
          |scala.meta.Pkg.Body
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 2f901e7483..0d59021b76 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
@@ -1039,4 +1039,20 @@ class GivenSyntax36Suite extends BaseDottySuite {
     runTestAssert[Stat](code, layout)(tree)
   }
 
+  test("abstract-given-anonymous") {
+    runTestAssert[Stat]("given Ord[Int]")(Decl.GivenAnonymous(Nil, anon, Nil, papply("Ord", "Int")))
+    runTestAssert[Stat]("given [T](using ord: Ord[T]): Ord[Set[T]]")(Decl.GivenAnonymous(
+      Nil,
+      anon,
+      List(Member.ParamClauseGroup(List(pparam("T")), List(List(tparamUsing("ord", papply("Ord", "T")))))),
+      papply("Ord", papply("Set", "T"))
+    ))
+    runTestAssert[Stat]("given (using Ord[String]): Ord[Int]")(Decl.GivenAnonymous(
+      Nil,
+      anon,
+      List(Member.ParamClauseGroup(Nil, List(List(tparamUsing("", papply("Ord", "String")))))),
+      papply("Ord", "Int")
+    ))
+  }
+
 }
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 67b7256d6e..07f2445b7a 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
@@ -5,6 +5,8 @@ import scala.meta._
 class GivenUsingSuite extends BaseDottySuite {
 
   implicit val parseBlock: String => Stat = blockStat(_)
+  // we have a separate suite for 3.6 features
+  override protected implicit val dialect: Dialect = dialects.Scala35
 
   /**
    * For checking examples in repl declare: