Skip to content

Commit b7d2a12

Browse files
authored
Merge pull request #12107 from dotty-staging/streamline-given-syntax
Streamline given syntax
2 parents 1379ffc + 14beb81 commit b7d2a12

38 files changed

+123
-120
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -3585,7 +3585,10 @@ object Parsers {
35853585
val tparams1 = tparams.map(tparam => tparam.withMods(tparam.mods | PrivateLocal))
35863586
val vparamss1 = vparamss.map(_.map(vparam =>
35873587
vparam.withMods(vparam.mods &~ Param | ParamAccessor | Protected)))
3588-
val templ = withTemplate(makeConstructor(tparams1, vparamss1), parents)
3588+
val constr = makeConstructor(tparams1, vparamss1)
3589+
val templ =
3590+
if in.token == WITH then withTemplate(constr, parents)
3591+
else Template(constr, parents, Nil, EmptyValDef, Nil)
35893592
if noParams then ModuleDef(name, templ)
35903593
else TypeDef(name.toTypeName, templ)
35913594
end gdef

docs/docs/internals/syntax.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ ObjectDef ::= id [Template]
411411
EnumDef ::= id ClassConstr InheritClauses EnumBody
412412
GivenDef ::= [GivenSig] (AnnotType [‘=’ Expr] | StructuralInstance)
413413
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’ -- one of `id`, `DefParamClause`, `UsingParamClause` must be present
414-
StructuralInstance ::= ConstrApp {‘with’ ConstrApp} ‘with’ TemplateBody
414+
StructuralInstance ::= ConstrApp {‘with’ ConstrApp} [‘with’ TemplateBody]
415415
Extension ::= ‘extension’ [DefTypeParamClause] ‘(’ DefParam ‘)’
416416
{UsingParamClause} ExtMethods
417417
ExtMethods ::= ExtMethod | [nl] <<< ExtMethod {semi ExtMethod} >>>

docs/docs/reference/contextual/extension-methods.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ trait SafeDiv:
209209
By the second rule, an extension method can be made available by defining a given instance containing it, like this:
210210

211211
```scala
212-
given ops1: IntOps with {} // brings safeMod into scope
212+
given ops1: IntOps() // brings safeMod into scope
213213

214214
1.safeMod(2)
215215
```

docs/docs/reference/contextual/givens.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ object Foo:
135135
given fooNotTagged[A](using NotGiven[Tagged[A]]): Foo[A] = Foo(false)
136136

137137
@main def test(): Unit =
138-
given Tagged[Int] with {}
138+
given Tagged[Int]()
139139
assert(summon[Foo[Int]].value) // fooTagged is found
140140
assert(!summon[Foo[String]].value) // fooNotTagged is found
141141
```

docs/docs/reference/syntax.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ ObjectDef ::= id [Template]
396396
EnumDef ::= id ClassConstr InheritClauses EnumBody
397397
GivenDef ::= [GivenSig] (AnnotType [‘=’ Expr] | StructuralInstance)
398398
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’ -- one of `id`, `DefParamClause`, `UsingParamClause` must be present
399-
StructuralInstance ::= ConstrApp {‘with’ ConstrApp} ‘with’ TemplateBody
399+
StructuralInstance ::= ConstrApp {‘with’ ConstrApp} [‘with’ TemplateBody]
400400
Extension ::= ‘extension’ [DefTypeParamClause] ‘(’ DefParam ‘)’
401401
{UsingParamClause} ExtMethods
402402
ExtMethods ::= ExtMethod | [nl] <<< ExtMethod {semi ExtMethod} >>>

tests/neg/implicit-params.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ object Test {
1111

1212
def h(x: Int) given () = x // error: missing return type
1313

14-
given C: C(11) with {}
15-
given D: D(11) with {}
14+
given C: C(11)
15+
given D: D(11)
1616

1717
f(1)
1818
f(1)(using C)

tests/neg/multi-param-derives.scala

+14-14
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ object Test extends App {
44
{
55
trait Show[T]
66
object Show {
7-
given Show[Int] with {}
8-
given [T](using st: Show[T]): Show[Tuple1[T]] with {}
9-
given t2[T, U](using st: Show[T], su: Show[U]): Show[(T, U)] with {}
10-
given t3[T, U, V](using st: Show[T], su: Show[U], sv: Show[V]): Show[(T, U, V)] with {}
7+
given Show[Int]()
8+
given [T](using st: Show[T]): Show[Tuple1[T]]()
9+
given t2[T, U](using st: Show[T], su: Show[U]): Show[(T, U)]()
10+
given t3[T, U, V](using st: Show[T], su: Show[U], sv: Show[V]): Show[(T, U, V)]()
1111

1212
def derived[T](using m: Mirror.Of[T], r: Show[m.MirroredElemTypes]): Show[T] = new Show[T] {}
1313
}
@@ -22,10 +22,10 @@ object Test extends App {
2222
{
2323
trait Functor[F[_]]
2424
object Functor {
25-
given [C]: Functor[[T] =>> C] with {}
26-
given Functor[[T] =>> Tuple1[T]] with {}
27-
given t2 [T]: Functor[[U] =>> (T, U)] with {}
28-
given t3 [T, U]: Functor[[V] =>> (T, U, V)] with {}
25+
given [C]: Functor[[T] =>> C]()
26+
given Functor[[T] =>> Tuple1[T]]()
27+
given t2 [T]: Functor[[U] =>> (T, U)]()
28+
given t3 [T, U]: Functor[[V] =>> (T, U, V)]()
2929

3030
def derived[F[_]](using m: Mirror { type MirroredType[X] = F[X] ; type MirroredElemTypes[_] }, r: Functor[m.MirroredElemTypes]): Functor[F] = new Functor[F] {}
3131
}
@@ -40,8 +40,8 @@ object Test extends App {
4040
{
4141
trait FunctorK[F[_[_]]]
4242
object FunctorK {
43-
given [C]: FunctorK[[F[_]] =>> C] with {}
44-
given [T]: FunctorK[[F[_]] =>> Tuple1[F[T]]] with {}
43+
given [C]: FunctorK[[F[_]] =>> C]()
44+
given [T]: FunctorK[[F[_]] =>> Tuple1[F[T]]]()
4545

4646
def derived[F[_[_]]](using m: Mirror { type MirroredType[X[_]] = F[X] ; type MirroredElemTypes[_[_]] }, r: FunctorK[m.MirroredElemTypes]): FunctorK[F] = new FunctorK[F] {}
4747
}
@@ -56,10 +56,10 @@ object Test extends App {
5656
{
5757
trait Bifunctor[F[_, _]]
5858
object Bifunctor {
59-
given [C]: Bifunctor[[T, U] =>> C] with {}
60-
given Bifunctor[[T, U] =>> Tuple1[U]] with {}
61-
given t2: Bifunctor[[T, U] =>> (T, U)] with {}
62-
given t3 [T]: Bifunctor[[U, V] =>> (T, U, V)] with {}
59+
given [C]: Bifunctor[[T, U] =>> C]()
60+
given Bifunctor[[T, U] =>> Tuple1[U]]()
61+
given t2: Bifunctor[[T, U] =>> (T, U)]()
62+
given t3 [T]: Bifunctor[[U, V] =>> (T, U, V)]()
6363

6464
def derived[F[_, _]](using m: Mirror { type MirroredType[X, Y] = F[X, Y] ; type MirroredElemTypes[_, _] }, r: Bifunctor[m.MirroredElemTypes]): Bifunctor[F] = ???
6565
}

tests/neg/overloading-specifity.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ object Generic {
1212

1313
object Test extends App {
1414
trait Context
15-
//given ctx: Context with {}
15+
//given ctx: Context()
1616

1717
object a {
1818
def foo[T](implicit gen: Generic): Show[T] = new Show[T](1)

tests/neg/transparent.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ transparent class c // error
55
transparent object y // error
66
transparent trait t // ok
77
transparent type T = c // error
8-
transparent given c with {} // error
8+
transparent given c() // error
99

tests/neg/type-qmark.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
| ^
2828
| `?` is not a valid type name
2929
-- Error: tests/neg/type-qmark.scala:31:8 ------------------------------------------------------------------------------
30-
31 | given ?[T]: Foo[T] with {} // error
30+
31 | given ?[T]: Foo[T]() // error
3131
| ^
3232
| `?` is not a valid type name
3333
-- Error: tests/neg/type-qmark.scala:3:8 -------------------------------------------------------------------------------

tests/neg/type-qmark.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ object J {
2828
}
2929
object K {
3030
class Foo[T]
31-
given ?[T]: Foo[T] with {} // error
31+
given ?[T]: Foo[T]() // error
3232
}

tests/pos-macros/i11479/Macro_1.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
trait Foo
2-
given Foo: Foo with {}
2+
given Foo: Foo()
33
inline def summonFoo(): Foo = scala.compiletime.summonInline[Foo]
44

55
package p:
66
trait Bar
7-
given Bar: Bar with {}
7+
given Bar: Bar()
88
inline def summonBar(): Bar = scala.compiletime.summonInline[Bar]
99

tests/pos/X.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import scala.deriving.*
22

33
trait FunctorK[F[_[_]]]
44
object FunctorK {
5-
given [C]: FunctorK[[F[_]] =>> C] with {}
6-
given [T]: FunctorK[[F[_]] =>> Tuple1[F[T]]] with {}
5+
given [C]: FunctorK[[F[_]] =>> C]()
6+
given [T]: FunctorK[[F[_]] =>> Tuple1[F[T]]]()
77

88
def derived[F[_[_]]](using m: Mirror { type MirroredType[X[_]] = F[X] ; type MirroredElemTypes[_[_]] }, r: FunctorK[m.MirroredElemTypes]): FunctorK[F] = new FunctorK[F] {}
99
}

tests/pos/i11168.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
trait Foo
2-
given foo: Foo with {}
2+
given foo: Foo()
33

44
extension (using Foo)(x: Any)
55
def foo1[A] = ???

tests/pos/i6864.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
class A
22
class B
33

4-
given A with {}
5-
given B with {}
4+
given A()
5+
given B()
66

77
trait Foo
88
trait Bar
99

10-
given Foo with {}
11-
given Bar with {}
10+
given Foo()
11+
given Bar()
1212

1313
trait C
1414
trait Baz[A]
1515

16-
given C with {}
17-
given [A]: Baz[A] with {}
16+
given C()
17+
given [A]: Baz[A]()

tests/pos/inlined-the.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object Instances {
55
class C {
66
def f() = {
77
locally {
8-
given d[T]: D[T] with {}
8+
given d[T]: D[T]()
99
summon[D[Int]]
1010
implicit val s: 3 = ???
1111
val a: 3 = summon[3]
@@ -14,7 +14,7 @@ object Instances {
1414
}
1515

1616
locally {
17-
given d[T]: D[T] with {}
17+
given d[T]: D[T]()
1818
the2[D[Int]]
1919
implicit val s: 3 = ???
2020
val a: 3 = the2[3]

tests/pos/multi-given.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ trait C
55
def fancy(using a: A, b: B, c: C) = "Fancy!"
66
def foo(implicit a: A, b: B, c: C) = "foo"
77

8-
given A with B with {}
8+
given A() with B
99

10-
given ops: A with B with {}
10+
given ops: A() with B()

tests/pos/reference/delegates.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ object Instances extends Common:
110110
println(summon[Context].value)
111111
}
112112
locally {
113-
given d[T]: D[T] with {}
113+
given d[T]: D[T]()
114114
println(summon[D[Int]])
115115
}
116116
locally {
117-
given (using Context): D[Int] with {}
117+
given (using Context): D[Int]()
118118
println(summon[D[Int]])
119119
}
120120
end C

tests/pos/reference/extension-methods.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ object ExtMethods:
7070
end SafeDiv
7171

7272
def test1 =
73-
given ops1: IntOps with {} // brings safeMod into scope
73+
given ops1: IntOps() // brings safeMod into scope
7474
1.safeMod(2)
7575

7676
class Lst[T](xs: T*):

tests/pos/the-given.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
object Test {
22

33
class Encoder { def apply(x: Int): Int = x }
4-
given Encoder with {}
4+
given Encoder()
55

66
summon[Encoder](2)
77

tests/run/exports.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Test extends App {
77
class Printer {
88
def print() = println("printing")
99
object cfg extends Config
10-
given config: Config with {}
10+
given config: Config()
1111
}
1212

1313
class Scanner {

tests/run/extmethod-overload.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ object Test extends App {
6161
extension [T](xs: List[T]) def +++ (ys: List[T]): List[T] = xs ++ ys ++ ys
6262
extension [T](xs: List[T]) def +++ (ys: Iterator[T]): List[T] = xs ++ ys ++ ys
6363
}
64-
given Bar: Foo with {}
64+
given Bar: Foo()
6565

6666
assert((1 |+| 2) == 3)
6767
assert((1 |+| "2") == 2)

tests/run/extra-implicits.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
case class A(x: String)
33
case class B(x: String)
4-
given a1: A("default") with {}
5-
given b1: B("default") with {}
4+
given a1: A("default")
5+
given b1: B("default")
66
val a2 = A("explicit")
77
val b2 = B("explicit")
88

tests/run/given-eta.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def f(x: Int)(using c: C) (y: Int) = x + c.x + y
88
def g(x: Int)(using d: D) (y: d.T): d.T = d.trans(y)
99

1010
@main def Test =
11-
given C(1) with {}
11+
given C(1)
1212
val x = f
1313
assert(x(2)(3) == 6)
1414

tests/run/i11542.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ object demo {
22

33
trait Reader[A]
44

5-
given Reader[Int] with {}
5+
given Reader[Int]()
66

77
inline def summonReader[T <: Tuple]: List[Reader[_]] = inline compiletime.erasedValue[T] match {
88
case _: EmptyTuple => Nil

tests/run/i2567.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class TC
22

3-
given tc: TC with {}
3+
given tc: TC()
44

55
class Foo(using TC) {
66
println("hi")

tests/run/i8396.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ object Prefix:
66
type UpperBoundedType <: String
77
type FullyBoundedType >: String <: String
88

9-
given A: Show[AbstractType] with {}
10-
given B: Show[UpperBoundedType] with {}
11-
given C: Show[FullyBoundedType] with {}
9+
given A: Show[AbstractType]()
10+
given B: Show[UpperBoundedType]()
11+
given C: Show[FullyBoundedType]()
1212

1313
@main def Test =
1414
summon[Show[Prefix.AbstractType]]

tests/run/implicit-alias.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ object Test extends App {
66

77
class TC1
88

9-
given TC1 with {}
9+
given TC1()
1010

1111
class TV(val tc: TC) extends AnyVal
1212

tests/run/implicit-disambiguation.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object M {
1414
}
1515
}
1616
object Test extends App {
17-
given b: B with {}
18-
given c: C with {}
17+
given b: B()
18+
given c: C()
1919
println(M.f)
2020
}

tests/run/implicit-specifity.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ object Generic2 {
2020

2121
class SubGen extends Generic
2222
object SubGen {
23-
given SubGen with {}
23+
given SubGen()
2424
}
2525
object Contextual {
2626
trait Context
2727

28-
given ctx: Context with {}
28+
given ctx: Context()
2929

3030
given showGen[T](using Generic): Show[T] = new Show[T](2)
3131

tests/run/implied-divergence.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// recursions.
33
case class E(x: E | Null)
44

5-
given e: E(null) with {}
5+
given e: E(null)
66

77
object Test extends App {
88

9-
given f(using e: E): E(e) with {}
9+
given f(using e: E): E(e)
1010

1111
assert(summon[E].toString == "E(E(null))")
1212

0 commit comments

Comments
 (0)