Skip to content

Commit 6b55449

Browse files
committed
Syntax change: allow capture sets in infix types
1 parent 8eb504b commit 6b55449

File tree

6 files changed

+21
-17
lines changed

6 files changed

+21
-17
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,11 +1486,7 @@ object Parsers {
14861486
else { accept(TLARROW); typ() }
14871487
}
14881488
else if in.token == LBRACE && followingIsCaptureSet() then
1489-
val refs = inBraces {
1490-
if in.token == RBRACE then Nil else commaSeparated(captureRef)
1491-
}
1492-
val t = typ()
1493-
CapturingTypeTree(refs, t)
1489+
CapturingTypeTree(captureSet(), typ())
14941490
else if (in.token == INDENT) enclosed(INDENT, typ())
14951491
else infixType()
14961492

@@ -1873,8 +1869,14 @@ object Parsers {
18731869
def typeDependingOn(location: Location): Tree =
18741870
if location.inParens then typ()
18751871
else if location.inPattern then rejectWildcardType(refinedType())
1872+
else if in.token == LBRACE && followingIsCaptureSet() then
1873+
CapturingTypeTree(captureSet(), infixType())
18761874
else infixType()
18771875

1876+
def captureSet(): List[Tree] = inBraces {
1877+
if in.token == RBRACE then Nil else commaSeparated(captureRef)
1878+
}
1879+
18781880
/* ----------- EXPRESSIONS ------------------------------------------------ */
18791881

18801882
/** Does the current conditional expression continue after
@@ -1944,7 +1946,7 @@ object Parsers {
19441946
* | ‘inline’ InfixExpr MatchClause
19451947
* Bindings ::= `(' [Binding {`,' Binding}] `)'
19461948
* Binding ::= (id | `_') [`:' Type]
1947-
* Ascription ::= `:' InfixType
1949+
* Ascription ::= `:' [CaptureSet] InfixType
19481950
* | `:' Annotation {Annotation}
19491951
* | `:' `_' `*'
19501952
* Catches ::= ‘catch’ (Expr | ExprCaseClause)
@@ -3900,7 +3902,7 @@ object Parsers {
39003902
stats.toList
39013903
}
39023904

3903-
/** TemplateStatSeq ::= [id [`:' Type] `=>'] TemplateStat {semi TemplateStat}
3905+
/** TemplateStatSeq ::= [SelfType] TemplateStat {semi TemplateStat}
39043906
* TemplateStat ::= Import
39053907
* | Export
39063908
* | Annotations Modifiers Def
@@ -3910,6 +3912,8 @@ object Parsers {
39103912
* |
39113913
* EnumStat ::= TemplateStat
39123914
* | Annotations Modifiers EnumCase
3915+
* SelfType ::= id [‘:’ [CaptureSet] InfixType] ‘=>’
3916+
* | ‘this’ ‘:’ [CaptureSet] InfixType ‘=>’
39133917
*/
39143918
def templateStatSeq(): (ValDef, List[Tree]) = checkNoEscapingPlaceholders {
39153919
var self: ValDef = EmptyValDef

tests/disabled/pos/lazylist.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package lazylists
22

33
abstract class LazyList[+T]:
4-
this: ({*} LazyList[T]) =>
4+
this: {*} LazyList[T] =>
55

66
def isEmpty: Boolean
77
def head: T

tests/pos-custom-args/captures/classes.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class B
22
type Cap = {*} B
33
class C(val n: Cap):
4-
this: ({n} C) =>
4+
this: {n} C =>
55
def foo(): {n} B = n
66

77

88
def test(x: Cap, y: Cap, z: Cap) =
99
val c0 = C(x)
1010
val c1: {x} C {val n: {x} B} = c0
1111
val d = c1.foo()
12-
d: ({x} B)
12+
d: {x} B
1313

1414
val c2 = if ??? then C(x) else C(y)
1515
val c2a = identity(c2)

tests/pos-custom-args/captures/iterators.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cctest
22

33
abstract class Iterator[T]:
4-
thisIterator: ({*} Iterator[T]) =>
4+
thisIterator: {*} Iterator[T] =>
55

66
def hasNext: Boolean
77
def next: T

tests/pos-custom-args/captures/lazylists.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class CC
22
type Cap = {*} CC
33

44
trait LazyList[+A]:
5-
this: ({*} LazyList[A]) =>
5+
this: {*} LazyList[A] =>
66

77
def isEmpty: Boolean
88
def head: A
@@ -16,12 +16,12 @@ object LazyNil extends LazyList[Nothing]:
1616
extension [A](xs: {*} LazyList[A])
1717
def map[B](f: A => B): {xs, f} LazyList[B] =
1818
final class Mapped extends LazyList[B]:
19-
this: ({xs, f} Mapped) =>
19+
this: {xs, f} Mapped =>
2020

2121
def isEmpty = false
2222
def head: B = f(xs.head)
2323
def tail: {this} LazyList[B] = xs.tail.map(f) // OK
24-
def concat(other: {f} LazyList[A]): {this, f} LazyList[A] = ??? : ({xs, f} LazyList[A]) // OK
24+
def concat(other: {f} LazyList[A]): {this, f} LazyList[A] = ??? : {xs, f} LazyList[A] // OK
2525
if xs.isEmpty then LazyNil
2626
else new Mapped
2727

@@ -31,7 +31,7 @@ def test(cap1: Cap, cap2: Cap) =
3131

3232
val xs =
3333
class Initial extends LazyList[String]:
34-
this: ({cap1} Initial) =>
34+
this: {cap1} Initial =>
3535

3636
def isEmpty = false
3737
def head = f("")

tests/pos-custom-args/captures/lazylists1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class CC
22
type Cap = {*} CC
33

44
trait LazyList[+A]:
5-
this: ({*} LazyList[A]) =>
5+
this: {*} LazyList[A] =>
66

77
def isEmpty: Boolean
88
def head: A
@@ -14,7 +14,7 @@ object LazyNil extends LazyList[Nothing]:
1414
def tail = ???
1515

1616
final class LazyCons[+T](val x: T, val xs: Int => {*} LazyList[T]) extends LazyList[T]:
17-
this: ({*} LazyList[T]) =>
17+
this: {*} LazyList[T] =>
1818

1919
def isEmpty = false
2020
def head = x

0 commit comments

Comments
 (0)