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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 14 additions & 14 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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 -------------------------------------------------------------------------------

0 commit comments

Comments
 (0)