Skip to content

Streamline given syntax #12107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3585,7 +3585,10 @@ object Parsers {
val tparams1 = tparams.map(tparam => tparam.withMods(tparam.mods | PrivateLocal))
val vparamss1 = vparamss.map(_.map(vparam =>
vparam.withMods(vparam.mods &~ Param | ParamAccessor | Protected)))
val templ = withTemplate(makeConstructor(tparams1, vparamss1), parents)
val constr = makeConstructor(tparams1, vparamss1)
val templ =
if in.token == WITH then withTemplate(constr, parents)
else Template(constr, parents, Nil, EmptyValDef, Nil)
if noParams then ModuleDef(name, templ)
else TypeDef(name.toTypeName, templ)
end gdef
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/internals/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ ObjectDef ::= id [Template]
EnumDef ::= id ClassConstr InheritClauses EnumBody
GivenDef ::= [GivenSig] (AnnotType [‘=’ Expr] | StructuralInstance)
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’ -- one of `id`, `DefParamClause`, `UsingParamClause` must be present
StructuralInstance ::= ConstrApp {‘with’ ConstrApp} ‘with’ TemplateBody
StructuralInstance ::= ConstrApp {‘with’ ConstrApp} [‘with’ TemplateBody]
Extension ::= ‘extension’ [DefTypeParamClause] ‘(’ DefParam ‘)’
{UsingParamClause} ExtMethods
ExtMethods ::= ExtMethod | [nl] <<< ExtMethod {semi ExtMethod} >>>
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/reference/contextual/extension-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ trait SafeDiv:
By the second rule, an extension method can be made available by defining a given instance containing it, like this:

```scala
given ops1: IntOps with {} // brings safeMod into scope
given ops1: IntOps() // brings safeMod into scope

1.safeMod(2)
```
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/reference/contextual/givens.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ object Foo:
given fooNotTagged[A](using NotGiven[Tagged[A]]): Foo[A] = Foo(false)

@main def test(): Unit =
given Tagged[Int] with {}
given Tagged[Int]()
assert(summon[Foo[Int]].value) // fooTagged is found
assert(!summon[Foo[String]].value) // fooNotTagged is found
```
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/reference/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ ObjectDef ::= id [Template]
EnumDef ::= id ClassConstr InheritClauses EnumBody
GivenDef ::= [GivenSig] (AnnotType [‘=’ Expr] | StructuralInstance)
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’ -- one of `id`, `DefParamClause`, `UsingParamClause` must be present
StructuralInstance ::= ConstrApp {‘with’ ConstrApp} ‘with’ TemplateBody
StructuralInstance ::= ConstrApp {‘with’ ConstrApp} [‘with’ TemplateBody]
Extension ::= ‘extension’ [DefTypeParamClause] ‘(’ DefParam ‘)’
{UsingParamClause} ExtMethods
ExtMethods ::= ExtMethod | [nl] <<< ExtMethod {semi ExtMethod} >>>
Expand Down
4 changes: 2 additions & 2 deletions tests/neg/implicit-params.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ object Test {

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

given C: C(11) with {}
given D: D(11) with {}
given C: C(11)
given D: D(11)

f(1)
f(1)(using C)
Expand Down
28 changes: 14 additions & 14 deletions tests/neg/multi-param-derives.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ object Test extends App {
{
trait Show[T]
object Show {
given Show[Int] with {}
given [T](using st: Show[T]): Show[Tuple1[T]] with {}
given t2[T, U](using st: Show[T], su: Show[U]): Show[(T, U)] with {}
given t3[T, U, V](using st: Show[T], su: Show[U], sv: Show[V]): Show[(T, U, V)] with {}
given Show[Int]()
given [T](using st: Show[T]): Show[Tuple1[T]]()
given t2[T, U](using st: Show[T], su: Show[U]): Show[(T, U)]()
given t3[T, U, V](using st: Show[T], su: Show[U], sv: Show[V]): Show[(T, U, V)]()

def derived[T](using m: Mirror.Of[T], r: Show[m.MirroredElemTypes]): Show[T] = new Show[T] {}
}
Expand All @@ -22,10 +22,10 @@ object Test extends App {
{
trait Functor[F[_]]
object Functor {
given [C]: Functor[[T] =>> C] with {}
given Functor[[T] =>> Tuple1[T]] with {}
given t2 [T]: Functor[[U] =>> (T, U)] with {}
given t3 [T, U]: Functor[[V] =>> (T, U, V)] with {}
given [C]: Functor[[T] =>> C]()
given Functor[[T] =>> Tuple1[T]]()
given t2 [T]: Functor[[U] =>> (T, U)]()
given t3 [T, U]: Functor[[V] =>> (T, U, V)]()

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

def derived[F[_[_]]](using m: Mirror { type MirroredType[X[_]] = F[X] ; type MirroredElemTypes[_[_]] }, r: FunctorK[m.MirroredElemTypes]): FunctorK[F] = new FunctorK[F] {}
}
Expand All @@ -56,10 +56,10 @@ object Test extends App {
{
trait Bifunctor[F[_, _]]
object Bifunctor {
given [C]: Bifunctor[[T, U] =>> C] with {}
given Bifunctor[[T, U] =>> Tuple1[U]] with {}
given t2: Bifunctor[[T, U] =>> (T, U)] with {}
given t3 [T]: Bifunctor[[U, V] =>> (T, U, V)] with {}
given [C]: Bifunctor[[T, U] =>> C]()
given Bifunctor[[T, U] =>> Tuple1[U]]()
given t2: Bifunctor[[T, U] =>> (T, U)]()
given t3 [T]: Bifunctor[[U, V] =>> (T, U, V)]()

def derived[F[_, _]](using m: Mirror { type MirroredType[X, Y] = F[X, Y] ; type MirroredElemTypes[_, _] }, r: Bifunctor[m.MirroredElemTypes]): Bifunctor[F] = ???
}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/overloading-specifity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object Generic {

object Test extends App {
trait Context
//given ctx: Context with {}
//given ctx: Context()

object a {
def foo[T](implicit gen: Generic): Show[T] = new Show[T](1)
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/transparent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ transparent class c // error
transparent object y // error
transparent trait t // ok
transparent type T = c // error
transparent given c with {} // error
transparent given c() // error

2 changes: 1 addition & 1 deletion tests/neg/type-qmark.check
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
| ^
| `?` is not a valid type name
-- Error: tests/neg/type-qmark.scala:31:8 ------------------------------------------------------------------------------
31 | given ?[T]: Foo[T] with {} // error
31 | given ?[T]: Foo[T]() // error
| ^
| `?` is not a valid type name
-- Error: tests/neg/type-qmark.scala:3:8 -------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/type-qmark.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ object J {
}
object K {
class Foo[T]
given ?[T]: Foo[T] with {} // error
given ?[T]: Foo[T]() // error
}
4 changes: 2 additions & 2 deletions tests/pos-macros/i11479/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
trait Foo
given Foo: Foo with {}
given Foo: Foo()
inline def summonFoo(): Foo = scala.compiletime.summonInline[Foo]

package p:
trait Bar
given Bar: Bar with {}
given Bar: Bar()
inline def summonBar(): Bar = scala.compiletime.summonInline[Bar]

4 changes: 2 additions & 2 deletions tests/pos/X.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import scala.deriving.*

trait FunctorK[F[_[_]]]
object FunctorK {
given [C]: FunctorK[[F[_]] =>> C] with {}
given [T]: FunctorK[[F[_]] =>> Tuple1[F[T]]] with {}
given [C]: FunctorK[[F[_]] =>> C]()
given [T]: FunctorK[[F[_]] =>> Tuple1[F[T]]]()

def derived[F[_[_]]](using m: Mirror { type MirroredType[X[_]] = F[X] ; type MirroredElemTypes[_[_]] }, r: FunctorK[m.MirroredElemTypes]): FunctorK[F] = new FunctorK[F] {}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/i11168.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
trait Foo
given foo: Foo with {}
given foo: Foo()

extension (using Foo)(x: Any)
def foo1[A] = ???
Expand Down
12 changes: 6 additions & 6 deletions tests/pos/i6864.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
class A
class B

given A with {}
given B with {}
given A()
given B()

trait Foo
trait Bar

given Foo with {}
given Bar with {}
given Foo()
given Bar()

trait C
trait Baz[A]

given C with {}
given [A]: Baz[A] with {}
given C()
given [A]: Baz[A]()
4 changes: 2 additions & 2 deletions tests/pos/inlined-the.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Instances {
class C {
def f() = {
locally {
given d[T]: D[T] with {}
given d[T]: D[T]()
summon[D[Int]]
implicit val s: 3 = ???
val a: 3 = summon[3]
Expand All @@ -14,7 +14,7 @@ object Instances {
}

locally {
given d[T]: D[T] with {}
given d[T]: D[T]()
the2[D[Int]]
implicit val s: 3 = ???
val a: 3 = the2[3]
Expand Down
4 changes: 2 additions & 2 deletions tests/pos/multi-given.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ trait C
def fancy(using a: A, b: B, c: C) = "Fancy!"
def foo(implicit a: A, b: B, c: C) = "foo"

given A with B with {}
given A() with B

given ops: A with B with {}
given ops: A() with B()
4 changes: 2 additions & 2 deletions tests/pos/reference/delegates.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ object Instances extends Common:
println(summon[Context].value)
}
locally {
given d[T]: D[T] with {}
given d[T]: D[T]()
println(summon[D[Int]])
}
locally {
given (using Context): D[Int] with {}
given (using Context): D[Int]()
println(summon[D[Int]])
}
end C
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/reference/extension-methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ object ExtMethods:
end SafeDiv

def test1 =
given ops1: IntOps with {} // brings safeMod into scope
given ops1: IntOps() // brings safeMod into scope
1.safeMod(2)

class Lst[T](xs: T*):
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/the-given.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
object Test {

class Encoder { def apply(x: Int): Int = x }
given Encoder with {}
given Encoder()

summon[Encoder](2)

Expand Down
2 changes: 1 addition & 1 deletion tests/run/exports.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object Test extends App {
class Printer {
def print() = println("printing")
object cfg extends Config
given config: Config with {}
given config: Config()
}

class Scanner {
Expand Down
2 changes: 1 addition & 1 deletion tests/run/extmethod-overload.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object Test extends App {
extension [T](xs: List[T]) def +++ (ys: List[T]): List[T] = xs ++ ys ++ ys
extension [T](xs: List[T]) def +++ (ys: Iterator[T]): List[T] = xs ++ ys ++ ys
}
given Bar: Foo with {}
given Bar: Foo()

assert((1 |+| 2) == 3)
assert((1 |+| "2") == 2)
Expand Down
4 changes: 2 additions & 2 deletions tests/run/extra-implicits.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

case class A(x: String)
case class B(x: String)
given a1: A("default") with {}
given b1: B("default") with {}
given a1: A("default")
given b1: B("default")
val a2 = A("explicit")
val b2 = B("explicit")

Expand Down
2 changes: 1 addition & 1 deletion tests/run/given-eta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def f(x: Int)(using c: C) (y: Int) = x + c.x + y
def g(x: Int)(using d: D) (y: d.T): d.T = d.trans(y)

@main def Test =
given C(1) with {}
given C(1)
val x = f
assert(x(2)(3) == 6)

Expand Down
2 changes: 1 addition & 1 deletion tests/run/i11542.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ object demo {

trait Reader[A]

given Reader[Int] with {}
given Reader[Int]()

inline def summonReader[T <: Tuple]: List[Reader[_]] = inline compiletime.erasedValue[T] match {
case _: EmptyTuple => Nil
Expand Down
2 changes: 1 addition & 1 deletion tests/run/i2567.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class TC

given tc: TC with {}
given tc: TC()

class Foo(using TC) {
println("hi")
Expand Down
6 changes: 3 additions & 3 deletions tests/run/i8396.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ object Prefix:
type UpperBoundedType <: String
type FullyBoundedType >: String <: String

given A: Show[AbstractType] with {}
given B: Show[UpperBoundedType] with {}
given C: Show[FullyBoundedType] with {}
given A: Show[AbstractType]()
given B: Show[UpperBoundedType]()
given C: Show[FullyBoundedType]()

@main def Test =
summon[Show[Prefix.AbstractType]]
Expand Down
2 changes: 1 addition & 1 deletion tests/run/implicit-alias.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Test extends App {

class TC1

given TC1 with {}
given TC1()

class TV(val tc: TC) extends AnyVal

Expand Down
4 changes: 2 additions & 2 deletions tests/run/implicit-disambiguation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object M {
}
}
object Test extends App {
given b: B with {}
given c: C with {}
given b: B()
given c: C()
println(M.f)
}
4 changes: 2 additions & 2 deletions tests/run/implicit-specifity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ object Generic2 {

class SubGen extends Generic
object SubGen {
given SubGen with {}
given SubGen()
}
object Contextual {
trait Context

given ctx: Context with {}
given ctx: Context()

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

Expand Down
4 changes: 2 additions & 2 deletions tests/run/implied-divergence.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// recursions.
case class E(x: E | Null)

given e: E(null) with {}
given e: E(null)

object Test extends App {

given f(using e: E): E(e) with {}
given f(using e: E): E(e)

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

Expand Down
Loading