Skip to content

Commit 5995e3b

Browse files
committed
Make self fresh in capability classes
1 parent d38f257 commit 5995e3b

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

compiler/src/dotty/tools/dotc/cc/Setup.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,11 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
729729
// is cls is known to be pure, nothing needs to be added to self type
730730
selfInfo
731731
else if cls.derivesFrom(defn.Caps_Capability) then
732-
CapturingType(cinfo.selfType, CaptureSet.csImpliedByCapability)
733-
// CapturingType(cinfo.selfType, CaptureSet.universal)
732+
// If cls is a capability class, we need to add a fresh capability to
733+
// ensure we cannot tread itself as pure.
734+
CapturingType(cinfo.selfType,
735+
CaptureSet.fresh(Origin.InDecl(cls)).readOnly
736+
++ CaptureSet.Var(cls, level = ccState.currentLevel))
734737
else if !cls.isEffectivelySealed && !cls.baseClassHasExplicitNonUniversalSelfType then
735738
// assume {cap} for completely unconstrained self types of publicly extensible classes
736739
CapturingType(cinfo.selfType, CaptureSet.universal)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import language.experimental.captureChecking
2+
import caps.*
3+
4+
class A:
5+
def a: A = this
6+
7+
class B extends A, Capability // error
8+
9+
def leak(b: B): A = b.a
10+
11+
class C extends Capability:
12+
def c: C^{} = this // error

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import language.experimental.captureChecking
2+
import caps.*
23

34
class Cap extends caps.Capability:
45
def use[T](body: Cap ?=> T) = body(using this)
56

7+
class Cap2 extends caps.Capability:
8+
def use[T](body: Cap2 => T) = body(this)
9+
610
class Box[T](body: Cap ?=> T):
711
inline def open(using cap: Cap) = cap.use(body)
812

0 commit comments

Comments
 (0)