Skip to content

Commit dcbc5b4

Browse files
committed
Implement new given syntax
1 parent 29082f0 commit dcbc5b4

File tree

12 files changed

+174
-115
lines changed

12 files changed

+174
-115
lines changed

compiler/src/dotty/tools/dotc/core/Flags.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ object Flags {
2222

2323
type Flag = opaques.Flag
2424

25-
given FlagOps {
25+
given /*FlagOps*/ {
2626

2727
def (x: FlagSet) bits: Long = opaques.toBits(x)
2828

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

Lines changed: 143 additions & 95 deletions
Large diffs are not rendered by default.

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,11 +872,14 @@ object Scanners {
872872
super.printState()
873873
}
874874

875-
final def skipParens(opening: Token): Unit = {
875+
/** Skip matching pairs of `(...)` or `[...]` parentheses.
876+
* @pre The current token is `(` or `[`
877+
*/
878+
final def skipParens(): Unit = {
879+
val opening = token
876880
nextToken()
877881
while token != EOF && token != opening + 1 do
878-
if (token == opening) skipParens(opening)
879-
else nextToken()
882+
if token == opening then skipParens() else nextToken()
880883
nextToken()
881884
}
882885
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,17 @@ object Tokens extends TokensCommon {
270270
statCtdTokens | BitSet(COLONEOL, EQUALS, ARROW, LARROW, WHILE, TRY, FOR)
271271
// `if` is excluded because it often comes after `else` which makes for awkward indentation rules
272272

273+
/** Faced with the choice between a type and a formal parameter, the following
274+
* tokens determine it's a formal parameter.
275+
*/
276+
final val startParamTokens: BitSet = modifierTokens | BitSet(VAL, VAR, AT)
277+
278+
/** Faced with the choice of a type `(...)` or a parameter or given type list
279+
* in `(...)`, the following tokens after the opening `(` determine it's
280+
* a parameter or given type list.
281+
*/
282+
final val startParamOrGivenTypeTokens: BitSet = startParamTokens | BitSet(GIVEN, ERASED)
283+
273284
final val scala3keywords = BitSet(ENUM, ERASED, GIVEN, IMPLIED)
274285

275286
final val softModifierNames = Set(nme.inline, nme.opaque)

library/src/scala/internal/quoted/Matcher.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ private[quoted] object Matcher {
8686
case _ => notMatched
8787
}
8888

89-
private given TreeListOps {
89+
private given /*TreeListOps*/ {
9090

9191
/** Check that all trees match with =?= and concatenate the results with && */
9292
def (scrutinees: List[Tree]) =?= (patterns: List[Tree]) given Context, Env: Matching =
9393
matchLists(scrutinees, patterns)(_ =?= _)
9494

9595
}
96-
97-
private given TreeOps {
96+
97+
private given /*TreeOps*/ {
9898

9999
/** Check that the trees match and return the contents from the pattern holes.
100100
* Return None if the trees do not match otherwise return Some of a tuple containing all the contents in the holes.
@@ -294,7 +294,7 @@ private[quoted] object Matcher {
294294
}
295295
}
296296

297-
private given PatternOps {
297+
private given /*PatternOps*/ {
298298

299299
/** Check that the pattern trees match and return the contents from the pattern holes.
300300
* Return a tuple with the new environment containing the bindings defined in this pattern and a matching.

tests/neg/erased-implicit-b.scala

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/neg/i6801.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
delegate MyNumericOps[T] {
1+
given MyNumericOps[T]: {
22
def (x: T) +(y: T) given (n: Numeric[T]): T = n.plus(x,y)
33
}
44
def foo[T: Numeric](x: T) = 1f + x // error: no implicit argument of type Numeric[Any]

tests/pos/i6900.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
object Test {
2-
given bla[A] { def (a: A) foo[C]: C => A = _ => a }
2+
given bla[A]: { def (a: A) foo[C]: C => A = _ => a }
33

44
1.foo.foo
55
1.foo.foo[String]

tests/pos/i7103.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class X
22
object X extends X
3-
given `as` as X = X
3+
given `as` : X = X

tests/pos/tasty-reflect-opaque-api-proto.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Reflect(val internal: CompilerInterface) {
1010
opaque type Term <: Tree = internal.Term
1111

1212
object Tree {
13-
given Ops (tree: Tree) {
13+
given Ops: (tree: Tree) {
1414
def show: String = ???
1515
}
1616
}

tests/run/extmethods2.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ object Test extends App {
22

33
class TC
44

5-
given StringListOps given TC {
5+
given stringListOps(given TC): {
66
type T = List[String]
77
def (x: T) foo (y: T) = (x ++ y, summon[TC])
88
def (x: T) bar (y: Int) = (x(0)(y), summon[TC])
@@ -16,7 +16,7 @@ object Test extends App {
1616
test given TC()
1717

1818
object A {
19-
given ListOps[T](xs: List[T]) {
19+
given listOps: [T](xs: List[T]) {
2020
def second: T = xs.tail.head
2121
def third: T = xs.tail.tail.head
2222
def concat(ys: List[T]) = xs ++ ys
@@ -34,8 +34,8 @@ object Test extends App {
3434
val xs = List(1, 2, 3)
3535
assert(xs.second[Int] == 2)
3636
assert(xs.third == 3)
37-
assert(A.ListOps.second[Int](xs) == 2)
38-
assert(A.ListOps.third(xs) == 3)
37+
assert(A.listOps.second[Int](xs) == 2)
38+
assert(A.listOps.third(xs) == 3)
3939
assert(xs.prod == 6)
4040
assert(xs.concat(xs).length == 6)
4141
assert(xs.zipp(xs).map(_ + _).prod == 36)

tests/run/i6902.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
object Test {
2-
given [A] { def (a: A) <<< : A = a }
2+
given [A]: { def (a: A) <<< : A = a }
33
given { def (b: Int) <<<< : Int = b }
44

55
def main(args: Array[String]): Unit = {

0 commit comments

Comments
 (0)