Skip to content

Commit 4c53fd1

Browse files
committed
Allow whitespace between -> and following capture set
Not allowing whitespace clashes with the rules for leading infix operators. For instance, the following does not work, since `->` is not recognized as a leading infix operator. ```scala A ->{x} B ``` Also: Print existential capture set id under -uniqids
1 parent f3ef42b commit 4c53fd1

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,7 @@ object Parsers {
16381638
}
16391639

16401640
def capturesAndResult(core: () => Tree): Tree =
1641-
if Feature.ccEnabled && in.token == LBRACE && in.offset == in.lastOffset
1641+
if Feature.ccEnabled && in.token == LBRACE && canStartCaptureSetContentsTokens.contains(in.lookahead.token)
16421642
then CapturesAndResult(captureSet(), core())
16431643
else core()
16441644

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

+2
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ object Tokens extends TokensCommon {
295295

296296
final val colonEOLPredecessors = BitSet(RPAREN, RBRACKET, BACKQUOTED_IDENT, THIS, SUPER, NEW)
297297

298+
final val canStartCaptureSetContentsTokens = BitSet(IDENTIFIER, BACKQUOTED_IDENT, THIS, RBRACE)
299+
298300
final val closingParens = BitSet(RPAREN, RBRACKET, RBRACE)
299301

300302
final val softModifierNames = Set(nme.inline, nme.opaque, nme.open, nme.transparent, nme.infix)

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

+3-2
Original file line numberDiff line numberDiff line change
@@ -454,13 +454,14 @@ class PlainPrinter(_ctx: Context) extends Printer {
454454
case ReadOnlyCapability(tp1) => toTextCaptureRef(tp1) ~ ".rd"
455455
case ReachCapability(tp1) => toTextCaptureRef(tp1) ~ "*"
456456
case MaybeCapability(tp1) => toTextCaptureRef(tp1) ~ "?"
457-
case Existential.Vble(binder) =>
457+
case tp @ Existential.Vble(binder) =>
458+
val idStr = s"##${tp.annot.asInstanceOf[Fresh.Annot].hidden.id}"
458459
// TODO: Better printing? USe a mode where we print more detailed
459460
val vbleText: Text = CCState.openExistentialScopes.indexOf(binder) match
460461
case -1 =>
461462
"<cap of " ~ toText(binder) ~ ">"
462463
case n => "outer_" * n ++ (if printFresh then "localcap" else "cap")
463-
vbleText ~ hashStr(binder)
464+
vbleText ~ hashStr(binder) ~ Str(idStr).provided(showUniqueIds)
464465
case Fresh(hidden) =>
465466
val idStr = if showUniqueIds then s"#${hidden.id}" else ""
466467
if printFreshDetailed then s"<cap$idStr hiding " ~ toTextCaptureSet(hidden) ~ ">"

tests/pending/pos/classcaps.scala

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import language.experimental.captureChecking
3+
import caps.*
4+
5+
case class A()
6+
7+
trait HasCap:
8+
def mkA: A^{this}
9+
10+
object HasCap:
11+
def apply[T](body: HasCap^ ?=> T): T = ???
12+
13+
class Box(using h: HasCap^):
14+
var t: A^{h} = h.mkA
15+
16+
def main() =
17+
HasCap: h ?=>
18+
val b = Box(using h)
19+
b.t = h.mkA
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import language.experimental.saferExceptions
2+
import language.experimental.erasedDefinitions
3+
import language.experimental.captureChecking
4+
5+
class Ex1 extends Exception("Ex1")
6+
class Ex2 extends Exception("Ex2")
7+
class Ex3 extends Exception("Ex3")
8+
9+
erased class CT[-E <: Exception] extends caps.Capability
10+
11+
def Throw[Ex <: Exception](ex: Ex)(using CT[Ex]^): Nothing = ???
12+
13+
def foo8a(i: Int) =
14+
(erased xx1: CT[Ex2]^) ?=> Throw(new Ex2)
15+
16+
def foo9a(i: Int)
17+
: (x$1: CT[Ex3]^)
18+
?=> (x$2: CT[Ex2]^)
19+
?-> {x$1, caps.cap} Unit
20+
= (x$1: CT[Ex3]^)
21+
?=> (x$2: CT[Ex2]^)
22+
?=>
23+
//given (CT[Ex3]^) = x$1
24+
Throw(new Ex3)
25+
26+
def foo10a(i: Int)
27+
: (erased x$0: CT[Ex3]^)
28+
?=> (erased x$1: CT[Ex2]^)
29+
?-> {x$0, caps.cap} (erased x$2: CT[Ex1]^)
30+
?-> {x$0, x$1, caps.cap} Unit
31+
= (erased x$1: CT[Ex3]^)
32+
?=> (erased x$2: CT[Ex2]^)
33+
?=> (erased x$3: CT[Ex1]^)
34+
?=> Throw(new Ex3)

0 commit comments

Comments
 (0)