Skip to content

Commit 81d56a5

Browse files
committed
Fix parsing of derived capabilities
Allow both .only[C] and .rd (in this order).
1 parent 21e7a30 commit 81d56a5

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,37 +1634,40 @@ object Parsers {
16341634
}
16351635

16361636
/** CaptureRef ::= { SimpleRef `.` } SimpleRef [`*`] [CapFilter] [`.` `rd`] -- under captureChecking
1637-
* CapFilter ::= `.` `as` `[` QualId `]`
1637+
* CapFilter ::= `.` `only` `[` QualId `]`
16381638
*/
16391639
def captureRef(): Tree =
16401640

1641-
def derived(ref: Tree): Tree =
1642-
atSpan(startOffset(ref)):
1643-
if in.isIdent(nme.raw.STAR) then
1641+
def reachOpt(ref: Tree): Tree =
1642+
if in.isIdent(nme.raw.STAR) then
1643+
atSpan(startOffset(ref)):
16441644
in.nextToken()
16451645
Annotated(ref, makeReachAnnot())
1646-
else if in.isIdent(nme.rd) then
1646+
else ref
1647+
1648+
def restrictedOpt(ref: Tree): Tree =
1649+
if in.token == DOT && in.lookahead.isIdent(nme.only) then
1650+
atSpan(startOffset(ref)):
16471651
in.nextToken()
1648-
Annotated(ref, makeReadOnlyAnnot())
1649-
else if in.isIdent(nme.only) then
16501652
in.nextToken()
16511653
Annotated(ref, makeOnlyAnnot(inBrackets(convertToTypeId(qualId()))))
1652-
else assert(false)
1654+
else ref
16531655

1654-
def recur(ref: Tree): Tree =
1655-
if in.token == DOT then
1656-
in.nextToken()
1657-
if in.isIdent(nme.rd) || in.isIdent(nme.only) then derived(ref)
1658-
else recur(selector(ref))
1659-
else if in.isIdent(nme.raw.STAR) then
1660-
val reachRef = derived(ref)
1661-
val next = in.lookahead
1662-
if in.token == DOT && (next.isIdent(nme.rd) || next.isIdent(nme.only)) then
1656+
def readOnlyOpt(ref: Tree): Tree =
1657+
if in.token == DOT && in.lookahead.isIdent(nme.rd) then
1658+
atSpan(startOffset(ref)):
16631659
in.nextToken()
1664-
derived(reachRef)
1665-
else reachRef
1660+
in.nextToken()
1661+
Annotated(ref, makeReadOnlyAnnot())
16661662
else ref
16671663

1664+
def recur(ref: Tree): Tree =
1665+
val ref1 = readOnlyOpt(restrictedOpt(reachOpt(ref)))
1666+
if (ref1 eq ref) && in.token == DOT then
1667+
in.nextToken()
1668+
recur(selector(ref))
1669+
else ref1
1670+
16681671
recur(simpleRef())
16691672
end captureRef
16701673

0 commit comments

Comments
 (0)